KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * PropertyPanel.java
3  *
4  * Created on November 18, 2002, 3:51 PM
5  */

6
7 package org.netbeans.test.editor.app.gui;
8
9 import java.awt.BorderLayout JavaDoc;
10 import java.awt.CardLayout JavaDoc;
11 import java.awt.event.ActionEvent JavaDoc;
12 import java.awt.event.ActionListener JavaDoc;
13 import java.awt.event.KeyEvent JavaDoc;
14 import javax.swing.JButton JavaDoc;
15 import javax.swing.JComboBox JavaDoc;
16 import javax.swing.JPanel JavaDoc;
17 import javax.swing.JTextArea JavaDoc;
18 import javax.swing.JTextField JavaDoc;
19 import org.netbeans.test.editor.app.Main;
20 import org.netbeans.test.editor.app.core.properties.BooleanProperty;
21 import org.netbeans.test.editor.app.core.properties.IntegerProperty;
22 import org.netbeans.test.editor.app.core.properties.MultiLineStringProperty;
23 import org.netbeans.test.editor.app.core.properties.Property;
24 import org.netbeans.test.editor.app.core.properties.StringProperty;
25
26 /**
27  *
28  * @author eh103527
29  */

30 public class PropertyPanel extends javax.swing.JPanel JavaDoc {
31     
32     // Variables declaration - do not modify
33
protected javax.swing.JButton JavaDoc button;
34     // End of variables declaration
35
protected javax.swing.JComponent JavaDoc editField;
36     protected javax.swing.JPanel JavaDoc editorPanel;
37     protected javax.swing.JButton JavaDoc editorButton;
38     
39     protected String JavaDoc propertyName;
40     protected Property property;
41     protected boolean buttonState=true;
42     protected PropertiesDialog dialog;
43     
44     protected String JavaDoc oldText;
45     protected boolean readOnly=false;
46     private boolean change=false;
47     
48     
49     protected PropertyPanel() {
50     }
51     
52     /** Creates new form PropertyPanel */
53     public PropertyPanel(Object JavaDoc prop,String JavaDoc name) {
54         propertyName=name;
55         property=(Property)prop;
56         initComponents();
57         generateEdit();
58         button.setText(property.getProperty());
59         oldText=button.getText();
60     }
61     
62     public PropertyPanel(Object JavaDoc prop,String JavaDoc name,boolean read) {
63         this(prop,name);
64         readOnly=read;
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     protected void initComponents() {
72         button = new javax.swing.JButton JavaDoc();
73         setLayout(new java.awt.CardLayout JavaDoc());
74         button.addActionListener(new java.awt.event.ActionListener JavaDoc() {
75             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
76                 buttonPressed(evt);
77             }
78         });
79         button.setHorizontalAlignment(JButton.LEFT);
80         add(button, "button");
81     }
82     
83     protected void buttonPressed(java.awt.event.ActionEvent JavaDoc evt) {
84         // Add your handling code here:
85
if (!readOnly) {
86             buttonState=false;
87             oldText=button.getText();
88             if (property instanceof StringProperty) {
89                 ((JTextField JavaDoc)editField).setText(property.getProperty());
90             } else if (property instanceof BooleanProperty) {
91                 ((JComboBox JavaDoc)editField).setSelectedItem(property.getProperty());
92             } else if (property instanceof IntegerProperty) {
93                 ((JTextField JavaDoc)editField).setText(property.getProperty());
94             } else if (property instanceof MultiLineStringProperty) {
95                 ((JTextArea JavaDoc)editField).setText(property.getProperty());
96             }
97             if (evt != null) {
98                 // evt.setSource(this);
99
evt=new ActionEvent JavaDoc(this,evt.getID(),evt.getActionCommand(),evt.getModifiers());
100                 dialog.buttonPressed(evt);
101             }
102             ((CardLayout JavaDoc)(getLayout())).show(this,"edit");
103             change=false;
104         }
105     }
106     
107     protected void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
108         // Add your handling code here:
109
buttonState=true;
110         if (property instanceof StringProperty) {
111             property.setProperty(((JTextField JavaDoc)editField).getText());
112         } else if (property instanceof BooleanProperty) {
113             property.setProperty((String JavaDoc)(((JComboBox JavaDoc)editField).getSelectedItem()));
114         } else if (property instanceof IntegerProperty) {
115             property.setProperty(((JTextField JavaDoc)editField).getText());
116         } else if (property instanceof MultiLineStringProperty) {
117             property.setProperty(((JTextArea JavaDoc)editField).getText());
118         }
119         button.setText(property.getProperty());
120         ((CardLayout JavaDoc)(getLayout())).show(this,"button");
121         if (button.getText().compareTo(oldText) != 0) {
122             oldText=button.getText();
123             dialog.propertyChanged(propertyName,property);
124         }
125     }
126     
127     protected void keyPressed(java.awt.event.KeyEvent JavaDoc evt) {
128         // Add your handling code here:
129
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
130             focusLost(null);
131         }
132     }
133     
134     public void changeState(ActionEvent JavaDoc ev) {
135         if (buttonState) {
136             buttonPressed(ev);
137         } else {
138             focusLost(null);
139         }
140     }
141     
142     public void setState(boolean state) {
143         if (state != buttonState) {
144             changeState(null);
145         }
146     }
147     
148     protected void generateEdit() {
149         editorPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
150         
151         if (property instanceof StringProperty) {
152             editField=new JTextField JavaDoc(property.getProperty());
153         } else if (property instanceof BooleanProperty) {
154             editField=new JComboBox JavaDoc(((BooleanProperty)property).getValues());
155             ((JComboBox JavaDoc)(editField)).setSelectedItem(((BooleanProperty)property).getProperty());
156         } else if (property instanceof IntegerProperty) {
157             editField=new JTextField JavaDoc(property.getProperty());
158         } else if (property instanceof MultiLineStringProperty) {
159             editField=new JTextArea JavaDoc(property.getProperty());
160             editorButton = new JButton JavaDoc("...");
161             editorButton.addActionListener(new ActionListener JavaDoc() {
162                 public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
163                     propertyEditorInvoked();
164                 }
165             });
166             editorPanel.add(editorButton, BorderLayout.EAST);
167         }
168         editField.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
169             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
170                 PropertyPanel.this.focusLost(evt);
171             }
172         });
173         editField.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
174             public void keyPressed(java.awt.event.KeyEvent JavaDoc evt) {
175                 PropertyPanel.this.keyPressed(evt);
176             }
177         });
178         editorPanel.add(editField, BorderLayout.CENTER);
179         add(editorPanel, "edit");
180     }
181     
182     private void propertyEditorInvoked() {
183         StringPropertyEditor dlg = new StringPropertyEditor(Main.frame, propertyName, ((MultiLineStringProperty)property).getProperty());
184         dlg.show();
185         if (dlg.getState()) {
186             property.setProperty(dlg.getValue());
187             ((JTextArea JavaDoc)editField).setText(dlg.getValue());
188             button.setText(dlg.getValue());
189             if (button.getText().compareTo(oldText) != 0) {
190                 oldText=button.getText();
191                 dialog.propertyChanged(propertyName,property);
192             }
193         }
194     }
195     
196     /** Setter for property dialog.
197      * @param dialog New value of property dialog.
198      *
199      */

200     public void setDialog(PropertiesDialog dialog) {
201         this.dialog = dialog;
202     }
203     
204     /** Getter for property property.
205      * @return Value of property property.
206      *
207      */

208     public java.lang.Object JavaDoc getProperty() {
209         return property;
210     }
211     
212     /** Setter for property property.
213      * @param property New value of property property.
214      *
215      */

216     public void setProperty(java.lang.Object JavaDoc property) {
217         this.property = (Property)property;
218         button.setText(this.property.getProperty());
219         oldText=button.getText();
220         repaint();
221     }
222     
223 }
224
Popular Tags