1 19 20 package org.netbeans.modules.j2ee.earproject.ui.customizer; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 import javax.swing.JCheckBox ; 29 import javax.swing.JComboBox ; 30 import javax.swing.JTextField ; 31 import javax.swing.event.DocumentEvent ; 32 import javax.swing.event.DocumentListener ; 33 import javax.swing.text.BadLocationException ; 34 import javax.swing.text.Document ; 35 36 43 public final class VisualPropertySupport { 44 45 private EarProjectProperties earProperties; 46 private Map <Object , String > component2property; 47 private ComponentListener componentListener; 48 49 53 private int comboType = -1; 54 55 private String [] comboValues; 56 57 public VisualPropertySupport(final EarProjectProperties earProperties) { 58 this.earProperties = earProperties; 59 this.component2property = new HashMap <Object , String >(10); 60 this.componentListener = new ComponentListener(); 61 } 62 63 66 public void register( JCheckBox component, String propertyName ) { 67 Boolean value = getAsType(earProperties, propertyName, Boolean .class); 68 component2property.put( component, propertyName ); 69 component.setSelected( value != null && value.booleanValue() ); 70 component.removeActionListener( componentListener ); 71 component.addActionListener( componentListener ); 72 } 73 74 77 public void register( JTextField component, String propertyName ) { 78 String value = getAsType(earProperties, propertyName, String .class); 79 component2property.put( component.getDocument(), propertyName ); 80 component.setText( value != null ? value : "" ); 81 component.getDocument().addDocumentListener( componentListener ); 82 } 83 84 88 public void register( VisualClasspathSupport component, String propertyName ) { 89 @SuppressWarnings ("unchecked") 90 List <VisualClassPathItem> value = getAsType(earProperties, propertyName, List .class); 91 component2property.put( component, propertyName ); 92 component.setVisualClassPathItems( value != null ? value : Collections.<VisualClassPathItem>emptyList()); 93 component.removeActionListener( componentListener ); 94 component.addActionListener( componentListener ); 95 } 96 97 101 public void register(VisualArchiveIncludesSupport component, String propertyName) { 102 @SuppressWarnings ("unchecked") 103 List <VisualClassPathItem> value = getAsType(earProperties, propertyName, List .class); 104 component2property.put(component, propertyName); 105 component.setVisualWarItems(value != null ? value : Collections.<VisualClassPathItem>emptyList()); 106 component.removeActionListener( componentListener ); 107 component.addActionListener(componentListener); 108 } 109 110 111 public void register(JComboBox component, String items[], String propertyName) { 112 checkJComboBoxRegistered(); 113 comboType = 0; 114 String value = getAsType(earProperties, propertyName, String .class); 115 component2property.put( component, propertyName ); 116 component.removeAllItems(); 118 int selectedIndex = -1; 119 for ( int i = 0; i < items.length; i++ ) { 120 component.addItem( items[i] ); 121 if ( items[i].equals( value ) ) { 122 selectedIndex = i; 123 } 124 } 125 if (selectedIndex > -1) { 126 component.setSelectedIndex( selectedIndex ); 127 } 128 component.removeActionListener( componentListener ); 129 component.addActionListener( componentListener ); 130 } 131 132 133 public void register(JComboBox component, String displayNames[], String [] values, String propertyName) { 134 checkJComboBoxRegistered(); 135 comboType = 1; 136 comboValues = values; 137 String value = getAsType(earProperties, propertyName, String .class); 138 component2property.put(component, propertyName); 139 component.removeAllItems(); 141 int selectedIndex = 0; 142 for (int i = 0; i < displayNames.length; i++) { 143 component.addItem(displayNames[i]); 144 if (values[i].equals(value)) { 145 selectedIndex = i; 146 } 147 } 148 if (selectedIndex < component.getItemCount()) { 149 component.setSelectedIndex( selectedIndex ); 150 } 151 component.removeActionListener( componentListener ); 152 component.addActionListener(componentListener); 153 } 154 155 private void checkJComboBoxRegistered() { 156 assert comboType == -1 : "JComboBox already registered and only " + 157 "one instance per VisualPropertySupport is supported. " + "Another VisualPropertySupport instance may be used."; } 160 161 163 private static Boolean readValue( JCheckBox checkBox ) { 164 return checkBox.isSelected(); 165 } 166 167 private static String readValue( Document document ) { 168 try { 169 return document.getText( 0, document.getLength() ); 170 } catch ( BadLocationException e ) { 171 assert false : e; 172 return ""; 173 } 174 } 175 176 private static String readValue( JComboBox comboBox ) { 177 return (String )comboBox.getSelectedItem(); 178 } 179 180 182 private static <T> T getAsType(final EarProjectProperties earProperties, 183 String propertyName, Class <T> expectedType) { 184 return getAsType(earProperties, propertyName, expectedType, true); 185 } 186 187 @SuppressWarnings ("unchecked") 188 private static <T> T getAsType(final EarProjectProperties earProperties, 189 String propertyName, Class <T> expectedType, boolean throwException) { 190 T result = null; 191 Object value = earProperties.get(propertyName); 192 if (value == null || expectedType.isInstance(value)) { 193 result = (T) value; 194 } else if ( throwException ) { 195 throw new IllegalArgumentException ( "Value of property: " + propertyName + " exbected to be: " + expectedType.getClass().getName() + " but was: " + value.getClass().getName() + "!" ); } 199 return result; 200 } 201 202 private class ComponentListener implements ActionListener , DocumentListener { 203 204 206 public void actionPerformed( ActionEvent e ) { 207 Object source = e.getSource(); 208 String propertyName = component2property.get( source ); 209 if( propertyName != null ) { 210 if ( source instanceof JCheckBox ) { 211 earProperties.put( propertyName, readValue( (JCheckBox )source ) ); 212 } else if ( source instanceof VisualClasspathSupport ) { 213 earProperties.put( propertyName, ((VisualClasspathSupport)source).getVisualClassPathItems() ); 214 } else if ( source instanceof JComboBox ) { 215 if (((JComboBox ) source).getItemCount() == 0) { 216 return; 217 } 218 switch (comboType) { 219 case 0: 220 earProperties.put(propertyName, readValue((JComboBox ) source)); 221 break; 222 case 1: 223 earProperties.put(propertyName, comboValues[((JComboBox ) source).getSelectedIndex()]); 224 break; 225 default: 226 assert false : "Unknown comboType: " + comboType; 227 } 228 } else if ( source instanceof VisualArchiveIncludesSupport ) { 229 earProperties.put( propertyName, ((VisualArchiveIncludesSupport)source).getVisualWarItems() ); 230 } 231 } 232 } 233 234 236 public void changedUpdate( DocumentEvent e ) { 237 Document document = e.getDocument(); 238 String propertyName = component2property.get( document ); 239 if( propertyName != null ) { 240 earProperties.put( propertyName, readValue( document ) ); 241 } 242 } 243 244 public void insertUpdate( DocumentEvent e ) { 245 changedUpdate( e ); 246 } 247 248 public void removeUpdate( DocumentEvent e ) { 249 changedUpdate( e ); 250 } 251 } 252 253 } 254 | Popular Tags |