KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > archive > 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.archive.customizer;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import javax.swing.JCheckBox JavaDoc;
27 import javax.swing.JComboBox JavaDoc;
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import javax.swing.text.BadLocationException JavaDoc;
31 import javax.swing.text.Document JavaDoc;
32 import org.netbeans.modules.j2ee.archive.project.ArchiveProjectProperties;
33 import org.openide.ErrorManager;
34
35 /** Class which makes creation of the GUI easier. Registers JComponent
36  * property names and handles reading/storing the values from the components
37  * automaticaly.
38  *
39  * @author Petr Hrebejk (original)
40  */

41 public final class VisualPropertySupport {
42     
43     private static final String JavaDoc WRONG_TYPE = java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/archive/customizer/Bundle").getString("WrongType");
44     
45     private ArchiveProjectProperties apProperties;
46     private HashMap JavaDoc component2property;
47     private ComponentListener componentListener;
48     
49     private int comboType; //0 ... display text == value
50
//1 ... display text != value
51
private String JavaDoc[] comboValues;
52     
53     public VisualPropertySupport( ArchiveProjectProperties apProperties ) {
54         this.apProperties = apProperties;
55         this.component2property = new HashMap JavaDoc( 10 );
56         this.componentListener = new ComponentListener();
57     }
58         
59     /** Registers combo box.
60      */

61     public void register(JComboBox JavaDoc component, String JavaDoc displayNames[], String JavaDoc[] values, String JavaDoc propertyName) {
62         comboType = 1;
63         comboValues = values.clone();
64         String JavaDoc value = (String JavaDoc) getAsType(propertyName, String JavaDoc.class);
65         component2property.put(component, propertyName);
66         // Add all items and find the selected one
67
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     // Static methods for reading components and models ------------------------
84

85     private static String JavaDoc readValue( JCheckBox JavaDoc checkBox ) {
86         return checkBox.isSelected() ? "true" : "false"; // NOI18N
87
}
88     
89     private static String JavaDoc readValue( Document JavaDoc document ) {
90         try {
91             return document.getText( 0, document.getLength() );
92         } catch ( BadLocationException JavaDoc e ) {
93             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
94                     e);
95             return ""; // NOI18N
96
}
97     }
98     
99     private static String JavaDoc readValue( JComboBox JavaDoc comboBox ) {
100         return (String JavaDoc)comboBox.getSelectedItem();
101     }
102     
103     // Private methods ---------------------------------------------------------
104

105     private Object JavaDoc getAsType( String JavaDoc propertyName, Class JavaDoc expectedType ) {
106         return getAsType( propertyName, expectedType, true );
107     }
108     
109     private Object JavaDoc getAsType( String JavaDoc propertyName, Class JavaDoc expectedType, boolean throwException ) {
110         Object JavaDoc value = apProperties.get( propertyName );
111         
112         if ( value == null || expectedType.isInstance( value ) ) {
113             return value;
114         } else if ( throwException ) {
115             throw new IllegalArgumentException JavaDoc( "Value of property: " + propertyName + // NOI18N
116
" exbected to be: " + expectedType.getName() + // NOI18N
117
" but was: " + value.getClass().getName() + "!" ); // NOI18N
118
} else {
119             return WRONG_TYPE;
120         }
121         
122     }
123     
124     private class ComponentListener implements ActionListener JavaDoc, DocumentListener JavaDoc {
125         
126         // Implementation of action listener -----------------------------------
127

128         public void actionPerformed( ActionEvent JavaDoc e ) {
129             Object JavaDoc source = e.getSource();
130             String JavaDoc propertyName = (String JavaDoc)component2property.get( source );
131             if( propertyName != null ) {
132                 if ( source instanceof JCheckBox JavaDoc ) {
133                     apProperties.put( propertyName, readValue( (JCheckBox JavaDoc)source ) );
134                 } else if ( source instanceof JComboBox JavaDoc ) {
135                     if (((JComboBox JavaDoc) source).getItemCount() == 0) {
136                         return;
137                     }
138                     
139                     switch (comboType) {
140                         case 0: apProperties.put(propertyName, readValue((JComboBox JavaDoc) source));
141                         break;
142                         case 1: apProperties.put(propertyName, comboValues[((JComboBox JavaDoc) source).getSelectedIndex()]);
143                         break;
144                     }
145                 }
146             }
147         }
148         
149         // Implementation of document listener ---------------------------------
150

151         public void changedUpdate( DocumentEvent JavaDoc e ) {
152             Document JavaDoc document = e.getDocument();
153             String JavaDoc propertyName = (String JavaDoc)component2property.get( document );
154             if( propertyName != null ) {
155                 apProperties.put( propertyName, readValue( document ) );
156             }
157         }
158         
159         public void insertUpdate( DocumentEvent JavaDoc e ) {
160             changedUpdate( e );
161         }
162         
163         public void removeUpdate( DocumentEvent JavaDoc e ) {
164             changedUpdate( e );
165         }
166     }
167 }
168
Popular Tags