KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > editor > app > gui > PropertiesDialog


1 /*
2  * PropertiesDialog.java
3  *
4  * Created on November 15, 2002, 7:10 PM
5  */

6
7 package org.netbeans.test.editor.app.gui;
8
9 import java.awt.Component JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.awt.GridLayout JavaDoc;
12 import java.awt.event.ActionEvent JavaDoc;
13 import java.awt.event.ActionListener JavaDoc;
14 import java.beans.PropertyChangeEvent JavaDoc;
15 import java.beans.PropertyChangeListener JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Vector JavaDoc;
19 import javax.swing.DefaultListCellRenderer JavaDoc;
20 import javax.swing.JButton JavaDoc;
21 import javax.swing.JComponent JavaDoc;
22 import javax.swing.JList JavaDoc;
23 import javax.swing.JTextField JavaDoc;
24 import javax.swing.ListCellRenderer JavaDoc;
25 import org.netbeans.test.editor.app.Main;
26 import org.netbeans.test.editor.app.core.properties.ArrayProperty;
27 import org.netbeans.test.editor.app.core.properties.BadPropertyNameException;
28 import org.netbeans.test.editor.app.core.properties.Properties;
29 import org.netbeans.test.editor.app.core.Test;
30 import org.netbeans.test.editor.app.core.TestCallAction;
31 import org.netbeans.test.editor.app.core.TestLogAction;
32 import org.netbeans.test.editor.app.core.TestNode;
33 import org.netbeans.test.editor.app.core.TestSetIEAction;
34 import org.netbeans.test.editor.app.core.TestSetKitAction;
35 import org.netbeans.test.editor.app.core.TestStringAction;
36 import org.netbeans.test.editor.app.core.XMLNode;
37 import org.netbeans.test.editor.app.core.properties.StringProperty;
38 import org.netbeans.test.editor.app.gui.QuestionDialog;
39
40 /**
41  *
42  * @author eh103527
43  */

44 public class PropertiesDialog extends javax.swing.JDialog JavaDoc implements PropertyChangeListener JavaDoc {
45     
46     XMLNode node;
47     static Hashtable JavaDoc lines=new Hashtable JavaDoc();
48     
49     String JavaDoc oldString;
50     
51     /** Creates new form PropertiesDialog */
52     public PropertiesDialog(java.awt.Frame JavaDoc parent, XMLNode n) {
53         super(parent, false);
54         this.node=n;
55         node.addPropertyChangeListener(this);
56         initComponents();
57         oldString=node.toString();
58         setTitle(getTitle()+" ["+node+"]");
59         generateButtons();
60         pack();
61         setSize(PropertiesDialog.PropertyLine.WIDTH*2+64,PropertiesDialog.PropertyLine.HEIGHT*(leftPanel.getComponentCount()+2));
62         splitPane.setDividerLocation((getWidth()/7)*3);
63         setLocation(parent.getX(),parent.getY()+parent.getHeight()-getHeight());
64     }
65     
66     /** This method is called from within the constructor to
67      * initialize the form.
68      * WARNING: Do NOT modify this code. The content of this method is
69      * always regenerated by the Form Editor.
70      */

71     private void initComponents() {//GEN-BEGIN:initComponents
72
jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
73         splitPane = new javax.swing.JSplitPane JavaDoc();
74         jPanel1 = new javax.swing.JPanel JavaDoc();
75         leftPanel = new javax.swing.JPanel JavaDoc();
76         jPanel2 = new javax.swing.JPanel JavaDoc();
77         rightPanel = new javax.swing.JPanel JavaDoc();
78
79         setTitle("Properties");
80         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
81             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
82                 closeDialog(evt);
83             }
84         });
85
86         splitPane.setDividerLocation(0);
87         splitPane.setDividerSize(4);
88         splitPane.setResizeWeight(1.0);
89         splitPane.setPreferredSize(new java.awt.Dimension JavaDoc(100, 100));
90         jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
91
92         leftPanel.setLayout(new java.awt.GridLayout JavaDoc(1, 1));
93
94         jPanel1.add(leftPanel, java.awt.BorderLayout.CENTER);
95
96         splitPane.setLeftComponent(jPanel1);
97
98         jPanel2.setLayout(new java.awt.BorderLayout JavaDoc());
99
100         rightPanel.setLayout(new java.awt.GridLayout JavaDoc(1, 1));
101
102         jPanel2.add(rightPanel, java.awt.BorderLayout.CENTER);
103
104         splitPane.setRightComponent(jPanel2);
105
106         jScrollPane1.setViewportView(splitPane);
107
108         getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
109
110         pack();
111     }//GEN-END:initComponents
112

113     /** Closes the dialog */
114     private void closeDialog(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_closeDialog
115
setVisible(false);
116         node.removePropertyChangeListener(this);
117         dispose();
118     }//GEN-LAST:event_closeDialog
119

120     /**
121      * @param args the command line arguments
122      */

123     public static void main(String JavaDoc args[]) {
124         new PropertiesDialog(new javax.swing.JFrame JavaDoc(), new TestStringAction("Aaaa","a")).show();
125     }
126     
127     private void generateButtons() {
128         try {
129             Properties ps=node.getProperties();
130             int rows=1;
131             PropertyLine line;
132             String JavaDoc propertyName;
133             
134             //Type read-only line
135
propertyName="Type";
136             String JavaDoc type=node.getClass().getName();
137             type=type.substring(type.lastIndexOf('.')+1);
138             line=getPropertyLine(propertyName,new StringProperty(type),true);
139             leftPanel.add(line.button);
140             rightPanel.add(line.panel);
141             line.panel.setDialog(this);
142             //END of type section
143

144             for (Enumeration JavaDoc en=ps.propertyNames();en.hasMoreElements();) {
145                 rows++;
146                 ((GridLayout JavaDoc)(leftPanel.getLayout())).setRows(rows);
147                 ((GridLayout JavaDoc)(rightPanel.getLayout())).setRows(rows);
148                 propertyName=(String JavaDoc)(en.nextElement());
149                 line=getPropertyLine(propertyName,ps.getProperty(propertyName));
150                 leftPanel.add(line.button);
151                 rightPanel.add(line.panel);
152                 line.panel.setDialog(this);
153             }
154         } catch (Exception JavaDoc ex) {
155             ex.printStackTrace();
156         }
157     }
158     
159     public void buttonPressed(java.awt.event.ActionEvent JavaDoc evt) { //unselect all except selected
160
Component JavaDoc[] cs=rightPanel.getComponents();
161         for (int i=0;i < cs.length;i++) {
162             if (!evt.getSource().equals(cs[i])) {
163                 ((PropertyPanel)(cs[i])).setState(true);
164             }
165         }
166     }
167     
168     public PropertyLine getPropertyLine(String JavaDoc propertyName,Object JavaDoc value) {
169         PropertyLine ret;
170         ret=new PropertiesDialog.PropertyLine(propertyName,value);
171         lines.put(propertyName,ret);
172         return ret;
173     }
174     
175     public PropertyLine getPropertyLine(String JavaDoc propertyName,Object JavaDoc value,boolean readOnly) {
176         PropertyLine ret;
177         ret=new PropertiesDialog.PropertyLine(propertyName,value,readOnly);
178         lines.put(propertyName,ret);
179         return ret;
180     }
181     
182     public void propertyChanged(String JavaDoc property,Object JavaDoc value) {
183         try {
184             node.setProperty(property, value);
185         } catch (BadPropertyNameException ex) {
186             QuestionDialog dlg=new QuestionDialog(Main.frame,ex.getMessage());
187             dlg.show();
188         }
189     }
190     
191     /** This method gets called when a bound property is changed.
192      * @param evt A PropertyChangeEvent object describing the event source
193      * and the property that has changed.
194      *
195      */

196     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
197         if (oldString.compareTo(node.toString()) != 0) {
198             oldString=node.toString();
199             setTitle("Properties ["+oldString+"]");
200         }
201         Properties ps=node.getProperties();
202         Component JavaDoc[] csl=leftPanel.getComponents();
203         Component JavaDoc[] csr=rightPanel.getComponents();
204         for (int i=1;i < csl.length;i++) { //first element is a Type
205
String JavaDoc s=((JButton JavaDoc)csl[i]).getText();
206             Object JavaDoc o=ps.getProperty(s);
207             ((PropertyPanel)(csr[i])).setProperty(o);
208         }
209     }
210     
211     static class PropertyLine {
212         
213         public static int HEIGHT=24;
214         public static int WIDTH=132;
215         
216         public JButton JavaDoc button;
217         public PropertyPanel panel;
218         
219         public PropertyLine(String JavaDoc name,Object JavaDoc data) {
220             if (data instanceof ArrayProperty) {
221                 panel=new ArrayPropertyPanel((ArrayProperty)data,name);
222             } else {
223                 panel=new PropertyPanel(data,name);
224             }
225             panel.setSize(WIDTH,HEIGHT);
226             button=new JButton JavaDoc(name);
227             button.setSize(WIDTH,HEIGHT);
228             button.addActionListener(new ActionListener JavaDoc() {
229                 public void actionPerformed(ActionEvent JavaDoc ev) {
230                     panel.changeState(ev);
231                 }
232             });
233         }
234         
235         public PropertyLine(String JavaDoc name,Object JavaDoc data,boolean readOnly) {
236             button=new JButton JavaDoc(name);
237             button.setSize(WIDTH,HEIGHT);
238             button.addActionListener(new ActionListener JavaDoc() {
239                 public void actionPerformed(ActionEvent JavaDoc ev) {
240                     panel.changeState(ev);
241                 }
242             });
243             panel=new PropertyPanel(data,name,readOnly);
244             panel.setSize(WIDTH,HEIGHT);
245         }
246         
247     }
248     
249     // Variables declaration - do not modify//GEN-BEGIN:variables
250
private javax.swing.JScrollPane JavaDoc jScrollPane1;
251     private javax.swing.JPanel JavaDoc jPanel2;
252     private javax.swing.JPanel JavaDoc leftPanel;
253     private javax.swing.JPanel JavaDoc jPanel1;
254     private javax.swing.JSplitPane JavaDoc splitPane;
255     private javax.swing.JPanel JavaDoc rightPanel;
256     // End of variables declaration//GEN-END:variables
257

258 }
259
Popular Tags