001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.soa.mappercore;
041:
042: import java.awt.Insets;
043: import java.awt.Rectangle;
044: import java.awt.event.ActionEvent;
045: import java.awt.event.KeyEvent;
046: import java.util.List;
047: import javax.swing.KeyStroke;
048: import javax.swing.tree.TreePath;
049: import org.netbeans.modules.soa.mappercore.model.Graph;
050: import org.netbeans.modules.soa.mappercore.model.Link;
051: import org.netbeans.modules.soa.mappercore.model.TargetPin;
052: import org.netbeans.modules.soa.mappercore.model.Vertex;
053: import org.netbeans.modules.soa.mappercore.model.VertexItem;
054:
055: /**
056: *
057: * @author alex
058: */
059: public class MoveDownCanvasAction extends MapperKeyboardAction {
060: private Canvas canvas;
061:
062: public MoveDownCanvasAction(Canvas canvas) {
063: this .canvas = canvas;
064: }
065:
066: @Override
067: public String getActionKey() {
068: return "press-move-down-action";
069: }
070:
071: @Override
072: public KeyStroke[] getShortcuts() {
073: return new KeyStroke[] {
074: KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
075: KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,
076: KeyEvent.SHIFT_DOWN_MASK),
077: KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,
078: KeyEvent.CTRL_DOWN_MASK),
079: KeyStroke.getKeyStroke(KeyEvent.VK_DOWN,
080: KeyEvent.CTRL_DOWN_MASK
081: + KeyEvent.SHIFT_DOWN_MASK) };
082: }
083:
084: public void actionPerformed(ActionEvent e) {
085: SelectionModel selectionModel = canvas.getSelectionModel();
086:
087: TreePath treePath = selectionModel.getSelectedPath();
088: if (treePath == null)
089: return;
090:
091: Mapper mapper = canvas.getMapper();
092: Graph graph = selectionModel.getSelectedGraph();
093: if (graph == null || graph.isEmpty()) {
094: MapperNode currentNode = mapper.getNode(treePath, true);
095: MapperNode nextNode = currentNode.getNextVisibleNode();
096: if (nextNode == null)
097: return;
098:
099: mapper.setSelectedNode(nextNode);
100: return;
101: }
102:
103: List<Vertex> sVertexeces = selectionModel.getSelectedVerteces();
104: List<Link> sLinks = selectionModel.getSelectedLinks();
105: VertexItem sVertexItem = selectionModel.getSelectedVertexItem();
106: // CONTROL+SHIFT
107: if (isControlPress(e) && isShiftPress(e)) {
108: if (sLinks != null && sLinks.size() > 0) {
109: Link link = sLinks.get(0);
110: List<Link> links = graph.getIngoingLinks();
111: if (!links.contains(link))
112: return;
113:
114: int index = links.indexOf(link);
115: if (index == links.size() - 1)
116: return;
117:
118: selectionModel.setSelected(treePath, links
119: .get(index + 1));
120: }
121: return;
122: }
123: // SHIFT
124: if (isShiftPress(e)) {
125: if (canvas.getScrollPane().getViewport() == null) {
126: return;
127: }
128:
129: Insets insets = canvas.getAutoscrollInsets();
130: int x = canvas.getScrollPane().getViewport().getViewRect().x;
131: Rectangle r = new Rectangle(x, 0, 1, 1);
132: r.y = canvas.getHeight()
133: - insets.bottom
134: + 16
135: + 2
136: * canvas.getScrollPane().getVerticalScrollBar()
137: .getUnitIncrement();
138: canvas.scrollRectToVisible(r);
139: }
140: //CONTROL
141: if (isControlPress(e)) {
142: // veretex is select
143: if (sVertexeces != null && sVertexeces.size() > 0) {
144: Vertex vertex = sVertexeces.get(0);
145: if (vertex.getItemCount() < 1) {
146: return;
147: }
148:
149: selectionModel.setSelected(treePath, vertex.getItem(0));
150: return;
151: }
152: // link is select
153: if (sLinks != null && sLinks.size() > 0) {
154: Link link = sLinks.get(0);
155: TargetPin targetPin = link.getTarget();
156: if (targetPin instanceof Graph)
157: return;
158:
159: VertexItem vertexItem = (VertexItem) targetPin;
160: Vertex vertex = vertexItem.getVertex();
161: for (int i = vertex.getItemIndex(vertexItem) + 1; i < vertex
162: .getItemCount(); i++) {
163: link = vertex.getItem(i).getIngoingLink();
164: if (link != null) {
165: selectionModel.setSelected(treePath, link);
166: break;
167: }
168: }
169: return;
170: }
171: // vertexItem is select
172: if (sVertexItem != null) {
173: Vertex vertex = sVertexItem.getVertex();
174: int index = vertex.getItemIndex(sVertexItem);
175: if (index >= vertex.getItemCount() - 1) {
176: selectionModel.setSelected(treePath, vertex);
177: return;
178: }
179:
180: VertexItem nextVertexItem = vertex.getItem(index + 1);
181: selectionModel.setSelected(treePath, nextVertexItem);
182: return;
183: }
184: // nothing is selected and graph have not vertex
185: MapperNode currentNode = mapper.getNode(treePath, true);
186: MapperNode nextNode = currentNode.getNextVisibleNode();
187: if (nextNode == null)
188: return;
189:
190: mapper.setSelectedNode(nextNode);
191: }
192: // NOTHING
193: if (isNothingPress(e)) {
194: TreePath currentTreePath = selectionModel.getSelectedPath();
195: if (currentTreePath != null) {
196: MapperNode currentNode = mapper.getNode(
197: currentTreePath, true);
198: MapperNode nextNode = currentNode.getNextVisibleNode();
199: if (nextNode != null) {
200: selectionModel.setSelected(nextNode.getTreePath());
201: }
202: } else if (mapper.getRoot() != null
203: && mapper.getRoot().getChildCount() > 0) {
204: mapper.setSelectedNode(mapper.getRoot().getChild(0));
205: }
206: }
207: }
208:
209: private boolean isControlPress(ActionEvent e) {
210: return (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
211: }
212:
213: private boolean isShiftPress(ActionEvent e) {
214: return (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
215: }
216:
217: private boolean isNothingPress(ActionEvent e) {
218: return e.getModifiers() == 0;
219: }
220: }
|