~fredericp/zaluum/wip-ide

« back to all changes in this revision

Viewing changes to org.zaluum.boxes.fsm/src/org/zaluum/boxes/fsm/editor/StateEditPart.java

  • Committer: Frederic Perez Ordeig
  • Date: 2009-12-18 16:21:23 UTC
  • Revision ID: frederic@zaluum.com-20091218162123-9fp07r8kr8raec5b
moved all fsm to one plugin

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   2008,2009       Elias Hortas Garcia
 
19
 */
 
20
package org.zaluum.boxes.fsm.editor;
 
21
 
 
22
import java.util.List;
 
23
 
 
24
import org.eclipse.draw2d.ConnectionAnchor;
 
25
import org.eclipse.draw2d.IFigure;
 
26
import org.eclipse.draw2d.geometry.Point;
 
27
import org.eclipse.draw2d.geometry.Rectangle;
 
28
import org.eclipse.gef.ConnectionEditPart;
 
29
import org.eclipse.gef.EditPolicy;
 
30
import org.eclipse.gef.GraphicalEditPart;
 
31
import org.eclipse.gef.NodeEditPart;
 
32
import org.eclipse.gef.Request;
 
33
import org.eclipse.gef.commands.Command;
 
34
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
 
35
import org.eclipse.gef.editpolicies.ComponentEditPolicy;
 
36
import org.eclipse.gef.editpolicies.GraphicalNodeEditPolicy;
 
37
import org.eclipse.gef.requests.CreateConnectionRequest;
 
38
import org.eclipse.gef.requests.GroupRequest;
 
39
import org.eclipse.gef.requests.ReconnectRequest;
 
40
import org.eclipse.ui.views.properties.IPropertySource;
 
41
import org.zaluum.boxes.fsm.model.State;
 
42
import org.zaluum.boxes.fsm.model.Transition;
 
43
import org.zaluum.ide.util.GraphicUtils;
 
44
import org.zaluum.util.Observer;
 
45
 
 
46
import com.google.common.collect.Lists;
 
47
 
 
48
/**
 
49
 * @author frede
 
50
 * 
 
51
 */
 
52
public class StateEditPart extends AbstractGraphicalEditPart implements
 
53
                Observer, NodeEditPart {
 
54
 
 
55
        private StatePropertySource psource;
 
56
 
 
57
        public StateEditPart(State p) {
 
58
                setModel(p);
 
59
                psource = new StatePropertySource(this);
 
60
        }
 
61
 
 
62
        @Override
 
63
        public void activate() {
 
64
                super.activate();
 
65
                getStateModel().addObserver(this);
 
66
        }
 
67
 
 
68
        @Override
 
69
        public void deactivate() {
 
70
                getStateModel().removeObserver(this);
 
71
                super.deactivate();
 
72
        };
 
73
 
 
74
        public State getStateModel() {
 
75
                return (State) getModel();
 
76
        }
 
77
 
 
78
        @Override
 
79
        protected List<Transition> getModelSourceConnections() {
 
80
                return Lists.newArrayList(getStateModel().getFrom());
 
81
        }
 
82
 
 
83
        @Override
 
84
        protected List<Transition> getModelTargetConnections() {
 
85
                return Lists.newArrayList(getStateModel().getTo());
 
86
        }
 
87
 
 
88
        @Override
 
89
        protected IFigure createFigure() {
 
90
                State s = getStateModel();
 
91
                return new StateFigure(s.isInitial(),s.hasEntryActions(),s.hasExitActions());
 
92
        }
 
93
        
 
94
        StateFigure getStateFigure(){
 
95
                return (StateFigure) getFigure();
 
96
        }
 
97
        
 
98
        @Override
 
99
        protected void refreshVisuals() {
 
100
                getStateFigure().updateFigure(getStateModel().getName(), getStateModel().isInitial(), getStateModel().hasEntryActions(), getStateModel().hasExitActions());
 
101
                Point point = GraphicUtils.modelToDraw2d(getStateModel().getPosition());
 
102
                Rectangle bounds = new Rectangle(point, getFigure().getPreferredSize());
 
103
                ((GraphicalEditPart) getParent()).setLayoutConstraint(
 
104
                                this,
 
105
                                getStateFigure(),
 
106
                                bounds);
 
107
        }
 
108
 
 
109
        @Override
 
110
        protected void createEditPolicies() {
 
111
                installEditPolicy(EditPolicy.COMPONENT_ROLE, new ComponentEditPolicy() {
 
112
                        @Override
 
113
                        protected Command createDeleteCommand(GroupRequest deleteRequest) {
 
114
                                return new DeleteStateCommand(getStateModel());
 
115
                        }
 
116
                });
 
117
                installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
 
118
                                new GraphicalNodeEditPolicy() {
 
119
 
 
120
                                        @Override
 
121
                                        protected Command getConnectionCompleteCommand(
 
122
                                                        CreateConnectionRequest request) {
 
123
                                                if (!(request.getStartCommand() instanceof CreateTransitionCommand))
 
124
                                                        return null;
 
125
                                                CreateTransitionCommand cmd = (CreateTransitionCommand) request
 
126
                                                                .getStartCommand();
 
127
                                                cmd.setTarget(getStateModel());
 
128
                                                return cmd;
 
129
                                        }
 
130
 
 
131
                                        @Override
 
132
                                        protected Command getConnectionCreateCommand(
 
133
                                                        CreateConnectionRequest request) {
 
134
                                                if (request.getNewObjectType() != Transition.class)
 
135
                                                        return null;
 
136
                                                CreateTransitionCommand cmd = new CreateTransitionCommand(
 
137
                                                                getStateModel());
 
138
                                                request.setStartCommand(cmd);
 
139
                                                return cmd;
 
140
                                        }
 
141
                                        private Command reconnect(ReconnectRequest request){
 
142
                                                if (request.getConnectionEditPart() instanceof TransitionEditPart){
 
143
                                                        Transition t = (Transition) request.getConnectionEditPart().getModel();
 
144
                                                        State src,trg;
 
145
                                                        if (request.isMovingStartAnchor()){
 
146
                                                                src = getStateModel();
 
147
                                                                trg = t.getTarget();
 
148
                                                        }else{
 
149
                                                                src = t.getSource();
 
150
                                                                trg = getStateModel();
 
151
                                                        }
 
152
                                                        if (src==trg) return null;
 
153
                                                        return new ReconnectTransitionCommand(t, src, trg);
 
154
                                                }
 
155
                                                return null;
 
156
                                        }
 
157
                                        @Override
 
158
                                        protected Command getReconnectSourceCommand(
 
159
                                                        ReconnectRequest request) {
 
160
                                                return reconnect(request);
 
161
                                        }
 
162
 
 
163
                                        @Override
 
164
                                        protected Command getReconnectTargetCommand(
 
165
                                                        ReconnectRequest request) {
 
166
                                                return reconnect(request);
 
167
                                        }
 
168
                                });
 
169
        }
 
170
 
 
171
        @Override
 
172
        public void update() {
 
173
                refresh();
 
174
        }
 
175
 
 
176
        @Override
 
177
        public ConnectionAnchor getSourceConnectionAnchor(
 
178
                        ConnectionEditPart connection) {
 
179
                return getStateFigure().getAnchor();
 
180
        }
 
181
 
 
182
        @Override
 
183
        public ConnectionAnchor getSourceConnectionAnchor(Request request) {
 
184
                return getStateFigure().getAnchor();
 
185
        }
 
186
 
 
187
        @Override
 
188
        public ConnectionAnchor getTargetConnectionAnchor(
 
189
                        ConnectionEditPart connection) {
 
190
                return getStateFigure().getAnchor();
 
191
        }
 
192
 
 
193
        @Override
 
194
        public ConnectionAnchor getTargetConnectionAnchor(Request request) {
 
195
                return getStateFigure().getAnchor();
 
196
        }
 
197
 
 
198
        @SuppressWarnings("unchecked")
 
199
        @Override
 
200
        public Object getAdapter(Class key) {
 
201
                if (key == IPropertySource.class) {
 
202
                        return psource;
 
203
                }
 
204
                return super.getAdapter(key);
 
205
        }
 
206
 
 
207
}