1 19 20 package org.netbeans.core.multiview; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.awt.event.KeyEvent ; 25 import java.util.Enumeration ; 26 import java.util.HashSet ; 27 import java.util.Set ; 28 import javax.swing.*; 29 import javax.swing.JToggleButton.ToggleButtonModel; 30 import javax.swing.KeyStroke ; 31 import javax.swing.border.Border ; 32 import javax.swing.border.CompoundBorder ; 33 import javax.swing.border.EmptyBorder ; 34 import javax.swing.plaf.metal.MetalLookAndFeel ; 35 import javax.swing.text.Keymap ; 36 import org.netbeans.core.spi.multiview.MultiViewDescription; 37 import org.netbeans.core.spi.multiview.MultiViewElement; 38 import org.openide.util.Lookup; 39 import org.openide.util.NbBundle; 40 import org.openide.util.actions.CallbackSystemAction; 41 42 43 47 class TabsComponent extends JPanel { 48 49 private JComponent EMPTY; 50 private final static String TOOLBAR_MARKER = "MultiViewPanel"; 52 MultiViewModel model; 53 private ActionListener listener; 54 private MouseListener buttonMouseListener = null; 55 private JComponent toolbarPanel; 56 private JPanel componentPanel; 57 private CardLayout cardLayout; 58 private Set alreadyAddedElements; 59 private JToolBar bar; 60 61 private static final boolean AQUA = "Aqua".equals(UIManager.getLookAndFeel().getID()); 63 private boolean toolbarVisible = true; 64 65 66 public TabsComponent(boolean toolVis) { 67 super(); 68 bar = AQUA ? new TB() : new JToolBar(); 69 Border b = (Border )UIManager.get("Nb.Editor.Toolbar.border"); bar.setBorder(b); 71 bar.setFloatable(false); 72 bar.setFocusable(true); 73 74 setLayout(new BorderLayout()); 75 add(bar, BorderLayout.NORTH); 76 startToggling(); 77 setToolbarBarVisible(toolVis); 78 } 79 80 81 82 public void setModel(MultiViewModel model) { 83 if (this.model != null) { 84 bar.removeAll(); 85 } 86 this.model = model; 87 88 componentPanel = new JPanel(); 89 cardLayout = new CardLayout(); 90 componentPanel.setLayout(cardLayout); 91 add(componentPanel, BorderLayout.CENTER); 92 alreadyAddedElements = new HashSet (); 93 94 MultiViewDescription[] descs = model.getDescriptions(); 95 MultiViewDescription def = model.getActiveDescription(); 96 GridBagLayout grid = new GridBagLayout(); 97 bar.setLayout(grid); 98 JToggleButton active = null; 99 int prefHeight = -1; 100 int prefWidth = -1; 101 for (int i = 0; i < descs.length; i++) { 102 JToggleButton button = createButton(descs[i]); 103 model.getButtonGroup().add(button); 104 GridBagConstraints cons = new GridBagConstraints(); 105 cons.anchor = GridBagConstraints.WEST; 106 prefHeight = Math.max(button.getPreferredSize().height, prefHeight); 107 bar.add(button, cons); 108 prefWidth = Math.max(button.getPreferredSize().width, prefWidth); 109 if (descs[i] == model.getActiveDescription()) { 110 active = button; 111 112 } 113 } 114 Enumeration en = model.getButtonGroup().getElements(); 115 while (en.hasMoreElements()) { 116 JToggleButton but = (JToggleButton)en.nextElement(); 117 Insets ins = but.getBorder().getBorderInsets(but); 118 but.setPreferredSize(new Dimension(prefWidth + 10, prefHeight)); 119 but.setMinimumSize(new Dimension(prefWidth + 10, prefHeight)); 120 121 } 122 if (active != null) { 123 active.setSelected(true); 124 } 125 toolbarPanel = getEmptyInnerToolBar(); 126 GridBagConstraints cons = new GridBagConstraints(); 127 cons.anchor = GridBagConstraints.EAST; 128 cons.fill = GridBagConstraints.BOTH; 129 cons.gridwidth = GridBagConstraints.REMAINDER; 130 cons.weightx = 1; 131 132 bar.add(toolbarPanel, cons); 133 } 134 135 136 void switchToCard(MultiViewElement elem, String id) { 137 if (! alreadyAddedElements.contains(elem)) { 138 componentPanel.add(elem.getVisualRepresentation(), id); 139 alreadyAddedElements.add(elem); 140 } 141 cardLayout.show(componentPanel, id); 142 } 143 144 145 void changeActiveManually(MultiViewDescription desc) { 146 Enumeration en = model.getButtonGroup().getElements(); 147 while (en.hasMoreElements()) { 148 JToggleButton obj = (JToggleButton)en.nextElement(); 149 150 if (obj.getModel() instanceof TabsComponent.TabsButtonModel) { 151 TabsButtonModel btnmodel = (TabsButtonModel)obj.getModel(); 152 if (btnmodel.getButtonsDescription().equals(desc)) { 153 obj.setSelected(true); 154 MultiViewElement elem = model.getElementForDescription(desc); 155 elem.getVisualRepresentation().requestFocus(); 156 break; 157 } 158 } 159 } 160 } 161 162 void changeVisibleManually(MultiViewDescription desc) { 163 Enumeration en = model.getButtonGroup().getElements(); 164 while (en.hasMoreElements()) { 165 JToggleButton obj = (JToggleButton)en.nextElement(); 166 167 if (obj.getModel() instanceof TabsComponent.TabsButtonModel) { 168 TabsButtonModel btnmodel = (TabsButtonModel)obj.getModel(); 169 if (btnmodel.getButtonsDescription().equals(desc)) { 170 obj.setSelected(true); 171 break; 172 } 173 } 174 } 175 } 176 177 private JToggleButton createButton(MultiViewDescription description) { 178 final JToggleButton button = new JToggleButton(description.getDisplayName()); 179 button.setModel(new TabsButtonModel(description)); 180 button.setRolloverEnabled(true); 181 Border b = (getButtonBorder()); 182 if (b != null) { 183 button.setBorder(b); 184 } 185 186 if (buttonMouseListener == null) { 187 buttonMouseListener = new ButtonMouseListener(); 188 } 189 button.addMouseListener (buttonMouseListener); 190 Font font = button.getFont(); 192 FontMetrics fm = button.getFontMetrics(font); 193 int height = fm.getHeight(); 194 195 Keymap map = (Keymap )Lookup.getDefault().lookup(Keymap .class); 197 KeyStroke stroke = null; 198 KeyStroke stroke2 = null; 199 if (map != null) { 201 Action[] acts = map.getBoundActions(); 203 for (int i = 0; i < acts.length;i++) { 204 if (acts[i] instanceof CallbackSystemAction) { 205 CallbackSystemAction sa = (CallbackSystemAction)acts[i]; 206 if ("NextViewAction".equals(sa.getActionMapKey())) { KeyStroke [] strokes = map.getKeyStrokesForAction(acts[i]); 208 if (strokes != null && strokes.length > 0) { 209 stroke = strokes[0]; 210 } 211 } 212 if ("PreviousViewAction".equals(sa.getActionMapKey())) { KeyStroke [] strokes = map.getKeyStrokesForAction(acts[i]); 214 if (strokes != null && strokes.length > 0) { 215 stroke2 = strokes[0]; 216 } 217 } 218 } 219 } 220 } 221 String key1 = stroke == null ? "" : KeyEvent.getKeyModifiersText(stroke.getModifiers()) + "+" + KeyEvent.getKeyText(stroke.getKeyCode()); String key2 = stroke2 == null ? "" : KeyEvent.getKeyModifiersText(stroke2.getModifiers()) + "+" + KeyEvent.getKeyText(stroke2.getKeyCode()); button.setToolTipText(NbBundle.getMessage(TabsComponent.class, "TabButton.tooltip", description.getDisplayName(), 226 key1, 227 key2)); 228 button.setFocusable(true); 229 button.setFocusPainted(true); 230 return button; 231 } 232 233 void setInnerToolBar(JComponent innerbar) { 234 synchronized (getTreeLock()) { 235 if (toolbarPanel != null) { 236 bar.remove(toolbarPanel); 237 } 238 if (innerbar == null) { 239 innerbar = getEmptyInnerToolBar(); 240 } 241 innerbar.putClientProperty(TOOLBAR_MARKER, "X"); if (!AQUA) { 244 innerbar.setBorder(null); 245 } else { 246 innerbar.setBorder (BorderFactory.createEmptyBorder(2, 0, 2, 0)); 247 } 248 toolbarPanel = innerbar; 249 if (toolbarPanel != null) { 250 GridBagConstraints cons = new GridBagConstraints(); 251 cons.anchor = GridBagConstraints.EAST; 252 cons.fill = GridBagConstraints.BOTH; 253 cons.weightx = 1; 254 toolbarPanel.setMinimumSize(new Dimension(10, 10)); 255 cons.gridwidth = GridBagConstraints.REMAINDER; 256 257 bar.add(toolbarPanel, cons); 258 } 259 bar.revalidate(); 262 bar.repaint(); 263 } 264 } 265 266 void setToolbarBarVisible(boolean visible) { 267 if (toolbarVisible == visible) { 268 return; 269 } 270 toolbarVisible = visible; 271 bar.setVisible(visible); 272 } 273 274 275 276 JComponent getEmptyInnerToolBar() { 277 if (EMPTY == null) { 278 EMPTY = new JPanel(); 279 } 280 return EMPTY; 281 } 282 283 284 void requestFocusForSelectedButton() { 285 bar.setFocusable(true); 286 Enumeration en = model.getButtonGroup().getElements(); 287 while (en.hasMoreElements()) { 288 JToggleButton but = (JToggleButton)en.nextElement(); 289 if (model.getButtonGroup().isSelected(but.getModel())) { 290 but.requestFocus(); 291 return; 292 } 293 } 294 throw new IllegalStateException ("How come none of the buttons is selected?"); 295 } 296 297 void requestFocusForPane() { 298 bar.setFocusable(false); 299 componentPanel.requestFocus(); 300 } 301 302 303 private Border buttonBorder = null; 304 private boolean isMetal = false; 305 private boolean isWindows = false; 306 private Border getButtonBorder() { 307 if (buttonBorder == null) { 308 buttonBorder = UIManager.getBorder ("nb.tabbutton.border"); } 311 312 return buttonBorder; 313 } 314 315 public static boolean isXPTheme () { 316 Boolean isXP = (Boolean )Toolkit.getDefaultToolkit(). 317 getDesktopProperty("win.xpstyle.themeActive"); return isXP == null ? false : isXP.booleanValue(); 319 } 320 321 322 void startToggling() { 323 ActionMap map = bar.getActionMap(); 324 Action act = new TogglesGoEastAction(); 325 map.put("navigateRight", act); 327 InputMap input = bar.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 328 329 act = new TogglesGoWestAction(); 330 map.put("navigateLeft", act); 332 333 act = new TogglesGoDownAction(); 334 map.put("TogglesGoDown", act); 335 map.put("navigateUp", act); 337 KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE"); input.put(stroke, "TogglesGoDown"); 339 } 340 341 342 private class TogglesGoWestAction extends AbstractAction { 343 344 public void actionPerformed(ActionEvent e) { 345 MultiViewDescription[] descs = model.getDescriptions(); 346 MultiViewDescription active = model.getActiveDescription(); 347 for (int i = 0; i < descs.length; i++) { 348 if (descs[i] == active) { 349 int next = i - 1; 350 if (next < 0) { 351 next = descs.length - 1; 352 } 353 changeVisibleManually(descs[next]); 354 requestFocusForSelectedButton(); 355 } 356 } 357 } 358 } 359 360 private class TogglesGoEastAction extends AbstractAction { 361 362 public void actionPerformed(ActionEvent e) { 363 MultiViewDescription[] descs = model.getDescriptions(); 364 MultiViewDescription active = model.getActiveDescription(); 365 for (int i = 0; i < descs.length; i++) { 366 if (descs[i] == active) { 367 int next = i + 1; 368 if (next >= descs.length) { 369 next = 0; 370 } 371 changeVisibleManually(descs[next]); 372 requestFocusForSelectedButton(); 373 } 374 } 375 } 376 } 377 378 private class TogglesGoDownAction extends AbstractAction { 379 380 public void actionPerformed(ActionEvent e) { 381 changeActiveManually(model.getActiveDescription()); 382 model.getActiveElement().getVisualRepresentation().requestFocusInWindow(); 383 } 384 } 385 386 387 390 static class TabsButtonModel extends ToggleButtonModel { 391 392 private MultiViewDescription desc; 393 public TabsButtonModel(MultiViewDescription description) { 394 super(); 395 desc = description; 396 } 397 398 public MultiViewDescription getButtonsDescription() { 399 return desc; 400 } 401 } 402 403 class ButtonMouseListener extends MouseAdapter { 404 public void mouseEntered(MouseEvent e) { 405 AbstractButton b = (AbstractButton)e.getComponent(); 406 b.getModel().setRollover(true); 407 } 408 public void mouseExited(MouseEvent e) { 409 AbstractButton b = (AbstractButton)e.getComponent(); 410 b.getModel().setRollover(false); 411 } 412 413 416 public void mousePressed(MouseEvent e) { 417 AbstractButton b = (AbstractButton)e.getComponent(); 418 MultiViewModel model = TabsComponent.this.model; 419 if (model != null) { 420 model.getButtonGroup().setSelected(b.getModel(), true); 421 model.fireActivateCurrent(); 422 } 423 424 } 425 426 } 427 428 429 private static final class TB extends JToolBar { 430 private boolean updating = false; 431 432 public TB() { 433 setName("editorToolbar"); 438 } 439 440 public void setBorder (Border b) { 441 if (!updating) { 442 return; 443 } 444 super.setBorder(b); 445 } 446 447 public void updateUI() { 448 updating = true; 449 try { 450 super.updateUI(); 451 } finally { 452 updating = false; 453 } 454 } 455 456 public String getUIClassID() { 457 return UIManager.get("Nb.Toolbar.ui") == null ? 458 super.getUIClassID() : "Nb.Toolbar.ui"; 459 } 460 } 461 } 462 | Popular Tags |