1 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 ; 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 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 public FormToolBar(FormDesigner designer) { 60 formDesigner = designer; 61 62 setFloatable(false); 64 setRollover(true); 65 setBorder(new EmptyBorder(0, 0, 0, 0)); 66 67 listener = new Listener(); 68 69 selectionButton = new JToggleButton( 71 new ImageIcon(getClass().getResource( 72 "/org/netbeans/modules/form/resources/selection_mode.png")), false); 74 selectionButton.addActionListener(listener); 75 selectionButton.addMouseListener(listener); 76 selectionButton.setToolTipText( 77 FormUtils.getBundleString("CTL_SelectionButtonHint")); HelpCtx.setHelpIDString(selectionButton, "gui.about"); selectionButton.setSelected(true); 80 initButton(selectionButton); 81 82 connectionButton = new JToggleButton( 84 new ImageIcon(getClass().getResource( 85 "/org/netbeans/modules/form/resources/connection_mode.png")), false); 87 connectionButton.addActionListener(listener); 88 connectionButton.addMouseListener(listener); 89 connectionButton.setToolTipText( 90 FormUtils.getBundleString("CTL_ConnectionButtonHint")); HelpCtx.setHelpIDString(connectionButton, "gui.connecting.intro"); initButton(connectionButton); 93 94 paletteButton = new JToggleButton( 96 new ImageIcon(getClass().getResource( 97 "/org/netbeans/modules/form/resources/beansButton.gif")), false); 99 paletteButton.addActionListener(listener); 100 paletteButton.addMouseListener(listener); 101 paletteButton.setToolTipText( 102 FormUtils.getBundleString("CTL_BeansButtonHint")); HelpCtx.setHelpIDString(paletteButton, "gui.components.adding"); ScrollPopupMenu.doNotCancelPopupHack(paletteButton); 106 initButton(paletteButton); 107 108 addLabel = new JLabel(); 110 111 addMouseListener(listener); 113 114 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")); selectionButton.getAccessibleContext().setAccessibleDescription(FormUtils.getBundleString("ACSD_SelectionMode")); paletteButton.getAccessibleContext().setAccessibleDescription(FormUtils.getBundleString("ACSD_AddMode")); 122 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(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 190 private void initButton(AbstractButton button) { 191 if (!("Windows".equals(UIManager.getLookAndFeel().getID()) && (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(""); 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")); 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 getUIClassID() { 252 if (UIManager.get("Nb.Toolbar.ui") != null) { return "Nb.Toolbar.ui"; } else { 256 return super.getUIClassID(); 257 } 258 } 259 260 262 private class Listener extends MouseAdapter 263 implements ActionListener, NodeAcceptor, 264 PopupMenuListener 265 { 266 private boolean showMenu; 268 269 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 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 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 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 |