1 19 20 package org.netbeans.modules.j2ee.archive.customizer; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.util.HashMap ; 25 26 import javax.swing.JCheckBox ; 27 import javax.swing.JComboBox ; 28 import javax.swing.event.DocumentEvent ; 29 import javax.swing.event.DocumentListener ; 30 import javax.swing.text.BadLocationException ; 31 import javax.swing.text.Document ; 32 import org.netbeans.modules.j2ee.archive.project.ArchiveProjectProperties; 33 import org.openide.ErrorManager; 34 35 41 public final class VisualPropertySupport { 42 43 private static final String WRONG_TYPE = java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/customizer/Bundle").getString("WrongType"); 44 45 private ArchiveProjectProperties apProperties; 46 private HashMap component2property; 47 private ComponentListener componentListener; 48 49 private int comboType; private String [] comboValues; 52 53 public VisualPropertySupport( ArchiveProjectProperties apProperties ) { 54 this.apProperties = apProperties; 55 this.component2property = new HashMap ( 10 ); 56 this.componentListener = new ComponentListener(); 57 } 58 59 61 public void register(JComboBox component, String displayNames[], String [] values, String propertyName) { 62 comboType = 1; 63 comboValues = values.clone(); 64 String value = (String ) getAsType(propertyName, String .class); 65 component2property.put(component, propertyName); 66 component.removeAllItems(); 68 int selectedIndex = 0; 69 for (int i = 0; i < displayNames.length; i++) { 70 component.addItem(displayNames[i]); 71 if (values[i].equals(value)) { 72 selectedIndex = i; 73 } 74 } 75 if (selectedIndex < component.getItemCount()) { 76 component.setSelectedIndex( selectedIndex ); 77 } 78 component.removeActionListener( componentListener ); 79 component.addActionListener(componentListener); 80 } 81 82 83 85 private static String readValue( JCheckBox checkBox ) { 86 return checkBox.isSelected() ? "true" : "false"; } 88 89 private static String readValue( Document document ) { 90 try { 91 return document.getText( 0, document.getLength() ); 92 } catch ( BadLocationException e ) { 93 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 94 e); 95 return ""; } 97 } 98 99 private static String readValue( JComboBox comboBox ) { 100 return (String )comboBox.getSelectedItem(); 101 } 102 103 105 private Object getAsType( String propertyName, Class expectedType ) { 106 return getAsType( propertyName, expectedType, true ); 107 } 108 109 private Object getAsType( String propertyName, Class expectedType, boolean throwException ) { 110 Object value = apProperties.get( propertyName ); 111 112 if ( value == null || expectedType.isInstance( value ) ) { 113 return value; 114 } else if ( throwException ) { 115 throw new IllegalArgumentException ( "Value of property: " + propertyName + " exbected to be: " + expectedType.getName() + " but was: " + value.getClass().getName() + "!" ); } else { 119 return WRONG_TYPE; 120 } 121 122 } 123 124 private class ComponentListener implements ActionListener , DocumentListener { 125 126 128 public void actionPerformed( ActionEvent e ) { 129 Object source = e.getSource(); 130 String propertyName = (String )component2property.get( source ); 131 if( propertyName != null ) { 132 if ( source instanceof JCheckBox ) { 133 apProperties.put( propertyName, readValue( (JCheckBox )source ) ); 134 } else if ( source instanceof JComboBox ) { 135 if (((JComboBox ) source).getItemCount() == 0) { 136 return; 137 } 138 139 switch (comboType) { 140 case 0: apProperties.put(propertyName, readValue((JComboBox ) source)); 141 break; 142 case 1: apProperties.put(propertyName, comboValues[((JComboBox ) source).getSelectedIndex()]); 143 break; 144 } 145 } 146 } 147 } 148 149 151 public void changedUpdate( DocumentEvent e ) { 152 Document document = e.getDocument(); 153 String propertyName = (String )component2property.get( document ); 154 if( propertyName != null ) { 155 apProperties.put( propertyName, readValue( document ) ); 156 } 157 } 158 159 public void insertUpdate( DocumentEvent e ) { 160 changedUpdate( e ); 161 } 162 163 public void removeUpdate( DocumentEvent e ) { 164 changedUpdate( e ); 165 } 166 } 167 } 168 | Popular Tags |