1 19 20 21 package org.netbeans.core.windows.view.ui; 22 23 24 import org.netbeans.core.windows.Constants; 25 import org.netbeans.core.windows.WindowManagerImpl; 26 import org.netbeans.core.windows.ModeImpl; 27 import org.netbeans.core.windows.actions.ActionUtils; 28 import org.netbeans.core.windows.actions.MaximizeWindowAction; 29 import org.netbeans.core.windows.view.ModeView; 30 import org.netbeans.core.windows.view.ui.tabcontrol.TabbedAdapter; 31 import org.netbeans.core.windows.WindowManagerImpl; 32 import org.netbeans.core.windows.view.SlidingView; 33 import org.netbeans.core.windows.view.ui.slides.SlideOperation; 34 import org.netbeans.core.windows.view.ui.slides.TabbedSlideAdapter; 35 import org.netbeans.swing.tabcontrol.TabbedContainer; 36 import org.netbeans.swing.tabcontrol.event.TabActionEvent; 37 import org.openide.windows.TopComponent; 38 import org.openide.util.Utilities; 39 40 import javax.swing.*; 41 import javax.swing.event.ChangeEvent ; 42 import javax.swing.event.ChangeListener ; 43 import java.awt.*; 44 import java.awt.event.ActionEvent ; 45 import java.awt.event.ActionListener ; 46 import java.awt.event.MouseEvent ; 47 import java.awt.event.AWTEventListener ; 48 import org.netbeans.core.windows.view.ui.slides.SlideBar; 49 import org.netbeans.core.windows.view.ui.slides.SlideBarActionEvent; 50 import org.netbeans.core.windows.view.ui.slides.SlideOperationFactory; 51 52 53 58 public final class TabbedHandler implements ChangeListener , ActionListener { 59 60 61 private final ModeView modeView; 62 63 64 private final Tabbed tabbed; 65 66 private final int kind; 67 68 69 private boolean ignoreChange = false; 70 71 private static ActivationManager activationManager = null; 72 73 74 public TabbedHandler(ModeView modeView, int kind, Tabbed tbd) { 75 this.modeView = modeView; 76 this.kind = kind; 77 78 synchronized (TabbedHandler.class) { 79 if (activationManager == null) { 80 activationManager = new ActivationManager(); 81 Toolkit.getDefaultToolkit().addAWTEventListener( 82 activationManager, AWTEvent.MOUSE_EVENT_MASK); 83 } 84 } 85 tabbed = tbd; 86 tabbed.addChangeListener(this); 87 tabbed.addActionListener(this); 88 90 ((Container)tabbed.getComponent()).setFocusCycleRoot(true); 92 } 93 94 95 113 public void requestAttention (TopComponent tc) { 114 tabbed.requestAttention(tc); 115 } 116 117 public void cancelRequestAttention (TopComponent tc) { 118 tabbed.cancelRequestAttention(tc); 119 } 120 121 public Component getComponent() { 122 return tabbed.getComponent(); 123 } 124 125 126 public void addTopComponent(TopComponent tc, int kind) { 127 addTCIntoTab(tc, kind); 128 } 129 130 131 public void setTopComponents(TopComponent[] tcs, TopComponent selected) { 132 ignoreChange = true; 133 try { 134 tabbed.setTopComponents(tcs, selected); 135 } finally { 136 ignoreChange = false; 137 } 138 } 139 140 141 private void addTCIntoTab(TopComponent tc, int kind) { 142 143 if(containsTC(tabbed, tc)) { 144 return; 145 } 146 147 Image icon = tc.getIcon(); 148 149 try { 150 ignoreChange = true; 151 String title = WindowManagerImpl.getInstance().getTopComponentDisplayName(tc); 152 if(title == null) { 153 title = ""; } 155 tabbed.addTopComponent( 156 title, 157 icon == null ? null : new ImageIcon(icon), 158 tc, tc.getToolTipText()); 159 } finally { 160 ignoreChange = false; 161 } 162 } 163 164 165 private static boolean containsTC(Tabbed tabbed, TopComponent tc) { 166 return tabbed.indexOf(tc) != -1; 167 } 168 169 170 public void removeTopComponent(TopComponent tc) { 171 removeTCFromTab(tc); 172 } 173 174 175 private void removeTCFromTab (TopComponent tc) { 176 if(tabbed.indexOf(tc) != -1) { 177 try { 178 ignoreChange = true; 179 tabbed.removeComponent(tc); 180 } finally { 181 ignoreChange = false; 182 } 183 184 tc.getAccessibleContext().setAccessibleParent(null); 187 } 188 } 189 190 191 public void topComponentIconChanged(TopComponent tc) { 192 int index = tabbed.indexOf(tc); 193 if (index < 0) { 194 return; 195 } 196 197 tabbed.setIconAt(index, new ImageIcon(tc.getIcon())); 198 } 199 200 201 public void topComponentNameChanged(TopComponent tc, int kind) { 202 int index = tabbed.indexOf(tc); 203 if (index < 0) { 204 return; 205 } 206 207 String title = WindowManagerImpl.getInstance().getTopComponentDisplayName(tc); 208 if(title == null) { 209 title = ""; } 211 tabbed.setTitleAt (index, title); 212 } 213 214 public void topComponentToolTipChanged(TopComponent tc) { 215 int index = tabbed.indexOf(tc); 216 if (index < 0) { 217 return; 218 } 219 220 tabbed.setToolTipTextAt(index, tc.getToolTipText()); 221 } 222 223 225 public void setSelectedTopComponent(TopComponent tc) { 226 if(tc == getSelectedTopComponent()) { 227 return; 228 } 229 if (tc == null && !isNullSelectionAllowed()) { 230 return; 231 } 232 233 if(tabbed.indexOf(tc) >= 0 || (isNullSelectionAllowed() && tc == null)) { 234 try { 235 ignoreChange = true; 236 tabbed.setSelectedComponent(tc); 237 } finally { 238 ignoreChange = false; 239 } 240 } 241 } 242 243 private boolean isNullSelectionAllowed() { 244 return kind == Constants.MODE_KIND_SLIDING; 245 } 246 247 public TopComponent getSelectedTopComponent() { 248 return tabbed.getSelectedTopComponent(); 249 } 250 251 public TopComponent[] getTopComponents() { 252 return tabbed.getTopComponents(); 253 } 254 255 public void setActive(boolean active) { 256 tabbed.setActive(active); 257 } 258 259 public void stateChanged(ChangeEvent evt) { 262 if(ignoreChange || evt.getSource() != tabbed) { 263 return; 264 } 265 TopComponent selected = tabbed.getSelectedTopComponent(); 266 modeView.getController().userSelectedTab(modeView, (TopComponent)selected); 267 } 268 269 public Shape getIndicationForLocation(Point location, TopComponent startingTransfer, 271 Point startingPoint, boolean attachingPossible) { 272 return tabbed.getIndicationForLocation(location, startingTransfer, 273 startingPoint, attachingPossible); 274 } 275 276 public Object getConstraintForLocation(Point location, boolean attachingPossible) { 277 return tabbed.getConstraintForLocation(location, attachingPossible); 278 } 279 280 public Rectangle getTabBounds(int tabIndex) { 282 return tabbed.getTabBounds(tabIndex); 283 } 284 285 public void actionPerformed(ActionEvent e) { 286 if (e instanceof TabActionEvent) { 287 TabActionEvent tae = (TabActionEvent) e; 288 String cmd = tae.getActionCommand(); 289 if (TabbedContainer.COMMAND_SELECT.equals(cmd)) { 290 return; 291 } 292 tae.consume(); 293 if (TabbedContainer.COMMAND_CLOSE == cmd) { TopComponent tc = (TopComponent) tabbed.getTopComponentAt(tae.getTabIndex()); 295 if (tc == null) { 296 throw new IllegalStateException ("Component to be closed " + 297 "is null at index " + tae.getTabIndex()); 298 } 299 modeView.getController().userClosedTopComponent(modeView, tc); 300 } else if (TabbedContainer.COMMAND_POPUP_REQUEST == cmd) { 301 handlePopupMenuShowing(tae.getMouseEvent(), tae.getTabIndex()); 302 } else if (TabbedContainer.COMMAND_MAXIMIZE == cmd) { 303 handleMaximization(tae); 304 } else if (TabbedContainer.COMMAND_CLOSE_ALL == cmd) { 305 ActionUtils.closeAllDocuments(); 306 } else if (TabbedContainer.COMMAND_CLOSE_ALL_BUT_THIS == cmd) { 307 TopComponent tc = (TopComponent) tabbed.getTopComponentAt(tae.getTabIndex()); 308 ActionUtils.closeAllExcept(tc); 309 } else if (TabbedContainer.COMMAND_ENABLE_AUTO_HIDE.equals(cmd)) { 311 TopComponent tc = (TopComponent) tabbed.getTopComponentAt(tae.getTabIndex()); 312 Component tabbedComp = tabbed.getComponent(); 314 315 String side = WindowManagerImpl.getInstance().guessSlideSide(tc); 316 SlideOperation operation = SlideOperationFactory.createSlideIntoEdge( 317 tabbedComp, side, true); 318 operation.setStartBounds( 319 new Rectangle(tabbedComp.getLocationOnScreen(), tabbedComp.getSize())); 320 operation.prepareEffect(); 321 322 modeView.getController().userEnabledAutoHide(modeView, tc); 323 modeView.getController().userTriggeredSlideIntoEdge(modeView, operation); 324 } 325 } else if (e instanceof SlideBarActionEvent) { 326 SlideBarActionEvent sbe = (SlideBarActionEvent)e; 328 String cmd = sbe.getActionCommand(); 329 if (SlideBar.COMMAND_POPUP_REQUEST.equals(cmd)) { 330 handlePopupMenuShowing(sbe.getMouseEvent(), sbe.getTabIndex()); 331 } else if (SlideBar.COMMAND_SLIDE_IN.equals(cmd)) { 332 modeView.getController().userTriggeredSlideIn(modeView, sbe.getSlideOperation()); 333 } else if (SlideBar.COMMAND_SLIDE_RESIZE.equals(cmd)) { 334 modeView.getController().userResizedSlidingWindow(modeView, sbe.getSlideOperation()); 335 } else if (SlideBar.COMMAND_SLIDE_OUT.equals(cmd)) { 336 SlideOperation op = new ProxySlideOperation(sbe.getSlideOperation(), ignoreChange); 340 modeView.getController().userTriggeredSlideOut(modeView, op); 341 } else if (SlideBar.COMMAND_DISABLE_AUTO_HIDE.equals(cmd)) { 342 TopComponent tc = (TopComponent) tabbed.getTopComponentAt(sbe.getTabIndex()); 343 modeView.getController().userDisabledAutoHide(modeView, tc); 344 } else if( SlideBar.COMMAND_MAXIMIZE == cmd ) { 345 TopComponent tc = (TopComponent) tabbed.getTopComponentAt(sbe.getTabIndex()); 346 MaximizeWindowAction mwa = new MaximizeWindowAction(tc); 347 mwa.actionPerformed(e); 348 } 349 } 350 } 351 352 353 public static void handlePopupMenuShowing(MouseEvent e, int idx) { 354 Component c = (Component) e.getSource(); 355 while (c != null && !(c instanceof Tabbed.Accessor)) 356 c = c.getParent(); 357 if (c == null) { 358 return; 359 } 360 final Tabbed tab = ((Tabbed.Accessor)c).getTabbed(); 361 362 final Point p = SwingUtilities.convertPoint((Component) e.getSource(), e.getPoint(), c); 363 364 final int clickTab = idx; 365 if (clickTab < 0) { 366 return; 367 } 368 369 TopComponent tc = tab.getTopComponentAt(clickTab); 370 if(tc == null) { 371 return; 372 } 373 374 Action[] actions = tab.getPopupActions(tc.getActions(), clickTab); 376 if (actions == null) { 377 actions = tc.getActions(); 378 } 379 380 showPopupMenu( 381 Utilities.actionsToPopup(actions, tc.getLookup()), p, c); 382 } 383 384 386 private static void showPopupMenu (JPopupMenu popup, Point p, Component comp) { 387 popup.show(comp, p.x, p.y); 388 } 389 390 391 public static void handleMaximization(TabActionEvent tae) { 392 Component c = (Component) tae.getSource(); 393 while (c != null && !(c instanceof Tabbed)) 394 c = c.getParent(); 395 if (c == null) { 396 return; 397 } 398 399 final Tabbed tab = (Tabbed) c; 400 TopComponent tc = tab.getTopComponentAt(tae.getTabIndex()); 401 MaximizeWindowAction mwa = new MaximizeWindowAction(tc); 403 mwa.actionPerformed(tae); 404 } 405 406 408 private static class ActivationManager implements AWTEventListener { 409 public void eventDispatched(AWTEvent e) { 410 if(e.getID() == MouseEvent.MOUSE_PRESSED) { 411 handleActivation((MouseEvent ) e); 412 } 413 } 414 415 433 private void handleActivation(MouseEvent evt) { 434 Object obj = evt.getSource(); 435 if (!(obj instanceof Component)) { 436 return; 437 } 438 Component comp = (Component) obj; 439 440 while (comp != null && !(comp instanceof ModeComponent)) { 441 if (comp instanceof JComponent) { 442 JComponent c = (JComponent)comp; 443 if (Boolean.TRUE.equals(c.getClientProperty("dontActivate"))) { return; 446 } 447 } 448 if (comp instanceof TopComponent) { 449 TopComponent tc = (TopComponent)comp; 450 if (Boolean.TRUE.equals(tc.getClientProperty("isSliding"))) { tc.requestActive(); 453 return; 454 } 455 } 456 comp = comp.getParent(); 457 } 458 459 if (comp instanceof ModeComponent) { 460 ModeComponent modeComp = (ModeComponent)comp; 461 if (modeComp.getKind() != Constants.MODE_KIND_SLIDING) { 463 ModeView modeView = modeComp.getModeView(); 464 modeView.getController().userActivatedModeView(modeView); 465 } 466 } 467 } 468 469 } 471 474 private static class ProxySlideOperation implements SlideOperation { 475 476 private SlideOperation original; 477 private boolean disable; 478 479 public ProxySlideOperation(SlideOperation orig, boolean disableActivation) { 480 original = orig; 481 disable = disableActivation; 482 } 483 484 public Component getComponent() { 485 return original.getComponent(); 486 } 487 488 public Rectangle getFinishBounds() { 489 return original.getFinishBounds(); 490 } 491 492 public String getSide() { 493 return original.getSide(); 494 } 495 496 public Rectangle getStartBounds() { 497 return original.getStartBounds(); 498 } 499 500 public int getType() { 501 return original.getType(); 502 } 503 504 public void prepareEffect() { 505 original.prepareEffect(); 506 } 507 508 public boolean requestsActivation() { 509 if (disable) { 510 return false; 511 } 512 return original.requestsActivation(); 513 } 514 515 public void run(JLayeredPane pane, Integer layer) { 516 original.run(pane, layer); 517 } 518 519 public void setFinishBounds(Rectangle bounds) { 520 original.setFinishBounds(bounds); 521 } 522 523 public void setStartBounds(Rectangle bounds) { 524 original.setStartBounds(bounds); 525 } 526 } 527 528 } 529 530 | Popular Tags |