KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > NonVisualTray


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.netbeans.modules.form;
20
21 import java.awt.*;
22 import java.beans.*;
23 import java.util.*;
24 import javax.swing.*;
25 import javax.swing.border.*;
26
27 import org.openide.nodes.*;
28 import org.openide.actions.*;
29 import org.openide.explorer.*;
30 import org.openide.explorer.view.*;
31
32 /**
33  * A component that displays non visual beans.
34  *
35  * @author Jan Stola
36  */

37 public class NonVisualTray extends JPanel implements ExplorerManager.Provider {
38     /** The corresponding form model. */
39     private FormModel formModel;
40     /** List view used to display beans. */
41     private NonVisualView listView;
42     /** Explorer manager for the list view. */
43     private ExplorerManager manager;
44     
45     /**
46      * Creates new <code>NonVisualTray</code>.
47      *
48      * @param formModel the corresponding form model.
49      */

50     public NonVisualTray(FormModel formModel) {
51         this.formModel = formModel;
52         manager = new ExplorerManager();
53         FormRootNode rootNode = (FormRootNode)FormEditor.getFormEditor(formModel).getFormRootNode();
54         Node othersNode = rootNode.getOthersNode();
55         manager.setRootContext(new NonVisualNode(othersNode, new NonVisualChildren(othersNode)));
56         Listener listener = new Listener();
57         manager.addPropertyChangeListener(listener);
58         ComponentInspector ci = ComponentInspector.getInstance();
59         ci.getExplorerManager().addPropertyChangeListener(listener);
60         listView = new NonVisualView();
61         setLayout(new BorderLayout());
62         add(listView, BorderLayout.CENTER);
63     }
64     
65     /**
66      * Returns explorer manager for the list view.
67      *
68      * @return explorer manager for the list view.
69      */

70     public ExplorerManager getExplorerManager() {
71         return manager;
72     }
73     
74     void updateVisualSettings() {
75         listView.updateVisualSettings();
76     }
77     
78     /**
79      * List view used in the non visual tray.
80      */

81     private static class NonVisualView extends ListView {
82         
83         /**
84          * Creates new <code>NonVisualView</code>.
85          */

86         public NonVisualView() {
87             list.setCellRenderer(new Renderer());
88             list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
89             list.setVisibleRowCount(-1);
90             list.setBorder(BorderFactory.createEmptyBorder(0,4,4,4));
91             updateVisualSettings();
92             setTraversalAllowed(false);
93             setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
94             setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
95         }
96         
97         public Dimension getPreferredSize() {
98             Dimension dim = super.getPreferredSize();
99             return new Dimension(1, (int)dim.getHeight());
100         }
101         
102         void updateVisualSettings() {
103             list.setBackground(FormLoaderSettings.getInstance().getFormDesignerBackgroundColor());
104         }
105
106     }
107
108     /**
109      * Renderer for the list view of the non visual tray.
110      */

111     private static class Renderer implements ListCellRenderer {
112         /** Button used as a renderer component. */
113         private JButton button;
114         /** Border for the selected rendered items. */
115         private Border selectedBorder;
116         /** Border for the unselected rendered items. */
117         private Border unselectedBorder;
118         
119         /**
120          * Creates new <code>Renderer</code>.
121          */

122         public Renderer() {
123             button = new JButton();
124             button.setUI(new javax.swing.plaf.basic.BasicButtonUI JavaDoc());
125             unselectedBorder = BorderFactory.createEmptyBorder(4,4,4,4);
126             Color selectionColor = FormLoaderSettings.getInstance().getSelectionBorderColor();
127             selectedBorder = BorderFactory.createCompoundBorder(
128                 BorderFactory.createEmptyBorder(3,3,3,3),
129                 BorderFactory.createLineBorder(selectionColor));
130             button.setOpaque(false);
131             int fontSize = button.getFontMetrics(button.getFont()).getHeight();
132             button.setPreferredSize(new Dimension(64+2*4, 50+fontSize));
133         }
134         
135         public Component getListCellRendererComponent(JList list,
136             Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
137             Node node = Visualizer.findNode(value);
138             ImageIcon icon = new ImageIcon(node.getIcon(java.beans.BeanInfo.ICON_COLOR_32x32));
139             button.setIcon(icon);
140             String JavaDoc text = node.getShortDescription();
141             button.setText(text);
142             button.setHorizontalTextPosition(SwingConstants.CENTER);
143             button.setVerticalTextPosition(SwingConstants.BOTTOM);
144             button.setHorizontalAlignment(SwingConstants.CENTER);
145             button.setBorder(isSelected ? selectedBorder : unselectedBorder);
146             return button;
147         }
148         
149     }
150     
151     /**
152      * Listener that synchronizes the selected nodes of the form designer
153      * and the non-visual tray.
154      */

155     private class Listener implements PropertyChangeListener {
156         
157         public void propertyChange(PropertyChangeEvent evt) {
158             if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
159                 if (evt.getSource() == manager) {
160                     Node[] newNodes = (Node[])evt.getNewValue();
161                     Node[] nodes = new Node[newNodes.length];
162                     for (int i=0; i<nodes.length; i++) {
163                         nodes[i] = ((NonVisualNode)newNodes[i]).getOriginal();
164                     }
165                     ComponentInspector ci = ComponentInspector.getInstance();
166                     Node[] ciNodes = ci.getSelectedNodes();
167                     if (!Arrays.asList(ciNodes).containsAll(Arrays.asList(nodes))) {
168                         try {
169                             ci.setSelectedNodes(nodes, FormEditor.getFormEditor(formModel));
170                         } catch (PropertyVetoException pvex) {}
171                     }
172                 } else {
173                     Node[] nodes = (Node[])evt.getNewValue();
174                     ArrayList list = new ArrayList();
175                     Node node = ((NonVisualNode)manager.getRootContext()).getOriginal();
176                     for (int i=0; i<nodes.length; i++) {
177                         if (node == nodes[i].getParentNode()) {
178                             list.add(findFilterNode(nodes[i]));
179                         }
180                     }
181                     try {
182                         manager.setSelectedNodes((Node[])list.toArray(new Node[list.size()]));
183                     } catch (PropertyVetoException pvex) {}
184                 }
185             }
186         }
187         
188         /**
189          * Finds a filter node (in the non-visual tray) that corresponds
190          * to the passed node (RADComponentNode).
191          */

192         private Node findFilterNode(Node original) {
193             Node root = manager.getRootContext();
194             Node[] nodes = root.getChildren().getNodes(false);
195             for (int i=0; i<nodes.length; i++) {
196                 NonVisualNode node = (NonVisualNode)nodes[i];
197                 if (node.getOriginal() == original) {
198                     return node;
199                 }
200             }
201             return null;
202         }
203         
204     }
205     
206     /**
207      * Class that manages children of the <code>NonVisualNode</code>.
208      */

209     private class NonVisualChildren extends FilterNode.Children {
210         
211         /**
212          * Creates new <code>NonVisualChildren</code>.
213          *
214          * @param original the original node.
215          */

216         public NonVisualChildren(Node original) {
217             super(original);
218         }
219         
220         /**
221          * Creates a replacement for the original subnode.
222          *
223          * @return a replacement for the original subnode.
224          */

225         protected Node copyNode(Node node) {
226             return new NonVisualNode(node);
227         }
228         
229     }
230
231     /**
232      * Nodes used in the non visual tray.
233      */

234     private static class NonVisualNode extends FilterNode {
235         
236         /**
237          * Creates new <code>NonVisualNode</code>.
238          *
239          * @param original the original node.
240          */

241         public NonVisualNode(Node original) {
242             super(original);
243             disableDelegation(DELEGATE_GET_SHORT_DESCRIPTION
244                 | DELEGATE_GET_ACTIONS);
245         }
246         
247         /**
248          * Creates new <code>NonVisualNode</code>
249          *
250          * @param original the original node.
251          * @param children management of the subnodes.
252          */

253         public NonVisualNode(Node original, Children children) {
254             super(original, children);
255         }
256         
257         /**
258          * Returns short description of the node (used in tooltips by default).
259          *
260          * @return short description of the node.
261          */

262         public String JavaDoc getShortDescription() {
263             return getName();
264         }
265         
266         /**
267          * Returns the original node.
268          *
269          * @return the original node.
270          */

271         protected Node getOriginal() {
272             return super.getOriginal();
273         }
274         
275         /**
276          * Returns actions of the node.
277          *
278          * @param context determines whether context actions should be returned.
279          * @return actions of the node.
280          */

281         public Action[] getActions(boolean context) {
282             java.util.List JavaDoc forbiddenActions = Arrays.asList(new Class JavaDoc[] {
283                 MoveUpAction.class,
284                 MoveDownAction.class
285             });
286             Action[] actions = getOriginal().getActions(context);
287             ArrayList actionList = new ArrayList(Arrays.asList(actions));
288             for (int i=0; i<actions.length; i++) {
289                 Action action = actions[i];
290                 if ((action != null) && (forbiddenActions.contains(action.getClass()))) {
291                     actionList.remove(action);
292                 }
293             }
294             return (Action[])actionList.toArray(new Action[actionList.size()]);
295         }
296         
297     }
298
299 }
300
Popular Tags