1 19 20 package org.netbeans.core.windows.view.ui.slides; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.Insets ; 25 import java.awt.Point ; 26 import java.awt.Rectangle ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.awt.event.MouseEvent ; 30 import java.util.ArrayList ; 31 import java.util.Arrays ; 32 import java.util.HashSet ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Set ; 36 import javax.swing.Box ; 37 import javax.swing.BoxLayout ; 38 import javax.swing.DefaultSingleSelectionModel ; 39 import javax.swing.JButton ; 40 import javax.swing.JComponent ; 41 import javax.swing.SingleSelectionModel ; 42 import javax.swing.SwingUtilities ; 43 import javax.swing.UIManager ; 44 import javax.swing.border.Border ; 45 import javax.swing.border.EmptyBorder ; 46 import javax.swing.event.ChangeEvent ; 47 import javax.swing.event.ChangeListener ; 48 import javax.swing.event.ListDataEvent ; 49 import org.netbeans.core.windows.Constants; 50 import org.netbeans.core.windows.WindowManagerImpl; 51 import org.netbeans.core.windows.view.ui.Tabbed; 52 import org.netbeans.core.windows.view.ui.tabcontrol.TabbedAdapter; 53 import org.netbeans.swing.tabcontrol.DefaultTabDataModel; 54 import org.netbeans.swing.tabcontrol.SlideBarDataModel; 55 import org.netbeans.swing.tabcontrol.SlidingButton; 56 import org.netbeans.swing.tabcontrol.TabData; 57 import org.netbeans.swing.tabcontrol.TabDataModel; 58 import org.netbeans.swing.tabcontrol.TabDisplayer; 59 import org.netbeans.swing.tabcontrol.TabbedContainer; 60 import org.netbeans.swing.tabcontrol.WinsysInfoForTabbed; 61 import org.netbeans.swing.tabcontrol.event.ComplexListDataEvent; 62 import org.netbeans.swing.tabcontrol.event.ComplexListDataListener; 63 import org.netbeans.swing.tabcontrol.event.TabActionEvent; 64 import org.openide.windows.TopComponent; 65 66 76 public final class SlideBar extends Box implements ComplexListDataListener, 77 SlideBarController, Tabbed.Accessor, WinsysInfoForTabbed, ChangeListener { 78 79 80 public static final String COMMAND_SLIDE_IN = "slideIn"; 82 83 public static final String COMMAND_SLIDE_OUT = "slideOut"; 85 public static final String COMMAND_SLIDE_RESIZE = "slideResize"; 87 88 public static final String COMMAND_POPUP_REQUEST = "popup"; 90 91 public static final String COMMAND_DISABLE_AUTO_HIDE = "disableAutoHide"; 93 94 public static final String COMMAND_MAXIMIZE = "slideMaximize"; 96 97 private final TabbedSlideAdapter tabbed; 98 99 private final SlideBarDataModel dataModel; 100 101 private final SingleSelectionModel selModel; 102 103 private SlideGestureRecognizer gestureRecognizer; 104 105 private List <SlidingButton> buttons; 106 107 private CommandManager commandMgr; 108 109 private boolean active = false; 110 111 114 public SlideBar(TabbedSlideAdapter tabbed, SlideBarDataModel dataModel, SingleSelectionModel selModel) { 115 super(dataModel.getOrientation() == SlideBarDataModel.SOUTH 116 ? BoxLayout.X_AXIS : BoxLayout.Y_AXIS); 117 this.tabbed = tabbed; 118 this.dataModel = dataModel; 119 this.selModel = selModel; 120 commandMgr = new CommandManager(this); 121 gestureRecognizer = new SlideGestureRecognizer(this, commandMgr.getResizer()); 122 buttons = new ArrayList <SlidingButton>(5); 123 124 syncWithModel(); 125 126 dataModel.addComplexListDataListener(this); 127 selModel.addChangeListener(this); 128 } 129 130 public SlideBarDataModel getModel() { 131 return dataModel; 132 } 133 134 public SingleSelectionModel getSelectionModel () { 135 return selModel; 136 } 137 138 139 140 public void intervalAdded(ListDataEvent e) { 141 assert SwingUtilities.isEventDispatchThread(); 142 143 int first = e.getIndex0(); 144 int last = e.getIndex1(); 145 SlideBarDataModel data = (SlideBarDataModel)e.getSource(); 146 SlidingButton curButton; 147 for (int i = first; i <= last; i++) { 148 curButton = new SlidingButton(data.getTab(i), data.getOrientation()); 149 gestureRecognizer.attachButton(curButton); 150 buttons.add(i, curButton); 151 add(curButton, i * 2); 152 add(createStrut(), i * 2 + 1); 153 revalidate(); 154 } 155 } 156 157 public void intervalRemoved(ListDataEvent e) { 158 assert SwingUtilities.isEventDispatchThread(); 159 160 int first = e.getIndex0(); 161 int last = e.getIndex1(); 162 SlideBarDataModel data = (SlideBarDataModel)e.getSource(); 163 SlidingButton curButton = null; 164 for (int i = last; i >= first; i--) { 165 gestureRecognizer.detachButton((SlidingButton)buttons.get(i)); 166 buttons.remove(i); 167 remove(i * 2 + 1); 169 remove(i * 2); 170 } 171 } 172 173 public void contentsChanged(ListDataEvent e) { 174 syncWithModel(); 175 } 176 177 public void indicesAdded(ComplexListDataEvent e) { 178 syncWithModel(); 179 } 180 181 public void indicesChanged(ComplexListDataEvent e) { 182 syncWithModel(); 183 } 184 185 public void indicesRemoved(ComplexListDataEvent e) { 186 syncWithModel(); 187 } 188 189 193 public int tabForCoordinate(int x, int y) { 194 Rectangle curBounds = new Rectangle (); 195 int index = 0; 196 for (Iterator iter = buttons.iterator(); iter.hasNext(); index++) { 197 ((Component )iter.next()).getBounds(curBounds); 198 if (curBounds.contains(x, y)) { 199 return index; 200 } 201 } 202 return -1; 203 } 204 205 int nextTabForCoordinate(int x, int y) { 206 Rectangle curBounds = new Rectangle (); 207 int index = 0; 208 Iterator iter = buttons.iterator(); 209 while (iter.hasNext()) { 210 Component comp = (Component )iter.next(); 211 comp.getBounds(curBounds); 212 if (dataModel.getOrientation() == SlideBarDataModel.SOUTH) { 213 if (curBounds.x + (curBounds.width/2) < x) { 214 index = index + 1; 215 continue; 216 } 217 } else { 218 if (curBounds.y + (curBounds.height/2) < y) { 219 index = index + 1; 220 continue; 221 } 222 } 223 return index; 224 } 225 return index; 226 } 227 228 229 232 public void stateChanged(ChangeEvent e) { 233 int selIndex = selModel.getSelectedIndex(); 234 235 tabbed.postSelectionEvent(); 237 if (isDisplayable() && isVisible()) { 239 if (selIndex != -1) { 241 commandMgr.slideIn(selIndex); 242 } else { 243 commandMgr.slideOut(true, true); 244 } 245 } 246 } 247 248 249 250 251 public void userToggledAutoHide(int tabIndex, boolean enabled) { 252 commandMgr.slideIntoDesktop(tabIndex, true); 253 } 254 255 public void userTriggeredPopup(MouseEvent mouseEvent, Component clickedButton) { 256 int index = getButtonIndex(clickedButton); 257 commandMgr.showPopup(mouseEvent, index); 258 } 259 260 private SlidingButton buttonFor (TopComponent tc) { 261 int idx = 0; 262 for (Iterator i=dataModel.getTabs().iterator(); i.hasNext();) { 263 TabData td = (TabData) i.next(); 264 if (td.getComponent() == tc) { 265 break; 266 } 267 if (!i.hasNext()) { 268 idx = -1; 269 } else { 270 idx++; 271 } 272 } 273 if (idx >= 0 && idx < dataModel.size()) { 274 return getButton(idx); 275 } else { 276 return null; 277 } 278 } 279 280 public void setBlinking (TopComponent tc, boolean val) { 281 SlidingButton button = buttonFor (tc); 282 if (button != null) { 283 button.setBlinking(val); 284 } 285 } 286 287 288 public void userClickedSlidingButton(Component clickedButton) { 289 int index = getButtonIndex(clickedButton); 290 SlidingButton button = (SlidingButton) buttons.get(index); 291 button.setBlinking(false); 292 293 if (index != selModel.getSelectedIndex() || !isActive()) { 294 TopComponent tc = (TopComponent)dataModel.getTab(index).getComponent(); 295 if (tc != null) { 296 tc.requestActive(); 297 } 298 } else { 299 selModel.setSelectedIndex(-1); 300 } 301 } 302 303 304 public boolean userTriggeredAutoSlideIn(Component sourceButton) { 305 int index = getButtonIndex(sourceButton); 306 if (index < 0) { 307 return false; 308 } 309 SlidingButton button = (SlidingButton) buttons.get(index); 310 button.setBlinking(false); 311 TopComponent tc = (TopComponent)dataModel.getTab(index).getComponent(); 312 if (tc == null) { 313 return false; 314 } 315 tc.requestVisible(); 316 return true; 317 } 318 319 320 public void userTriggeredAutoSlideOut() { 321 selModel.setSelectedIndex(-1); 322 } 323 324 public Rectangle getTabBounds(int tabIndex) { 325 Component button = getButton(tabIndex); 326 if (button == null) { 327 return null; 328 } 329 Insets insets = getInsets(); 330 Point leftTop = new Point (insets.left, insets.top); 331 332 Dimension strutPrefSize = createStrut().getPreferredSize(); 333 if (dataModel.getOrientation() == SlideBarDataModel.SOUTH) { 334 for (int i = 0; i < tabIndex; i++) { 336 leftTop.x += getButton(i).getPreferredSize().width; 337 leftTop.x += strutPrefSize.width; 338 } 339 } else { 340 for (int i = 0; i < tabIndex; i++) { 342 leftTop.y += getButton(i).getPreferredSize().height; 343 leftTop.y += strutPrefSize.height; 344 } 345 } 346 return new Rectangle (leftTop, button.getPreferredSize()); 347 } 348 349 350 351 public Tabbed getTabbed () { 352 return tabbed; 353 } 354 355 356 357 public Object getOrientation(Component comp) { 358 if (WindowManagerImpl.getInstance().getEditorAreaState() != Constants.EDITOR_AREA_JOINED) { 359 return TabDisplayer.ORIENTATION_INVISIBLE; 360 } 361 return TabDisplayer.ORIENTATION_CENTER; 362 } 363 364 public boolean inMaximizedMode(Component comp) { 365 return TabbedAdapter.isInMaximizedMode(comp); 366 } 367 368 369 370 371 374 Component getSlidedComp() { 375 return commandMgr.getSlidedComp(); 376 } 377 378 void setActive(boolean active) { 379 this.active = active; 380 commandMgr.setActive(active); 381 } 382 383 boolean isActive() { 384 return active; 385 } 386 387 boolean isHoveringAllowed() { 388 return !isActive() || !commandMgr.isCompSlided(); 389 } 390 391 int getButtonIndex(Component button) { 392 return buttons.indexOf(button); 393 } 394 395 SlidingButton getButton(int index) { 396 return (SlidingButton)buttons.get(index); 397 } 398 399 400 boolean containsComp(Component comp) { 401 List tabs = getModel().getTabs(); 402 TabData curTab = null; 403 for (Iterator iter = tabs.iterator(); iter.hasNext(); ) { 404 curTab = (TabData)iter.next(); 405 if (comp.equals(curTab.getComponent())) { 406 return true; 407 } 408 } 409 return false; 410 } 411 412 private Component createStrut () { 413 return dataModel.getOrientation() == SlideBarDataModel.SOUTH 414 ? createHorizontalStrut(5) : createVerticalStrut(5); 415 } 416 417 private void syncWithModel () { 418 assert SwingUtilities.isEventDispatchThread(); 419 Set <TabData> blinks = null; 420 for (SlidingButton curr: buttons) { 421 if (curr.isBlinking()) { 422 if (blinks == null) { 423 blinks = new HashSet <TabData>(); 424 } 425 blinks.add (curr.getData()); 426 } 427 gestureRecognizer.detachButton(curr); 428 } 429 removeAll(); 430 buttons.clear(); 431 432 List <TabData> dataList = dataModel.getTabs(); 433 SlidingButton curButton; 434 for (Iterator iter = dataList.iterator(); iter.hasNext(); ) { 435 TabData td = (TabData) iter.next(); 436 curButton = new SlidingButton(td, dataModel.getOrientation()); 437 if (blinks != null && blinks.contains(td)) { 438 curButton.setBlinking(true); 439 } 440 gestureRecognizer.attachButton(curButton); 441 buttons.add(curButton); 442 add(curButton); 443 add(createStrut()); 444 } 445 446 commandMgr.syncWithModel(); 447 revalidate(); 449 repaint(); 452 } 453 454 } 455 456 457 459 460 461 463 467 474 477 478 485 489 490 493 496 | Popular Tags |