KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > types > ObjectTypeCustomizer


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.search.types;
22
23
24 import java.awt.event.HierarchyEvent JavaDoc;
25 import java.awt.event.HierarchyListener JavaDoc;
26 import java.beans.Customizer JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.TreeSet JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35 import javax.swing.border.CompoundBorder JavaDoc;
36 import javax.swing.border.TitledBorder JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38
39 import org.openide.ErrorManager;
40 import org.openide.awt.Mnemonics;
41 import org.openide.loaders.DataLoader;
42 import org.openide.loaders.DataLoaderPool;
43 import org.openide.util.Lookup;
44 import org.openide.util.NbBundle;
45
46
47 /**
48  * Customizer of <code>ObjectTypeType</code> bean.
49  *
50  * @author Petr Kuzel
51  * @author Marian Petras
52  */

53 public class ObjectTypeCustomizer extends JPanel JavaDoc implements Customizer JavaDoc,
54                                                             HierarchyListener JavaDoc {
55     
56     /**
57      * representation class name of the folder loader.
58      * This allows to eliminate object type &quot;Folders&quot;
59      * from the list of displayed object types.
60      */

61     private static final String JavaDoc FOLDER_LOADER_NAME
62             = "org.openide.loaders.DataFolder"; //NOI18N
63

64     /** Object to customize. */
65     private ObjectTypeType peer;
66     
67     /** Flag indicating the are setting of object in run. */
68     private boolean setting = false;
69     /**
70      * has this customizer been discovered since the Find dialog had been
71      * displayed?
72      */

73     private boolean discovered = false;
74     
75     /**
76      * cached collection of all loaders from the <code>DataLoaderPool</code>
77      *
78      * @see org.openide.loaders.DataLoaderPool#allLoaders()
79      */

80     private transient Collection JavaDoc loaders;
81
82     
83     /** Creates new form <code>ObjectTextCustomizer</code>. */
84     public ObjectTypeCustomizer() {
85         initComponents();
86         initAccessibility();
87         addHierarchyListener(this);
88         TitledBorder JavaDoc tb = new TitledBorder JavaDoc(NbBundle.getBundle(ObjectTypeCustomizer.class).getString("TEXT_LABEL_OBJECT_TYPE"));
89         
90         tb.setBorder(new CompoundBorder JavaDoc());
91         setBorder (tb);
92     }
93
94     private void initAccessibility() {
95         ResourceBundle JavaDoc bundle = NbBundle.getBundle(ObjectTypeCustomizer.class);
96         this.getAccessibleContext().setAccessibleDescription(bundle.getString("TEXT_LABEL_OBJECT_TYPE"));
97         typeList.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_TypeList"));
98         typeList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TypeList"));
99         jScrollPane1.getHorizontalScrollBar().getAccessibleContext()
100                 .setAccessibleName(bundle.getString("ACSN_HScrollBar"));//NOI18N
101
jScrollPane1.getVerticalScrollBar().getAccessibleContext()
102                 .setAccessibleName(bundle.getString("ACSN_VScrollBar"));//NOI18N
103
}
104
105     /** This method is called from within the constructor to
106      * initialize the form.
107      * WARNING: Do NOT modify this code. The content of this method is
108      * always regenerated by the FormEditor.
109      */

110     private void initComponents() {//GEN-BEGIN:initComponents
111
lblType = new javax.swing.JLabel JavaDoc();
112         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
113         typeList = new javax.swing.JList JavaDoc();
114
115         setLayout(new java.awt.BorderLayout JavaDoc(0, 6));
116
117         lblType.setLabelFor(typeList);
118         Mnemonics.setLocalizedText(lblType, NbBundle.getMessage(ObjectTypeCustomizer.class, "LBL_Type"));
119         add(lblType, java.awt.BorderLayout.NORTH);
120
121         typeList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
122             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc evt) {
123                 typeListValueChanged(evt);
124             }
125         });
126
127         jScrollPane1.setViewportView(typeList);
128
129         add(jScrollPane1, java.awt.BorderLayout.CENTER);
130
131     }//GEN-END:initComponents
132

133     private void typeListValueChanged (javax.swing.event.ListSelectionEvent JavaDoc evt) {//GEN-FIRST:event_typeListValueChanged
134
if (setting) {
135             // ignore calls caused by setObject() implementation
136
return;
137         }
138         if (loaders == null) {
139             peer.setMask(new Class JavaDoc[0]);
140             return;
141         }
142
143         List JavaDoc toret = new ArrayList JavaDoc();
144         Object JavaDoc[] sel = typeList.getSelectedValues();
145
146         for (Iterator JavaDoc i = loaders.iterator(); i.hasNext(); ) {
147             DataLoader nextLoader = (DataLoader) i.next();
148             String JavaDoc id = nextLoader.getDisplayName();
149
150             if (id == null) {
151                 continue; //may be null :-(
152
}
153             for (int j = 0; j < sel.length; j++) {
154                 if (sel[j] == null) {
155                     continue; //may be null :-(
156
}
157                 if (id.equals((String JavaDoc) sel[j])) {
158                     toret.add(nextLoader);
159                     break;
160                 }
161             }
162         }
163
164         Class JavaDoc[] ret = new Class JavaDoc[toret.size()];
165
166         Iterator JavaDoc it = toret.iterator();
167         int k = 0;
168         while (it.hasNext()) {
169             ret[k++] = it.next().getClass();
170         }
171
172         peer.setMask(ret);
173     }//GEN-LAST:event_typeListValueChanged
174

175
176     // Variables declaration - do not modify//GEN-BEGIN:variables
177
private javax.swing.JScrollPane JavaDoc jScrollPane1;
178     private javax.swing.JLabel JavaDoc lblType;
179     private javax.swing.JList JavaDoc typeList;
180     // End of variables declaration//GEN-END:variables
181

182     // implements java.beans.Customizer
183
public void setObject(Object JavaDoc obj) {
184         peer = (ObjectTypeType) obj;
185     }
186     
187     /**
188      * Loads names of registered object types into the <code>JList</code>.
189      */

190     private void loadObjectTypes() {
191         assert loaders == null;
192         
193         setting = true;
194         try {
195
196             Enumeration JavaDoc en = ((DataLoaderPool) Lookup.getDefault().lookup(DataLoaderPool.class))
197                              .allLoaders();
198
199             // Model data for list.
200
TreeSet JavaDoc types = new TreeSet JavaDoc();
201
202             ArrayList JavaDoc selected = new ArrayList JavaDoc();
203
204             boolean folderLoaderFound = false;
205             
206             Class JavaDoc[] mask = peer.getMask();
207             if ((mask != null) && (mask.length == 0)) {
208                 mask = null;
209             }
210             int loadersToFind = (mask != null) ? mask.length : 0;
211             
212             loaders = new ArrayList JavaDoc();
213             while (en.hasMoreElements()) {
214                 DataLoader nextLoader = (DataLoader) en.nextElement();
215                 loaders.add(nextLoader);
216                 
217                 /*
218                  * Skip the folder object type - we are not able to search
219                  * for folders:
220                  */

221                 if (!folderLoaderFound) {
222                     if (FOLDER_LOADER_NAME
223                             .equals(nextLoader.getRepresentationClassName())) {
224                         folderLoaderFound = true;
225                         continue;
226                     }
227                 }
228
229                 String JavaDoc displayName;
230                 try {
231                     displayName = nextLoader.getDisplayName();
232                 } catch (Exception JavaDoc ex) {
233                     ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
234                     continue;
235                 }
236                 if (displayName == null) {
237                     continue;
238                 }
239                 types.add(displayName);
240                 
241                 // Create indices of values to select.
242
if (loadersToFind > 0) {
243                     Class JavaDoc repreClass = nextLoader.getRepresentationClass();
244                     if (repreClass != null) {
245                         for (int j = 0; j < mask.length; j++) {
246                             if (mask[j] == null) {
247                                 continue;
248                             }
249                             DataLoader loader = DataLoader.getLoader(mask[j]);
250                             if (loader != null && repreClass.equals(loader.getRepresentationClass())) {
251                                 selected.add(displayName);
252                                 loadersToFind--;
253                                 mask[j] = null;
254                                 break;
255                             }
256                         }
257                     }
258                 }
259             }
260             ((ArrayList JavaDoc) loaders).trimToSize();
261
262             // select saved values
263
typeList.setListData((String JavaDoc[]) types.toArray(new String JavaDoc[types.size()]));
264
265             if (selected.size() > 0) {
266                 int[] indexes = new int[selected.size()];
267
268                 ArrayList JavaDoc typesList = new ArrayList JavaDoc(types);
269                 for (int i = 0; i < indexes.length; i++) {
270                     indexes[i] = typesList.indexOf(selected.get(i));
271                 }
272                 typeList.setSelectedIndices(indexes);
273             }
274         } catch (Exception JavaDoc e) {
275             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
276         } finally {
277             setting = false;
278         }
279
280     }
281
282     /** Dummy implementation of <code>Customizer</code> interface method. */
283     public void addPropertyChangeListener(PropertyChangeListener JavaDoc p1) {}
284
285     /** Dummy implementation of <code>Customizer</code> interface method. */
286     public void removePropertyChangeListener(PropertyChangeListener JavaDoc p1) {}
287
288     /**
289      * Initiates loading of object types the first time this customizer
290      * is displayed.
291      */

292     public void hierarchyChanged(HierarchyEvent JavaDoc e) {
293         if (discovered) {
294             return;
295         }
296         
297         if (((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0)
298                 && isShowing()) {
299             discovered = true;
300             loadObjectTypes();
301         }
302     }
303     
304     /** Overrides superclass method. Requests ffocus si delegated to typeList component. */
305     public void requestFocus() {
306         typeList.requestFocus();
307     }
308
309     
310 }
311
Popular Tags