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

« back to all changes in this revision

Viewing changes to src/pipe/gui/SelectionManager.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:
18
18
import pipe.dataLayer.PetriNetObject;
19
19
import pipe.dataLayer.PlaceTransitionObject;
20
20
 
21
 
 
22
21
/**
23
 
 * @author Peter Kyme, Michael Camacho
24
 
 * Class to handle selection rectangle functionality
 
22
 * @author Peter Kyme, Michael Camacho Class to handle selection rectangle
 
23
 *         functionality
25
24
 */
26
 
public class SelectionManager 
27
 
        extends javax.swing.JComponent
28
 
        implements java.awt.event.MouseListener, 
29
 
                   java.awt.event.MouseWheelListener,
30
 
                   java.awt.event.MouseMotionListener {
 
25
public class SelectionManager extends javax.swing.JComponent implements
 
26
                java.awt.event.MouseListener, java.awt.event.MouseWheelListener,
 
27
                java.awt.event.MouseMotionListener {
31
28
 
32
 
   /**
 
29
        /**
33
30
         * 
34
31
         */
35
32
        private static final long serialVersionUID = 9057152447545103393L;
36
 
private Point startPoint;
37
 
   private Rectangle selectionRectangle = new Rectangle(-1,-1);
38
 
   private boolean isSelecting;
39
 
   private static final Color selectionColor = new Color(000, 000, 255, 030);
40
 
   private static final Color selectionColorOutline = new Color(000, 000, 100);
41
 
   private GuiView view;
42
 
   private boolean enabled = true;
43
 
 
44
 
   
45
 
   public SelectionManager(GuiView _view) {
46
 
      addMouseListener(this);
47
 
      addMouseMotionListener(this);
48
 
      addMouseWheelListener(this);
49
 
      this.view = _view;
50
 
   }
51
 
   
52
 
   
53
 
   public void updateBounds() {
54
 
      if (enabled == true) {
55
 
         setBounds(0,0,view.getWidth(),view.getHeight());
56
 
      }
57
 
   }
58
 
 
59
 
   
60
 
   public void enableSelection() {
61
 
      if (enabled == false) {
62
 
         view.add(this);
63
 
         enabled = true;
64
 
         updateBounds();
65
 
      }
66
 
   }
67
 
 
68
 
   
69
 
   public void disableSelection() {
70
 
       if (enabled == true) {
71
 
         view.remove(this);
72
 
         enabled = false;
73
 
      }
74
 
   }
75
 
 
76
 
   
77
 
   private void processSelection(MouseEvent e) {
78
 
      if (!e.isShiftDown()){
79
 
         clearSelection();
80
 
      }
81
 
      
82
 
      // Get all the objects in the current window
83
 
      ArrayList <PetriNetObject> pnObjects = view.getPNObjects();
84
 
      for (PetriNetObject pnObject : pnObjects) {
85
 
         pnObject.select(selectionRectangle);
86
 
      }      
87
 
   }
88
 
 
89
 
   
90
 
   @Override
91
 
public void paintComponent(Graphics g) {
92
 
      super.paintComponent(g);
93
 
      Graphics2D g2d = (Graphics2D) g;
94
 
      g2d.setPaint(selectionColor);
95
 
      g2d.fill(selectionRectangle);
96
 
      g2d.setPaint(selectionColorOutline);
97
 
      g2d.draw(selectionRectangle);
98
 
   }
99
 
 
100
 
   
101
 
   public void deleteSelection() {
102
 
      // Get all the objects in the current window
103
 
      ArrayList <PetriNetObject> pnObjects = view.getPNObjects();
104
 
      for (int i = 0; i < pnObjects.size(); i++) {
105
 
         if (pnObjects.get(i).isSelected()) {
106
 
            pnObjects.get(i).delete();
107
 
         }         
108
 
      }
109
 
      view.updatePreferredSize();
110
 
   }
111
 
 
112
 
   
113
 
   public void clearSelection() {
114
 
      // Get all the objects in the current window
115
 
      ArrayList <PetriNetObject> pnObjects = view.getPNObjects();
116
 
      for (PetriNetObject pnObject : pnObjects) {    
117
 
         if (pnObject.isSelectable()) {
118
 
            pnObject.deselect();
119
 
         }
120
 
      }
121
 
   }
122
 
 
123
 
   
 
33
        private Point startPoint;
 
34
        private Rectangle selectionRectangle = new Rectangle(-1, -1);
 
35
        private boolean isSelecting;
 
36
        private static final Color selectionColor = new Color(000, 000, 255, 030);
 
37
        private static final Color selectionColorOutline = new Color(000, 000, 100);
 
38
        private DrawingSurfaceImpl drawingSurface;
 
39
        private boolean enabled = true;
 
40
 
 
41
        public SelectionManager(DrawingSurfaceImpl _view) {
 
42
                addMouseListener(this);
 
43
                addMouseMotionListener(this);
 
44
                addMouseWheelListener(this);
 
45
                this.drawingSurface = _view;
 
46
        }
 
47
 
 
48
        public void updateBounds() {
 
49
                if (enabled == true) {
 
50
                        setBounds(0, 0, drawingSurface.getWidth(), drawingSurface.getHeight());
 
51
                }
 
52
        }
 
53
 
 
54
        public void enableSelection() {
 
55
                if (enabled == false) {
 
56
                        drawingSurface.add(this);
 
57
                        enabled = true;
 
58
                        updateBounds();
 
59
                }
 
60
        }
 
61
 
 
62
        public void disableSelection() {
 
63
                if (enabled == true) {
 
64
                        this.clearSelection();
 
65
                        drawingSurface.remove(this);
 
66
                        enabled = false;
 
67
                }
 
68
        }
 
69
 
 
70
        private void processSelection(MouseEvent e) {
 
71
                if (!e.isShiftDown()) {
 
72
                        clearSelection();
 
73
                }
 
74
 
 
75
                // Get all the objects in the current window
 
76
                ArrayList<PetriNetObject> pnObjects = drawingSurface.getPNObjects();
 
77
                for (PetriNetObject pnObject : pnObjects) {
 
78
                        pnObject.select(selectionRectangle);
 
79
                }
 
80
        }
 
81
 
 
82
        @Override
 
83
        public void paintComponent(Graphics g) {
 
84
                super.paintComponent(g);
 
85
                Graphics2D g2d = (Graphics2D) g;
 
86
                g2d.setPaint(selectionColor);
 
87
                g2d.fill(selectionRectangle);
 
88
                g2d.setPaint(selectionColorOutline);
 
89
                g2d.draw(selectionRectangle);
 
90
        }
 
91
 
 
92
        public void deleteSelection() {
 
93
                // Get all the objects in the current window
 
94
                ArrayList<PetriNetObject> pnObjects = drawingSurface.getPNObjects();
 
95
                for (int i = 0; i < pnObjects.size(); i++) {
 
96
                        if (pnObjects.get(i).isSelected()) {
 
97
                                pnObjects.get(i).delete();
 
98
                        }
 
99
                }
 
100
                drawingSurface.updatePreferredSize();
 
101
        }
 
102
 
 
103
        public void clearSelection() {
 
104
                // Get all the objects in the current window
 
105
                ArrayList<PetriNetObject> pnObjects = drawingSurface.getPNObjects();
 
106
                for (PetriNetObject pnObject : pnObjects) {
 
107
                        if (pnObject.isSelectable()) {
 
108
                                pnObject.deselect();
 
109
                        }
 
110
                }
 
111
        }
 
112
 
124
113
        public void translateSelection(int transX, int transY) {
125
 
      
126
 
      if (transX == 0 && transY == 0) {
127
 
         return;
128
 
      }
129
 
 
130
 
      // First see if translation will put anything at a negative location
131
 
      Point point = null;
132
 
      Point topleft = null;
133
 
 
134
 
      // Get all the objects in the current window
135
 
      ArrayList <PetriNetObject> pnObjects = view.getPNObjects();
136
 
      for (PetriNetObject pnObject : pnObjects) {
137
 
         if (pnObject.isSelected()){
138
 
            point = pnObject.getLocation();
139
 
            if (topleft == null) {
140
 
               topleft = point;
141
 
            } else {
142
 
               if (point.x < topleft.x) {
143
 
                  topleft.x = point.x;
144
 
               }
145
 
               if (point.y < topleft.y) {
146
 
                  topleft.y = point.y;
147
 
               }
148
 
            }
149
 
         }
150
 
      }
151
 
      
152
 
      if (topleft != null) {
153
 
         topleft.translate(transX, transY);
154
 
         if (topleft.x < 0){
155
 
            transX -= topleft.x;
156
 
         }
157
 
         if (topleft.y < 0){
158
 
            transY -= topleft.y;
159
 
         }
160
 
         if (transX == 0 && transY == 0){
161
 
            return;
162
 
         }
163
 
      }
164
 
      
165
 
      for (PetriNetObject pnObject : pnObjects) {  
166
 
         if (pnObject.isSelected()) {
167
 
            pnObject.translate(transX, transY);
168
 
         }
169
 
      }
170
 
      view.updatePreferredSize();
171
 
   }
172
 
 
173
 
   
174
 
   public ArrayList<PetriNetObject> getSelection() {
175
 
      ArrayList<PetriNetObject> selection = new ArrayList<PetriNetObject>();
176
 
 
177
 
      // Get all the objects in the current window
178
 
      ArrayList <PetriNetObject> pnObjects = view.getPNObjects();
179
 
      for (PetriNetObject pnObject : pnObjects) {      
180
 
         if (pnObject.isSelected()){
181
 
            selection.add(pnObject);
182
 
         }
183
 
      }
184
 
      return selection;
185
 
   }
186
 
 
187
 
   
188
 
   /* (non-Javadoc)
189
 
    * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
190
 
    */
191
 
   public void mousePressed(MouseEvent e) {
192
 
      if (e.getButton() == MouseEvent.BUTTON1 && !(e.isControlDown())) {
193
 
         isSelecting = true;
194
 
         view.setLayer(this, Pipe.SELECTION_LAYER_OFFSET);
195
 
         startPoint = e.getPoint();
196
 
         selectionRectangle.setRect(startPoint.getX(), startPoint.getY(), 0, 0);
197
 
         // Select anything that intersects with the rectangle.
198
 
         processSelection(e);
199
 
         repaint();
200
 
      } else {
201
 
         startPoint = e.getPoint();
202
 
      }
203
 
   }
204
 
 
205
 
   
206
 
   /* (non-Javadoc)
207
 
    * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
208
 
    */
209
 
   public void mouseReleased(MouseEvent e) {
210
 
      if (isSelecting == true) {
211
 
         // Select anything that intersects with the rectangle.
212
 
         processSelection(e);
213
 
         isSelecting = false;
214
 
         view.setLayer(this, Pipe.LOWEST_LAYER_OFFSET);
215
 
         selectionRectangle.setRect(-1, -1, 0, 0);
216
 
         repaint();
217
 
      }
218
 
   }
219
 
 
220
 
   
221
 
   /* (non-Javadoc)
222
 
    * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
223
 
    */
224
 
   public void mouseDragged(MouseEvent e) {
225
 
      if (isSelecting) {
226
 
         selectionRectangle.setSize(
227
 
                  (int)Math.abs(e.getX() - startPoint.getX()),
228
 
                  (int)Math.abs(e.getY() - startPoint.getY()));
229
 
         selectionRectangle.setLocation(
230
 
                  (int)Math.min(startPoint.getX(), e.getX()),
231
 
                  (int)Math.min(startPoint.getY(), e.getY()));
232
 
         // Select anything that intersects with the rectangle.
233
 
         processSelection(e);
234
 
         repaint();
235
 
      } else {   
236
 
         view.drag(startPoint, e.getPoint());
237
 
      }
238
 
   }
239
 
 
240
 
 
241
 
   public void mouseWheelMoved(MouseWheelEvent e) {
242
 
      if (e.isControlDown()) {
243
 
         if (e.getWheelRotation()> 0) {
244
 
            view.zoomIn();
245
 
         } else {
246
 
            view.zoomOut();
247
 
         }
248
 
      }
249
 
   }   
250
 
   
251
 
   public void mouseClicked(MouseEvent e) {
252
 
      ;// Not needed
253
 
   }
254
 
 
255
 
   
256
 
   public void mouseEntered(MouseEvent e) {
257
 
      ; // Not needed
258
 
   }
259
 
 
260
 
   
261
 
   public void mouseExited(MouseEvent e) {
262
 
      ; // Not needed
263
 
   }
264
 
 
265
 
   
266
 
   /* (non-Javadoc)
267
 
    * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
268
 
    */
269
 
   public void mouseMoved(MouseEvent e) {
270
 
      ; // Not needed
271
 
   }   
272
 
   
273
 
   
274
 
   public int getSelectionCount() {
275
 
      Component netObj[] = view.getComponents();
276
 
      int selectionCount = 0;
277
 
      // Get all the objects in the current window
278
 
      for (int i=0; i< netObj.length; i++) {
279
 
         // Handle Arcs and Arc Points
280
 
         if ((netObj[i] instanceof Arc) && ((PetriNetObject)netObj[i]).isSelectable())  {
281
 
            Arc thisArc = (Arc)netObj[i];
282
 
            ArcPath thisArcPath = thisArc.getArcPath();
283
 
            for (int j=1; j < thisArcPath.getEndIndex(); j++){
284
 
               if (thisArcPath.isPointSelected(j)) {
285
 
                  selectionCount++;
286
 
               }
287
 
            }
288
 
         }
289
 
         
290
 
         // Handle PlaceTransition Objects
291
 
         if ((netObj[i] instanceof PlaceTransitionObject) && 
292
 
                 ((PetriNetObject)netObj[i]).isSelectable()) {
293
 
            if (((PlaceTransitionObject)netObj[i]).isSelected()) {
294
 
               selectionCount++;
295
 
            }
296
 
         }
297
 
      }
298
 
      return selectionCount;
299
 
   }
 
114
 
 
115
                if (transX == 0 && transY == 0) {
 
116
                        return;
 
117
                }
 
118
 
 
119
                // First see if translation will put anything at a negative location
 
120
                Point point = null;
 
121
                Point topleft = null;
 
122
 
 
123
                // Get all the objects in the current window
 
124
                ArrayList<PetriNetObject> pnObjects = drawingSurface.getPNObjects();
 
125
                for (PetriNetObject pnObject : pnObjects) {
 
126
                        if (pnObject.isSelected()) {
 
127
                                point = pnObject.getLocation();
 
128
                                if (topleft == null) {
 
129
                                        topleft = point;
 
130
                                } else {
 
131
                                        if (point.x < topleft.x) {
 
132
                                                topleft.x = point.x;
 
133
                                        }
 
134
                                        if (point.y < topleft.y) {
 
135
                                                topleft.y = point.y;
 
136
                                        }
 
137
                                }
 
138
                        }
 
139
                }
 
140
 
 
141
                if (topleft != null) {
 
142
                        topleft.translate(transX, transY);
 
143
                        if (topleft.x < 0) {
 
144
                                transX -= topleft.x;
 
145
                        }
 
146
                        if (topleft.y < 0) {
 
147
                                transY -= topleft.y;
 
148
                        }
 
149
                        if (transX == 0 && transY == 0) {
 
150
                                return;
 
151
                        }
 
152
                }
 
153
 
 
154
                for (PetriNetObject pnObject : pnObjects) {
 
155
                        if (pnObject.isSelected()) {
 
156
                                pnObject.translate(transX, transY);
 
157
                        }
 
158
                }
 
159
                drawingSurface.updatePreferredSize();
 
160
        }
 
161
 
 
162
        public ArrayList<PetriNetObject> getSelection() {
 
163
                ArrayList<PetriNetObject> selection = new ArrayList<PetriNetObject>();
 
164
 
 
165
                // Get all the objects in the current window
 
166
                ArrayList<PetriNetObject> pnObjects = drawingSurface.getPNObjects();
 
167
                for (PetriNetObject pnObject : pnObjects) {
 
168
                        if (pnObject.isSelected()) {
 
169
                                selection.add(pnObject);
 
170
                        }
 
171
                }
 
172
                return selection;
 
173
        }
 
174
 
 
175
        /*
 
176
         * (non-Javadoc)
 
177
         * 
 
178
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
 
179
         */
 
180
        public void mousePressed(MouseEvent e) {
 
181
                if (e.getButton() == MouseEvent.BUTTON1 && !(e.isControlDown())) {
 
182
                        isSelecting = true;
 
183
                        drawingSurface.setLayer(this, Pipe.SELECTION_LAYER_OFFSET);
 
184
                        startPoint = e.getPoint();
 
185
                        selectionRectangle.setRect(startPoint.getX(), startPoint.getY(), 0,
 
186
                                        0);
 
187
                        // Select anything that intersects with the rectangle.
 
188
                        processSelection(e);
 
189
                        repaint();
 
190
                } else {
 
191
                        startPoint = e.getPoint();
 
192
                }
 
193
        }
 
194
 
 
195
        /*
 
196
         * (non-Javadoc)
 
197
         * 
 
198
         * @see
 
199
         * java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
 
200
         */
 
201
        public void mouseReleased(MouseEvent e) {
 
202
                if (isSelecting == true) {
 
203
                        // Select anything that intersects with the rectangle.
 
204
                        processSelection(e);
 
205
                        isSelecting = false;
 
206
                        drawingSurface.setLayer(this, Pipe.LOWEST_LAYER_OFFSET);
 
207
                        selectionRectangle.setRect(-1, -1, 0, 0);
 
208
                        repaint();
 
209
                }
 
210
        }
 
211
 
 
212
        /*
 
213
         * (non-Javadoc)
 
214
         * 
 
215
         * @see
 
216
         * java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent
 
217
         * )
 
218
         */
 
219
        public void mouseDragged(MouseEvent e) {
 
220
                if (isSelecting) {
 
221
                        selectionRectangle.setSize((int) Math.abs(e.getX()
 
222
                                        - startPoint.getX()), (int) Math.abs(e.getY()
 
223
                                        - startPoint.getY()));
 
224
                        selectionRectangle.setLocation((int) Math.min(startPoint.getX(), e
 
225
                                        .getX()), (int) Math.min(startPoint.getY(), e.getY()));
 
226
                        // Select anything that intersects with the rectangle.
 
227
                        processSelection(e);
 
228
                        repaint();
 
229
                } else {
 
230
                        drawingSurface.drag(startPoint, e.getPoint());
 
231
                }
 
232
        }
 
233
 
 
234
        public void mouseWheelMoved(MouseWheelEvent e) {
 
235
                if (e.isControlDown()) {
 
236
                        if (e.getWheelRotation() > 0) {
 
237
                                drawingSurface.zoomIn();
 
238
                        } else {
 
239
                                drawingSurface.zoomOut();
 
240
                        }
 
241
                }
 
242
        }
 
243
 
 
244
        public void mouseClicked(MouseEvent e) {
 
245
                ;// Not needed
 
246
        }
 
247
 
 
248
        public void mouseEntered(MouseEvent e) {
 
249
                ; // Not needed
 
250
        }
 
251
 
 
252
        public void mouseExited(MouseEvent e) {
 
253
                ; // Not needed
 
254
        }
 
255
 
 
256
        /*
 
257
         * (non-Javadoc)
 
258
         * 
 
259
         * @see
 
260
         * java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
 
261
         */
 
262
        public void mouseMoved(MouseEvent e) {
 
263
                ; // Not needed
 
264
        }
 
265
 
 
266
        public int getSelectionCount() {
 
267
                Component netObj[] = drawingSurface.getComponents();
 
268
                int selectionCount = 0;
 
269
                // Get all the objects in the current window
 
270
                for (int i = 0; i < netObj.length; i++) {
 
271
                        // Handle Arcs and Arc Points
 
272
                        if ((netObj[i] instanceof Arc)
 
273
                                        && ((PetriNetObject) netObj[i]).isSelectable()) {
 
274
                                Arc thisArc = (Arc) netObj[i];
 
275
                                ArcPath thisArcPath = thisArc.getArcPath();
 
276
                                for (int j = 1; j < thisArcPath.getEndIndex(); j++) {
 
277
                                        if (thisArcPath.isPointSelected(j)) {
 
278
                                                selectionCount++;
 
279
                                        }
 
280
                                }
 
281
                        }
 
282
 
 
283
                        // Handle PlaceTransition Objects
 
284
                        if ((netObj[i] instanceof PlaceTransitionObject)
 
285
                                        && ((PetriNetObject) netObj[i]).isSelectable()) {
 
286
                                if (((PlaceTransitionObject) netObj[i]).isSelected()) {
 
287
                                        selectionCount++;
 
288
                                }
 
289
                        }
 
290
                }
 
291
                return selectionCount;
 
292
        }
300
293
 
301
294
}