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.Dimension;
26  import java.awt.Image;
27  
28  import javax.swing.JComponent;
29  import javax.swing.JEditorPane;
30  import javax.swing.JScrollPane;
31  
32  import net.sf.jdiskcatalog.analysis.JpegImage;
33  import net.sf.jdiskcatalog.api.Document;
34  import net.sf.jdiskcatalog.api.Node;
35  import net.sf.jdiskcatalog.api.NodeSelectionListener;
36  import net.sf.jdiskcatalog.api.View;
37  
38  
39  public class SimpleDetailsView implements View
40  {
41  	private JEditorPane htmlPane;
42  	private JScrollPane panel;
43  	private HtmlEditorKit editorKit;
44  
45  	private Node node;
46  
47  	public SimpleDetailsView()
48  	{
49  		editorKit = new HtmlEditorKit();
50  		htmlPane = new JEditorPane("text/html", "");
51  		htmlPane.setEditable(false);
52  		htmlPane.setEditorKit(editorKit);
53  		panel = new JScrollPane(htmlPane);
54  		panel.setPreferredSize(new Dimension(500, 200));
55  	}
56  
57  	public JComponent getComponent()
58  	{
59  		return panel;
60  	}
61  
62  	public void nodeSelected(Node node)
63  	{
64  		this.node = node;
65  		editorKit.clearImages();
66  		if (node == null)
67  		{
68  			htmlPane.setText("");
69  			return;
70  		}
71  
72  		StringBuilder sb = new StringBuilder();
73  		sb.append("<table>");
74  		for (String key : node.getNodeInfo().getProperties())
75  		{
76  			Object prop = node.getNodeInfo().getProperty(key);
77  			String value;
78  			if (prop instanceof Image)
79  			{
80  				editorKit.addImage(key, (Image)prop);
81  				value = "<img src=\"" + key + "\"/>";
82  			}
83  			else if (prop instanceof JpegImage)
84  			{
85  				editorKit.addImage(key, (Image)((JpegImage)prop).getImage());
86  				value = "<img src=\"" + key + "\"/>";
87  			}
88  			else
89  				value = prop.toString();
90  
91  			sb.append("<tr><th>" + key + ":</th><td>" + value + "</td></tr>");
92  		}
93  		sb.append("</table>");
94  		htmlPane.setText(sb.toString());
95  	}
96  
97  	public void setDocument(Document document)
98  	{
99  		nodeSelected(null);
100 	}
101 
102 	public void setNodeSelectionListener(NodeSelectionListener listener)
103 	{
104 	}
105 
106 	public void refresh()
107 	{
108 		nodeSelected(node);
109 	}
110 }