KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > customizer > VisualPropertySupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.earproject.ui.customizer;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.swing.JCheckBox JavaDoc;
29 import javax.swing.JComboBox JavaDoc;
30 import javax.swing.JTextField JavaDoc;
31 import javax.swing.event.DocumentEvent JavaDoc;
32 import javax.swing.event.DocumentListener JavaDoc;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.Document JavaDoc;
35
36 /**
37  * Class which makes creation of the GUI easier. Registers JComponent
38  * property names and handles reading/storing the values from the components
39  * automatically.
40  *
41  * @author Petr Hrebejk
42  */

43 public final class VisualPropertySupport {
44     
45     private EarProjectProperties earProperties;
46     private Map JavaDoc<Object JavaDoc, String JavaDoc> component2property;
47     private ComponentListener componentListener;
48     
49     /**
50      * 0 ... display text == value<br>
51      * 1 ... display text != value
52      */

53     private int comboType = -1;
54     
55     private String JavaDoc[] comboValues;
56     
57     public VisualPropertySupport(final EarProjectProperties earProperties) {
58         this.earProperties = earProperties;
59         this.component2property = new HashMap JavaDoc<Object JavaDoc, String JavaDoc>(10);
60         this.componentListener = new ComponentListener();
61     }
62         
63     /** Registers the component with given property, Fills the component
64      * with given object.
65      */

66     public void register( JCheckBox JavaDoc component, String JavaDoc propertyName ) {
67         Boolean JavaDoc value = getAsType(earProperties, propertyName, Boolean JavaDoc.class);
68         component2property.put( component, propertyName );
69         component.setSelected( value != null && value.booleanValue() );
70         component.removeActionListener( componentListener );
71         component.addActionListener( componentListener );
72     }
73     
74     /** Registers the component with given property, Fills the component
75      * with given object.
76      */

77     public void register( JTextField JavaDoc component, String JavaDoc propertyName ) {
78         String JavaDoc value = getAsType(earProperties, propertyName, String JavaDoc.class);
79         component2property.put( component.getDocument(), propertyName );
80         component.setText( value != null ? value : "" );
81         component.getDocument().addDocumentListener( componentListener );
82     }
83     
84     /**
85      * Registers JTable containing VisualClassPath items and accompanying
86      * buttons for handling the class path.
87      */

88     public void register( VisualClasspathSupport component, String JavaDoc propertyName ) {
89         @SuppressWarnings JavaDoc("unchecked")
90         List JavaDoc<VisualClassPathItem> value = getAsType(earProperties, propertyName, List JavaDoc.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     /**
98      * Registers JList containing VisualClassPath items and accompanying
99      * buttons for handling the class path.
100      */

101     public void register(VisualArchiveIncludesSupport component, String JavaDoc propertyName) {
102         @SuppressWarnings JavaDoc("unchecked")
103         List JavaDoc<VisualClassPathItem> value = getAsType(earProperties, propertyName, List JavaDoc.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     /** Registers combo box. */
111     public void register(JComboBox JavaDoc component, String JavaDoc items[], String JavaDoc propertyName) {
112         checkJComboBoxRegistered();
113         comboType = 0;
114         String JavaDoc value = getAsType(earProperties, propertyName, String JavaDoc.class);
115         component2property.put( component, propertyName );
116         // Add all items and find the selected one
117
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     /** Registers combo box. */
133     public void register(JComboBox JavaDoc component, String JavaDoc displayNames[], String JavaDoc[] values, String JavaDoc propertyName) {
134         checkJComboBoxRegistered();
135         comboType = 1;
136         comboValues = values;
137         String JavaDoc value = getAsType(earProperties, propertyName, String JavaDoc.class);
138         component2property.put(component, propertyName);
139         // Add all items and find the selected one
140
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. " + // NOI18N
158
"Another VisualPropertySupport instance may be used."; // NOI18N
159
}
160     
161     // Static methods for reading components and models ------------------------
162

163     private static Boolean JavaDoc readValue( JCheckBox JavaDoc checkBox ) {
164         return checkBox.isSelected();
165     }
166     
167     private static String JavaDoc readValue( Document JavaDoc document ) {
168         try {
169             return document.getText( 0, document.getLength() );
170         } catch ( BadLocationException JavaDoc e ) {
171             assert false : e;
172             return "";
173         }
174     }
175     
176     private static String JavaDoc readValue( JComboBox JavaDoc comboBox ) {
177         return (String JavaDoc)comboBox.getSelectedItem();
178     }
179     
180     // Private methods ---------------------------------------------------------
181

182     private static <T> T getAsType(final EarProjectProperties earProperties,
183             String JavaDoc propertyName, Class JavaDoc<T> expectedType) {
184         return getAsType(earProperties, propertyName, expectedType, true);
185     }
186     
187     @SuppressWarnings JavaDoc("unchecked")
188     private static <T> T getAsType(final EarProjectProperties earProperties,
189             String JavaDoc propertyName, Class JavaDoc<T> expectedType, boolean throwException) {
190         T result = null;
191         Object JavaDoc value = earProperties.get(propertyName);
192         if (value == null || expectedType.isInstance(value)) {
193             result = (T) value;
194         } else if ( throwException ) {
195             throw new IllegalArgumentException JavaDoc( "Value of property: " + propertyName + // NOI18N
196
" exbected to be: " + expectedType.getClass().getName() + // NOI18N
197
" but was: " + value.getClass().getName() + "!" ); // NOI18N
198
}
199         return result;
200     }
201     
202     private class ComponentListener implements ActionListener JavaDoc, DocumentListener JavaDoc {
203         
204         // Implementation of action listener -----------------------------------
205

206         public void actionPerformed( ActionEvent JavaDoc e ) {
207             Object JavaDoc source = e.getSource();
208             String JavaDoc propertyName = component2property.get( source );
209             if( propertyName != null ) {
210                 if ( source instanceof JCheckBox JavaDoc ) {
211                     earProperties.put( propertyName, readValue( (JCheckBox JavaDoc)source ) );
212                 } else if ( source instanceof VisualClasspathSupport ) {
213                     earProperties.put( propertyName, ((VisualClasspathSupport)source).getVisualClassPathItems() );
214                 } else if ( source instanceof JComboBox JavaDoc ) {
215                     if (((JComboBox JavaDoc) source).getItemCount() == 0) {
216                         return;
217                     }
218                     switch (comboType) {
219                         case 0:
220                             earProperties.put(propertyName, readValue((JComboBox JavaDoc) source));
221                             break;
222                         case 1:
223                             earProperties.put(propertyName, comboValues[((JComboBox JavaDoc) 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         // Implementation of document listener ---------------------------------
235

236         public void changedUpdate( DocumentEvent JavaDoc e ) {
237             Document JavaDoc document = e.getDocument();
238             String JavaDoc propertyName = component2property.get( document );
239             if( propertyName != null ) {
240                 earProperties.put( propertyName, readValue( document ) );
241             }
242         }
243         
244         public void insertUpdate( DocumentEvent JavaDoc e ) {
245             changedUpdate( e );
246         }
247         
248         public void removeUpdate( DocumentEvent JavaDoc e ) {
249             changedUpdate( e );
250         }
251     }
252
253 }
254
Popular Tags