1 /***
2 * RegistryBrowser
3 *
4 * This is a basic window, which will
5 * browse the registry.
6 *
7 * Looks sort of like this:
8 *
9 * +================+==================+
10 * | Folder | /T1\/T2\ |
11 * | * Entry 1 | | +------------+|
12 * | * Entry 2 | | ||
13 * | | +---------------+|
14 * +================+==================+
15 */
16
17 package junit.quilt.util;
18
19 import java.awt.BorderLayout;
20 import java.awt.Component;
21
22 import javax.swing.*;
23 import javax.swing.tree.*;
24 import javax.swing.event.*;
25
26 import java.io.*;
27 import java.util.Properties;
28
29 public class RegistryBrowser
30 extends JPanel
31 {
32 private Registry contents;
33 private JTree registryTree;
34 private JTabbedPane pane;
35
36 /***
37 * To set up a registry browser. You need to provide
38 * the registry it will be browsing, and the type of
39 * stuff in there.
40 *
41 * The type is used to read the panels from the properties
42 * file.
43 *
44 * Properties are of the type:
45 * browser.<type>.panel.count - Count of panels.
46 * browser.<type>.panel.<n>.name - Name for the panel.
47 * browser.<type>.panel.<n>.class - Class of the panel. Implement
48 * RegistryBrowserAware if you would like the panel to know about
49 * the browser it is on.
50 */
51 public RegistryBrowser( Registry contents, String type ) {
52 // First, we set up the Tree.
53 this.contents = contents;
54 registryTree =
55 new JTree( new DefaultTreeModel( contents.getContents() ) );
56
57 // Then we set up the Tabbed Pane.
58 pane = new JTabbedPane();
59 try {
60 InputStream is =
61 getClass().getClassLoader()
62 .getResourceAsStream( "quilt.properties" );
63 Properties prop = new Properties();
64 prop.load( is );
65
66 int panelCount =
67 Integer.parseInt( (String) prop.get( "browser." +
68 type + ".panel.count") );
69 for (int i = 0; i < panelCount; i++) {
70 String panelName =
71 (String) prop.get( "browser." + type + ".panel." +
72 i + ".name" );
73 String panelClass =
74 (String) prop.get( "browser." + type + ".panel." +
75 i + ".class" );
76 Class clazz = Class.forName( panelClass.trim() );
77
78 Component comp = (Component) clazz.newInstance();
79 if (comp instanceof RegistryBrowserAware) {
80 ((RegistryBrowserAware) comp).setRegistryBrowser( this );
81 }
82
83 pane.addTab( panelName, comp );
84 }
85 } catch (Exception e) {
86 e.printStackTrace();
87 System.err.println("WARNING: Failed reading properties.");
88 }
89
90 JSplitPane split
91 = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT );
92 split.setLeftComponent( new JScrollPane( registryTree ) );
93 split.setRightComponent( pane );
94
95 setLayout( new BorderLayout() );
96 add( split, BorderLayout.CENTER );
97 }
98
99 /***
100 * This class watches the Registry, and changes
101 * it when stuff happens.
102 */
103 public class Lstnr implements RegistryListener
104 {
105 RegistryBrowser browser;
106 public Lstnr( RegistryBrowser browser ) {
107 this.browser = browser;
108 }
109
110 public void newRegistration( Object keys[], Object value ) {
111 browser.refresh();
112 }
113 }
114
115 /***
116 * This adds a panel to the right hand side of
117 * the browser.
118 */
119 public void addPanel( String name, Component panel ) {
120 pane.addTab( name, new JScrollPane( panel ) );
121 }
122
123 /***
124 * Returns the JTree component of the panel.
125 */
126 public JTree getTree() {
127 return registryTree;
128 }
129
130 /***
131 * Returns the registry which the Tree is showing
132 * the data from.
133 */
134 public Registry getRegistry() {
135 return contents;
136 }
137
138 /***
139 * This class refreshes the tree.
140 */
141 public void refresh() {
142 registryTree.setModel( new DefaultTreeModel( contents.getContents() ));
143 }
144 }
This page was automatically generated by Maven