1 19 20 package org.netbeans.core.windows.view.ui.slides; 21 22 import java.awt.Component ; 23 import java.awt.Rectangle ; 24 import java.awt.Dimension ; 25 import java.awt.Point ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.KeyEvent ; 29 import java.awt.event.MouseEvent ; 30 import java.util.Map ; 31 import javax.swing.*; 32 import javax.swing.UIManager ; 33 import javax.swing.border.Border ; 34 import org.netbeans.core.windows.Constants; 35 import org.netbeans.swing.tabcontrol.*; 36 import org.netbeans.swing.tabcontrol.event.TabActionEvent; 37 import org.openide.util.Utilities; 38 import org.openide.windows.TopComponent; 39 40 48 final class CommandManager implements ActionListener { 49 50 51 private final SlideBar slideBar; 52 53 private TabbedContainer slidedTabContainer; 54 55 56 private Component curSlidedComp; 57 private SlidingButton curSlideButton; 58 private int curSlideOrientation; 59 private int curSlidedIndex; 60 private ResizeGestureRecognizer recog; 61 62 63 public CommandManager(SlideBar slideBar) { 64 this.slideBar = slideBar; 65 recog = new ResizeGestureRecognizer(this); 66 } 67 68 ResizeGestureRecognizer getResizer() { 69 return recog; 70 } 71 72 public void slideResize(int delta) { 73 if (!isCompSlided()) { 74 return; 75 } 76 SlideOperation op = SlideOperationFactory.createSlideResize(getSlidedTabContainer(), curSlideOrientation); 77 Rectangle finish = getSlidedTabContainer().getBounds(null); 78 String side = orientation2Side(curSlideOrientation); 79 if (Constants.BOTTOM.equals(side)) { 80 finish.height = finish.height - delta; 81 finish.y = finish.y + delta; 82 } 83 if (Constants.RIGHT.equals(side)) { 84 finish.width = finish.width - delta; 85 finish.x = finish.x + delta; 86 } 87 if (Constants.LEFT.equals(side)) { 88 finish.width = finish.width + delta; 89 } 90 op.setFinishBounds(finish); 91 postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_SLIDE_RESIZE, op)); 92 93 } 94 95 public void slideIn(int tabIndex) { 96 SlideBarDataModel model = slideBar.getModel(); 97 if (isCompSlided()) { 98 if (curSlidedComp != model.getTab(tabIndex).getComponent()) { 99 slideOut(false, false); 101 } 102 } 103 104 curSlidedIndex = tabIndex; 105 curSlidedComp = model.getTab(tabIndex).getComponent(); 106 curSlideOrientation = model.getOrientation(); 107 curSlideButton = slideBar.getButton(tabIndex); 108 TabbedContainer cont = updateSlidedTabContainer(tabIndex); 109 SlideOperation operation = SlideOperationFactory.createSlideIn( 110 cont, curSlideOrientation, true, true); 111 112 curSlideButton.setSelected(true); 113 114 postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_SLIDE_IN, operation)); 115 recog.attachResizeRecognizer(orientation2Side(curSlideOrientation), cont); 116 } 117 118 122 public void slideOut(boolean requestsActivation, boolean useEffect) { 123 if (!isCompSlided()) { 124 return; 125 } 126 127 SlideOperation operation = SlideOperationFactory.createSlideOut( 128 getSlidedTabContainer(), curSlideOrientation, useEffect, requestsActivation); 129 130 curSlideButton.setSelected(false); 131 132 recog.detachResizeRecognizer(orientation2Side(curSlideOrientation), getSlidedTabContainer()); 133 134 curSlidedComp = null; 135 curSlideButton = null; 136 curSlideOrientation = -1; 137 curSlidedIndex = -1; 138 139 postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_SLIDE_OUT, operation)); 140 } 141 142 143 public void slideIntoDesktop(int tabIndex, boolean useEffect) { 144 SlideOperation operation = null; 145 if (isCompSlided()) { 146 operation = SlideOperationFactory.createSlideIntoDesktop( 147 getSlidedTabContainer(), curSlideOrientation, useEffect); 148 } 149 recog.detachResizeRecognizer(orientation2Side(curSlideOrientation), getSlidedTabContainer()); 150 postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_DISABLE_AUTO_HIDE, operation, null, tabIndex)); 151 } 152 153 public void showPopup(MouseEvent mouseEvent, int tabIndex) { 154 postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_POPUP_REQUEST, mouseEvent, tabIndex)); 155 } 156 157 protected static String orientation2Side (int orientation) { 158 String side = Constants.LEFT; 159 if (orientation == SlideBarDataModel.WEST) { 160 side = Constants.LEFT; 161 } else if (orientation == SlideBarDataModel.EAST) { 162 side = Constants.RIGHT; 163 } else if (orientation == SlideBarDataModel.SOUTH) { 164 side = Constants.BOTTOM; 165 } 166 return side; 167 } 168 169 170 173 public void setActive(boolean active) { 174 getSlidedTabContainer().setActive(active); 175 } 176 177 178 179 183 public void actionPerformed(ActionEvent e) { 184 if (TabbedContainer.COMMAND_POPUP_REQUEST.equals(e.getActionCommand())) { 185 TabActionEvent tae = (TabActionEvent) e; 186 if (curSlidedComp != null && curSlidedComp instanceof TopComponent) { 187 TopComponent tc = (TopComponent)curSlidedComp; 188 Action[] actions = slideBar.getTabbed().getPopupActions(tc.getActions(), curSlidedIndex); 189 if (actions == null) { 190 actions = tc.getActions(); 191 } 192 193 showPopupMenu( 194 Utilities.actionsToPopup(actions, tc.getLookup()), tae.getMouseEvent().getPoint(), tae.getMouseEvent().getComponent()); 195 196 } 197 } else if (TabbedContainer.COMMAND_DISABLE_AUTO_HIDE.equals(e.getActionCommand())) { 198 slideIntoDesktop(curSlidedIndex, true); 199 } else if (TabbedContainer.COMMAND_MAXIMIZE.equals(e.getActionCommand())) { 200 postEvent(new SlideBarActionEvent(slideBar, SlideBar.COMMAND_MAXIMIZE, null, null, curSlidedIndex)); 202 } else { 203 TabActionEvent tae = (TabActionEvent)e; 205 TabActionEvent newEvt = new TabActionEvent( 206 tae.getSource(), tae.getActionCommand(), curSlidedIndex, tae.getMouseEvent()); 207 208 postEvent(newEvt); 209 } 210 } 211 212 213 214 private Rectangle getScreenCompRect(Component comp) { 215 Rectangle result = new Rectangle (comp.getLocationOnScreen(), comp.getSize()); 216 217 return result; 218 } 219 220 private static final boolean NO_POPUP_PLACEMENT_HACK = Boolean.getBoolean("netbeans.popup.no_hack"); 225 227 private static void showPopupMenu (JPopupMenu popup, Point p, Component comp) { 228 if (NO_POPUP_PLACEMENT_HACK) { 229 popup.show(comp, p.x, p.y); 230 return; 231 } 232 233 SwingUtilities.convertPointToScreen (p, comp); 234 Dimension popupSize = popup.getPreferredSize (); 235 Rectangle screenBounds = Utilities.getUsableScreenBounds(comp.getGraphicsConfiguration()); 236 237 if (p.x + popupSize.width > screenBounds.x + screenBounds.width) { 238 p.x = screenBounds.x + screenBounds.width - popupSize.width; 239 } 240 if (p.y + popupSize.height > screenBounds.y + screenBounds.height) { 241 p.y = screenBounds.y + screenBounds.height - popupSize.height; 242 } 243 244 SwingUtilities.convertPointFromScreen (p, comp); 245 popup.show(comp, p.x, p.y); 246 } 247 248 249 private TabbedContainer getSlidedTabContainer () { 250 if (slidedTabContainer == null) { 251 TabDataModel slidedCompModel = new DefaultTabDataModel(); 252 slidedTabContainer = new TabbedContainer(slidedCompModel, TabbedContainer.TYPE_VIEW, slideBar); 253 slidedTabContainer.addActionListener(this); 254 Border b = (Border ) UIManager.get ("floatingBorder"); if (b != null) { 256 slidedTabContainer.setBorder (b); 257 } 258 259 registerEscHandler(slidedTabContainer); 260 } 261 return slidedTabContainer; 262 } 263 264 private TabbedContainer updateSlidedTabContainer(int tabIndex) { 265 TabbedContainer container = getSlidedTabContainer(); 266 TabDataModel containerModel = container.getModel(); 267 SlideBarDataModel dataModel = slideBar.getModel(); 268 TabData origTab = dataModel.getTab(tabIndex); 272 TabData newTab = new TabData(origTab.getUserObject(), origTab.getIcon(), 273 origTab.getText(), origTab.getTooltip()); 274 if (containerModel.size() == 0) { 275 containerModel.addTab(0, newTab); 276 } else { 277 containerModel.setTab(0, newTab); 278 } 279 container.getSelectionModel().setSelectedIndex(0); 280 return container; 281 } 282 283 private void registerEscHandler (JComponent comp) { 284 comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "slideOut"); 285 comp.getActionMap().put("slideOut", escapeAction); 286 } 287 288 289 327 328 331 boolean isCompSlided() { 332 return curSlidedComp != null; 333 } 334 335 338 Component getSlidedComp() { 339 if (!isCompSlided()) { 340 return null; 341 } 342 return slidedTabContainer; 343 } 344 345 349 void syncWithModel() { 350 if (curSlidedComp == null) { 351 return; 352 } 353 354 if (!slideBar.containsComp(curSlidedComp)) { 355 slideOut(false, false); 358 } else { 359 SlideBarDataModel model = slideBar.getModel(); 361 if (curSlidedIndex < model.size()) { 364 String freshText = model.getTab(curSlidedIndex).getText(); 365 TabDataModel slidedModel = getSlidedTabContainer().getModel(); 366 String slidedText = slidedModel.getTab(0).getText(); 367 if (slidedText == null || !slidedText.equals(freshText)) { 368 slidedModel.setText(0, freshText); 369 slideBar.repaint(); 370 } 371 } 372 } 373 } 374 375 378 private void postEvent(ActionEvent evt) { 379 ((TabbedSlideAdapter)slideBar.getTabbed()).postActionEvent(evt); 380 } 381 382 private final Action escapeAction = new EscapeAction(); 383 384 private final class EscapeAction extends javax.swing.AbstractAction { 385 public void actionPerformed(ActionEvent e) { 386 slideOut(true, true); 387 } 388 } 390 391 } 392 | Popular Tags |