View Javadoc

1   /*
2    * JDiskCatalog
3    *
4    * Copyright 2007 Przemek Więch
5    *
6    * This file is part of JDiskCatalog.
7    *
8    * JDiskCatalog is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17  
18   * You should have received a copy of the GNU General Public License
19   * along with this program; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  
23  package net.sf.jdiskcatalog.view;
24  
25  import java.awt.Image;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import javax.swing.text.Element;
30  import javax.swing.text.StyleConstants;
31  import javax.swing.text.View;
32  import javax.swing.text.ViewFactory;
33  import javax.swing.text.html.HTML;
34  
35  /**
36   * Our own implementation to use images from memory.
37   *
38   * @author Przemek Wiech <pwiech@losthive.org>
39   */
40  public class HtmlEditorKit extends javax.swing.text.html.HTMLEditorKit {
41  
42      /**
43       * Factory to create views.
44       */
45      private ViewFactory factory = new HtmlFactory();
46  
47      private Map<String, Image> images = new HashMap<String, Image>();
48  
49      public void addImage(String name, Image image)
50      {
51      	images.put(name, image);
52      }
53  
54      public void clearImages()
55      {
56      	images.clear();
57      }
58  
59      /**
60       * Fetch a factory that is suitable for producing views of any models that
61       * are produced by this kit.
62       *
63       * @return the factory
64       */
65      @Override
66  	public ViewFactory getViewFactory() {
67          return factory;
68      }
69  
70      /**
71       * Factory to create views.
72       */
73      private class HtmlFactory extends
74              javax.swing.text.html.HTMLEditorKit.HTMLFactory {
75  
76          /**
77           * Creates a view from an element. If it's an IMG tag then use our
78           * ImageView.
79           *
80           * @param elem the element
81           * @return the view
82           */
83          @Override
84  		public View create(Element elem) {
85              Object o = elem.getAttributes().getAttribute(
86                      StyleConstants.NameAttribute);
87              if (o instanceof HTML.Tag) {
88                  HTML.Tag kind = (HTML.Tag) o;
89                  if (kind == HTML.Tag.IMG)
90                  {
91                  	String src = (String)elem.getAttributes().getAttribute(HTML.Attribute.SRC);
92                      return new ImageView(elem, images.get(src));
93                  }
94              }
95              return super.create(elem);
96          }
97      }
98  }