1 19 24 25 package org.netbeans.swing.tabcontrol.plaf; 26 27 import org.netbeans.swing.tabcontrol.TabDisplayer; 28 import org.netbeans.swing.tabcontrol.TabDisplayerUI; 29 import org.netbeans.swing.tabcontrol.event.ComplexListDataEvent; 30 import org.netbeans.swing.tabcontrol.event.ComplexListDataListener; 31 32 import javax.swing.*; 33 import javax.swing.event.ChangeEvent ; 34 import javax.swing.event.ChangeListener ; 35 import javax.swing.event.ListDataEvent ; 36 import java.awt.*; 37 import java.awt.event.*; 38 import java.beans.PropertyChangeEvent ; 39 import java.beans.PropertyChangeListener ; 40 41 56 public abstract class AbstractTabDisplayerUI extends TabDisplayerUI { 57 61 protected TabLayoutModel layoutModel = null; 62 66 protected MouseListener mouseListener = null; 67 72 protected ComponentListener componentListener = null; 73 78 protected PropertyChangeListener propertyChangeListener = null; 79 84 protected ModelListener modelListener = null; 85 89 protected ChangeListener selectionListener = null; 90 91 protected HierarchyListener hierarchyListener = null; 92 93 96 public AbstractTabDisplayerUI(TabDisplayer displayer) { 97 super(displayer); 98 } 99 100 106 public final void installUI(JComponent c) { 107 assert c == displayer; 108 super.installUI(c); ToolTipManager.sharedInstance().registerComponent(displayer); 110 layoutModel = createLayoutModel(); 111 mouseListener = createMouseListener(); 112 componentListener = createComponentListener(); 113 modelListener = createModelListener(); 114 propertyChangeListener = createPropertyChangeListener(); 115 selectionListener = createSelectionListener(); 116 hierarchyListener = createHierarchyListener(); 117 install(); 118 installListeners(); 119 displayer.setFont(createFont()); 120 } 121 122 127 public final void uninstallUI(JComponent c) { 128 assert c == displayer; 129 ToolTipManager.sharedInstance().unregisterComponent(displayer); 130 super.uninstallUI(c); 131 uninstallListeners(); 134 uninstall(); 135 layoutModel = null; 136 mouseListener = null; 137 selectionModel = null; 138 componentListener = null; 139 selectionListener = null; 140 } 141 142 148 protected void install() { 149 } 151 152 158 protected void uninstall() { 159 } 161 162 167 protected final void installListeners() { 168 displayer.addHierarchyListener (hierarchyListener); 169 displayer.addPropertyChangeListener(propertyChangeListener); 170 if (componentListener != null) { 171 displayer.addComponentListener(componentListener); 172 } 173 displayer.getModel().addComplexListDataListener(modelListener); 174 displayer.getModel().addChangeListener(modelListener); 175 if (mouseListener != null) { 176 displayer.addMouseListener(mouseListener); 177 if (mouseListener instanceof MouseMotionListener) { 178 displayer.addMouseMotionListener( 179 (MouseMotionListener) mouseListener); 180 } 181 if (mouseListener instanceof MouseWheelListener) { 182 displayer.addMouseWheelListener((MouseWheelListener) mouseListener); 183 } 184 } 185 selectionModel.addChangeListener(selectionListener); 186 } 187 188 193 protected final void uninstallListeners() { 194 if (mouseListener instanceof MouseMotionListener) { 195 displayer.removeMouseMotionListener( 196 (MouseMotionListener) mouseListener); 197 } 198 if (mouseListener instanceof MouseWheelListener) { 199 displayer.removeMouseWheelListener( 200 (MouseWheelListener) mouseListener); 201 } 202 if (mouseListener != null) { 203 displayer.removeMouseListener(mouseListener); 204 } 205 if (componentListener != null) { 206 displayer.removeComponentListener(componentListener); 207 } 208 displayer.getModel().removeComplexListDataListener(modelListener); 209 displayer.getModel().removeChangeListener(modelListener); 210 displayer.removePropertyChangeListener(propertyChangeListener); 211 displayer.removeHierarchyListener(hierarchyListener); 212 selectionModel.removeChangeListener(selectionListener); 213 mouseListener = null; 214 componentListener = null; 215 propertyChangeListener = null; 216 selectionListener = null; 217 modelListener = null; 218 hierarchyListener = null; 219 } 220 221 protected HierarchyListener createHierarchyListener() { 222 return new DisplayerHierarchyListener(); 223 } 224 225 229 protected abstract TabLayoutModel createLayoutModel(); 231 240 protected abstract MouseListener createMouseListener(); 241 242 248 protected abstract ChangeListener createSelectionListener(); 249 250 protected Font createFont() { 251 return UIManager.getFont("controlFont"); } 253 254 258 protected ModelListener createModelListener() { 259 return new ModelListener(); 260 } 261 262 266 protected ComponentListener createComponentListener() { 267 return null; 268 } 269 270 274 protected PropertyChangeListener createPropertyChangeListener() { 275 return new DisplayerPropertyChangeListener(); 276 } 277 278 279 protected SingleSelectionModel createSelectionModel() { 280 return new DefaultTabSelectionModel(displayer.getModel()); 281 } 282 283 292 public int dropIndexOfPoint(Point p) { 293 Point p2 = toDropPoint(p); 294 int max = displayer.getModel().size(); 295 for (int i=0; i < max; i++) { 296 Rectangle r = getTabRect (i, null); 297 if (r.contains(p2)) { 298 return i; 299 } 300 } 301 return -1; 302 } 303 304 308 protected void modelChanged() { 309 displayer.repaint(); 310 } 311 312 private Point scratchPoint = new Point(); 313 319 protected Point toDropPoint (Point location) { 320 if (displayer.getWidth() > displayer.getHeight()) { 323 scratchPoint.setLocation(location.x, (displayer.getHeight() / 2)); 325 } else { 326 scratchPoint.setLocation (displayer.getWidth() / 2, location.y); 328 } 329 return scratchPoint; 330 } 331 332 333 public void unregisterShortcuts(JComponent comp) { 334 } 336 337 338 public void registerShortcuts(JComponent comp) { 339 } 341 342 346 protected class DisplayerPropertyChangeListener 347 implements PropertyChangeListener { 348 public void propertyChange(PropertyChangeEvent e) { 349 if (displayer.isShowing() 350 && TabDisplayer.PROP_ACTIVE.equals(e.getPropertyName())) { 351 activationChanged(); 352 } 353 } 354 355 358 protected void activationChanged() { 359 int i = selectionModel.getSelectedIndex(); 360 if (i != -1) { 361 Rectangle r = new Rectangle(); 362 getTabRect(i, r); 363 if (r.width != 0 && r.height != 0) { 364 displayer.repaint(r.x, r.y, r.width, r.height); 365 } 366 } 367 } 368 } 369 370 374 protected class DisplayerHierarchyListener implements HierarchyListener { 375 public DisplayerHierarchyListener() { 376 377 } 378 379 public void hierarchyChanged(HierarchyEvent e) { 380 if (e.getChanged() == displayer && (e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) { 381 if (displayer.isShowing()) { 382 ToolTipManager.sharedInstance().registerComponent(displayer); 383 } else { 384 ToolTipManager.sharedInstance().unregisterComponent(displayer); 385 } 386 } 387 } 388 } 389 390 395 protected class ModelListener implements ComplexListDataListener, 396 ChangeListener { 397 398 private boolean checkVisible = false; 399 402 public void contentsChanged(ListDataEvent e) { 403 } 405 406 409 public void indicesAdded(ComplexListDataEvent e) { 410 } 412 413 416 public void indicesChanged(ComplexListDataEvent e) { 417 } 419 420 423 public void indicesRemoved(ComplexListDataEvent e) { 424 } 426 427 430 public void intervalAdded(ListDataEvent e) { 431 } 433 434 437 public void intervalRemoved(ListDataEvent e) { 438 } 440 441 447 public final void stateChanged(ChangeEvent e) { 448 modelChanged(); 449 } 450 } 451 452 } 453 | Popular Tags |