2
* This file is part of Zaluum
4
* Zaluum is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
9
* Zaluum is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with Zaluum. If not, see <http://www.gnu.org/licenses/>.
17
* Copyright 2008,2009 Frederic Perez Ordeig
18
* Copyright 2009 Matias Lizana Garcia
1
20
package org.zaluum.ide.editor.parts;
22
import org.eclipse.draw2d.ColorConstants;
23
import org.eclipse.draw2d.Triangle;
3
24
import org.eclipse.gef.EditPart;
4
25
import org.eclipse.gef.Request;
5
26
import org.eclipse.gef.RequestConstants;
6
27
import org.eclipse.gef.commands.Command;
7
28
import org.eclipse.gef.editpolicies.GraphicalEditPolicy;
29
import org.eclipse.swt.SWT;
8
30
import org.zaluum.ide.editor.command.MovePortCommand;
9
31
import org.zaluum.ide.editor.figures.BoxFigure;
10
32
import org.zaluum.ide.editor.requests.AnchorLocationRequest;
12
34
import org.zaluum.ide.util.GraphicUtils;
13
35
import org.zaluum.model.Port;
38
* Policy that creates feedback for the {@link MoveAnchorTool} and create the
15
43
public class BoxAnchorsEditPolicy extends GraphicalEditPolicy {
45
private static int FEEDBACK_DETECTION_DISTANCE = 20;
46
private Triangle sourceArrowFeedback;
47
private Triangle targetArrowFeedback;
17
49
public BoxAnchorsEditPolicy() {
50
sourceArrowFeedback = new Triangle();
22
54
public EditPart getTargetEditPart(Request request) {
23
55
if (MoveAnchorTool.REQ_MOVE_ANCHOR_START.equals(request.getType())
24
|| MoveAnchorTool.REQ_MOVE_ANCHOR_ACROSS.equals(request.getType())
25
|| MoveAnchorTool.REQ_MOVE_ANCHOR_DRAG.equals(request.getType())
56
|| MoveAnchorTool.REQ_MOVE_ANCHOR_ACROSS.equals(request
58
|| MoveAnchorTool.REQ_MOVE_ANCHOR_DRAG
59
.equals(request.getType())
26
60
|| MoveAnchorTool.REQ_MOVE_ANCHOR_END.equals(request.getType()))
32
66
public Command getCommand(Request request) {
33
67
if (MoveAnchorTool.REQ_MOVE_ANCHOR_START.equals(request.getType()))
40
74
return getCommandReleaseAnchor(request);
45
79
* Takes the detected anchor (if any)
47
81
private Command getCommandHoldAnchor(Request request) {
82
// TODO: Port distance
49
83
AnchorLocationRequest req = ((AnchorLocationRequest) request);
50
Port p = ((BoxFigure) getHostFigure()).getNearPort(req.getLocation(), 15);
51
//((BoxFigure) getHostFigure()).setName(((Box)getHost().getModel()).getName());
52
return new MovePortCommand(GraphicUtils.draw2dToModel(req.getLocation()),p);
84
Port p = ((BoxFigure) getHostFigure()).getNearPort(req.getLocation(),
85
FEEDBACK_DETECTION_DISTANCE);
89
return new MovePortCommand(GraphicUtils.draw2dToModel(req
56
94
public void showSourceFeedback(Request request) {
57
// if (MoveAnchorTool.REQ_MOVE_ANCHOR_START.equals(request.getType())
58
// || MoveAnchorTool.REQ_MOVE_ANCHOR_ACROSS.equals(request.getType())
59
// || MoveAnchorTool.REQ_MOVE_ANCHOR_DRAG.equals(request.getType())
60
// || MoveAnchorTool.REQ_MOVE_ANCHOR_END.equals(request.getType()))
62
// AnchorLocationRequest req = ((AnchorLocationRequest)request);
63
// Port anchor = req.getAnchor();
66
// arrow = new ArrowFigure(GraphicUtils.modelToDraw2d(anchor.getPosition()));
67
// arrow.setFill(true);
68
// arrow.setBackgroundColor(ColorConstants.red);
69
// addFeedback(arrow);
95
if (MoveAnchorTool.REQ_MOVE_ANCHOR_START.equals(request.getType())
96
|| MoveAnchorTool.REQ_MOVE_ANCHOR_ACROSS.equals(request
98
|| MoveAnchorTool.REQ_MOVE_ANCHOR_DRAG
99
.equals(request.getType())) {
100
AnchorLocationRequest req = ((AnchorLocationRequest) request);
101
// TODO: Extends Triangle and create more methods
102
sourceArrowFeedback = new Triangle();
103
sourceArrowFeedback.setLocation(BoxFigure.getExternalPoint(GraphicUtils
104
.modelToDraw2d(((BoxEditPart) getHost()).getBox()
105
.getBounds()), req.getAnchor()));
106
sourceArrowFeedback.setDirection(Triangle.EAST);
107
sourceArrowFeedback.setSize(10, 10);
108
if (req.getAnchor().isAtLeft()) {
109
if (!req.getAnchor().isIn())
110
sourceArrowFeedback.setDirection(Triangle.WEST);
112
if (req.getAnchor().isIn())
113
sourceArrowFeedback.setDirection(Triangle.WEST);
115
sourceArrowFeedback.setFill(true);
116
sourceArrowFeedback.setAntialias(SWT.ON);
117
if(MoveAnchorTool.REQ_MOVE_ANCHOR_DRAG.equals(request.getType())) {
118
sourceArrowFeedback.setBackgroundColor(ColorConstants.white);
119
sourceArrowFeedback.setForegroundColor(ColorConstants.lightGray);
121
sourceArrowFeedback.setBackgroundColor(ColorConstants.lightGreen);
122
sourceArrowFeedback.setForegroundColor(ColorConstants.darkGreen);
124
addFeedback(sourceArrowFeedback);
126
} else if (MoveAnchorTool.REQ_MOVE_ANCHOR_END.equals(request.getType())) {
75
133
public void showTargetFeedback(Request request) {
76
if(request.getType().equals(RequestConstants.REQ_SELECTION))
79
}else if(request.getType().equals(RequestConstants.REQ_MOVE))
82
// AnchorLocationRequest req = ((AnchorLocationRequest)request);
83
// addFeedback(new ArrowFigure(req.getLocation()));
84
// System.out.println("Hola!");
89
138
public void eraseSourceFeedback(Request request) {
90
139
if (MoveAnchorTool.REQ_MOVE_ANCHOR_START.equals(request.getType())
91
|| MoveAnchorTool.REQ_MOVE_ANCHOR_ACROSS.equals(request.getType())
92
|| MoveAnchorTool.REQ_MOVE_ANCHOR_DRAG.equals(request.getType())
93
|| MoveAnchorTool.REQ_MOVE_ANCHOR_END.equals(request.getType()))
95
//removeFeedback(arrow);
140
|| MoveAnchorTool.REQ_MOVE_ANCHOR_ACROSS.equals(request
142
|| MoveAnchorTool.REQ_MOVE_ANCHOR_DRAG
143
.equals(request.getType())
144
|| MoveAnchorTool.REQ_MOVE_ANCHOR_END.equals(request.getType())) {
145
getFeedbackLayer().remove(sourceArrowFeedback);
100
150
public void eraseTargetFeedback(Request request) {
101
if(request.getType().equals(RequestConstants.REQ_SELECTION))
104
//((BoxFigure) getHostFigure()).setName(((Box)getHost().getModel()).getName());
151
if (request.getType().equals(RequestConstants.REQ_SELECTION)) {
152
// Mouse selecting...
154
// getHostFigure()).setName(.getName());
108
158
private Command getCommandAcrossAnchor(Request request) {
109
//TODO: Add feedback!
159
// TODO: Add feedback!
110
160
return getCommandHoldAnchor(request);