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

« back to all changes in this revision

Viewing changes to src/pipe/gui/handler/ArcHandler.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:
2
2
 
3
3
import java.awt.Container;
4
4
import java.awt.Point;
5
 
import java.awt.event.ActionEvent;
6
 
import java.awt.event.ActionListener;
7
5
import java.awt.event.MouseEvent;
8
6
import java.awt.event.MouseWheelEvent;
9
7
import java.awt.geom.Point2D;
10
8
 
11
 
import javax.swing.AbstractAction;
12
 
import javax.swing.BoxLayout;
13
9
import javax.swing.JMenuItem;
14
10
import javax.swing.JPopupMenu;
15
11
 
16
12
import pipe.dataLayer.Arc;
17
13
import pipe.dataLayer.InhibitorArc;
18
 
import pipe.dataLayer.NormalArc;
19
 
import pipe.dataLayer.Place;
20
 
import pipe.dataLayer.TimedArc;
21
 
import pipe.dataLayer.TransportArc;
22
 
import pipe.dataLayer.colors.ColoredOutputArc;
 
14
import pipe.dataLayer.TimedInputArcComponent;
 
15
import pipe.dataLayer.TimedOutputArcComponent;
 
16
import pipe.dataLayer.TransportArcComponent;
23
17
import pipe.gui.CreateGui;
 
18
import pipe.gui.DrawingSurfaceImpl;
24
19
import pipe.gui.Grid;
25
 
import pipe.gui.GuiView;
26
20
import pipe.gui.Pipe;
27
21
import pipe.gui.action.EditWeightAction;
28
 
import pipe.gui.action.InsertPointAction;
29
22
import pipe.gui.action.SplitArcAction;
30
 
import pipe.gui.undo.UndoManager;
31
 
import pipe.gui.widgets.EscapableDialog;
32
 
import pipe.gui.widgets.OutputValueEditorPanel;
33
 
 
34
23
 
35
24
/**
36
25
 * Class used to implement methods corresponding to mouse events on arcs.
37
26
 */
38
 
public class ArcHandler 
39
 
extends PetriNetObjectHandler {
40
 
 
 
27
public class ArcHandler extends PetriNetObjectHandler {
41
28
 
42
29
        public ArcHandler(Container contentpane, Arc obj) {
43
30
                super(contentpane, obj);
44
31
                enablePopup = true;
45
32
        }
46
33
 
47
 
 
48
 
        /** 
49
 
         * Creates the popup menu that the user will see when they right click on a 
50
 
         * component 
 
34
        /**
 
35
         * Creates the popup menu that the user will see when they right click on a
 
36
         * component
51
37
         */
52
38
        @Override
53
39
        public JPopupMenu getPopup(MouseEvent e) {
57
43
 
58
44
                if (myObject instanceof InhibitorArc) {
59
45
                        menuItem = new JMenuItem(new EditWeightAction(contentPane,
60
 
                                        (Arc)myObject));
 
46
                                        (Arc) myObject));
61
47
                        menuItem.setText("Edit Weight");
62
48
                        popup.insert(menuItem, popupIndex++);
63
49
 
64
 
                        menuItem = new JMenuItem(new SplitArcAction((Arc)myObject, 
65
 
                                        e.getPoint()));
 
50
                        menuItem = new JMenuItem(new SplitArcAction((Arc) myObject, e
 
51
                                        .getPoint()));
66
52
                        menuItem.setText("Split Arc Segment");
67
53
                        popup.insert(menuItem, popupIndex++);
68
54
 
69
 
                        popup.insert(new JPopupMenu.Separator(), popupIndex++);         
70
 
                        /*CB Joakim Byg - timed arcs should not be handled here*/         
71
 
                } else if (myObject instanceof NormalArc && !(myObject instanceof TimedArc) && !(myObject instanceof TransportArc)) {
72
 
                        /*EOC*/
73
 
                        if (((NormalArc)myObject).isJoined()){
74
 
                                NormalArc PTArc;
75
 
                                NormalArc TPArc;
76
 
 
77
 
                                if (((NormalArc)myObject).getSource() instanceof Place){
78
 
                                        PTArc = (NormalArc)myObject;
79
 
                                        TPArc = ((NormalArc)myObject).getInverse();
80
 
                                } else {
81
 
                                        PTArc = ((NormalArc)myObject).getInverse();
82
 
                                        TPArc = (NormalArc)myObject;               
83
 
                                }
84
 
 
85
 
                                //            if (!PTArc.isTagged()) { //pendentnou
86
 
                                        menuItem = new JMenuItem(new EditWeightAction(contentPane, PTArc));
87
 
                                        menuItem.setText("Edit Weight (PT Arc)");
88
 
                                        popup.insert(menuItem, popupIndex++);
89
 
                                        //               menuItem = new JMenuItem(
90
 
                                        //                       new EditTaggedAction(contentPane, PTArc));
91
 
                                        //               menuItem.setText("Make Tagged (PT Arc)");               
92
 
                                        //               popup.insert(menuItem, popupIndex++);
93
 
                                        //            } else {
94
 
                                        //               menuItem = new JMenuItem(
95
 
                                        //                       new EditTaggedAction(contentPane, PTArc));
96
 
                                        //               menuItem.setText("Make Non-Tagged (PT Arc)");               
97
 
                                        //               popup.insert(menuItem, popupIndex++);               
98
 
                                        //            }
99
 
                                        popup.insert(new JPopupMenu.Separator(), popupIndex++);
100
 
 
101
 
                                        //            if (!TPArc.isTagged()) {
102
 
                                        menuItem = new JMenuItem(new EditWeightAction(contentPane, TPArc));
103
 
                                        menuItem.setText("Edit Weight (TP Arc)");
104
 
                                        popup.insert(menuItem, popupIndex++);  
105
 
                                        //               menuItem = new JMenuItem(
106
 
                                        //                       new EditTaggedAction(contentPane, TPArc));
107
 
                                        //               menuItem.setText("Make Tagged (TP Arc)");               
108
 
                                        //               popup.insert(menuItem, popupIndex++);               
109
 
                                        //            } else {
110
 
                                        //               menuItem = new JMenuItem(
111
 
                                        //                       new EditTaggedAction(contentPane, TPArc));
112
 
                                        //               menuItem.setText("Make Non-Tagged (TP Arc)");               
113
 
                                        //               popup.insert(menuItem, popupIndex++);  
114
 
                                        //            }
115
 
 
116
 
                                        popup.insert(new JPopupMenu.Separator(), popupIndex++);
117
 
 
118
 
                                        menuItem = new JMenuItem(new InsertPointAction((Arc)myObject, 
119
 
                                                        e.getPoint()));            
120
 
                                        menuItem.setText("Insert Point");
121
 
                                        /*                        
122
 
            menuItem = new JMenuItem(new SplitArcAction((Arc)myObject, 
123
 
                                                         e.getPoint()));
124
 
            menuItem.setText("Split Arc Segment");
125
 
                                         */
126
 
                                        popup.insert(menuItem, popupIndex++);
127
 
 
128
 
                                        menuItem = new JMenuItem(
129
 
                                                        new SplitArcsAction((NormalArc)myObject, true));
130
 
                                        menuItem.setText("Split Arcs (PT / TP)");
131
 
                                        popup.insert(menuItem, popupIndex++);            
132
 
 
133
 
                                        popup.insert(new JPopupMenu.Separator(), popupIndex++);   
134
 
 
135
 
                                        menuItem = new JMenuItem(new DeleteInverseArcAction(PTArc));
136
 
                                        menuItem.setText("Delete (PT Arc)");
137
 
                                        popup.insert(menuItem, popupIndex++);  
138
 
 
139
 
                                        menuItem = new JMenuItem(new DeleteInverseArcAction(TPArc));
140
 
                                        menuItem.setText("Delete (TP Arc)");
141
 
                                        popup.insert(menuItem, popupIndex++);
142
 
                                        /*
143
 
            menuItem = new JMenuItem(new DeleteBothAction((NormalArc)myObject));
144
 
            menuItem.setText("Delete Both");
145
 
            popup.insert(menuItem, 8);                                    
146
 
                                         */
147
 
                        } else {
148
 
                                //            if(!((NormalArc)myObject).isTagged()) {
149
 
                                //menuItem = new JMenuItem(new EditWeightAction(contentPane, 
150
 
                                //              (Arc)myObject));
151
 
                                //menuItem.setText("Edit Weight");
152
 
                                //popup.insert(menuItem, popupIndex++);
153
 
                                //            }
154
 
 
155
 
                                //            menuItem = new JMenuItem(
156
 
                                //                    new EditTaggedAction(contentPane,(NormalArc)myObject));
157
 
                                //            if (((NormalArc)myObject).isTagged()) {
158
 
                                //               menuItem.setText("Make Non-Tagged");
159
 
                                //            } else { 
160
 
                                //               menuItem.setText("Make Tagged");
161
 
                                //            }
162
 
                                //            popup.insert(menuItem, popupIndex++);            
163
 
 
164
 
                                //menuItem = new JMenuItem(new SplitArcAction((Arc)myObject, 
165
 
                                //                                             e.getPoint()));
166
 
                                //menuItem.setText("Split Arc Segment");
167
 
 
168
 
                                if(CreateGui.getModel().isUsingColors() && myObject instanceof ColoredOutputArc){
169
 
                                        menuItem = new JMenuItem();            
170
 
                                        menuItem.setText("Properties");
171
 
                                        menuItem.addActionListener(new ActionListener(){
172
 
                                                public void actionPerformed(ActionEvent e) {
173
 
                                                        showOutputValueEditor((ColoredOutputArc)myObject);
174
 
                                                }
175
 
                                        }); 
176
 
                                        popup.insert(menuItem, popupIndex++);
177
 
                                }
178
 
                                
179
 
                                menuItem = new JMenuItem(new SplitArcAction((Arc)myObject, 
180
 
                                                e.getPoint()));            
181
 
                                menuItem.setText("Insert Point");
182
 
                                popup.insert(menuItem, popupIndex++);
183
 
 
184
 
                                
185
 
                                if (((NormalArc)myObject).hasInverse()){
186
 
                                        menuItem = new JMenuItem(
187
 
                                                        new SplitArcsAction((NormalArc)myObject, false));
188
 
 
189
 
                                        menuItem.setText("Join Arcs (PT / TP)");
190
 
                                        popup.insert(menuItem, popupIndex++);            
191
 
                                }
192
 
                                popup.insert(new JPopupMenu.Separator(), popupIndex);
193
 
                        }
 
55
                        popup.insert(new JPopupMenu.Separator(), popupIndex++);
 
56
                        /* CB Joakim Byg - timed arcs should not be handled here */
 
57
                } else if (myObject instanceof TimedOutputArcComponent
 
58
                                && !(myObject instanceof TimedInputArcComponent)
 
59
                                && !(myObject instanceof TransportArcComponent)) {
 
60
                        /* EOC */
 
61
                        
 
62
                        menuItem = new JMenuItem(new SplitArcAction((Arc) myObject, e
 
63
                                        .getPoint()));
 
64
                        menuItem.setText("Insert Point");
 
65
                        popup.insert(menuItem, popupIndex++);
 
66
 
 
67
                        popup.insert(new JPopupMenu.Separator(), popupIndex);
194
68
                }
195
69
                return popup;
196
70
        }
197
71
 
198
 
 
199
72
        @Override
200
73
        public void mousePressed(MouseEvent e) {
201
74
                super.mousePressed(e);
202
 
                if (CreateGui.getApp().isEditionAllowed() == false){
 
75
                if (CreateGui.getApp().isEditionAllowed() == false) {
203
76
                        return;
204
 
                }      
205
 
                if (e.getClickCount() == 2){
206
 
                        Arc arc = (Arc)myObject;
207
 
                        if (e.isControlDown()){
 
77
                }
 
78
                if (e.getClickCount() == 2) {
 
79
                        Arc arc = (Arc) myObject;
 
80
                        if (e.isControlDown()) {
208
81
                                CreateGui.getView().getUndoManager().addNewEdit(
209
82
                                                arc.getArcPath().insertPoint(
210
 
                                                                new Point2D.Float(arc.getX() + e.getX(), 
211
 
                                                                                arc.getY() + e.getY()), e.isAltDown()));
 
83
                                                                new Point2D.Float(arc.getX() + e.getX(), arc
 
84
                                                                                .getY()
 
85
                                                                                + e.getY()), e.isAltDown()));
212
86
                        } else {
213
 
                                if(CreateGui.getModel().isUsingColors()){
214
 
                                        if(arc instanceof ColoredOutputArc){
215
 
                                                showOutputValueEditor((ColoredOutputArc)arc);
216
 
                                        }
217
 
                                }else{
218
 
                                        arc.getSource().select();
219
 
                                        arc.getTarget().select();
220
 
                                        justSelected = true;
221
 
                                }
 
87
                                arc.getSource().select();
 
88
                                arc.getTarget().select();
 
89
                                justSelected = true;
222
90
                        }
223
91
                }
224
92
        }
225
93
 
226
 
 
227
 
        private void showOutputValueEditor(ColoredOutputArc arc) {
228
 
                EscapableDialog guiDialog = 
229
 
                        new EscapableDialog(CreateGui.getApp(), Pipe.TOOL + " " + Pipe.VERSION, true);
230
 
 
231
 
                Container contentPane = guiDialog.getContentPane();
232
 
 
233
 
                // 1 Set layout
234
 
                contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));      
235
 
 
236
 
                // 2 Add Place editor
237
 
                contentPane.add( new OutputValueEditorPanel(guiDialog.getRootPane(), arc, CreateGui.getModel(), CreateGui.getView().getUndoManager()));
238
 
 
239
 
                guiDialog.setResizable(false);     
240
 
 
241
 
                // Make window fit contents' preferred size
242
 
                guiDialog.pack();
243
 
 
244
 
                // Move window to the middle of the screen
245
 
                guiDialog.setLocationRelativeTo(null);
246
 
                guiDialog.setVisible(true);
247
 
        }
248
 
 
249
 
 
 
94
        
250
95
        @Override
251
96
        public void mouseDragged(MouseEvent e) {
252
97
                switch (CreateGui.getApp().getMode()) {
253
98
                case Pipe.SELECT:
254
 
                        if (!isDragging){
 
99
                        if (!isDragging) {
255
100
                                break;
256
101
                        }
257
 
                        Arc currentObject = (Arc)myObject;
 
102
                        Arc currentObject = (Arc) myObject;
258
103
                        Point oldLocation = currentObject.getLocation();
259
104
                        // Calculate translation in mouse
260
105
                        int transX = (Grid.getModifiedX(e.getX() - dragInit.x));
261
106
                        int transY = (Grid.getModifiedY(e.getY() - dragInit.y));
262
 
                        ((GuiView)contentPane).getSelectionObject().translateSelection(
263
 
                                        transX, transY);
 
107
                        ((DrawingSurfaceImpl) contentPane).getSelectionObject()
 
108
                                        .translateSelection(transX, transY);
264
109
                        dragInit.translate(
265
110
                                        -(currentObject.getLocation().x - oldLocation.x - transX),
266
111
                                        -(currentObject.getLocation().y - oldLocation.y - transY));
267
112
                }
268
113
        }
269
114
 
270
 
 
271
115
        @Override
272
 
        public void mouseWheelMoved (MouseWheelEvent e) {
 
116
        public void mouseWheelMoved(MouseWheelEvent e) {
273
117
 
274
 
                if (CreateGui.getApp().isEditionAllowed() == false){
 
118
                if (CreateGui.getApp().isEditionAllowed() == false) {
275
119
                        return;
276
120
                }
277
121
 
278
 
                Arc arc = ((Arc)myObject);
279
 
                if (arc instanceof NormalArc){
 
122
                Arc arc = ((Arc) myObject);
 
123
                if (arc instanceof TimedOutputArcComponent) {
280
124
 
281
 
                        //         if (e.isControlDown()) {
282
 
                        //            if (arc.getWeight() == 1) {
283
 
                        //               if (((NormalArc)arc).hasInvisibleInverse()) {
284
 
                        //                  if (arc.getSource() instanceof Place){
285
 
                        //                     if (e.isShiftDown()) {
286
 
                        //                        arc = ((NormalArc)arc).getInverse();
287
 
                        //                     }
288
 
                        //                  } else {
289
 
                        //                     if (!e.isShiftDown()) {
290
 
                        //                        arc = ((NormalArc)arc).getInverse();
291
 
                        //                     }
292
 
                        //                  }
293
 
                        //               }
294
 
                        //               CreateGui.getView().getUndoManager().addNewEdit(
295
 
                        //                       ((NormalArc)arc).setTagged(!((NormalArc)arc).isTagged()));
296
 
                        //            }
297
 
                        //            return;
298
 
                        //         }
299
 
                        /*   
300
 
         if (((NormalArc)arc).hasInvisibleInverse()) {
301
 
            if (arc.getSource() instanceof Place){
302
 
               if (e.isShiftDown()) {
303
 
                  arc = ((NormalArc)arc).getInverse();                  
304
 
               }
305
 
            } else {
306
 
               if (!e.isShiftDown()) {
307
 
                  arc = ((NormalArc)arc).getInverse();                  
308
 
               }
309
 
            }
310
 
         }*/
311
 
                }      
 
125
                        // if (e.isControlDown()) {
 
126
                        // if (arc.getWeight() == 1) {
 
127
                        // if (((NormalArc)arc).hasInvisibleInverse()) {
 
128
                        // if (arc.getSource() instanceof Place){
 
129
                        // if (e.isShiftDown()) {
 
130
                        // arc = ((NormalArc)arc).getInverse();
 
131
                        // }
 
132
                        // } else {
 
133
                        // if (!e.isShiftDown()) {
 
134
                        // arc = ((NormalArc)arc).getInverse();
 
135
                        // }
 
136
                        // }
 
137
                        // }
 
138
                        // CreateGui.getView().getUndoManager().addNewEdit(
 
139
                        // ((NormalArc)arc).setTagged(!((NormalArc)arc).isTagged()));
 
140
                        // }
 
141
                        // return;
 
142
                        // }
 
143
                        /*
 
144
                         * if (((NormalArc)arc).hasInvisibleInverse()) { if (arc.getSource()
 
145
                         * instanceof Place){ if (e.isShiftDown()) { arc =
 
146
                         * ((NormalArc)arc).getInverse(); } } else { if (!e.isShiftDown()) {
 
147
                         * arc = ((NormalArc)arc).getInverse(); } } }
 
148
                         */
 
149
                }
312
150
                /*
313
 
      int oldWeight = arc.getWeight();
314
 
      int newWeight = oldWeight - e.getWheelRotation();
315
 
      if (newWeight < 1) {
316
 
         newWeight = 1;
317
 
      }
318
 
      if (newWeight != oldWeight) {
319
 
         CreateGui.getView().getUndoManager().addNewEdit(
320
 
               arc.setWeight(newWeight));
321
 
         arc.repaint();
322
 
      }*/
323
 
        }
324
 
 
325
 
 
326
 
 
327
 
        class SplitArcsAction extends AbstractAction {
328
 
 
329
 
                /**
330
 
                 * 
331
 
                 */
332
 
                private static final long serialVersionUID = 6647793002696424279L;
333
 
                NormalArc arc;
334
 
                boolean joined;
335
 
 
336
 
 
337
 
                public SplitArcsAction(NormalArc _arc, boolean _joined){
338
 
                        arc = _arc;
339
 
                        joined = _joined;
340
 
                }
341
 
 
342
 
                public void actionPerformed(ActionEvent e) {
343
 
                        if (joined) {
344
 
                                CreateGui.getView().getUndoManager().addNewEdit(
345
 
                                                arc.split());
346
 
                        } else {         
347
 
                                CreateGui.getView().getUndoManager().addNewEdit(
348
 
                                                arc.join());
349
 
                        }
350
 
                }
351
 
 
352
 
        }
353
 
 
354
 
 
355
 
 
356
 
        class DeleteInverseArcAction extends AbstractAction {
357
 
 
358
 
                /**
359
 
                 * 
360
 
                 */
361
 
                private static final long serialVersionUID = -7956567157135376160L;
362
 
                NormalArc arc, inverse;
363
 
                boolean switchArcs;
364
 
 
365
 
 
366
 
                public DeleteInverseArcAction(NormalArc _arc){
367
 
                        arc = _arc;
368
 
                        inverse = arc.getInverse();
369
 
                        switchArcs = arc.inView();
370
 
                }
371
 
 
372
 
 
373
 
                public void actionPerformed(ActionEvent e) {
374
 
                        UndoManager undoManager = CreateGui.getView().getUndoManager();
375
 
 
376
 
                        if (switchArcs) {
377
 
                                undoManager.addNewEdit(arc.split());
378
 
                        } else {
379
 
                                undoManager.addNewEdit(inverse.split());
380
 
                        }
381
 
                        undoManager.deleteSelection(arc);
382
 
 
383
 
                        arc.delete();   
384
 
                }
385
 
        }   
 
151
                 * int oldWeight = arc.getWeight(); int newWeight = oldWeight -
 
152
                 * e.getWheelRotation(); if (newWeight < 1) { newWeight = 1; } if
 
153
                 * (newWeight != oldWeight) {
 
154
                 * CreateGui.getView().getUndoManager().addNewEdit(
 
155
                 * arc.setWeight(newWeight)); arc.repaint(); }
 
156
                 */
 
157
        }
386
158
 
387
159
}