1 19 24 25 package org.netbeans.swing.tabcontrol.plaf; 26 27 import org.netbeans.swing.tabcontrol.TabDisplayer; 28 29 import javax.swing.*; 30 import java.awt.*; 31 import java.awt.event.*; 32 import java.awt.image.BufferedImage ; 33 import java.lang.ref.SoftReference ; 34 35 40 public abstract class BasicScrollingTabDisplayerUI extends BasicTabDisplayerUI { 41 private Rectangle scratch = new Rectangle(); 42 43 private JPanel controlButtons; 44 45 private TabControlButton btnScrollLeft; 46 private TabControlButton btnScrollRight; 47 private TabControlButton btnDropDown; 48 private TabControlButton btnMaximizeRestore; 49 50 53 public BasicScrollingTabDisplayerUI(TabDisplayer displayer) { 54 super(displayer); 55 } 56 57 protected final TabLayoutModel createLayoutModel() { 58 DefaultTabLayoutModel dtlm = new DefaultTabLayoutModel( 59 displayer.getModel(), 60 displayer); 61 return new ScrollingTabLayoutModel(dtlm, selectionModel, 62 displayer.getModel()); 63 } 64 65 protected TabState createTabState() { 66 return new ScrollingTabState(); 67 } 68 69 protected HierarchyListener createHierarchyListener() { 70 return new ScrollingHierarchyListener(); 71 } 72 73 public void makeTabVisible (int tab) { 74 if (scroll().makeVisible(tab, getTabsAreaWidth())) { 75 getTabsVisibleArea(scratch); 76 displayer.repaint(scratch.x, scratch.y, scratch.width, scratch.height); 77 } 78 } 79 80 83 protected final int getTabsAreaWidth() { 84 int result = displayer.getWidth(); 85 Insets ins = getTabAreaInsets(); 86 return result - (ins.left + ins.right); 87 } 88 89 public Insets getTabAreaInsets() { 90 return new Insets(0, 0, 0, getControlButtons().getPreferredSize().width + 5); 91 } 92 93 protected final int getLastVisibleTab() { 94 if (displayer.getModel().size() == 0) { 95 return -1; 96 } 97 return scroll().getLastVisibleTab(getTabsAreaWidth()); 98 } 99 100 protected final int getFirstVisibleTab() { 101 if (displayer.getModel().size() == 0) { 102 return -1; 103 } 104 return scroll().getFirstVisibleTab(getTabsAreaWidth()); 105 } 106 107 protected void install() { 108 super.install(); 109 installControlButtons(); 110 ((ScrollingTabLayoutModel) layoutModel).setPixelsToAddToSelection ( 111 defaultRenderer.getPixelsToAddToSelection()); 112 } 113 114 protected void uninstall() { 115 super.uninstall(); 116 displayer.setLayout(null); 117 displayer.removeAll(); 118 } 119 120 protected LayoutManager createLayout() { 121 return new WCLayout(); 122 } 123 124 128 protected Component getControlButtons() { 129 if( null == controlButtons ) { 130 JPanel buttonsPanel = new JPanel( null ); 131 buttonsPanel.setOpaque( false ); 132 133 int width = 0; 134 int height = 0; 135 136 Action a = scroll().getBackwardAction(); 138 a.putValue( "control", displayer ); btnScrollLeft = TabControlButtonFactory.createScrollLeftButton( displayer, a ); 140 buttonsPanel.add( btnScrollLeft ); 141 Icon icon = btnScrollLeft.getIcon(); 142 btnScrollLeft.setBounds( width, 0, icon.getIconWidth(), icon.getIconHeight() ); 143 width += icon.getIconWidth(); 144 145 a = scroll().getForwardAction(); 147 a.putValue( "control", displayer ); btnScrollRight = TabControlButtonFactory.createScrollRightButton( displayer, a ); 149 buttonsPanel.add( btnScrollRight ); 150 icon = btnScrollRight.getIcon(); 151 btnScrollRight.setBounds( width, 0, icon.getIconWidth(), icon.getIconHeight() ); 152 width += icon.getIconWidth(); 153 154 btnDropDown = TabControlButtonFactory.createDropDownButton( displayer ); 156 buttonsPanel.add( btnDropDown ); 157 158 icon = btnDropDown.getIcon(); 159 width += 3; 160 btnDropDown.setBounds( width, 0, icon.getIconWidth(), icon.getIconHeight() ); 161 width += icon.getIconWidth(); 162 height = icon.getIconHeight(); 163 164 if( null != displayer.getWinsysInfo() ) { 166 width += 3; 167 btnMaximizeRestore = TabControlButtonFactory.createMaximizeRestoreButton( displayer ); 168 buttonsPanel.add( btnMaximizeRestore ); 169 icon = btnMaximizeRestore.getIcon(); 170 btnMaximizeRestore.setBounds( width, 0, icon.getIconWidth(), icon.getIconHeight() ); 171 width += icon.getIconWidth(); 172 } 173 174 Dimension size = new Dimension( width, height ); 175 buttonsPanel.setMinimumSize( size ); 176 buttonsPanel.setSize( size ); 177 buttonsPanel.setPreferredSize( size ); 178 buttonsPanel.setMaximumSize( size ); 179 180 controlButtons = buttonsPanel; 181 } 182 return controlButtons; 183 } 184 185 protected ComponentListener createComponentListener() { 186 return new ScrollingDisplayerComponentListener(); 187 } 188 189 private int lastKnownModelSize = Integer.MAX_VALUE; 190 195 protected void modelChanged() { 196 scroll().clearCachedData(); 197 int index = selectionModel.getSelectedIndex(); 198 199 if (index >= scroll().getCachedFirstVisibleTab() && index < scroll().getCachedLastVisibleTab()) { 202 makeTabVisible(selectionModel.getSelectedIndex()); 203 } 204 205 int modelSize = displayer.getModel().size(); 206 if (modelSize < lastKnownModelSize) { 207 scroll().ensureAvailableSpaceUsed(true); 210 } 211 lastKnownModelSize = modelSize; 212 super.modelChanged(); 213 } 214 215 protected void installControlButtons() { 216 displayer.setLayout(createLayout()); 217 displayer.add(getControlButtons()); 218 } 219 220 public Dimension getMinimumSize(JComponent c) { 221 return getPreferredSize(c); 222 } 223 224 228 protected final ScrollingTabLayoutModel scroll() { 229 return (ScrollingTabLayoutModel) layoutModel; 230 } 231 232 236 protected void processMouseWheelEvent(MouseWheelEvent e) { 237 int i = e.getWheelRotation(); 238 tabState.clearTransientStates(); 242 int offset = scroll().getOffset(); 243 if (i > 0 && (offset < displayer.getModel().size() - 1)) { 244 if (scroll().isLastTabClipped()) { 245 scroll().setOffset(offset + 1); 246 } 247 } else if (i < 0) { 248 if (offset >= 0) { 249 scroll().setOffset(offset - 1); 250 } 251 } else { 252 return; 253 } 254 255 256 displayer.repaint(); 260 } 261 262 263 protected class ScrollingTabState extends BasicTabState { 264 public int getState(int tabIndex) { 265 int result = super.getState(tabIndex); 266 int first = getFirstVisibleTab(); 267 int last = getLastVisibleTab(); 268 269 if (tabIndex < first || tabIndex > last) { 270 return TabState.NOT_ONSCREEN; 271 } 272 if (first == last && first == tabIndex 273 && displayer.getModel().size() > 1) { 274 result |= TabState.CLIP_LEFT | TabState.CLIP_RIGHT; 278 279 } else if (getTabsAreaWidth() < scroll() 280 .getMinimumLeftClippedWidth() 281 + scroll().getMinimumRightClippedWidth() 282 && tabIndex == first && last == first - 1 && displayer.getModel() 283 .size() 284 > 1 && scroll().isLastTabClipped()) { 285 result |= TabState.CLIP_LEFT; 288 } else { 289 if (tabIndex == first && scroll().getOffset() == first) { 290 result |= TabState.CLIP_LEFT; 291 } 292 if (tabIndex == last && scroll().isLastTabClipped()) { 293 result |= TabState.CLIP_RIGHT; 294 } 295 } 296 return result; 297 } 298 } 299 300 protected class ScrollingDisplayerComponentListener extends ComponentAdapter { 301 public void componentResized(ComponentEvent e) { 302 makeTabVisible(selectionModel.getSelectedIndex()); 304 } 305 } 306 307 protected class ScrollingHierarchyListener extends DisplayerHierarchyListener { 308 public void hierarchyChanged(HierarchyEvent e) { 309 super.hierarchyChanged (e); 310 if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) { 311 if (displayer.isShowing()) { 312 if (tabState != null && selectionModel != null) { 315 tabState.setActive (displayer.isActive()); 316 makeTabVisible (selectionModel.getSelectedIndex()); 317 } 318 } 319 } 320 } 321 } 322 323 static SoftReference <BufferedImage > ctx = null; 324 325 329 public static Graphics2D getOffscreenGraphics() { 330 BufferedImage result = null; 331 if (ctx != null) { 334 result = ctx.get(); 335 } 336 if (result == null) { 337 result = new BufferedImage (10, 10, BufferedImage.TYPE_INT_RGB); 338 ctx = new SoftReference <BufferedImage >(result); 339 } 340 return (Graphics2D) result.getGraphics(); 341 } 342 343 346 protected Rectangle getControlButtonsRectangle( Container parent ) { 347 Component c = getControlButtons(); 348 return new Rectangle( parent.getWidth()-c.getWidth(), 0, c.getWidth(), c.getHeight() ); 349 } 350 351 355 private class WCLayout implements LayoutManager { 356 357 public void addLayoutComponent(String name, Component comp) { 358 } 359 360 public void layoutContainer(java.awt.Container parent) { 361 362 Rectangle r = getControlButtonsRectangle( parent ); 363 Component c = getControlButtons(); 364 c.setBounds( r ); 365 } 366 367 public Dimension minimumLayoutSize(Container parent) { 368 return getPreferredSize((JComponent) parent); 369 } 370 371 public Dimension preferredLayoutSize(Container parent) { 372 return getPreferredSize((JComponent) parent); 373 } 374 375 public void removeLayoutComponent(java.awt.Component comp) { 376 } 377 } 378 } 379 | Popular Tags |