KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > ServiceTypePanel


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.beaninfo.editors;
21
22 import java.util.*;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 import org.openide.*;
27 import org.openide.explorer.*;
28 import org.openide.nodes.*;
29 import org.openide.util.HelpCtx;
30 import org.openide.util.Lookup;
31
32 /** Service type panel for viewing, selecting and configuring
33 * of executors and other services.
34 *
35 * @author Jaroslav Tulach
36 */

37 @SuppressWarnings JavaDoc("deprecation")
38 public class ServiceTypePanel extends org.netbeans.beaninfo.ExplorerPanel {
39
40     private int width_components=0,width_leftcomponent=0;
41
42     /** the super class of objects that we display.
43     */

44     private Class JavaDoc<? extends ServiceType> clazz;
45
46     /** list of all services */
47     private List<ServiceType> services;
48
49     /** @see ServiceTypeEditor#none */
50     private ServiceType none;
51     
52     /**
53      * False - we are selecting from the registered service types, true - creating
54      * new instances of the services
55      */

56     private boolean createNew = false;
57
58     static final long serialVersionUID =861345226525021334L;
59     /** Creates new Panel PropertyEditor
60     * @param clazz the super class of objects that we display
61     * @param name string to name the panel with
62     * @param none no-op type, or null
63     */

64     public ServiceTypePanel(Class JavaDoc<? extends ServiceType> clazz, String JavaDoc name, ServiceType none, boolean createNew) {
65         this.clazz = clazz;
66         this.none = none;
67         this.createNew = createNew;
68         update ();
69
70         initComponents ();
71
72         // #20886 Workaround for jdk JSplitPane bug.
73
handleDividerLocation();
74         
75         label.setText(name);
76         listView1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(ServiceTypePanel.class).getString("ACSD_ServiceTypeList"));
77         getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getBundle(ServiceTypePanel.class).getString("ACSD_ServiceTypePanel"));
78
79         getExplorerManager ().addPropertyChangeListener (new java.beans.PropertyChangeListener JavaDoc () {
80                     public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
81                         if ( ExplorerManager.PROP_SELECTED_NODES.equals(ev.getPropertyName()) ) {
82                             firePropertyChange( DialogDescriptor.PROP_HELP_CTX, null, null );
83                         }
84                         firePropertyChange ();
85                     }
86                 });
87         if (name.length() > 0) {
88             label.setDisplayedMnemonic(name.charAt(0));
89         }
90     }
91
92     private void handleDividerLocation() {
93         // There is a problem with divider of JSplitPane
94
// in some cases -> see bugtraq #4786896.
95
int listWidth = listView1.getPreferredSize().width;
96         int propWidth = propertySheetView1.getPreferredSize().width;
97         int splitWidth = jSplitPane1.getPreferredSize().width;
98         int location = (int)((float)listWidth/(listWidth + propWidth) * splitWidth);
99         if(location > 0) {
100             jSplitPane1.setDividerLocation(location);
101         }
102     }
103     
104     public HelpCtx getHelpCtx () {
105         return HelpCtx.DEFAULT_HELP;
106     }
107
108     /** Sets the selected value of the component.
109     */

110     public void setServiceType (ServiceType s) {
111         if (s == null) {
112             return;
113         }
114         
115         int i = -1;//services.indexOf (s);
116
for (int n = 0; n < services.size(); n++) {
117             if ((services.get(n)).getName().equals(s.getName())) {
118                 i = n;
119             }
120         }
121
122         if (i < 0) {
123             // if s is not found try to add s to the nodes by temporarily
124
// assigning the value to the none var (none value is added to the list of services)
125
ServiceType oldNone = none;
126             none = s;
127             update(); // recreates the nodes
128
none = oldNone;
129             i = services.indexOf (s);
130         }
131
132         if (i < 0) {
133             Logger.getAnonymousLogger().warning("ServiceTypePanel: Unable to add service " + s.getName()); // NOI18N
134
i = 0;
135         }
136         
137         Node[] nodes = getExplorerManager ().getRootContext ().getChildren ().getNodes ();
138         if (i >= nodes.length) return;
139
140         try {
141             getExplorerManager ().setSelectedNodes (new Node[] {
142                                                         nodes[i]
143                                                     });
144         } catch (java.beans.PropertyVetoException JavaDoc ex) {
145             Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex);
146         }
147
148         firePropertyChange ();
149     }
150
151     /** Sets the selected value of the component.
152     * @return selected type or null
153     */

154     public ServiceType getServiceType () {
155         Node[] arr = getExplorerManager ().getSelectedNodes ();
156         if (arr.length > 0) {
157             return ((MN) arr[0]).getServiceType ();
158         }
159         return null;
160     }
161
162     /** Fires property change.
163     */

164     void firePropertyChange () {
165         firePropertyChange ("serviceType", null, null); // NOI18N
166
}
167
168     /** Updates the current state of the explorer manager.
169     */

170     private void update () {
171         Children ch = new Children.Array ();
172         AbstractNode n = new AbstractNode (ch);
173
174         ch.add ((Node[])nodes ().toArray (new Node[nodes().size()]));
175
176         getExplorerManager ().setRootContext (n);
177         setActivatedNodes(new Node[0]);
178     }
179
180     /** Computes the list of nodes that should represent all services classes
181     * of the given type.
182     *
183     * @return list of Nodes
184     */

185     private List<Node> nodes () {
186         services = new ArrayList<ServiceType> (20);
187         List<Node> l = new LinkedList<Node> ();
188         ServiceType.Registry registry = Lookup.getDefault().lookup(ServiceType.Registry.class);
189         Enumeration<? extends ServiceType> en = registry.services (clazz);
190         while (en.hasMoreElements ()) {
191             try {
192                 ServiceType service = en.nextElement ();
193                 if (createNew) {
194                     // in this case create a new instance for all types
195
ServiceType newObject = service.getClass().newInstance();
196                     l.add(new MN(newObject));
197                     services.add(newObject);
198                 } else {
199                     l.add (new MN (service));
200                     services.add (service);
201                 }
202             } catch (java.beans.IntrospectionException JavaDoc ex) {
203                 Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex);
204             } catch (InstantiationException JavaDoc ex) {
205                 Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex);
206             } catch (IllegalAccessException JavaDoc ex) {
207                 Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex);
208             }
209         }
210         try {
211             if (none != null) {
212                 l.add (new MN (none));
213                 services.add (none);
214             }
215         } catch (java.beans.IntrospectionException JavaDoc ex) {
216             Logger.getLogger(ServiceTypePanel.class.getName()).log(Level.WARNING, null, ex);
217         }
218         return l;
219     }
220
221     /** This method is called from within the constructor to
222      * initialize the form.
223      * WARNING: Do NOT modify this code. The content of this method is
224      * always regenerated by the FormEditor.
225      */

226     private void initComponents() {//GEN-BEGIN:initComponents
227
jSplitPane1 = new javax.swing.JSplitPane JavaDoc();
228         listView1 = new org.openide.explorer.view.ListView();
229         propertySheetView1 = new org.openide.explorer.propertysheet.PropertySheetView();
230         label = new javax.swing.JLabel JavaDoc();
231
232         setLayout(new java.awt.BorderLayout JavaDoc(0, 2));
233
234         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 0, 11)));
235         jSplitPane1.setDividerSize(5);
236         jSplitPane1.addComponentListener(new java.awt.event.ComponentAdapter JavaDoc() {
237             public void componentResized(java.awt.event.ComponentEvent JavaDoc evt) {
238                 jSplitPane1ComponentResized(evt);
239             }
240         });
241
242         listView1.setDefaultProcessor(new java.awt.event.ActionListener JavaDoc() { public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {} });
243         listView1.setPopupAllowed(false);
244         listView1.setSelectionMode(1);
245         listView1.setTraversalAllowed(false);
246         listView1.addComponentListener(new java.awt.event.ComponentAdapter JavaDoc() {
247             public void componentResized(java.awt.event.ComponentEvent JavaDoc evt) {
248                 listView1ComponentResized(evt);
249             }
250         });
251
252         jSplitPane1.setLeftComponent(listView1);
253
254         jSplitPane1.setRightComponent(propertySheetView1);
255
256         add(jSplitPane1, java.awt.BorderLayout.CENTER);
257
258         label.setLabelFor(listView1);
259         add(label, java.awt.BorderLayout.NORTH);
260
261     }//GEN-END:initComponents
262

263     private void listView1ComponentResized(java.awt.event.ComponentEvent JavaDoc evt) {//GEN-FIRST:event_listView1ComponentResized
264
width_leftcomponent=listView1.getWidth();
265     }//GEN-LAST:event_listView1ComponentResized
266

267     private void jSplitPane1ComponentResized(java.awt.event.ComponentEvent JavaDoc evt) {//GEN-FIRST:event_jSplitPane1ComponentResized
268
int width,locator;
269         if(width_components>0&&width_leftcomponent>0) {
270             width=listView1.getWidth()+propertySheetView1.getWidth();
271             locator=width*width_leftcomponent/width_components;
272             jSplitPane1.setDividerLocation(locator);
273             width_leftcomponent=locator;
274             width_components=width;
275         } else {
276             width_leftcomponent=listView1.getWidth();
277             width_components=width_leftcomponent+propertySheetView1.getWidth();
278         }
279     }//GEN-LAST:event_jSplitPane1ComponentResized
280

281
282     private void removeButtonPressed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonPressed
283
}//GEN-LAST:event_removeButtonPressed
284

285     private void addButtonPressed (java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_addButtonPressed
286
}//GEN-LAST:event_addButtonPressed
287

288
289     // Variables declaration - do not modify//GEN-BEGIN:variables
290
private javax.swing.JSplitPane JavaDoc jSplitPane1;
291     private javax.swing.JLabel JavaDoc label;
292     private org.openide.explorer.view.ListView listView1;
293     private org.openide.explorer.propertysheet.PropertySheetView propertySheetView1;
294     // End of variables declaration//GEN-END:variables
295

296     /** Node for displaying services */
297     private final class MN extends BeanNode {
298         public MN (ServiceType t) throws java.beans.IntrospectionException JavaDoc {
299             super (t);
300         }
301
302         public ServiceType getServiceType () {
303             return (ServiceType)getBean ();
304         }
305
306         // Prevent folks from changing the name here!
307
public Node.PropertySet[] getPropertySets () {
308             final Node.PropertySet[] sets = super.getPropertySets ();
309             if (createNew) {
310                 return sets; // when creating new copies user can change the name
311
}
312             Node.PropertySet[] nue = new Node.PropertySet[sets.length];
313             for (int i = 0; i < sets.length; i++) {
314                 final int ii = i;
315                 nue[i] = new Node.PropertySet () {
316                              {
317                                  this.setName (sets[ii].getName ());
318                                  this.setDisplayName (sets[ii].getDisplayName ());
319                                  this.setShortDescription (sets[ii].getShortDescription ());
320                              }
321                              public Node.Property[] getProperties () {
322                                  Node.Property[] props = sets[ii].getProperties ();
323                                  List<Node.Property> nueprops = new ArrayList<Node.Property> ();
324                                  for (int j = 0; j < props.length; j++)
325                                      if (! "name".equals (props[j].getName ())) // NOI18N
326
nueprops.add (props[j]);
327                                  return nueprops.toArray (new Node.Property[nueprops.size ()]);
328                              }
329                          };
330             }
331             return nue;
332         }
333     }
334 }
335
Popular Tags