~tapaal-contributor/tapaal/disappearing-tokens-1940098

« back to all changes in this revision

Viewing changes to src/pipe/dataLayer/TransportArcComponent.java

  • Committer: Kenneth Yrke Jørgensen
  • Date: 2011-04-12 09:50:16 UTC
  • mfrom: (329.1.188 tapaal-1.5)
  • Revision ID: mail@yrke.dk-20110412095016-e4hqdgab5596ja09
Merged with branch addning support for new 1.5 features

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import java.awt.Color;
4
4
import java.awt.Graphics;
5
5
import java.awt.Polygon;
 
6
import java.util.Hashtable;
6
7
 
7
 
import pipe.gui.GuiView;
 
8
import pipe.gui.DrawingSurfaceImpl;
 
9
import pipe.gui.handler.TransportArcHandler;
 
10
import pipe.gui.undo.ArcTimeIntervalEdit;
8
11
import pipe.gui.undo.TransportArcGroupEdit;
9
 
import pipe.gui.undo.UndoableEdit;
 
12
import dk.aau.cs.gui.undo.Command;
 
13
import dk.aau.cs.model.tapn.TimeInterval;
 
14
import dk.aau.cs.model.tapn.TimedArcPetriNet;
 
15
import dk.aau.cs.model.tapn.TransportArc;
10
16
 
11
 
public class TransportArc extends TimedArc {
12
 
        /**
13
 
         * 
14
 
         */
 
17
public class TransportArcComponent extends TimedInputArcComponent {
15
18
        private static final long serialVersionUID = 3728885532894319528L;
16
19
        private int group;
17
 
        private boolean isInPreSet; 
18
 
        private TransportArc connectedTo=null;
19
 
        
20
 
        public TransportArc(PlaceTransitionObject newSource, int groupNr, boolean isInPreSet) {
21
 
                super(new NormalArc(newSource));
 
20
        private boolean isInPreSet;
 
21
        private TransportArcComponent connectedTo = null;
 
22
        private TransportArc underlyingTransportArc;
 
23
 
 
24
        public TransportArcComponent(PlaceTransitionObject newSource, int groupNr,
 
25
                        boolean isInPreSet) {
 
26
                super(new TimedOutputArcComponent(newSource));
22
27
                this.isInPreSet = isInPreSet;
23
28
                setHead();
24
29
                setGroup(groupNr);
25
 
                //hack to reprint the label of the arc
26
 
                updateWeightLabel();
 
30
                // hack to reprint the label of the arc
 
31
                updateWeightLabel(true);
27
32
        }
28
 
        public TransportArc(TimedArc timedArc, int group, boolean isInPreSet) {
29
 
                super(timedArc, timedArc.getGuard());
 
33
 
 
34
        public TransportArcComponent(TimedInputArcComponent timedArc, int group,
 
35
                        boolean isInPreSet) {
 
36
                super(timedArc, "");
30
37
                this.isInPreSet = isInPreSet;
31
38
                setHead();
32
39
                this.setGroup(group);
33
 
                //hack to reprint the label of the arc
34
 
                updateWeightLabel();
35
 
        }
36
 
        private void setHead(){
37
 
                head = new Polygon(new int[]{0, 5, 0, -5}, new int[]{0, -11, -18, -11}, 4);
38
 
        }
39
 
        public void setColor(){
 
40
                // hack to reprint the label of the arc
 
41
                updateWeightLabel(true);
 
42
        }
 
43
 
 
44
        public void setUnderlyingArc(TransportArc arc) {
 
45
                this.underlyingTransportArc = arc; // must explicitly set underlying arc
 
46
                                                                                        // on connected to
 
47
                updateWeightLabel(true);
 
48
        }
 
49
 
 
50
        public TransportArc underlyingTransportArc() {
 
51
                return underlyingTransportArc;
 
52
        }
 
53
 
 
54
        private void setHead() {
 
55
                head = new Polygon(new int[] { 0, 5, 0, -5 }, new int[] { 0, -11, -18,
 
56
                                -11 }, 4);
 
57
        }
 
58
 
 
59
        public void setColor() {
40
60
                getGraphics().setColor(Color.RED);
41
61
                repaint();
42
62
        }
43
 
        public UndoableEdit setGroupNr(int groupNr){
 
63
 
 
64
        public Command setGroupNr(int groupNr) {
44
65
                int oldGroup = this.getGroup();
45
66
                setGroup(groupNr);
46
67
 
47
 
                //hacks - I use the weight to display the TimeInterval
48
 
                updateWeightLabel();
 
68
                // hacks - I use the weight to display the TimeInterval
 
69
                updateWeightLabel(true);
49
70
                repaint();
50
 
                
 
71
 
51
72
                return new TransportArcGroupEdit(this, oldGroup, this.getGroup());
52
73
        }
53
 
        public int getGroupNr(){
 
74
 
 
75
        public int getGroupNr() {
54
76
                return getGroup();
55
77
        }
56
 
        
 
78
 
57
79
        @Override
58
 
        public void updateWeightLabel(){   
59
 
                if (isInPreSet){
60
 
                weightLabel.setText(timeInterval+" : "+getGroup());
 
80
        public void updateWeightLabel(boolean displayConstantNames) {
 
81
                if (isInPreSet && underlyingTransportArc != null) {
 
82
                        weightLabel.setText(underlyingTransportArc.interval().toString(
 
83
                                        displayConstantNames)
 
84
                                        + " : " + getGroup());
 
85
                } else if (!isInPreSet) {
 
86
                        weightLabel.setText(String.valueOf(getGroup()));
61
87
                } else {
62
 
                        weightLabel.setText(""+getGroup());
 
88
                        weightLabel.setText("");
63
89
                }
64
90
                this.setWeightLabelPosition();
65
91
        }
66
 
        
 
92
 
67
93
        @Override
68
 
        public TransportArc copy() {
69
 
                
 
94
        public TransportArcComponent copy() {
 
95
 
70
96
                return null;
71
97
        }
72
 
        
 
98
 
73
99
        @Override
74
100
        public void delete() {
75
 
                
76
 
                
 
101
                if (underlyingTransportArc != null) {
 
102
                        underlyingTransportArc.delete();
 
103
                        underlyingTransportArc = null;
 
104
                        connectedTo.underlyingTransportArc = null;
 
105
                }
 
106
 
77
107
                // kyrke - do ekstra suff when deleting a transport arc
78
 
                
79
 
                super.delete();
80
 
                
 
108
 
 
109
                super.delete();
 
110
 
81
111
                // xxx - hack to awoid delete loop
82
 
                
83
 
                TransportArc a = connectedTo;
 
112
 
 
113
                TransportArcComponent a = connectedTo;
84
114
                connectedTo = null;
85
 
                if (a != null && a.connectedTo != null){
 
115
                if (a != null && a.connectedTo != null) {
86
116
                        a.delete();
87
117
                }
88
118
                connectedTo = a;
89
 
                
90
 
        
 
119
 
91
120
        }
92
 
        
 
121
 
93
122
        @Override
94
 
        public void undelete(DataLayer model, GuiView view) {
95
 
                super.undelete(model, view);
96
 
                
97
 
                TransportArc a = connectedTo;
 
123
        public void undelete(DrawingSurfaceImpl view) {
 
124
                super.undelete(view);
 
125
 
 
126
                TransportArcComponent a = connectedTo;
98
127
                connectedTo = null;
99
 
                if (a.connectedTo != null){
100
 
                        a.undelete(model,view);
 
128
                if (a.connectedTo != null) {
 
129
                        a.undelete(view);
101
130
                        a.connectedTo = this;
102
131
                }
103
132
                connectedTo = a;
104
133
        }
105
134
 
106
135
        @Override
107
 
        public TransportArc paste(double despX, double despY, boolean toAnotherView) {
108
 
                
 
136
        public TransportArcComponent paste(double despX, double despY,
 
137
                        boolean toAnotherView) {
 
138
 
109
139
                return null;
110
140
        }
 
141
 
111
142
        /*
112
 
        public TransportArc getBuddy() {
113
 
                TransportArc toReturn = null;
114
 
                if (isInPreSet){
115
 
                        HashMap<TransportArc, TransportArc> transportArcMap = 
116
 
                                (HashMap<TransportArc, TransportArc>) ((HashMap) CreateGui.getModel().getTransportArcMap() ).
117
 
                                                                                                                                        get( this.getTarget() );
118
 
 
119
 
                        toReturn = transportArcMap.get(this);
120
 
                }else {
121
 
                        HashMap<TransportArc, TransportArc> transportArcMap = 
122
 
                                (HashMap<TransportArc, TransportArc>) ((HashMap) CreateGui.getModel().getTransportArcMap() ).
123
 
                                                                                                                                        get( this.getSource() );
124
 
                        for ( TransportArc ta : transportArcMap.keySet() ){
125
 
                                if (transportArcMap.get(ta) == this){
126
 
                                        toReturn = ta;
127
 
                                }
128
 
                        }
129
 
                }
130
 
                return toReturn;
131
 
        }*/
 
143
         * public TransportArc getBuddy() { TransportArc toReturn = null; if
 
144
         * (isInPreSet){ HashMap<TransportArc, TransportArc> transportArcMap =
 
145
         * (HashMap<TransportArc, TransportArc>) ((HashMap)
 
146
         * CreateGui.getModel().getTransportArcMap() ). get( this.getTarget() );
 
147
         * 
 
148
         * toReturn = transportArcMap.get(this); }else { HashMap<TransportArc,
 
149
         * TransportArc> transportArcMap = (HashMap<TransportArc, TransportArc>)
 
150
         * ((HashMap) CreateGui.getModel().getTransportArcMap() ). get(
 
151
         * this.getSource() ); for ( TransportArc ta : transportArcMap.keySet() ){
 
152
         * if (transportArcMap.get(ta) == this){ toReturn = ta; } } } return
 
153
         * toReturn; }
 
154
         */
132
155
        public boolean isInPreSet() {
133
156
                return isInPreSet;
134
157
        }
135
 
        public TransportArc getConnectedTo() {
 
158
 
 
159
        public TransportArcComponent getConnectedTo() {
136
160
                return connectedTo;
137
161
        }
138
 
        
139
 
        public void setConnectedTo(TransportArc connectedTo) {
 
162
 
 
163
        public void setConnectedTo(TransportArcComponent connectedTo) {
140
164
                this.connectedTo = connectedTo;
141
165
        }
142
 
        
 
166
 
143
167
        @Override
144
168
        public void paintComponent(Graphics g) {
145
169
                super.paintComponent(g);
152
176
        public int getGroup() {
153
177
                return group;
154
178
        }
 
179
 
 
180
        @Override
 
181
        public String getGuardAsString() {
 
182
                return underlyingTransportArc.interval().toString();
 
183
        }
 
184
 
 
185
        @Override
 
186
        public TimeInterval getGuard() {
 
187
                return underlyingTransportArc.interval();
 
188
        }
 
189
 
 
190
        @Override
 
191
        public Command setGuard(TimeInterval guard) {
 
192
 
 
193
                TimeInterval oldTimeInterval = underlyingTransportArc.interval();
 
194
                underlyingTransportArc.setTimeInterval(guard);
 
195
 
 
196
                // hacks - I use the weight to display the TimeInterval
 
197
                updateWeightLabel(true);
 
198
                repaint();
 
199
 
 
200
                return new ArcTimeIntervalEdit(this, oldTimeInterval,
 
201
                                underlyingTransportArc.interval());
 
202
        }
155
203
        
 
204
        public TransportArcComponent copy(TimedArcPetriNet tapn, DataLayer guiModel, Hashtable<PlaceTransitionObject, PlaceTransitionObject> oldToNewMapping) {
 
205
                TransportArcComponent arc = new TransportArcComponent(this, group, isInPreSet);
 
206
                arc.setSource(oldToNewMapping.get(this.getSource()));
 
207
                arc.setTarget(oldToNewMapping.get(this.getTarget()));
 
208
                
 
209
                arc.setUnderlyingArc(tapn.getTransportArcFromPlaceTransitionAndPlace(tapn.getPlaceByName(underlyingTransportArc.source().name()), 
 
210
                                                                                                                                                         tapn.getTransitionByName(underlyingTransportArc.transition().name()), 
 
211
                                                                                                                                                         tapn.getPlaceByName(underlyingTransportArc.destination().name())));
 
212
                
 
213
                arc.getSource().addConnectFrom(arc);
 
214
                arc.getTarget().addConnectTo(arc);
 
215
                
 
216
                TransportArcHandler transportArcHandler = new TransportArcHandler((DrawingSurfaceImpl)getParent(), arc);
 
217
                arc.addMouseListener(transportArcHandler);
 
218
                arc.addMouseWheelListener(transportArcHandler);
 
219
                arc.addMouseMotionListener(transportArcHandler);
 
220
                
 
221
                arc.setGuiModel(guiModel);
 
222
                
 
223
                return arc;
 
224
        }
 
225
 
156
226
}