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.Component;
023: import java.awt.Cursor;
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.FocusEvent;
029: import java.awt.event.FocusListener;
030: import java.awt.event.KeyEvent;
031: import java.awt.event.KeyListener;
032: import java.awt.event.MouseEvent;
033: import java.awt.event.MouseListener;
034: import java.awt.event.MouseMotionListener;
035: import java.awt.event.MouseWheelEvent;
036: import java.awt.event.MouseWheelListener;
037: import javax.swing.ButtonGroup;
038: import javax.swing.Icon;
039: import javax.swing.ImageIcon;
040: import javax.swing.JComponent;
041: import javax.swing.JPanel;
042: import javax.swing.JScrollPane;
043: import javax.swing.JToggleButton;
044: import javax.swing.JViewport;
045: import org.openide.util.NbBundle;
046:
047: /**
048: *
049: * @author anjeleevich
050: */
051: public class NavigationTools extends JPanel implements MouseListener,
052: MouseMotionListener, MouseWheelListener, KeyListener,
053: FocusListener, ActionListener {
054:
055: private DiagramView currentDiagramView;
056: private DesignView designView;
057:
058: private Mode mode = Mode.OFF;
059:
060: private int toolX;
061: private int toolY;
062:
063: private JToggleButton editingModeButton;
064: private JToggleButton navigationModeButton;
065:
066: private ButtonGroup buttonGroup;
067:
068: private JComponent[] controllers;
069:
070: public NavigationTools(DesignView designView) {
071: this .designView = designView;
072:
073: setFocusable(false);
074: setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
075: setBackground(null);
076: setOpaque(false);
077: setVisible(false);
078:
079: designView.getProcessView().addKeyListener(this );
080: designView.getConsumersView().addKeyListener(this );
081: designView.getProvidersView().addKeyListener(this );
082:
083: designView.getProcessView().addFocusListener(this );
084: designView.getConsumersView().addFocusListener(this );
085: designView.getProvidersView().addFocusListener(this );
086:
087: editingModeButton = new JToggleButton(SELECT_TOOL_ICON);
088: editingModeButton.setSelected(true);
089: editingModeButton.setFocusable(false);
090: editingModeButton
091: .setToolTipText(getMessage("LBL_NavigationTools_EditingMode")); // NOI18N
092:
093: navigationModeButton = new JToggleButton(HAND_TOOL_ICON);
094: navigationModeButton.setSelected(false);
095: navigationModeButton.setFocusable(false);
096: navigationModeButton
097: .setToolTipText(getMessage("LBL_NavigationTools_NavigationMode")); // NOI18N
098:
099: buttonGroup = new ButtonGroup();
100: buttonGroup.add(editingModeButton);
101: buttonGroup.add(navigationModeButton);
102:
103: addMouseListener(this );
104: addMouseMotionListener(this );
105: addMouseWheelListener(this );
106:
107: editingModeButton.addActionListener(this );
108: navigationModeButton.addActionListener(this );
109:
110: controllers = new JComponent[] { editingModeButton,
111: navigationModeButton };
112: }
113:
114: public int getControllersCount() {
115: return controllers.length;
116: }
117:
118: public JComponent getController(int i) {
119: return controllers[i];
120: }
121:
122: public DesignView getDesignView() {
123: return designView;
124: }
125:
126: private void turnOn(Mode newMode) {
127: this .mode = newMode;
128: setVisible(true);
129: }
130:
131: private void turnOff() {
132: this .mode = Mode.OFF;
133: setVisible(false);
134: }
135:
136: public void setEnabled(boolean v) {
137: editingModeButton.setEnabled(v);
138: navigationModeButton.setEnabled(v);
139: }
140:
141: public void keyPressed(KeyEvent e) {
142: if (e.getKeyCode() == KeyEvent.VK_SPACE && mode == Mode.OFF) {
143: turnOn(Mode.WEAK);
144: }
145: }
146:
147: public void keyReleased(KeyEvent e) {
148: if (e.getKeyCode() == KeyEvent.VK_SPACE && mode == Mode.WEAK) {
149: turnOff();
150: }
151: }
152:
153: public void focusLost(FocusEvent e) {
154: if (mode == Mode.WEAK) {
155: turnOff();
156: }
157: }
158:
159: public void mouseWheelMoved(MouseWheelEvent e) {
160: if (e.getWheelRotation() > 0) {
161: getDesignView().getZoomManager().zoomIn(e.getX(), e.getY());
162: } else {
163: getDesignView().getZoomManager()
164: .zoomOut(e.getX(), e.getY());
165: }
166:
167: Point p = getDesignView().getMousePosition(true);
168:
169: if (p != null) {
170: toolX = p.x;
171: toolY = p.y;
172: }
173: }
174:
175: public void mouseDragged(MouseEvent e) {
176: int mx = e.getX();
177: int my = e.getY();
178:
179: int dx = mx - toolX;
180: int dy = my - toolY;
181:
182: toolX = mx;
183: toolY = my;
184:
185: if (currentDiagramView != null) {
186: Rectangle rect = currentDiagramView.getVisibleRect();
187:
188: rect.x -= dx;
189: rect.y -= dy;
190:
191: currentDiagramView.scrollRectToVisible(rect);
192: }
193: }
194:
195: public void mousePressed(MouseEvent e) {
196: TriScrollPane scrollPane = (TriScrollPane) getParent();
197:
198: int mx = e.getX();
199: int my = e.getY();
200:
201: JViewport processViewport = (JViewport) (designView
202: .getProcessView().getParent());
203: JComponent consumerTopComponent = (JComponent) (designView
204: .getConsumersView().getParent().getParent().getParent());
205: JComponent providerTopComponent = (JComponent) (designView
206: .getProvidersView().getParent().getParent().getParent());
207:
208: if (contains(processViewport, mx, my)) {
209: currentDiagramView = designView.getProcessView();
210: } else if (contains(consumerTopComponent, mx, my)) {
211: currentDiagramView = designView.getConsumersView();
212: } else if (contains(providerTopComponent, mx, my)) {
213: currentDiagramView = designView.getProvidersView();
214: } else {
215: currentDiagramView = null;
216: }
217:
218: toolX = e.getX();
219: toolY = e.getY();
220: }
221:
222: public void mouseReleased(MouseEvent e) {
223: currentDiagramView = null;
224: }
225:
226: public void mouseClicked(MouseEvent e) {
227: }
228:
229: public void mouseEntered(MouseEvent e) {
230: }
231:
232: public void mouseExited(MouseEvent e) {
233: }
234:
235: public void mouseMoved(MouseEvent e) {
236: }
237:
238: public void keyTyped(KeyEvent e) {
239: }
240:
241: public void focusGained(FocusEvent e) {
242: }
243:
244: public void actionPerformed(ActionEvent e) {
245: if (navigationModeButton.isSelected()) {
246: turnOn(Mode.STRONG);
247: }
248:
249: if (editingModeButton.isSelected()) {
250: turnOff();
251: }
252: }
253:
254: private static enum Mode {
255: OFF, WEAK, STRONG
256: }
257:
258: private static String getMessage(String key) {
259: return NbBundle.getBundle(NavigationTools.class).getString(key);
260: }
261:
262: private static Icon loadIcon(String name) {
263: return new ImageIcon(NavigationTools.class
264: .getResource("resources/" + name)); // NOI18N
265: }
266:
267: private static boolean contains(Component c, int px, int py) {
268: if (!c.isVisible())
269: return false;
270: px -= c.getX();
271: py -= c.getY();
272: return 0 <= px && 0 <= py && px < c.getWidth()
273: && py < c.getHeight();
274: }
275:
276: private static Icon HAND_TOOL_ICON = loadIcon("hand_tool.png"); // NOI18N
277: private static Icon SELECT_TOOL_ICON = loadIcon("select_tool.png"); // NOI18N
278: }
|