KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > ui > Customizer


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.palette.ui;
21
22 import java.awt.Dialog JavaDoc;
23 import javax.swing.*;
24 import javax.swing.text.DefaultEditorKit JavaDoc;
25 import java.beans.*;
26 import org.netbeans.spi.palette.PaletteActions;
27 import org.netbeans.spi.palette.PaletteController;
28 import org.openide.awt.Mnemonics;
29
30 import org.openide.util.*;
31 import org.openide.explorer.*;
32 import org.openide.explorer.view.BeanTreeView;
33 import org.openide.*;
34 import org.openide.nodes.*;
35 import org.netbeans.modules.palette.*;
36
37
38 /**
39  * This class provides the UI for managing palette content (adding components
40  * etc). Shown to the user as "Palette Customizer" window.
41  *
42  * @author Tomas Pavek, S. Aubrecht
43  */

44
45 public class Customizer extends JPanel implements ExplorerManager.Provider,
46                                                       Lookup.Provider
47 {
48     private ExplorerManager explorerManager;
49     private Lookup lookup;
50     
51     private Node root;
52     private PaletteController controller;
53     private Settings settings;
54     
55     private JButton[] customButtons;
56
57     // ------------
58

59     /**
60      * Opens the manager window.
61      *
62      * @param paletteRoot Palette root node.
63      */

64     public static void show( Node paletteRoot, PaletteController controller, Settings settings ) {
65         JButton closeButton = new JButton();
66         org.openide.awt.Mnemonics.setLocalizedText(
67             closeButton, Utils.getBundleString("CTL_Close_Button")); // NOI18N
68
closeButton.getAccessibleContext().setAccessibleDescription( Utils.getBundleString("ACSD_Close") );
69         DialogDescriptor dd = new DialogDescriptor(
70             new Customizer( paletteRoot, controller, settings ),
71             Utils.getBundleString("CTL_Customizer_Title"), // NOI18N
72
false,
73             new Object JavaDoc[] { closeButton },
74             closeButton,
75             DialogDescriptor.DEFAULT_ALIGN,
76             null,
77             null);
78         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(dd);
79         dialog.setVisible(true);
80     }
81
82     /** Creates new Customizer */
83     public Customizer( Node paletteRoot, PaletteController controller, Settings settings ) {
84         this.root = paletteRoot;
85         this.controller = controller;
86         this.settings = settings;
87         explorerManager = new ExplorerManager();
88
89         ActionMap map = getActionMap();
90         map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(explorerManager));
91         map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(explorerManager));
92         map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(explorerManager));
93         map.put("delete", ExplorerUtils.actionDelete(explorerManager, true)); // NOI18N
94

95         lookup = ExplorerUtils.createLookup(explorerManager, map);
96
97         explorerManager.setRootContext(paletteRoot);
98
99         initComponents();
100
101         CheckTreeView treeView = new CheckTreeView( settings );
102         treeView.getAccessibleContext().setAccessibleName(
103             Utils.getBundleString("ACSN_PaletteContentsTree")); // NOI18N
104
treeView.getAccessibleContext().setAccessibleDescription(
105             Utils.getBundleString("ACSD_PaletteContentsTree")); // NOI18N
106
infoLabel.setLabelFor( treeView );
107         treePanel.add(treeView, java.awt.BorderLayout.CENTER);
108         captionLabel.setLabelFor(treeView);
109
110         explorerManager.addPropertyChangeListener(new PropertyChangeListener() {
111             public void propertyChange(PropertyChangeEvent ev) {
112                 if (ExplorerManager.PROP_SELECTED_NODES.equals(ev.getPropertyName())) {
113                     updateInfoLabel(explorerManager.getSelectedNodes());
114                     updateButtons();
115                 }
116             }
117         });
118         updateButtons();
119     }
120
121     public void addNotify() {
122         super.addNotify();
123         ExplorerUtils.activateActions(explorerManager, true);
124     }
125
126     public void removeNotify() {
127         ExplorerUtils.activateActions(explorerManager, false);
128         super.removeNotify();
129     }
130
131     // ExplorerManager.Provider
132
public ExplorerManager getExplorerManager() {
133         return explorerManager;
134     }
135
136     // Lookup.Provider from TopComponent
137
public Lookup getLookup() {
138         return lookup;
139     }
140
141     private void updateButtons() {
142         Node[] selNodes = explorerManager.getSelectedNodes();
143         boolean canRemove = null != selNodes && selNodes.length > 0;
144         boolean canMoveUp = null != selNodes && selNodes.length == 1;
145         boolean canMoveDown = null != selNodes && selNodes.length == 1;
146         
147         for( int i=0; null != selNodes && i<selNodes.length; i++ ) {
148             Node node = selNodes[i];
149             if( !node.canDestroy() )
150                 canRemove = false;
151             
152             Node parent = node.getParentNode();
153             if( null == parent || movePossible( node, parent, true ) < 0 )
154                 canMoveUp = false;
155             if( null == parent || movePossible( node, parent, false ) < 0 )
156                 canMoveDown = false;
157         }
158         removeButton.setEnabled( canRemove );
159         moveUpButton.setEnabled( canMoveUp );
160         moveDownButton.setEnabled( canMoveDown );
161     }
162     // -------
163

164     /** This method is called from within the constructor to
165      * initialize the form.
166      * WARNING: Do NOT modify this code. The content of this method is
167      * always regenerated by the Form Editor.
168      */

169     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
170
private void initComponents() {
171         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
172
173         captionLabel = new javax.swing.JLabel JavaDoc();
174         treePanel = new javax.swing.JPanel JavaDoc();
175         infoLabel = new javax.swing.JLabel JavaDoc();
176         moveUpButton = new javax.swing.JButton JavaDoc();
177         moveDownButton = new javax.swing.JButton JavaDoc();
178         removeButton = new javax.swing.JButton JavaDoc();
179         newCategoryButton = new javax.swing.JButton JavaDoc();
180         customActionsPanel = new javax.swing.JPanel JavaDoc();
181         resetButton = new javax.swing.JButton JavaDoc();
182
183         setLayout(new java.awt.GridBagLayout JavaDoc());
184
185         getAccessibleContext().setAccessibleDescription(null); // NOI18N
186
org.openide.awt.Mnemonics.setLocalizedText(captionLabel, Utils.getBundleString("CTL_Caption")); // NOI18N
187
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
188         gridBagConstraints.gridwidth = 2;
189         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
190         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
191         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 10, 0, 10);
192         add(captionLabel, gridBagConstraints);
193
194         treePanel.setLayout(new java.awt.BorderLayout JavaDoc());
195
196         treePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
197         treePanel.setPreferredSize(new java.awt.Dimension JavaDoc(288, 336));
198         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
199         gridBagConstraints.gridx = 0;
200         gridBagConstraints.gridy = 1;
201         gridBagConstraints.gridheight = 7;
202         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
203         gridBagConstraints.weightx = 1.0;
204         gridBagConstraints.weighty = 1.0;
205         gridBagConstraints.insets = new java.awt.Insets JavaDoc(6, 10, 0, 0);
206         add(treePanel, gridBagConstraints);
207
208         infoLabel.setText(" ");
209         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
210         gridBagConstraints.gridx = 0;
211         gridBagConstraints.gridy = 8;
212         gridBagConstraints.gridwidth = 2;
213         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
214         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
215         gridBagConstraints.insets = new java.awt.Insets JavaDoc(10, 10, 0, 10);
216         add(infoLabel, gridBagConstraints);
217
218         org.openide.awt.Mnemonics.setLocalizedText(moveUpButton, Utils.getBundleString("CTL_MoveUp_Button")); // NOI18N
219
moveUpButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
220             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
221                 moveUpButtonActionPerformed(evt);
222             }
223         });
224
225         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
226         gridBagConstraints.gridx = 1;
227         gridBagConstraints.gridy = 2;
228         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
229         gridBagConstraints.insets = new java.awt.Insets JavaDoc(28, 12, 0, 10);
230         add(moveUpButton, gridBagConstraints);
231         moveUpButton.getAccessibleContext().setAccessibleDescription(null); // NOI18N
232

233         org.openide.awt.Mnemonics.setLocalizedText(moveDownButton, Utils.getBundleString("CTL_MoveDown_Button")); // NOI18N
234
moveDownButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
235             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
236                 moveDownButtonActionPerformed(evt);
237             }
238         });
239
240         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
241         gridBagConstraints.gridx = 1;
242         gridBagConstraints.gridy = 3;
243         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
244         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 10);
245         add(moveDownButton, gridBagConstraints);
246         moveDownButton.getAccessibleContext().setAccessibleDescription(null); // NOI18N
247

248         org.openide.awt.Mnemonics.setLocalizedText(removeButton, Utils.getBundleString("CTL_Remove_Button")); // NOI18N
249
removeButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
250             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
251                 removeButtonActionPerformed(evt);
252             }
253         });
254
255         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
256         gridBagConstraints.gridx = 1;
257         gridBagConstraints.gridy = 4;
258         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
259         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 10);
260         add(removeButton, gridBagConstraints);
261         removeButton.getAccessibleContext().setAccessibleDescription(null); // NOI18N
262

263         org.openide.awt.Mnemonics.setLocalizedText(newCategoryButton, Utils.getBundleString("CTL_NewCategory_Button")); // NOI18N
264
newCategoryButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
265             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
266                 newCategoryButtonActionPerformed(evt);
267             }
268         });
269
270         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
271         gridBagConstraints.gridx = 1;
272         gridBagConstraints.gridy = 5;
273         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
274         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 10);
275         add(newCategoryButton, gridBagConstraints);
276         newCategoryButton.getAccessibleContext().setAccessibleDescription(null); // NOI18N
277

278         customActionsPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
279
280         createCustomButtons();
281         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
282         gridBagConstraints.gridx = 1;
283         gridBagConstraints.gridy = 1;
284         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
285         add(customActionsPanel, gridBagConstraints);
286
287         org.openide.awt.Mnemonics.setLocalizedText(resetButton, Utils.getBundleString("CTL_ResetPalette")); // NOI18N
288
resetButton.setActionCommand("Reset Palette");
289         resetButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
290             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
291                 resetButtonActionPerformed(evt);
292             }
293         });
294
295         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
296         gridBagConstraints.gridx = 1;
297         gridBagConstraints.gridy = 6;
298         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
299         gridBagConstraints.insets = new java.awt.Insets JavaDoc(28, 12, 0, 10);
300         add(resetButton, gridBagConstraints);
301         resetButton.getAccessibleContext().setAccessibleName(Utils.getBundleString("ASCN_ResetPalette")); // NOI18N
302
resetButton.getAccessibleContext().setAccessibleDescription(Utils.getBundleString("ASCD_ResetPalette")); // NOI18N
303

304     }// </editor-fold>//GEN-END:initComponents
305

306     private void resetButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_resetButtonActionPerformed
307
Utils.resetPalette( controller, settings );
308     }//GEN-LAST:event_resetButtonActionPerformed
309

310     private void removeButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_removeButtonActionPerformed
311
Node[] selected = explorerManager.getSelectedNodes();
312         if (selected.length == 0)
313             return;
314
315         if( selected.length == 1 && !selected[0].canDestroy() )
316             return;
317         
318         // first user confirmation...
319
NotifyDescriptor desc = new NotifyDescriptor.Confirmation(
320             Utils.getBundleString("MSG_ConfirmPaletteDelete"), // NOI18N
321
Utils.getBundleString("CTL_ConfirmDeleteTitle"), // NOI18N
322
NotifyDescriptor.YES_NO_OPTION);
323
324         if (NotifyDescriptor.YES_OPTION.equals(
325                     DialogDisplayer.getDefault().notify(desc)))
326         {
327             try {
328                 for (int i=0; i < selected.length; i++) {
329                     if( selected[i].canDestroy() )
330                         selected[i].destroy();
331                 }
332             }
333             catch (java.io.IOException JavaDoc e) {
334                 ErrorManager.getDefault().notify(e);
335             }
336         }
337     }//GEN-LAST:event_removeButtonActionPerformed
338

339     private void moveDownButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
340
moveNode(false);
341     }//GEN-LAST:event_moveDownButtonActionPerformed
342

343     private void moveUpButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
344
moveNode(true);
345     }//GEN-LAST:event_moveUpButtonActionPerformed
346

347     private void newCategoryButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_newCategoryButtonActionPerformed
348
new Utils.NewCategoryAction( root ).actionPerformed( evt );
349     }//GEN-LAST:event_newCategoryButtonActionPerformed
350

351
352     // Variables declaration - do not modify//GEN-BEGIN:variables
353
private javax.swing.JLabel JavaDoc captionLabel;
354     private javax.swing.JPanel JavaDoc customActionsPanel;
355     private javax.swing.JLabel JavaDoc infoLabel;
356     private javax.swing.JButton JavaDoc moveDownButton;
357     private javax.swing.JButton JavaDoc moveUpButton;
358     private javax.swing.JButton JavaDoc newCategoryButton;
359     private javax.swing.JButton JavaDoc removeButton;
360     private javax.swing.JButton JavaDoc resetButton;
361     private javax.swing.JPanel JavaDoc treePanel;
362     // End of variables declaration//GEN-END:variables
363

364     private void moveNode(boolean up) {
365         Node[] selected = explorerManager.getSelectedNodes();
366         if (selected.length != 1)
367             return;
368
369         Node node = selected[0];
370         Node parent = node.getParentNode();
371         if (parent == null)
372             return;
373
374         Index indexCookie = (Index) parent.getCookie(Index.class);
375         if (indexCookie == null)
376             return;
377
378         int index = movePossible(node, parent, up);
379         if (index != -1) {
380             if (up) {
381                 indexCookie.moveUp(index);
382             } else {
383                 indexCookie.moveDown(index);
384             }
385         }
386     }
387
388     private static int movePossible(Node node, Node parentNode, boolean up) {
389         if (parentNode == null)
390             return -1;
391
392         Node[] nodes = parentNode.getChildren().getNodes();
393         for (int i=0; i < nodes.length; i++)
394             if (nodes[i].getName().equals(node.getName()))
395                 return (up && i > 0) || (!up && i+1 < nodes.length) ? i : -1;
396
397         return -1;
398     }
399
400     private void updateInfoLabel(org.openide.nodes.Node[] nodes) {
401         String JavaDoc text = " "; // NOI18N
402
if (nodes.length == 1) {
403             Item item = nodes[0].getCookie( Item.class );
404             if (item != null)
405                 text = item.getShortDescription(); //TODO revisit PaletteSupport.getItemComponentDescription(item);
406
}
407         infoLabel.setText(text);
408     }
409     
410     private void createCustomButtons() {
411         PaletteActions customActions = (PaletteActions)root.getLookup().lookup( PaletteActions.class );
412         if( null == customActions )
413             return;
414         
415         Action[] actions = customActions.getImportActions();
416         if( null == actions || actions.length == 0 )
417             return;
418         
419         customButtons = new JButton[actions.length];
420         for( int i=0; i<actions.length; i++ ) {
421             customButtons[i] = new JButton( actions[i] );
422             if( null != actions[i].getValue( Action.NAME ) )
423                 Mnemonics.setLocalizedText( customButtons[i], actions[i].getValue( Action.NAME ).toString() );
424             if( null != actions[i].getValue( Action.LONG_DESCRIPTION ) )
425                 customButtons[i].getAccessibleContext().setAccessibleDescription( actions[i].getValue( Action.LONG_DESCRIPTION ).toString() );
426             java.awt.GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
427             gridBagConstraints.gridx = 0;
428             gridBagConstraints.gridy = i;
429             gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
430             gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 12, 0, 10);
431             customActionsPanel.add( customButtons[i], gridBagConstraints);
432         }
433     }
434     
435     
436     private static class CheckTreeView extends BeanTreeView {
437         /** Creates a new instance of CheckTreeView */
438         public CheckTreeView( Settings settings ) {
439             if( settings instanceof DefaultSettings ) {
440                 CheckListener l = new CheckListener( (DefaultSettings)settings );
441                 tree.addMouseListener( l );
442                 tree.addKeyListener( l );
443
444                 CheckRenderer check = new CheckRenderer( (DefaultSettings)settings );
445                 tree.setCellRenderer( check );
446             }
447         }
448     }
449 }
450
Popular Tags