KickJava   Java API By Example, From Geeks To Geeks.

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


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.form;
21
22 import javax.swing.*;
23 import javax.swing.border.*;
24 import javax.swing.event.*;
25 import java.awt.*;
26 import java.awt.event.*;
27 import java.beans.BeanInfo JavaDoc;
28 import java.util.*;
29
30 import org.openide.nodes.*;
31 import org.openide.util.HelpCtx;
32 import org.openide.util.actions.SystemAction;
33
34 import org.netbeans.modules.form.palette.*;
35 import org.netbeans.modules.form.actions.TestAction;
36
37 /**
38  * ToolBar in the FormDesigner - by default it holds buttons for selection and
39  * connection mode and for testing the form. May contain other buttons for
40  * some form editor actions.
41  *
42  * @author Tomas Pavek
43  */

44
45 class FormToolBar extends JToolBar {
46
47     private FormDesigner formDesigner;
48
49     private JToggleButton selectionButton;
50     private JToggleButton connectionButton;
51     private JToggleButton paletteButton;
52     private JLabel addLabel;
53
54     private PaletteMenuView paletteMenuView;
55
56     private Listener listener;
57
58     // ctor
59
public FormToolBar(FormDesigner designer) {
60         formDesigner = designer;
61
62         // the toolbar should have roll-over buttons and no handle for dragging
63
setFloatable(false);
64         setRollover(true);
65         setBorder(new EmptyBorder(0, 0, 0, 0));
66
67         listener = new Listener();
68
69         // selection button
70
selectionButton = new JToggleButton(
71             new ImageIcon(getClass().getResource(
72                           "/org/netbeans/modules/form/resources/selection_mode.png")), // NOI18N
73
false);
74         selectionButton.addActionListener(listener);
75         selectionButton.addMouseListener(listener);
76         selectionButton.setToolTipText(
77             FormUtils.getBundleString("CTL_SelectionButtonHint")); // NOI18N
78
HelpCtx.setHelpIDString(selectionButton, "gui.about"); // NOI18N
79
selectionButton.setSelected(true);
80         initButton(selectionButton);
81
82         // connection button
83
connectionButton = new JToggleButton(
84             new ImageIcon(getClass().getResource(
85                           "/org/netbeans/modules/form/resources/connection_mode.png")), // NOI18N
86
false);
87         connectionButton.addActionListener(listener);
88         connectionButton.addMouseListener(listener);
89         connectionButton.setToolTipText(
90             FormUtils.getBundleString("CTL_ConnectionButtonHint")); // NOI18N
91
HelpCtx.setHelpIDString(connectionButton, "gui.connecting.intro"); // NOI18N
92
initButton(connectionButton);
93
94         // palette button
95
paletteButton = new JToggleButton(
96             new ImageIcon(getClass().getResource(
97                           "/org/netbeans/modules/form/resources/beansButton.gif")), // NOI18N
98
false);
99         paletteButton.addActionListener(listener);
100         paletteButton.addMouseListener(listener);
101         paletteButton.setToolTipText(
102             FormUtils.getBundleString("CTL_BeansButtonHint")); // NOI18N
103
HelpCtx.setHelpIDString(paletteButton, "gui.components.adding"); // NOI18N
104
// Issue 46673
105
ScrollPopupMenu.doNotCancelPopupHack(paletteButton);
106         initButton(paletteButton);
107
108         // status label
109
addLabel = new JLabel();
110
111         // popup menu
112
addMouseListener(listener);
113
114         // a11y
115
connectionButton.getAccessibleContext().setAccessibleName(connectionButton.getToolTipText());
116         selectionButton.getAccessibleContext().setAccessibleName(selectionButton.getToolTipText());
117         paletteButton.getAccessibleContext().setAccessibleName(paletteButton.getToolTipText());
118         connectionButton.getAccessibleContext().setAccessibleDescription(FormUtils.getBundleString("ACSD_ConnectionMode")); // NOI18N
119
selectionButton.getAccessibleContext().setAccessibleDescription(FormUtils.getBundleString("ACSD_SelectionMode")); // NOI18N
120
paletteButton.getAccessibleContext().setAccessibleDescription(FormUtils.getBundleString("ACSD_AddMode")); // NOI18N
121

122         // adding the components to the toolbar
123
JToolBar.Separator separator1 = new JToolBar.Separator();
124         separator1.setOrientation(JSeparator.VERTICAL);
125         JToolBar.Separator separator2 = new JToolBar.Separator();
126         separator2.setOrientation(JSeparator.VERTICAL);
127         JToolBar.Separator separator3 = new JToolBar.Separator();
128         separator3.setOrientation(JSeparator.VERTICAL);
129
130         TestAction testAction = (TestAction) SystemAction.get(TestAction.class);
131         JButton testButton = (JButton) testAction.getToolbarPresenter();
132         testButton.addMouseListener(listener);
133         initButton(testButton);
134
135         add(Box.createHorizontalStrut(4));
136         add(separator1);
137         add(Box.createHorizontalStrut(6));
138         add(selectionButton);
139         add(connectionButton);
140         add(paletteButton);
141         add(Box.createHorizontalStrut(6));
142         add(testButton);
143         add(Box.createHorizontalStrut(4));
144         add(separator2);
145         add(Box.createHorizontalStrut(4));
146         installDesignerActions();
147         add(Box.createHorizontalStrut(4));
148         add(separator3);
149         add(Box.createHorizontalStrut(4));
150         installResizabilityActions();
151
152         // Add "addLabel" at the end of the toolbar
153
add(Box.createHorizontalGlue());
154         add(addLabel);
155
156         if (!FormLoaderSettings.getInstance().isPaletteInToolBar()) {
157             addLabel.setVisible(false);
158             paletteButton.setVisible(false);
159         }
160     }
161
162     void installDesignerActions() {
163         Collection actions = formDesigner.getDesignerActions(true);
164         Iterator iter = actions.iterator();
165         while (iter.hasNext()) {
166             Action action = (Action)iter.next();
167             JButton button = add(action);
168             initButton(button);
169         }
170     }
171
172     void installResizabilityActions() {
173         Collection actions = formDesigner.getResizabilityActions();
174         Iterator iter = actions.iterator();
175         JToggleButton[] resButtons = new JToggleButton[2];
176         for (int i=0; i < actions.size(); i++) {
177             Action action = (Action)iter.next();
178             JToggleButton button = new JToggleButton();
179             button.setAction(action);
180             initButton(button);
181             resButtons[i] = button;
182             add(button);
183             add(Box.createHorizontalStrut(2));
184         }
185         formDesigner.setResizabilityButtons(resButtons);
186     }
187     
188     // --------
189

190     private void initButton(AbstractButton button) {
191         if (!("Windows".equals(UIManager.getLookAndFeel().getID()) // NOI18N
192
&& (button instanceof JToggleButton))) {
193             button.setBorderPainted(false);
194         }
195         button.setOpaque(false);
196         button.setFocusPainted(false);
197         button.setMargin(new Insets(0, 0, 0, 0));
198     }
199     
200     void updateDesignerMode(int mode) {
201         selectionButton.setSelected(mode == FormDesigner.MODE_SELECT);
202         connectionButton.setSelected(mode == FormDesigner.MODE_CONNECT);
203         paletteButton.setSelected(mode == FormDesigner.MODE_ADD);
204
205         if (addLabel.isVisible()) {
206             PaletteItem item = PaletteUtils.getSelectedItem();
207             if (item != null && mode == FormDesigner.MODE_ADD) {
208                 addLabel.setIcon(
209                     new ImageIcon(item.getNode().getIcon(BeanInfo.ICON_COLOR_16x16)));
210                 addLabel.setText(item.getNode().getDisplayName());
211             }
212             else {
213                 addLabel.setText(""); // NOI18N
214
addLabel.setIcon(null);
215             }
216         }
217     }
218
219     void showPaletteButton(boolean visible) {
220         addLabel.setVisible(visible);
221         paletteButton.setVisible(visible);
222     }
223
224     private void showPaletteViewMenu() {
225         if (paletteMenuView == null) {
226             paletteMenuView = new PaletteMenuView(listener);
227             paletteMenuView.getPopupMenu().addPopupMenuListener(listener);
228         }
229
230         Point p = paletteButton.getLocation();
231         p.y += paletteButton.getHeight() + 2;
232
233         paletteMenuView.getPopupMenu().show(this, p.x, p.y);
234     }
235
236     private void showVisibilityPopupMenu(Point p) {
237         JPopupMenu menu = new JPopupMenu();
238         final JMenuItem item = new JCheckBoxMenuItem(
239                 FormUtils.getBundleString("CTL_PaletteButton_MenuItem")); // NOI18N
240
item.setSelected(FormLoaderSettings.getInstance().isPaletteInToolBar());
241         item.addActionListener(new ActionListener() {
242             public void actionPerformed(ActionEvent e) {
243                 FormLoaderSettings.getInstance().setPaletteInToolBar(
244                                                          item.isSelected());
245             }
246         });
247         menu.add(item);
248         menu.show(this, p.x, p.y);
249     }
250     
251     public String JavaDoc getUIClassID() {
252         // For GTK and Aqua look and feels, we provide a custom toolbar UI
253
if (UIManager.get("Nb.Toolbar.ui") != null) { // NOI18N
254
return "Nb.Toolbar.ui"; // NOI18N
255
} else {
256             return super.getUIClassID();
257         }
258     }
259
260     // -------
261

262     private class Listener extends MouseAdapter
263                            implements ActionListener, NodeAcceptor,
264                                       PopupMenuListener
265     {
266         // Determines whether palette popup menu should be shown (see issue 46673)
267
private boolean showMenu;
268         
269         /** Action to switch to selection, connection or add mode. */
270         public void actionPerformed(ActionEvent ev) {
271             if (ev.getSource() == selectionButton)
272                 formDesigner.toggleSelectionMode();
273             else if (ev.getSource() == connectionButton)
274                 formDesigner.toggleConnectionMode();
275             else if (ev.getSource() == paletteButton) {
276                 if (showMenu) {
277                     formDesigner.toggleAddMode();
278                     showPaletteViewMenu();
279                 } else {
280                     MenuSelectionManager.defaultManager().clearSelectedPath();
281                     formDesigner.toggleSelectionMode();
282                 }
283             }
284         }
285
286         /** Acceptor for nodes in PaletteMenuView */
287         public boolean acceptNodes(Node[] nodes) {
288             if (nodes.length == 0)
289                 return false;
290
291             PaletteItem item = (PaletteItem) nodes[0].getCookie(PaletteItem.class);
292             PaletteUtils.selectItem( item );
293             return true;
294         }
295
296         /** Handles closing of PaletteMenuView popup */
297         public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
298             if( PaletteUtils.getSelectedItem() == null )
299                 formDesigner.toggleSelectionMode();
300         }
301         public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
302         }
303         public void popupMenuCanceled(PopupMenuEvent e) {
304         }
305         
306         public void mousePressed(MouseEvent e) {
307             if (e.getSource() == paletteButton) {
308                 showMenu = !paletteButton.isSelected();
309             }
310         }
311
312         /** Reacts on right mouse button up - showing toolbar's popup menu. */
313         public void mouseReleased(MouseEvent e) {
314             if (SwingUtilities.isRightMouseButton(e)
315                   && formDesigner.getDesignerMode() == FormDesigner.MODE_SELECT)
316                 showVisibilityPopupMenu(e.getPoint());
317         }
318     }
319 }
320
Popular Tags