1 19 20 package org.netbeans.modules.xml.wsdl.ui.view.grapheditor; 21 22 import java.awt.Dimension ; 23 import java.awt.Image ; 24 import java.awt.Point ; 25 import java.awt.Rectangle ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.util.EventListener ; 29 import java.util.EventObject ; 30 import javax.swing.AbstractAction ; 31 import javax.swing.Action ; 32 import javax.swing.DefaultComboBoxModel ; 33 import javax.swing.ImageIcon ; 34 import javax.swing.JComboBox ; 35 import javax.swing.JComponent ; 36 import javax.swing.JScrollPane ; 37 import javax.swing.JToolBar ; 38 import javax.swing.JViewport ; 39 import javax.swing.SwingUtilities ; 40 import javax.swing.event.EventListenerList ; 41 import org.netbeans.api.visual.widget.Scene; 42 import org.openide.util.NbBundle; 43 import org.openide.util.Utilities; 44 45 50 public class ZoomManager { 51 52 public static final int DEFAULT_ZOOM_PERCENT = 100; 53 54 public static final int MIN_ZOOM_PERCENT = 33; 55 56 public static final int MAX_ZOOM_PERCENT = 200; 57 59 private static final int ZOOM_STEP_THRESHOLD = DEFAULT_ZOOM_PERCENT; 60 61 private static final int ZOOM_STEP_SMALL = 5; 62 63 private static final int ZOOM_STEP_LARGE = 25; 64 65 private Scene scene; 66 67 private int zoomPercentage = DEFAULT_ZOOM_PERCENT; 68 69 private EventListenerList listeners; 70 71 76 public ZoomManager(Scene scene) { 77 this.scene = scene; 78 listeners = new EventListenerList (); 79 } 80 81 87 public void addZoomListener(ZoomListener listener) { 88 listeners.add(ZoomListener.class, listener); 89 } 90 91 96 public void addToolbarActions(JToolBar toolbar) { 97 toolbar.add(new FitDiagramAction(this)); 98 toolbar.add(new FitWidthAction(this)); 99 toolbar.add(new ZoomDefaultAction(this)); 100 toolbar.add(new ZoomComboBox(this)); 101 ZoomInAction inAction = new ZoomInAction(this); 102 addZoomListener(inAction); 103 toolbar.add(inAction); 104 ZoomOutAction outAction = new ZoomOutAction(this); 105 addZoomListener(outAction); 106 toolbar.add(outAction); 107 } 108 109 116 public static int calculateZoomInValue(int percent) { 117 int newZoomValue; 118 if (percent >= ZOOM_STEP_THRESHOLD) { 119 newZoomValue = ((percent + ZOOM_STEP_LARGE) / ZOOM_STEP_LARGE) 120 * ZOOM_STEP_LARGE; 121 } else { 122 newZoomValue = ((percent + ZOOM_STEP_SMALL) / ZOOM_STEP_SMALL) 123 * ZOOM_STEP_SMALL; 124 } 125 return newZoomValue; 126 } 127 128 135 public static int calculateZoomOutValue(int percent) { 136 int newZoomValue; 137 if (percent > ZOOM_STEP_THRESHOLD) { 138 newZoomValue = ((percent - 1) / ZOOM_STEP_LARGE) * ZOOM_STEP_LARGE; 139 } else { 140 newZoomValue = ((percent - 1) / ZOOM_STEP_SMALL) * ZOOM_STEP_SMALL; 141 } 142 return newZoomValue; 143 } 144 145 150 private void fireZoomEvent(int percent) { 151 Object [] list = listeners.getListenerList(); 152 ZoomEvent event = null; 153 for (int ii = list.length - 2; ii >= 0; ii -= 2) { 154 if (list[ii] == ZoomListener.class) { 155 if (event == null) { 156 event = new ZoomEvent(this, percent); 157 } 158 ((ZoomListener) list[ii + 1]).zoomChanged(event); 159 } 160 } 161 } 162 163 168 public Scene getScene() { 169 return scene; 170 } 171 172 179 public int getZoom() { 180 return zoomPercentage; 181 } 182 183 189 public void removeZoomListener(ZoomListener listener) { 190 listeners.remove(ZoomListener.class, listener); 191 } 192 193 203 public void setZoom(int percent) { 204 JScrollPane pane = (JScrollPane ) SwingUtilities.getAncestorOfClass( 205 JScrollPane .class, scene.getView()); 206 assert pane != null : "Scene view component not in a JScrollPane?!?"; 207 if (pane == null) { 208 return; 209 } 210 JViewport viewport = pane.getViewport(); 211 Rectangle visRect = viewport.getViewRect(); 212 Point center = new Point (visRect.x + visRect.width / 2, 213 visRect.y + visRect.height / 2); 214 setZoom(percent, center); 215 } 216 217 228 public void setZoom(int percent, Point center) { 229 if (percent < MIN_ZOOM_PERCENT) { 230 percent = MIN_ZOOM_PERCENT; 231 } else if (percent > MAX_ZOOM_PERCENT) { 232 percent = MAX_ZOOM_PERCENT; 233 } 234 235 Point sceneCenter = scene.convertViewToScene(center); 237 zoomPercentage = percent; 238 double factor = ((double) percent) / 100.0d; 242 scene.setZoomFactor(factor); 243 scene.validate(); 246 scene.repaint(); 247 248 Point newViewCenter = scene.convertSceneToView(sceneCenter); 250 JComponent view = scene.getView(); 251 Rectangle visRect = view.getVisibleRect(); 252 visRect.x = newViewCenter.x - (center.x - visRect.x); 253 visRect.y = newViewCenter.y - (center.y - visRect.y); 254 Dimension viewSize = view.getSize(); 255 if (visRect.x + visRect.width > viewSize.width) { 256 visRect.x = viewSize.width - visRect.width; 257 } 258 if (visRect.y + visRect.height > viewSize.height) { 259 visRect.y = viewSize.height - visRect.height; 260 } 261 if (visRect.x < 0) { 262 visRect.x = 0; 263 } 264 if (visRect.y < 0) { 265 visRect.y = 0; 266 } 267 view.scrollRectToVisible(visRect); 268 view.revalidate(); 269 view.repaint(); 270 271 fireZoomEvent(percent); 273 } 274 275 278 private static class ZoomComboBox extends JComboBox { 279 280 private ZoomManager manager; 281 282 287 public ZoomComboBox(ZoomManager manager) { 288 super(new Model ()); 289 this.manager = manager; 290 setPrototypeDisplayValue(new Integer (10000)); 295 setMaximumSize(getPreferredSize()); 296 setEditable(true); 297 Listener l = new Listener (manager); 298 addActionListener(l); 300 manager.addZoomListener(l); 301 } 302 303 306 private static class Model extends DefaultComboBoxModel { 307 308 311 public Model() { 312 addElement(new Value(33)); 313 addElement(new Value(50)); 314 addElement(new Value(75)); 315 Value def = new Value(DEFAULT_ZOOM_PERCENT); 317 addElement(def); 318 addElement(new Value(150)); 319 addElement(new Value(200)); 320 setSelectedItem(def); 321 } 322 } 323 324 327 private static class Value { 328 329 private int value; 330 331 private String str; 332 333 338 public Value(int value) { 339 this.value = value; 340 str = value + "%"; 341 } 342 343 public boolean equals(Object o) { 344 if (o instanceof Value) { 345 return value == ((Value) o).getValue(); 346 } 347 return false; 348 } 349 350 public int hashCode() { 351 return value; 352 } 353 354 359 public int getValue() { 360 return value; 361 } 362 363 public String toString() { 364 return str; 365 } 366 } 367 368 371 private class Listener implements ActionListener , ZoomListener { 372 373 private ZoomManager manager; 374 375 380 public Listener(ZoomManager manager) { 381 this.manager = manager; 382 } 383 384 public void actionPerformed(ActionEvent event) { 385 Object src = event.getSource(); 386 String cmd = event.getActionCommand(); 387 if (src == ZoomComboBox.this && 388 cmd.equals(ZoomComboBox.this.getActionCommand())) { 389 Object item = ZoomComboBox.this.getSelectedItem(); 392 Value value = null; 393 if (item instanceof String ) { 394 String str = (String ) item; 395 if (str.endsWith("%")) { 396 str = str.substring(0, str.length() - 1); 397 } 398 try { 399 int i = Integer.parseInt(str); 400 value = new Value(i); 401 } catch (NumberFormatException nfe) { 402 } 404 } else if (item instanceof Value) { 405 value = (Value) item; 406 } 407 if (value == null) { 408 value = new Value(ZoomComboBox.this.manager.getZoom()); 409 } 410 manager.setZoom(value.getValue()); 411 } 412 } 413 414 public void zoomChanged(ZoomEvent event) { 415 ZoomComboBox.this.removeActionListener(this); 417 ZoomComboBox.this.setSelectedItem(new Value(event.getPercent())); 418 ZoomComboBox.this.addActionListener(this); 419 } 420 } 421 } 422 423 426 private static class ZoomEvent extends EventObject { 427 428 private int percent; 429 430 436 public ZoomEvent(Object src, int percent) { 437 super(src); 438 this.percent = percent; 439 } 440 441 447 public int getPercent() { 448 return percent; 449 } 450 } 451 452 455 private static interface ZoomListener extends EventListener { 456 457 462 void zoomChanged(ZoomEvent event); 463 } 464 465 470 private static class FitDiagramAction extends AbstractAction { 471 472 private ZoomManager manager; 473 474 479 public FitDiagramAction(ZoomManager manager) { 480 this.manager = manager; 481 String path = NbBundle.getMessage(FitDiagramAction.class, 482 "IMG_FitDiagramAction"); 483 Image img = Utilities.loadImage(path); 484 if (img != null) { 485 putValue(Action.SMALL_ICON, new ImageIcon (img)); 486 } 487 String desc = NbBundle.getMessage(FitDiagramAction.class, 488 "LBL_FitDiagramAction"); 489 putValue(Action.NAME, desc); putValue(Action.SHORT_DESCRIPTION, desc); 491 } 492 493 public void actionPerformed(ActionEvent e) { 494 Scene scene = manager.getScene(); 495 JScrollPane pane = (JScrollPane ) SwingUtilities.getAncestorOfClass( 496 JScrollPane .class, scene.getView()); 497 if (pane == null) { 498 return; 500 } 501 JViewport viewport = pane.getViewport(); 502 Rectangle visRect = viewport.getViewRect(); 503 Rectangle compRect = scene.getPreferredBounds(); 504 int zoomX = visRect.width * 100 / compRect.width; 505 int zoomY = visRect.height * 100 / compRect.height; 506 int zoom = Math.min(zoomX, zoomY); 507 manager.setZoom(zoom); 508 } 509 } 510 511 516 private static class FitWidthAction extends AbstractAction { 517 518 private ZoomManager manager; 519 520 525 public FitWidthAction(ZoomManager manager) { 526 this.manager = manager; 527 String path = NbBundle.getMessage(FitWidthAction.class, 528 "IMG_FitWidthAction"); 529 Image img = Utilities.loadImage(path); 530 if (img != null) { 531 putValue(Action.SMALL_ICON, new ImageIcon (img)); 532 } 533 String desc = NbBundle.getMessage(FitWidthAction.class, 534 "LBL_FitWidthAction"); 535 putValue(Action.NAME, desc); putValue(Action.SHORT_DESCRIPTION, desc); 537 } 538 539 public void actionPerformed(ActionEvent e) { 540 Scene scene = manager.getScene(); 541 JScrollPane pane = (JScrollPane ) SwingUtilities.getAncestorOfClass( 542 JScrollPane .class, scene.getView()); 543 if (pane == null) { 544 return; 546 } 547 JViewport viewport = pane.getViewport(); 548 Rectangle visRect = viewport.getViewRect(); 549 Rectangle compRect = scene.getPreferredBounds(); 550 int zoom = visRect.width * 100 / compRect.width; 551 manager.setZoom(zoom); 552 } 553 } 554 555 559 private static class ZoomDefaultAction extends AbstractAction { 560 561 private ZoomManager manager; 562 563 568 public ZoomDefaultAction(ZoomManager manager) { 569 this.manager = manager; 570 String path = NbBundle.getMessage(ZoomDefaultAction.class, 571 "IMG_ZoomDefaultAction"); 572 Image img = Utilities.loadImage(path); 573 if (img != null) { 574 putValue(Action.SMALL_ICON, new ImageIcon (img)); 575 } 576 String desc = NbBundle.getMessage(ZoomDefaultAction.class, 577 "LBL_ZoomDefaultAction"); 578 putValue(Action.NAME, desc); putValue(Action.SHORT_DESCRIPTION, desc); 580 } 581 582 public void actionPerformed(ActionEvent e) { 583 manager.setZoom(ZoomManager.DEFAULT_ZOOM_PERCENT); 584 } 585 } 586 587 591 private static class ZoomInAction extends AbstractAction implements ZoomListener { 592 593 private ZoomManager manager; 594 595 600 public ZoomInAction(ZoomManager manager) { 601 this.manager = manager; 602 String path = NbBundle.getMessage(ZoomInAction.class, 603 "IMG_ZoomInAction"); 604 Image img = Utilities.loadImage(path); 605 if (img != null) { 606 putValue(Action.SMALL_ICON, new ImageIcon (img)); 607 } 608 String desc = NbBundle.getMessage(ZoomInAction.class, 609 "LBL_ZoomInAction"); 610 putValue(Action.NAME, desc); putValue(Action.SHORT_DESCRIPTION, desc); 612 } 613 614 public void actionPerformed(ActionEvent e) { 615 int percent = manager.getZoom(); 616 percent = ZoomManager.calculateZoomInValue(percent); 617 manager.setZoom(percent); 618 } 619 620 public void zoomChanged(ZoomEvent event) { 621 boolean enable = event.getPercent() < MAX_ZOOM_PERCENT; 622 setEnabled(enable); 623 } 624 } 625 626 630 private static class ZoomOutAction extends AbstractAction implements ZoomListener { 631 632 private ZoomManager manager; 633 634 639 public ZoomOutAction(ZoomManager manager) { 640 this.manager = manager; 641 String path = NbBundle.getMessage(ZoomOutAction.class, 642 "IMG_ZoomOutAction"); 643 Image img = Utilities.loadImage(path); 644 if (img != null) { 645 putValue(Action.SMALL_ICON, new ImageIcon (img)); 646 } 647 String desc = NbBundle.getMessage(ZoomOutAction.class, 648 "LBL_ZoomOutAction"); 649 putValue(Action.NAME, desc); putValue(Action.SHORT_DESCRIPTION, desc); 651 } 652 653 public void actionPerformed(ActionEvent e) { 654 int percent = manager.getZoom(); 655 percent = ZoomManager.calculateZoomOutValue(percent); 656 manager.setZoom(percent); 657 } 658 659 public void zoomChanged(ZoomEvent event) { 660 boolean enable = event.getPercent() > MIN_ZOOM_PERCENT; 661 setEnabled(enable); 662 } 663 } 664 } 665 | Popular Tags |