001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.design;
021:
022: import java.awt.Dimension;
023: import java.awt.Insets;
024: import java.awt.Point;
025: import java.awt.Rectangle;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.KeyEvent;
029: import javax.swing.AbstractAction;
030: import javax.swing.Action;
031: import javax.swing.ActionMap;
032: import javax.swing.Icon;
033: import javax.swing.ImageIcon;
034: import javax.swing.InputMap;
035: import javax.swing.JButton;
036: import javax.swing.JComboBox;
037: import javax.swing.JComponent;
038: import javax.swing.JScrollPane;
039: import javax.swing.JTextField;
040: import javax.swing.JViewport;
041: import javax.swing.KeyStroke;
042: import org.netbeans.modules.bpel.design.geometry.FDimension;
043: import org.netbeans.modules.bpel.design.geometry.FPoint;
044: import org.openide.util.NbBundle;
045:
046: /**
047: *
048: * @author anjeleevich
049: */
050: public class ZoomManager implements ActionListener {
051:
052: private int zoomValue = 100;
053:
054: private JButton fitDiagramButton;
055: private JButton fitWidthButton;
056: private JButton oneToOneButton;
057:
058: private JComboBox zoomChooser;
059:
060: private JButton zoomOutButton;
061: private JButton zoomInButton;
062:
063: private JComponent[] components;
064:
065: private DesignView designView;
066:
067: private Action zoomInAction;
068: private Action zoomOutAction;
069:
070: public ZoomManager(DesignView designView) {
071: this .designView = designView;
072:
073: fitDiagramButton = createButton(FIT_DIAGRAM_ICON,
074: "LBL_ZoomPanel_FitDiagram"); // NOI18N
075: fitWidthButton = createButton(FIT_WIDTH_ICON,
076: "LBL_ZoomPanel_FitWidth"); // NOI18N
077: oneToOneButton = createButton(ONE_TO_ONE_ICON,
078: "LBL_ZoomPanel_OneToOne"); // NOI18N
079:
080: zoomOutButton = createButton(ZOOM_OUT_ICON,
081: "LBL_ZoomPanel_ZoomOut"); // NOI18N
082: zoomInButton = createButton(ZOOM_IN_ICON,
083: "LBL_ZoomPanel_ZoomIn"); // NOI18N
084:
085: zoomChooser = new JComboBox(ZOOM_PRESETS);
086: zoomChooser.setSelectedItem("100%"); // NOI18N
087: zoomChooser.setEditable(true);
088: zoomChooser.addActionListener(this );
089: zoomChooser
090: .setToolTipText(getMessage("LBL_ZoomPanel_ZoomValue")); // NOI18N
091:
092: ((JTextField) zoomChooser.getEditor().getEditorComponent())
093: .setColumns(4);
094:
095: components = new JComponent[] { fitDiagramButton,
096: fitWidthButton, oneToOneButton, zoomChooser,
097: zoomOutButton, zoomInButton };
098:
099: zoomInAction = new AbstractAction() {
100: public void actionPerformed(ActionEvent e) {
101: zoomIn();
102: }
103: };
104:
105: zoomOutAction = new AbstractAction() {
106: public void actionPerformed(ActionEvent e) {
107: zoomOut();
108: }
109: };
110:
111: registerZoomInZoomOutActions(zoomChooser);
112: registerZoomInZoomOutActions(designView);
113: }
114:
115: public int getComponentCount() {
116: return components.length;
117: }
118:
119: public JComponent getComponent(int i) {
120: return components[i];
121: }
122:
123: public DesignView getDesignView() {
124: return designView;
125: }
126:
127: public void actionPerformed(ActionEvent e) {
128: Object eventSource = e.getSource();
129:
130: if (eventSource == fitWidthButton) {
131: fitWidth();
132: } else if (eventSource == fitDiagramButton) {
133: fitDiagram();
134: } else if (eventSource == oneToOneButton) {
135: oneToOne();
136: } else if (eventSource == zoomInButton) {
137: zoomIn();
138: } else if (eventSource == zoomOutButton) {
139: zoomOut();
140: } else if (eventSource == zoomChooser) {
141: String s = zoomChooser.getSelectedItem().toString().trim();
142: if (s.endsWith("%")) { // NOI18N
143: s = s.substring(0, s.length() - 1);
144: }
145:
146: int newZoomPercents = zoomValue;
147:
148: try {
149: newZoomPercents = Integer.parseInt(s);
150: } catch (NumberFormatException exception) {
151: }
152:
153: setZoom(newZoomPercents);
154: }
155: }
156:
157: private void registerZoomInZoomOutActions(JComponent component) {
158: InputMap im1 = component
159: .getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
160: InputMap im2 = component.getInputMap(JComponent.WHEN_FOCUSED);
161:
162: im1.put(ZOOM_IN_KEY_STROKE, ZOOM_IN_KEY);
163: im2.put(ZOOM_IN_KEY_STROKE, ZOOM_IN_KEY);
164:
165: im1.put(ZOOM_OUT_KEY_STROKE, ZOOM_OUT_KEY);
166: im2.put(ZOOM_OUT_KEY_STROKE, ZOOM_OUT_KEY);
167:
168: ActionMap am = component.getActionMap();
169:
170: am.put(ZOOM_IN_KEY, zoomInAction);
171: am.put(ZOOM_OUT_KEY, zoomOutAction);
172: }
173:
174: private Point getDefaultZoomPoint() {
175: Rectangle rect = getDesignView().getVisibleRect();
176: return new Point(rect.x + rect.width / 2, rect.y + rect.height
177: / 2);
178: }
179:
180: public void fitWidth() {
181: double zc = DiagramFontUtil.getZoomCorrection();
182:
183: int screen_width = designView.getWidth();
184: screen_width -= DiagramViewLayout.MARGIN_LEFT * 3;
185: screen_width -= DiagramViewLayout.MARGIN_RIGHT * 3;
186:
187: float content_width = getDesignView().getProcessView()
188: .getContentSize().width
189: + getDesignView().getConsumersView().getContentSize().width
190: + getDesignView().getProvidersView().getContentSize().width;
191:
192: double newZoom = screen_width * 100.0 / content_width / zc;
193:
194: if (newZoom != newZoom) {
195: newZoom = 100;
196: } else if (newZoom < MIN_ZOOM_VALUE) {
197: newZoom = MIN_ZOOM_VALUE;
198: } else if (newZoom > MAX_ZOOM_VALUE) {
199: newZoom = MAX_ZOOM_VALUE;
200: }
201:
202: setZoom((int) newZoom);
203: }
204:
205: public void fitDiagram() {
206: double zc = DiagramFontUtil.getZoomCorrection();
207:
208: JScrollPane scrollPane = (JScrollPane) getDesignView()
209: .getProcessView().getParent().getParent();
210:
211: Dimension pSize = scrollPane.getSize();
212: Insets insets = scrollPane.getInsets();
213:
214: FDimension dSize = getDesignView().getDiagramSize();
215:
216: int width = pSize.width - insets.left - insets.right;
217: width -= scrollPane.getVerticalScrollBar().getWidth();
218: width -= DiagramViewLayout.MARGIN_LEFT;
219: width -= DiagramViewLayout.MARGIN_RIGHT;
220:
221: int height = pSize.height - insets.top - insets.bottom;
222: height -= scrollPane.getHorizontalScrollBar().getHeight();
223: height -= DiagramViewLayout.MARGIN_BOTTOM;
224: height -= DiagramViewLayout.MARGIN_TOP;
225:
226: double zoomX = width * 100.0 / dSize.width / zc;
227: double zoomY = height * 100.0 / dSize.height / zc;
228:
229: double newZoom = Math.min(zoomX, zoomY);
230:
231: if (newZoom != newZoom) {
232: newZoom = 100;
233: } else if (newZoom < MIN_ZOOM_VALUE) {
234: newZoom = MIN_ZOOM_VALUE;
235: } else if (newZoom > MAX_ZOOM_VALUE) {
236: newZoom = MAX_ZOOM_VALUE;
237: }
238:
239: setZoom((int) newZoom);
240: }
241:
242: public void oneToOne() {
243: setZoom(100);
244: }
245:
246: public void zoomIn() {
247: Point zoomPoint = getDefaultZoomPoint();
248: zoomIn(zoomPoint.x, zoomPoint.y);
249: }
250:
251: public void zoomIn(int zoomPointX, int zoomPointY) {
252: int newZoomValue;
253:
254: if (zoomValue >= 100) {
255: newZoomValue = ((zoomValue + ZOOM_STEP_G100) / ZOOM_STEP_G100)
256: * ZOOM_STEP_G100;
257: } else {
258: newZoomValue = ((zoomValue + ZOOM_STEP_L100) / ZOOM_STEP_L100)
259: * ZOOM_STEP_L100;
260: }
261:
262: setZoom(newZoomValue, zoomPointX, zoomPointY);
263: }
264:
265: public void zoomOut() {
266: Point zoomPoint = getDefaultZoomPoint();
267: zoomOut(zoomPoint.x, zoomPoint.y);
268: }
269:
270: public void zoomOut(int zoomPointX, int zoomPointY) {
271: int newZoomValue;
272:
273: if (zoomValue > 100) {
274: newZoomValue = ((zoomValue - 1) / ZOOM_STEP_G100)
275: * ZOOM_STEP_G100;
276: } else {
277: newZoomValue = ((zoomValue - 1) / ZOOM_STEP_L100)
278: * ZOOM_STEP_L100;
279: }
280:
281: setZoom(newZoomValue, zoomPointX, zoomPointY);
282: }
283:
284: public void setZoom(int newZoomValue) {
285: Point zoomPoint = getDefaultZoomPoint();
286: setZoom(newZoomValue, zoomPoint.x, zoomPoint.y);
287: }
288:
289: public void setZoom(int newZoomValue, int zoomPointX, int zoomPointY) {
290: DesignView desingView = getDesignView();
291: // JViewport viewport = (JViewport) desingView.getParent();
292: // JScrollPane scrollPane = (JScrollPane) viewport.getParent();
293:
294: if (newZoomValue < MIN_ZOOM_VALUE) {
295: newZoomValue = MIN_ZOOM_VALUE;
296: } else if (newZoomValue > MAX_ZOOM_VALUE) {
297: newZoomValue = MAX_ZOOM_VALUE;
298: }
299:
300: if (newZoomValue != zoomValue) {
301: /* FPoint diagramZoomPoint = desingView.convertScreenToDiagram(
302: new Point(zoomPointX, zoomPointY));
303:
304: Rectangle rect = getDesignView().getVisibleRect();
305:
306: int dx = zoomPointX - rect.x;
307: int dy = zoomPointY - rect.y;*/
308:
309: zoomValue = newZoomValue;
310:
311: // desingView.getDecorationManager().repositionComponentsRecursive();
312:
313: // viewport.setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
314:
315: // desingView.invalidate();
316: // desingView.validate();
317:
318: // viewport.setScrollMode(JViewport.BLIT_SCROLL_MODE);
319:
320: //FIXME desingView.getNameEditor().updateBounds();
321: /*
322: Point newScreenZoomPoint = desingView.convertDiagramToScreen(
323: diagramZoomPoint);
324:
325: rect = desingView.getVisibleRect();
326: rect.x = newScreenZoomPoint.x - dx;
327: rect.y = newScreenZoomPoint.y - dy;
328:
329:
330: int designViewWidth = desingView.getWidth();
331: int designViewHeight = designView.getHeight();
332:
333: if (rect.x + rect.width > designViewWidth) {
334: rect.x = designViewWidth - rect.width;
335: }
336:
337: if (rect.y + rect.height > designViewHeight) {
338: rect.y = designViewHeight - rect.height;
339: }
340:
341: if (rect.x < 0) {
342: rect.x = 0;
343: }
344:
345: if (rect.y < 0) {
346: rect.y = 0;
347: }
348:
349: desingView.scrollRectToVisible(rect);*/
350: desingView.getProcessView().invalidate();
351: desingView.getConsumersView().invalidate();
352: desingView.getProvidersView().invalidate();
353:
354: desingView.validate();
355: desingView.repaint();
356:
357: designView.getRightStripe().repaint();
358: }
359:
360: updateComponentsStates();
361: }
362:
363: private void updateComponentsStates() {
364: zoomChooser.removeActionListener(this );
365: zoomChooser.setSelectedItem(getZoomString());
366: zoomChooser.addActionListener(this );
367: zoomInButton.setEnabled(!isMaxZoom());
368: zoomOutButton.setEnabled(!isMinZoom());
369: }
370:
371: public void setEnabled(boolean value) {
372: fitWidthButton.setEnabled(value);
373: fitDiagramButton.setEnabled(value);
374: oneToOneButton.setEnabled(value);
375:
376: zoomChooser.setEnabled(value);
377:
378: zoomInButton.setEnabled(value);
379: zoomOutButton.setEnabled(value);
380:
381: zoomInAction.setEnabled(value);
382: zoomOutAction.setEnabled(value);
383:
384: if (value) {
385: updateComponentsStates();
386: }
387: }
388:
389: public int getZoom() {
390: return zoomValue;
391: }
392:
393: public String getZoomString() {
394: return zoomValue + "%";
395: } // NOI18N
396:
397: public double getScale() {
398: return 0.01 * zoomValue;
399: }
400:
401: public boolean isMinZoom() {
402: return zoomValue == MIN_ZOOM_VALUE;
403: }
404:
405: public boolean isMaxZoom() {
406: return zoomValue == MAX_ZOOM_VALUE;
407: }
408:
409: private JButton createButton(Icon icon, String toolTipTextKey) {
410: JButton button = new JButton(icon);
411: button.setToolTipText(getMessage(toolTipTextKey));
412: button.addActionListener(this );
413: return button;
414: }
415:
416: private static String getMessage(String key) {
417: return NbBundle.getBundle(ZoomManager.class).getString(key);
418: }
419:
420: private static Icon loadIcon(String name) {
421: return new ImageIcon(ZoomManager.class.getResource("resources/"
422: + name)); // NOI18N
423: }
424:
425: public static final int MIN_ZOOM_VALUE = 33;
426: public static final int MAX_ZOOM_VALUE = 200;
427:
428: private static final String[] ZOOM_PRESETS = { "33%", "50%", "75%",
429: "100%", "150%", "200%" }; // NOI18N
430:
431: private static final Icon FIT_DIAGRAM_ICON = loadIcon("fit_diagram.png"); // NOI18N
432: private static final Icon FIT_WIDTH_ICON = loadIcon("fit_width.png"); // NOI18N
433: private static final Icon ONE_TO_ONE_ICON = loadIcon("normal_size.png"); // NOI18N
434:
435: private static final Icon ZOOM_IN_ICON = loadIcon("zoom_in.png"); // NOI18N
436: private static final Icon ZOOM_OUT_ICON = loadIcon("zoom_out.png"); // NOI18N
437:
438: private static final int ZOOM_STEP_L100 = 5;
439: private static final int ZOOM_STEP_G100 = 25;
440:
441: private static final String ZOOM_IN_KEY = "diagramZoomInAction";
442: private static final String ZOOM_OUT_KEY = "diagramZoomOutAction";
443:
444: private static final KeyStroke ZOOM_IN_KEY_STROKE = KeyStroke
445: .getKeyStroke(KeyEvent.VK_EQUALS, KeyEvent.CTRL_DOWN_MASK);
446:
447: private static final KeyStroke ZOOM_OUT_KEY_STROKE = KeyStroke
448: .getKeyStroke(KeyEvent.VK_MINUS, KeyEvent.CTRL_DOWN_MASK);
449: }
|