1 6 7 package org.netbeans.test.editor.app.gui; 8 9 import java.awt.BorderLayout ; 10 import java.awt.CardLayout ; 11 import java.awt.event.ActionEvent ; 12 import java.awt.event.ActionListener ; 13 import java.awt.event.KeyEvent ; 14 import javax.swing.JButton ; 15 import javax.swing.JComboBox ; 16 import javax.swing.JPanel ; 17 import javax.swing.JTextArea ; 18 import javax.swing.JTextField ; 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 30 public class PropertyPanel extends javax.swing.JPanel { 31 32 protected javax.swing.JButton button; 34 protected javax.swing.JComponent editField; 36 protected javax.swing.JPanel editorPanel; 37 protected javax.swing.JButton editorButton; 38 39 protected String propertyName; 40 protected Property property; 41 protected boolean buttonState=true; 42 protected PropertiesDialog dialog; 43 44 protected String oldText; 45 protected boolean readOnly=false; 46 private boolean change=false; 47 48 49 protected PropertyPanel() { 50 } 51 52 53 public PropertyPanel(Object prop,String 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 prop,String name,boolean read) { 63 this(prop,name); 64 readOnly=read; 65 } 66 71 protected void initComponents() { 72 button = new javax.swing.JButton (); 73 setLayout(new java.awt.CardLayout ()); 74 button.addActionListener(new java.awt.event.ActionListener () { 75 public void actionPerformed(java.awt.event.ActionEvent 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 evt) { 84 if (!readOnly) { 86 buttonState=false; 87 oldText=button.getText(); 88 if (property instanceof StringProperty) { 89 ((JTextField )editField).setText(property.getProperty()); 90 } else if (property instanceof BooleanProperty) { 91 ((JComboBox )editField).setSelectedItem(property.getProperty()); 92 } else if (property instanceof IntegerProperty) { 93 ((JTextField )editField).setText(property.getProperty()); 94 } else if (property instanceof MultiLineStringProperty) { 95 ((JTextArea )editField).setText(property.getProperty()); 96 } 97 if (evt != null) { 98 evt=new ActionEvent (this,evt.getID(),evt.getActionCommand(),evt.getModifiers()); 100 dialog.buttonPressed(evt); 101 } 102 ((CardLayout )(getLayout())).show(this,"edit"); 103 change=false; 104 } 105 } 106 107 protected void focusLost(java.awt.event.FocusEvent evt) { 108 buttonState=true; 110 if (property instanceof StringProperty) { 111 property.setProperty(((JTextField )editField).getText()); 112 } else if (property instanceof BooleanProperty) { 113 property.setProperty((String )(((JComboBox )editField).getSelectedItem())); 114 } else if (property instanceof IntegerProperty) { 115 property.setProperty(((JTextField )editField).getText()); 116 } else if (property instanceof MultiLineStringProperty) { 117 property.setProperty(((JTextArea )editField).getText()); 118 } 119 button.setText(property.getProperty()); 120 ((CardLayout )(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 evt) { 128 if (evt.getKeyCode() == KeyEvent.VK_ENTER) { 130 focusLost(null); 131 } 132 } 133 134 public void changeState(ActionEvent 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 (new BorderLayout ()); 150 151 if (property instanceof StringProperty) { 152 editField=new JTextField (property.getProperty()); 153 } else if (property instanceof BooleanProperty) { 154 editField=new JComboBox (((BooleanProperty)property).getValues()); 155 ((JComboBox )(editField)).setSelectedItem(((BooleanProperty)property).getProperty()); 156 } else if (property instanceof IntegerProperty) { 157 editField=new JTextField (property.getProperty()); 158 } else if (property instanceof MultiLineStringProperty) { 159 editField=new JTextArea (property.getProperty()); 160 editorButton = new JButton ("..."); 161 editorButton.addActionListener(new ActionListener () { 162 public void actionPerformed(java.awt.event.ActionEvent evt) { 163 propertyEditorInvoked(); 164 } 165 }); 166 editorPanel.add(editorButton, BorderLayout.EAST); 167 } 168 editField.addFocusListener(new java.awt.event.FocusAdapter () { 169 public void focusLost(java.awt.event.FocusEvent evt) { 170 PropertyPanel.this.focusLost(evt); 171 } 172 }); 173 editField.addKeyListener(new java.awt.event.KeyAdapter () { 174 public void keyPressed(java.awt.event.KeyEvent 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 )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 200 public void setDialog(PropertiesDialog dialog) { 201 this.dialog = dialog; 202 } 203 204 208 public java.lang.Object getProperty() { 209 return property; 210 } 211 212 216 public void setProperty(java.lang.Object property) { 217 this.property = (Property)property; 218 button.setText(this.property.getProperty()); 219 oldText=button.getText(); 220 repaint(); 221 } 222 223 } 224 | Popular Tags |