~fredericp/zaluum/wip-ide

« back to all changes in this revision

Viewing changes to org.zaluum.ide.editor/src/org/zaluum/ide/editor/parts/BoxAnchorsEditPolicy.java

  • Committer: Frederic Perez Ordeig
  • Date: 2009-12-17 11:22:39 UTC
  • mfrom: (200.1.22 zaluum)
  • Revision ID: frederic@zaluum.com-20091217112239-qycwncx3vzyx5ai3
merged
refactored clone
fixed instantiation port ordering

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  This file is part of Zaluum
 
3
 *   
 
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.
 
8
 *
 
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.
 
13
 *
 
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/>.
 
16
 *  
 
17
 *  Copyright   2008,2009       Frederic Perez Ordeig
 
18
 *  Copyright   2009            Matias Lizana Garcia
 
19
 */
1
20
package org.zaluum.ide.editor.parts;
2
21
 
 
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;
14
36
 
 
37
/**
 
38
 * Policy that creates feedback for the {@link MoveAnchorTool} and create the
 
39
 * commands to execute
 
40
 * 
 
41
 * @author matias
 
42
 */
15
43
public class BoxAnchorsEditPolicy extends GraphicalEditPolicy {
16
44
 
 
45
        private static int FEEDBACK_DETECTION_DISTANCE = 20;
 
46
        private Triangle sourceArrowFeedback;
 
47
        private Triangle targetArrowFeedback;
 
48
 
17
49
        public BoxAnchorsEditPolicy() {
18
 
                //TODO:
 
50
                sourceArrowFeedback = new Triangle();
19
51
        }
20
52
 
21
53
        @Override
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
 
57
                                                .getType())
 
58
                                || MoveAnchorTool.REQ_MOVE_ANCHOR_DRAG
 
59
                                                .equals(request.getType())
26
60
                                || MoveAnchorTool.REQ_MOVE_ANCHOR_END.equals(request.getType()))
27
61
                        return getHost();
28
62
                return null;
29
63
        }
30
 
        
 
64
 
31
65
        @Override
32
66
        public Command getCommand(Request request) {
33
67
                if (MoveAnchorTool.REQ_MOVE_ANCHOR_START.equals(request.getType()))
40
74
                        return getCommandReleaseAnchor(request);
41
75
                return null;
42
76
        }
43
 
        
 
77
 
44
78
        /**
45
79
         * Takes the detected anchor (if any)
46
80
         */
47
81
        private Command getCommandHoldAnchor(Request request) {
48
 
                //TODO: Port distance
 
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);
 
86
                if (p == null)
 
87
                        return null;
 
88
                else
 
89
                        return new MovePortCommand(GraphicUtils.draw2dToModel(req
 
90
                                        .getLocation()), p);
53
91
        }
54
 
        
 
92
 
55
93
        @Override
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()))
61
 
//              {
62
 
//                      AnchorLocationRequest req = ((AnchorLocationRequest)request);
63
 
//                      Port anchor = req.getAnchor();
64
 
//                      if(anchor!=null)
65
 
//                      {       
66
 
//                              arrow = new ArrowFigure(GraphicUtils.modelToDraw2d(anchor.getPosition()));
67
 
//                              arrow.setFill(true);
68
 
//                              arrow.setBackgroundColor(ColorConstants.red);
69
 
//                              addFeedback(arrow);
70
 
//                      }
71
 
//              }
 
95
                if (MoveAnchorTool.REQ_MOVE_ANCHOR_START.equals(request.getType())
 
96
                                || MoveAnchorTool.REQ_MOVE_ANCHOR_ACROSS.equals(request
 
97
                                                .getType())
 
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);
 
111
                        } else {
 
112
                                if (req.getAnchor().isIn())
 
113
                                        sourceArrowFeedback.setDirection(Triangle.WEST);
 
114
                        }
 
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);
 
120
                        }else{
 
121
                                sourceArrowFeedback.setBackgroundColor(ColorConstants.lightGreen);
 
122
                                sourceArrowFeedback.setForegroundColor(ColorConstants.darkGreen);
 
123
                        }
 
124
                        addFeedback(sourceArrowFeedback);
 
125
 
 
126
                } else if (MoveAnchorTool.REQ_MOVE_ANCHOR_END.equals(request.getType())) {
 
127
 
 
128
                }
 
129
 
72
130
        }
73
 
        
 
131
 
74
132
        @Override
75
133
        public void showTargetFeedback(Request request) {
76
 
                if(request.getType().equals(RequestConstants.REQ_SELECTION))
77
 
                {
78
 
                        //Mouse selecting...
79
 
                }else if(request.getType().equals(RequestConstants.REQ_MOVE))
80
 
                {
81
 
                        getFeedbackLayer();
82
 
//                      AnchorLocationRequest req = ((AnchorLocationRequest)request);
83
 
//                      addFeedback(new ArrowFigure(req.getLocation()));
84
 
//                      System.out.println("Hola!");
85
 
                }
 
134
 
86
135
        }
87
 
        
 
136
 
88
137
        @Override
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()))
94
 
                {
95
 
                        //removeFeedback(arrow);
 
140
                                || MoveAnchorTool.REQ_MOVE_ANCHOR_ACROSS.equals(request
 
141
                                                .getType())
 
142
                                || MoveAnchorTool.REQ_MOVE_ANCHOR_DRAG
 
143
                                                .equals(request.getType())
 
144
                                || MoveAnchorTool.REQ_MOVE_ANCHOR_END.equals(request.getType())) {
 
145
                        getFeedbackLayer().remove(sourceArrowFeedback);
96
146
                }
97
147
        }
98
148
 
99
149
        @Override
100
150
        public void eraseTargetFeedback(Request request) {
101
 
                if(request.getType().equals(RequestConstants.REQ_SELECTION))
102
 
                {
103
 
                        //Mouse selecting...
104
 
                        //((BoxFigure) getHostFigure()).setName(((Box)getHost().getModel()).getName());
 
151
                if (request.getType().equals(RequestConstants.REQ_SELECTION)) {
 
152
                        // Mouse selecting...
 
153
                        // ((BoxFigure)
 
154
                        // getHostFigure()).setName(.getName());
105
155
                }
106
156
        }
107
 
        
 
157
 
108
158
        private Command getCommandAcrossAnchor(Request request) {
109
 
                //TODO: Add feedback!
 
159
                // TODO: Add feedback!
110
160
                return getCommandHoldAnchor(request);
111
161
        }
112
162
 
116
166
        private Command getCommandMoveAnchor(Request request) {
117
167
                return null;
118
168
        }
119
 
        
 
169
 
120
170
        /**
121
171
         * Release the anchor and calls a command that modify the model (anchor
122
172
         * external position)
123
173
         */
124
174
        private Command getCommandReleaseAnchor(Request request) {
125
 
                //Port p = ((AnchorLocationRequest) request).getAnchor();
126
 
//              MoveExternalPortCommand c = new MoveExternalPortCommand(p,
127
 
//                              ((AnchorLocationRequest) request).getTargetPosition());
 
175
                // Port p = ((AnchorLocationRequest) request).getAnchor();
 
176
                // MoveExternalPortCommand c = new MoveExternalPortCommand(p,
 
177
                // ((AnchorLocationRequest) request).getTargetPosition());
128
178
                return null;
129
179
        }
130
180