KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > IndexedEditorPanel


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 package org.openide.explorer.propertysheet;
20
21 import org.openide.awt.Mnemonics;
22 import org.openide.explorer.ExplorerManager;
23 import org.openide.explorer.view.TreeTableView;
24 import org.openide.nodes.Node;
25 import org.openide.util.Lookup;
26 import org.openide.util.NbBundle;
27 import org.openide.util.actions.NodeAction;
28 import org.openide.util.actions.SystemAction;
29
30 import java.awt.Component JavaDoc;
31
32 import java.beans.*;
33
34 import javax.swing.Action JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.JScrollPane JavaDoc;
37 import javax.swing.Scrollable JavaDoc;
38 import javax.swing.UIManager JavaDoc;
39 import javax.swing.border.Border JavaDoc;
40
41
42 /**
43  * Panel displaying indexed properties.
44  * @author dstrupl@netbeans.org
45  */

46 class IndexedEditorPanel extends javax.swing.JPanel JavaDoc implements ExplorerManager.Provider, PropertyChangeListener,
47     Lookup.Provider {
48     private ExplorerManager em;
49
50     /** lookup for move up and down actions */
51     private Lookup selectedLookup;
52     private Action moveUp;
53     private Action moveDown;
54     private Action newAction;
55     private boolean showingDetails = false;
56     private Node rootNode;
57     private Node.Property prop;
58     private JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
59     private JPanel JavaDoc detailsPanel = new JPanel JavaDoc();
60
61     // Variables declaration - do not modify//GEN-BEGIN:variables
62
private javax.swing.JButton JavaDoc deleteButton;
63     private javax.swing.JButton JavaDoc detailsButton;
64     private javax.swing.JButton JavaDoc downButton;
65     private javax.swing.JPanel JavaDoc jPanel1;
66     private javax.swing.JPanel JavaDoc jPanel2;
67     private javax.swing.JButton JavaDoc newButton;
68     private javax.swing.JLabel JavaDoc propertiesLabel;
69     private javax.swing.JButton JavaDoc upButton;
70     // End of variables declaration//GEN-END:variables
71
// XXX look into constructor
72
private TreeTableView treeTableView1;
73
74     /** Creates new form IndexedEditorPanel */
75     public IndexedEditorPanel(Node node, Node.Property[] props) {
76         treeTableView1 = new TreeTableView();
77
78         // install proper border
79
setBorder((Border JavaDoc) UIManager.get("Nb.ScrollPane.border")); // NOI18N
80
initComponents();
81         propertiesLabel.setLabelFor(treeTableView1);
82
83         jPanel2.setLayout(new java.awt.BorderLayout JavaDoc());
84         jPanel2.add(treeTableView1);
85
86         detailsPanel.setLayout(new java.awt.BorderLayout JavaDoc());
87         getExplorerManager().setRootContext(node);
88
89         rootNode = node;
90         prop = props[0];
91         getExplorerManager().addPropertyChangeListener(this);
92         treeTableView1.setProperties(props);
93         treeTableView1.setRootVisible(false);
94         treeTableView1.setDefaultActionAllowed(false);
95         treeTableView1.setTreePreferredWidth(200);
96
97         node.addPropertyChangeListener(this);
98
99         try {
100             selectedLookup = org.openide.util.lookup.Lookups.proxy(this);
101
102             NodeAction globalMoveUp = SystemAction.get(Class.forName("org.openide.actions.MoveUpAction").asSubclass(NodeAction.class)); // NOI18N
103
NodeAction globalMoveDown = SystemAction.get(Class.forName("org.openide.actions.MoveDownAction").asSubclass(NodeAction.class)); // NOI18N
104
NodeAction globalNewAction = SystemAction.get(Class.forName("org.openide.actions.NewAction").asSubclass(NodeAction.class)); // NOI18N
105

106             // Get context aware instances.
107
moveUp = globalMoveUp.createContextAwareInstance(selectedLookup);
108             moveDown = globalMoveDown.createContextAwareInstance(selectedLookup);
109             newAction = globalNewAction.createContextAwareInstance(selectedLookup);
110         } catch (ClassNotFoundException JavaDoc cnfe) {
111         }
112
113         java.util.ResourceBundle JavaDoc bundle = NbBundle.getBundle(IndexedEditorPanel.class);
114         treeTableView1.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Properties"));
115         newButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_New"));
116         deleteButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_Delete"));
117         upButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MoveUp"));
118         downButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MoveDown"));
119         getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_IndexedEditorPanel"));
120     }
121
122     public void addNotify() {
123         super.addNotify();
124         updateButtonState();
125     }
126
127     /** Returns the lookup of currently selected node.
128      */

129     public Lookup getLookup() {
130         Node[] arr = getExplorerManager().getSelectedNodes();
131
132         return (arr.length == 1) ? arr[0].getLookup() : Lookup.EMPTY;
133     }
134
135     /** This method is called from within the constructor to
136      * initialize the form.
137      * WARNING: Do NOT modify this code. The content of this method is
138      * always regenerated by the Form Editor.
139      */

140     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
141
private void initComponents() {
142         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
143
144         jPanel1 = new javax.swing.JPanel JavaDoc();
145         newButton = new javax.swing.JButton JavaDoc();
146         deleteButton = new javax.swing.JButton JavaDoc();
147         upButton = new javax.swing.JButton JavaDoc();
148         downButton = new javax.swing.JButton JavaDoc();
149         detailsButton = new javax.swing.JButton JavaDoc();
150         propertiesLabel = new javax.swing.JLabel JavaDoc();
151         jPanel2 = new javax.swing.JPanel JavaDoc();
152
153         FormListener formListener = new FormListener();
154
155         setLayout(new java.awt.GridBagLayout JavaDoc());
156
157         setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 0, 12));
158         jPanel1.setLayout(new java.awt.GridLayout JavaDoc(5, 1, 0, 5));
159
160         org.openide.awt.Mnemonics.setLocalizedText(newButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_New"));
161         newButton.addActionListener(formListener);
162
163         jPanel1.add(newButton);
164
165         org.openide.awt.Mnemonics.setLocalizedText(deleteButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_Delete"));
166         deleteButton.addActionListener(formListener);
167
168         jPanel1.add(deleteButton);
169
170         org.openide.awt.Mnemonics.setLocalizedText(upButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_MoveUp"));
171         upButton.addActionListener(formListener);
172
173         jPanel1.add(upButton);
174
175         org.openide.awt.Mnemonics.setLocalizedText(downButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_MoveDown"));
176         downButton.addActionListener(formListener);
177
178         jPanel1.add(downButton);
179
180         org.openide.awt.Mnemonics.setLocalizedText(detailsButton, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_HideDetails"));
181         detailsButton.addActionListener(formListener);
182
183         jPanel1.add(detailsButton);
184
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.gridx = 1;
187         gridBagConstraints.gridy = 1;
188         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
189         gridBagConstraints.gridheight = java.awt.GridBagConstraints.RELATIVE;
190         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
191         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 0);
192         add(jPanel1, gridBagConstraints);
193
194         org.openide.awt.Mnemonics.setLocalizedText(propertiesLabel, org.openide.util.NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_Properties"));
195         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
196         gridBagConstraints.gridx = 0;
197         gridBagConstraints.gridy = 0;
198         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
199         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
200         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 2, 11);
201         add(propertiesLabel, gridBagConstraints);
202
203         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
204         gridBagConstraints.gridx = 0;
205         gridBagConstraints.gridy = 1;
206         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
207         gridBagConstraints.weightx = 1.0;
208         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 11, 11);
209         add(jPanel2, gridBagConstraints);
210
211     }
212
213     // Code for dispatching events from components to event handlers.
214

215     private class FormListener implements java.awt.event.ActionListener JavaDoc {
216         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
217             if (evt.getSource() == newButton) {
218                 IndexedEditorPanel.this.newButtonActionPerformed(evt);
219             }
220             else if (evt.getSource() == deleteButton) {
221                 IndexedEditorPanel.this.deleteButtonActionPerformed(evt);
222             }
223             else if (evt.getSource() == upButton) {
224                 IndexedEditorPanel.this.upButtonActionPerformed(evt);
225             }
226             else if (evt.getSource() == downButton) {
227                 IndexedEditorPanel.this.downButtonActionPerformed(evt);
228             }
229             else if (evt.getSource() == detailsButton) {
230                 IndexedEditorPanel.this.detailsButtonActionPerformed(evt);
231             }
232         }
233     }// </editor-fold>//GEN-END:initComponents
234

235     private void detailsButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_detailsButtonActionPerformed
236
showingDetails = !showingDetails;
237
238         if (showingDetails && !this.equals(detailsPanel.getParent())) {
239             initDetails();
240         }
241
242         updateButtonState();
243         updateDetailsPanel();
244     }
245 //GEN-LAST:event_detailsButtonActionPerformed
246

247     private void newButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_newButtonActionPerformed
248

249         try {
250             getExplorerManager().setSelectedNodes(new Node[] { rootNode });
251         } catch (PropertyVetoException pve) {
252             // this should be always possible --> if not, notify problem
253
PropertyDialogManager.notify(pve);
254         }
255
256         if ((newAction != null) && (newAction.isEnabled())) {
257             newAction.actionPerformed(evt);
258         }
259     }
260 //GEN-LAST:event_newButtonActionPerformed
261

262     private void deleteButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_deleteButtonActionPerformed
263

264         Node[] sn = getExplorerManager().getSelectedNodes();
265
266         if ((sn == null) || (sn.length != 1) || (sn[0] == rootNode)) {
267             return;
268         }
269
270         try {
271             sn[0].destroy();
272         } catch (java.io.IOException JavaDoc ioe) {
273             PropertyDialogManager.notify(ioe);
274         }
275
276         rootNode = getExplorerManager().getRootContext();
277     }
278 //GEN-LAST:event_deleteButtonActionPerformed
279

280     private void downButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_downButtonActionPerformed
281

282         Node[] sn = getExplorerManager().getSelectedNodes();
283
284         if ((moveDown != null) && (moveDown.isEnabled())) {
285             moveDown.actionPerformed(evt);
286         }
287
288         if ((sn == null) || (sn.length != 1) || (sn[0] == rootNode)) {
289             return;
290         }
291
292         try {
293             getExplorerManager().setSelectedNodes(sn);
294         } catch (PropertyVetoException pve) {
295         }
296     }
297 //GEN-LAST:event_downButtonActionPerformed
298

299     private void upButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_upButtonActionPerformed
300

301         Node[] sn = getExplorerManager().getSelectedNodes();
302
303         if ((moveUp != null) && (moveUp.isEnabled())) {
304             moveUp.actionPerformed(evt);
305         }
306
307         if ((sn == null) || (sn.length != 1) || (sn[0] == rootNode)) {
308             return;
309         }
310
311         try {
312             getExplorerManager().setSelectedNodes(sn);
313         } catch (PropertyVetoException pve) {
314         }
315     }
316 //GEN-LAST:event_upButtonActionPerformed
317

318     public synchronized ExplorerManager getExplorerManager() {
319         if (em == null) {
320             em = new ExplorerManager();
321         }
322
323         return em;
324     }
325
326     private void updateButtonState() {
327         // refresh the lookup
328
selectedLookup.lookup(Object JavaDoc.class);
329
330         if (showingDetails) {
331             detailsButton.setText(NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_HideDetails"));
332         } else {
333             detailsButton.setText(NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_ShowDetails"));
334         }
335
336         upButton.setEnabled((moveUp != null) && (moveUp.isEnabled()));
337         downButton.setEnabled((moveDown != null) && (moveDown.isEnabled()));
338
339         Node[] sn = getExplorerManager().getSelectedNodes();
340         deleteButton.setEnabled((sn != null) && (sn.length == 1) && (sn[0] != rootNode));
341         detailsButton.setVisible(
342             (prop != null) && (prop.getPropertyEditor() != null) && (prop.getPropertyEditor().supportsCustomEditor())
343         );
344
345         if (detailsButton.isVisible()) {
346             if (showingDetails) {
347                 Mnemonics.setLocalizedText(detailsButton, NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_HideDetails"));
348                 detailsButton.getAccessibleContext().setAccessibleDescription(
349                     NbBundle.getBundle(IndexedEditorPanel.class).getString("ACSD_HideDetails")
350                 );
351             } else {
352                 Mnemonics.setLocalizedText(detailsButton, NbBundle.getBundle(IndexedEditorPanel.class).getString("CTL_ShowDetails"));
353                 detailsButton.getAccessibleContext().setAccessibleDescription(
354                     NbBundle.getBundle(IndexedEditorPanel.class).getString("ACSD_ShowDetails")
355                 );
356             }
357
358             detailsButton.setEnabled((sn != null) && (sn.length == 1) && (sn[0] != rootNode));
359         }
360     }
361
362     private void updateDetailsPanel() {
363         detailsPanel.removeAll();
364
365         if (!showingDetails) {
366             remove(detailsPanel);
367             revalidateDetailsPanel();
368
369             return;
370         }
371
372         Node[] selN = getExplorerManager().getSelectedNodes();
373
374         if ((selN == null) || (selN.length == 0)) {
375             revalidateDetailsPanel();
376
377             return;
378         }
379
380         Node n = selN[0];
381
382         if (n == rootNode) {
383             revalidateDetailsPanel();
384
385             return;
386         }
387
388         if (selN.length > 1) {
389             n = new ProxyNode(selN);
390         }
391
392         // beware - this will function only if the DisplayIndexedNode has
393
// one property on the first sheet and the property is of type
394
// ValueProp
395
Node.Property prop = n.getPropertySets()[0].getProperties()[0];
396         PropertyPanel p = new PropertyPanel(prop);
397         p.setPreferences(PropertyPanel.PREF_CUSTOM_EDITOR);
398
399         if (isEditorScrollable(p)) {
400             detailsPanel.add(p, java.awt.BorderLayout.CENTER);
401         } else {
402             jScrollPane1.setViewportView(p);
403             detailsPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);
404         }
405
406         revalidateDetailsPanel();
407     }
408
409     private void revalidateDetailsPanel() {
410         detailsPanel.invalidate();
411         repaint();
412
413         if (detailsPanel.getParent() != null) {
414             detailsPanel.getParent().validate();
415         } else {
416             detailsPanel.validate();
417         }
418     }
419
420     /**
421      * This method gets called when a bound property is changed.
422      * @param evt A PropertyChangeEvent object describing the event source
423      * and the property that has changed.
424      */

425     public void propertyChange(PropertyChangeEvent evt) {
426         if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
427             updateButtonState();
428             updateDetailsPanel();
429         }
430     }
431
432     private void initDetails() {
433         java.awt.GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
434         gridBagConstraints.gridx = 0;
435         gridBagConstraints.gridy = 2;
436         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
437         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
438         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
439         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
440         gridBagConstraints.weighty = 1.0;
441         add(detailsPanel, gridBagConstraints);
442     }
443
444     private boolean isEditorScrollable(PropertyPanel p) {
445         Component JavaDoc[] comps = p.getComponents();
446
447         for (int i = 0; i < comps.length; i++) {
448             if (comps[i] instanceof Scrollable JavaDoc || isInstanceOfTopComponent(comps[i])) {
449                 return true;
450             }
451         }
452
453         return false;
454     }
455
456     /** Checks whether an object is instanceof TopComponent
457      * @param obj the object
458      * @return true or false
459      */

460     private static boolean isInstanceOfTopComponent(Object JavaDoc obj) {
461         ClassLoader JavaDoc l = org.openide.util.Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
462
463         if (l == null) {
464             l = IndexedEditorPanel.class.getClassLoader();
465         }
466
467         try {
468             Class JavaDoc c = Class.forName("org.openide.windows.TopComponent", true, l); // NOI18N
469

470             return c.isInstance(obj);
471         } catch (Exception JavaDoc ex) {
472             return false;
473         }
474     }
475 }
476
Popular Tags