766
|
1 |
/*
|
|
2 |
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
package com.sun.hotspot.igv.view;
|
|
25 |
|
|
26 |
import com.sun.hotspot.igv.view.widgets.BlockWidget;
|
|
27 |
import com.sun.hotspot.igv.view.widgets.LineWidget;
|
|
28 |
import com.sun.hotspot.igv.util.DoubleClickAction;
|
|
29 |
import com.sun.hotspot.igv.data.InputBlock;
|
|
30 |
import com.sun.hotspot.igv.data.InputNode;
|
|
31 |
import com.sun.hotspot.igv.graph.Connection;
|
|
32 |
import com.sun.hotspot.igv.graph.Diagram;
|
|
33 |
import com.sun.hotspot.igv.graph.Figure;
|
|
34 |
import com.sun.hotspot.igv.graph.InputSlot;
|
|
35 |
import com.sun.hotspot.igv.graph.OutputSlot;
|
|
36 |
import com.sun.hotspot.igv.graph.Slot;
|
|
37 |
import com.sun.hotspot.igv.hierarchicallayout.HierarchicalClusterLayoutManager;
|
|
38 |
import com.sun.hotspot.igv.hierarchicallayout.OldHierarchicalLayoutManager;
|
|
39 |
import com.sun.hotspot.igv.hierarchicallayout.HierarchicalLayoutManager;
|
|
40 |
import com.sun.hotspot.igv.view.widgets.FigureWidget;
|
|
41 |
import com.sun.hotspot.igv.view.widgets.InputSlotWidget;
|
|
42 |
import com.sun.hotspot.igv.view.widgets.OutputSlotWidget;
|
|
43 |
import com.sun.hotspot.igv.view.widgets.SlotWidget;
|
|
44 |
import com.sun.hotspot.igv.layout.LayoutGraph;
|
|
45 |
import com.sun.hotspot.igv.data.services.Scheduler;
|
|
46 |
import com.sun.hotspot.igv.data.ChangedListener;
|
|
47 |
import com.sun.hotspot.igv.graph.Block;
|
|
48 |
import com.sun.hotspot.igv.util.ColorIcon;
|
|
49 |
import com.sun.hotspot.igv.util.ExtendedSelectAction;
|
|
50 |
import java.awt.Color;
|
|
51 |
import java.awt.Dimension;
|
|
52 |
import java.awt.Point;
|
|
53 |
import java.awt.Rectangle;
|
|
54 |
import java.awt.event.ActionEvent;
|
|
55 |
import java.awt.event.FocusEvent;
|
|
56 |
import java.awt.event.FocusListener;
|
|
57 |
import java.awt.event.MouseEvent;
|
|
58 |
import java.awt.event.MouseListener;
|
|
59 |
import java.awt.event.MouseMotionListener;
|
|
60 |
import java.awt.event.MouseWheelEvent;
|
|
61 |
import java.awt.event.MouseWheelListener;
|
|
62 |
import java.util.ArrayList;
|
|
63 |
import java.util.Collection;
|
|
64 |
import java.util.HashMap;
|
|
65 |
import java.util.HashSet;
|
1497
|
66 |
import java.util.HashMap;
|
766
|
67 |
import java.util.List;
|
|
68 |
import java.util.Map;
|
|
69 |
import java.util.Set;
|
|
70 |
import javax.swing.AbstractAction;
|
|
71 |
import javax.swing.Action;
|
|
72 |
import javax.swing.BorderFactory;
|
|
73 |
import javax.swing.JComponent;
|
|
74 |
import javax.swing.JPopupMenu;
|
|
75 |
import javax.swing.JScrollPane;
|
|
76 |
import javax.swing.SwingUtilities;
|
|
77 |
import javax.swing.event.ChangeEvent;
|
|
78 |
import javax.swing.event.ChangeListener;
|
|
79 |
import javax.swing.event.UndoableEditEvent;
|
|
80 |
import javax.swing.undo.AbstractUndoableEdit;
|
|
81 |
import javax.swing.undo.CannotRedoException;
|
|
82 |
import javax.swing.undo.CannotUndoException;
|
|
83 |
import org.netbeans.api.visual.action.ActionFactory;
|
|
84 |
import org.netbeans.api.visual.action.PopupMenuProvider;
|
|
85 |
import org.netbeans.api.visual.action.RectangularSelectDecorator;
|
|
86 |
import org.netbeans.api.visual.action.RectangularSelectProvider;
|
|
87 |
import org.netbeans.api.visual.action.SelectProvider;
|
|
88 |
import org.netbeans.api.visual.action.WidgetAction;
|
|
89 |
import org.netbeans.api.visual.animator.SceneAnimator;
|
|
90 |
import org.netbeans.api.visual.layout.LayoutFactory;
|
|
91 |
import org.netbeans.api.visual.widget.ConnectionWidget;
|
|
92 |
import org.netbeans.api.visual.widget.LayerWidget;
|
|
93 |
import org.netbeans.api.visual.widget.Scene;
|
|
94 |
import org.netbeans.api.visual.widget.Widget;
|
|
95 |
import org.netbeans.api.visual.widget.LabelWidget;
|
|
96 |
import org.openide.awt.UndoRedo;
|
|
97 |
import org.openide.util.Lookup;
|
|
98 |
import org.openide.util.lookup.AbstractLookup;
|
|
99 |
import org.openide.util.lookup.InstanceContent;
|
|
100 |
|
|
101 |
/**
|
|
102 |
*
|
|
103 |
* @author Thomas Wuerthinger
|
|
104 |
*/
|
|
105 |
public class DiagramScene extends Scene implements ChangedListener<DiagramViewModel> {
|
|
106 |
|
1497
|
107 |
private HashMap<Figure, FigureWidget> figureWidgets;
|
|
108 |
private HashMap<Slot, SlotWidget> slotWidgets;
|
|
109 |
private HashMap<Connection, ConnectionWidget> connectionWidgets;
|
|
110 |
private HashMap<InputBlock, BlockWidget> blockWidgets;
|
766
|
111 |
private Widget hoverWidget;
|
|
112 |
private WidgetAction hoverAction;
|
|
113 |
private List<FigureWidget> selectedWidgets;
|
|
114 |
private Lookup lookup;
|
|
115 |
private InstanceContent content;
|
|
116 |
private Action[] actions;
|
|
117 |
private LayerWidget connectionLayer;
|
|
118 |
private JScrollPane scrollPane;
|
|
119 |
private UndoRedo.Manager undoRedoManager;
|
|
120 |
private LayerWidget mainLayer;
|
|
121 |
private LayerWidget slotLayer;
|
|
122 |
private LayerWidget blockLayer;
|
|
123 |
private double realZoomFactor;
|
|
124 |
private BoundedZoomAction zoomAction;
|
|
125 |
private WidgetAction panAction;
|
|
126 |
private Widget topLeft;
|
|
127 |
private Widget bottomRight;
|
|
128 |
private LayerWidget startLayer;
|
|
129 |
private LabelWidget startLabel;
|
|
130 |
private DiagramViewModel model;
|
|
131 |
private DiagramViewModel modelCopy;
|
|
132 |
public static final int AFTER = 1;
|
|
133 |
public static final int BEFORE = 1;
|
|
134 |
public static final float ALPHA = 0.4f;
|
|
135 |
public static final int GRID_SIZE = 30;
|
|
136 |
public static final int BORDER_SIZE = 20;
|
|
137 |
public static final int UNDOREDO_LIMIT = 100;
|
|
138 |
public static final int SCROLL_UNIT_INCREMENT = 80;
|
|
139 |
public static final int SCROLL_BLOCK_INCREMENT = 400;
|
|
140 |
public static final float ZOOM_MAX_FACTOR = 3.0f;
|
|
141 |
public static final float ZOOM_MIN_FACTOR = 0.0f;//0.15f;
|
|
142 |
public static final float ZOOM_INCREMENT = 1.5f;
|
|
143 |
public static final int SLOT_OFFSET = 6;
|
|
144 |
public static final int ANIMATION_LIMIT = 40;
|
|
145 |
private PopupMenuProvider popupMenuProvider = new PopupMenuProvider() {
|
|
146 |
|
|
147 |
public JPopupMenu getPopupMenu(Widget widget, Point localLocation) {
|
|
148 |
return DiagramScene.this.createPopupMenu();
|
|
149 |
}
|
|
150 |
};
|
|
151 |
private RectangularSelectDecorator rectangularSelectDecorator = new RectangularSelectDecorator() {
|
|
152 |
|
|
153 |
public Widget createSelectionWidget() {
|
|
154 |
Widget widget = new Widget(DiagramScene.this);
|
|
155 |
widget.setBorder(BorderFactory.createLineBorder(Color.black, 2));
|
|
156 |
widget.setForeground(Color.red);
|
|
157 |
return widget;
|
|
158 |
}
|
|
159 |
};
|
|
160 |
private RectangularSelectProvider rectangularSelectProvider = new RectangularSelectProvider() {
|
|
161 |
|
|
162 |
public void performSelection(Rectangle rectangle) {
|
|
163 |
if (rectangle.width < 0) {
|
|
164 |
rectangle.x += rectangle.width;
|
|
165 |
rectangle.width *= -1;
|
|
166 |
}
|
|
167 |
|
|
168 |
if (rectangle.height < 0) {
|
|
169 |
rectangle.y += rectangle.height;
|
|
170 |
rectangle.height *= -1;
|
|
171 |
}
|
|
172 |
|
|
173 |
boolean updated = false;
|
|
174 |
for (Figure f : getModel().getDiagramToView().getFigures()) {
|
|
175 |
FigureWidget w = figureWidgets.get(f);
|
|
176 |
Rectangle r = new Rectangle(w.getBounds());
|
|
177 |
r.setLocation(w.getLocation());
|
|
178 |
if (r.intersects(rectangle)) {
|
|
179 |
if (!selectedWidgets.contains(w)) {
|
|
180 |
addToSelection(w);
|
|
181 |
updated = true;
|
|
182 |
}
|
|
183 |
} else {
|
|
184 |
if (selectedWidgets.contains(w)) {
|
|
185 |
selectedWidgets.remove(w);
|
|
186 |
content.remove(w.getNode());
|
|
187 |
w.setState(w.getState().deriveSelected(false));
|
|
188 |
updated = true;
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
if (updated) {
|
|
194 |
selectionUpdated();
|
|
195 |
}
|
|
196 |
}
|
|
197 |
};
|
|
198 |
private SelectProvider selectProvider = new SelectProvider() {
|
|
199 |
|
|
200 |
public boolean isAimingAllowed(Widget widget, Point point, boolean b) {
|
|
201 |
return false;
|
|
202 |
}
|
|
203 |
|
|
204 |
public boolean isSelectionAllowed(Widget widget, Point point, boolean b) {
|
|
205 |
return widget instanceof FigureWidget || widget == DiagramScene.this;
|
|
206 |
}
|
|
207 |
|
|
208 |
public void select(Widget w, Point point, boolean change) {
|
|
209 |
|
|
210 |
boolean updated = false;
|
|
211 |
|
|
212 |
if (w == DiagramScene.this) {
|
|
213 |
if (DiagramScene.this.selectedWidgets.size() != 0) {
|
|
214 |
clearSelection();
|
|
215 |
selectionUpdated();
|
|
216 |
}
|
|
217 |
return;
|
|
218 |
}
|
|
219 |
|
|
220 |
FigureWidget widget = (FigureWidget) w;
|
|
221 |
|
|
222 |
|
|
223 |
if (change) {
|
|
224 |
if (widget.getState().isSelected()) {
|
|
225 |
assert selectedWidgets.contains(widget);
|
|
226 |
widget.setState(widget.getState().deriveSelected(false));
|
|
227 |
selectedWidgets.remove(widget);
|
|
228 |
content.remove(widget.getNode());
|
|
229 |
updated = true;
|
|
230 |
} else {
|
|
231 |
assert !selectedWidgets.contains(widget);
|
|
232 |
addToSelection(widget);
|
|
233 |
updated = true;
|
|
234 |
assert widget.getState().isSelected();
|
|
235 |
}
|
|
236 |
} else {
|
|
237 |
|
|
238 |
if (widget.getState().isSelected()) {
|
|
239 |
assert selectedWidgets.contains(widget);
|
|
240 |
} else {
|
|
241 |
|
|
242 |
assert !selectedWidgets.contains(widget);
|
|
243 |
clearSelection();
|
|
244 |
addToSelection(widget);
|
|
245 |
updated = true;
|
|
246 |
assert widget.getState().isSelected();
|
|
247 |
}
|
|
248 |
}
|
|
249 |
|
|
250 |
if (updated) {
|
|
251 |
selectionUpdated();
|
|
252 |
}
|
|
253 |
|
|
254 |
}
|
|
255 |
};
|
|
256 |
|
|
257 |
private FigureWidget getFigureWidget(Figure f) {
|
|
258 |
return figureWidgets.get(f);
|
|
259 |
}
|
|
260 |
private FocusListener focusListener = new FocusListener() {
|
|
261 |
|
|
262 |
public void focusGained(FocusEvent e) {
|
|
263 |
DiagramScene.this.getView().requestFocus();
|
|
264 |
}
|
|
265 |
|
|
266 |
public void focusLost(FocusEvent e) {
|
|
267 |
}
|
|
268 |
};
|
|
269 |
private MouseWheelListener mouseWheelListener = new MouseWheelListener() {
|
|
270 |
|
|
271 |
public void mouseWheelMoved(MouseWheelEvent e) {
|
|
272 |
DiagramScene.this.zoomAction.mouseWheelMoved(DiagramScene.this, new WidgetAction.WidgetMouseWheelEvent(0, e));
|
|
273 |
DiagramScene.this.validate();
|
|
274 |
}
|
|
275 |
};
|
|
276 |
private MouseListener mouseListener = new MouseListener() {
|
|
277 |
|
|
278 |
public void mouseClicked(MouseEvent e) {
|
|
279 |
DiagramScene.this.panAction.mouseClicked(DiagramScene.this, new WidgetAction.WidgetMouseEvent(0, e));
|
|
280 |
}
|
|
281 |
|
|
282 |
public void mousePressed(MouseEvent e) {
|
|
283 |
DiagramScene.this.panAction.mousePressed(DiagramScene.this, new WidgetAction.WidgetMouseEvent(0, e));
|
|
284 |
}
|
|
285 |
|
|
286 |
public void mouseReleased(MouseEvent e) {
|
|
287 |
DiagramScene.this.panAction.mouseReleased(DiagramScene.this, new WidgetAction.WidgetMouseEvent(0, e));
|
|
288 |
}
|
|
289 |
|
|
290 |
public void mouseEntered(MouseEvent e) {
|
|
291 |
DiagramScene.this.panAction.mouseEntered(DiagramScene.this, new WidgetAction.WidgetMouseEvent(0, e));
|
|
292 |
}
|
|
293 |
|
|
294 |
public void mouseExited(MouseEvent e) {
|
|
295 |
DiagramScene.this.panAction.mouseExited(DiagramScene.this, new WidgetAction.WidgetMouseEvent(0, e));
|
|
296 |
}
|
|
297 |
};
|
|
298 |
private MouseMotionListener mouseMotionListener = new MouseMotionListener() {
|
|
299 |
|
|
300 |
public void mouseDragged(MouseEvent e) {
|
|
301 |
DiagramScene.this.panAction.mouseDragged(DiagramScene.this, new WidgetAction.WidgetMouseEvent(0, e));
|
|
302 |
}
|
|
303 |
|
|
304 |
public void mouseMoved(MouseEvent e) {
|
|
305 |
}
|
|
306 |
};
|
|
307 |
private ScrollChangeListener scrollChangeListener = new ScrollChangeListener();
|
|
308 |
|
|
309 |
private class ScrollChangeListener implements ChangeListener {
|
|
310 |
|
|
311 |
private Map<Widget, Point> relativePositions = new HashMap<Widget, Point>();
|
|
312 |
private Point oldPosition;
|
|
313 |
|
|
314 |
public void register(Widget w, Point p) {
|
|
315 |
relativePositions.put(w, p);
|
|
316 |
}
|
|
317 |
|
|
318 |
public void unregister(Widget w) {
|
|
319 |
relativePositions.remove(w);
|
|
320 |
}
|
|
321 |
|
|
322 |
public void stateChanged(ChangeEvent e) {
|
|
323 |
Point p = DiagramScene.this.getScrollPane().getViewport().getViewPosition();
|
|
324 |
if (oldPosition == null || !p.equals(oldPosition)) {
|
|
325 |
for (Widget w : relativePositions.keySet()) {
|
|
326 |
Point curPoint = relativePositions.get(w);
|
|
327 |
Point newPoint = new Point(p.x + curPoint.x, p.y + curPoint.y);
|
|
328 |
w.setPreferredLocation(newPoint);
|
|
329 |
DiagramScene.this.validate();
|
|
330 |
}
|
|
331 |
oldPosition = p;
|
|
332 |
}
|
|
333 |
}
|
|
334 |
}
|
|
335 |
|
|
336 |
public Point getScrollPosition() {
|
|
337 |
return getScrollPane().getViewport().getViewPosition();
|
|
338 |
}
|
|
339 |
|
|
340 |
public void setScrollPosition(Point p) {
|
|
341 |
getScrollPane().getViewport().setViewPosition(p);
|
|
342 |
}
|
|
343 |
|
|
344 |
public DiagramScene(Action[] actions, DiagramViewModel model) {
|
|
345 |
this.actions = actions;
|
|
346 |
selectedWidgets = new ArrayList<FigureWidget>();
|
|
347 |
content = new InstanceContent();
|
|
348 |
lookup = new AbstractLookup(content);
|
|
349 |
this.setCheckClipping(true);
|
|
350 |
this.getInputBindings().setZoomActionModifiers(0);
|
|
351 |
|
|
352 |
JComponent comp = this.createView();
|
|
353 |
comp.setDoubleBuffered(true);
|
|
354 |
comp.setBackground(Color.WHITE);
|
|
355 |
comp.setOpaque(true);
|
|
356 |
|
|
357 |
this.setBackground(Color.WHITE);
|
|
358 |
this.setOpaque(true);
|
|
359 |
scrollPane = new JScrollPane(comp);
|
|
360 |
scrollPane.setBackground(Color.WHITE);
|
|
361 |
scrollPane.getVerticalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
|
|
362 |
scrollPane.getVerticalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
|
|
363 |
scrollPane.getHorizontalScrollBar().setUnitIncrement(SCROLL_UNIT_INCREMENT);
|
|
364 |
scrollPane.getHorizontalScrollBar().setBlockIncrement(SCROLL_BLOCK_INCREMENT);
|
|
365 |
scrollPane.getViewport().addChangeListener(scrollChangeListener);
|
|
366 |
hoverAction = this.createWidgetHoverAction();
|
|
367 |
|
|
368 |
blockLayer = new LayerWidget(this);
|
|
369 |
this.addChild(blockLayer);
|
|
370 |
|
|
371 |
startLayer = new LayerWidget(this);
|
|
372 |
this.addChild(startLayer);
|
|
373 |
// TODO: String startLabelString = "Loading graph with " + originalDiagram.getFigures().size() + " figures and " + originalDiagram.getConnections().size() + " connections...";
|
|
374 |
String startLabelString = "";
|
|
375 |
LabelWidget w = new LabelWidget(this, startLabelString);
|
|
376 |
scrollChangeListener.register(w, new Point(10, 10));
|
|
377 |
w.setAlignment(LabelWidget.Alignment.CENTER);
|
|
378 |
startLabel = w;
|
|
379 |
startLayer.addChild(w);
|
|
380 |
|
|
381 |
mainLayer = new LayerWidget(this);
|
|
382 |
this.addChild(mainLayer);
|
|
383 |
|
|
384 |
topLeft = new Widget(this);
|
|
385 |
topLeft.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
|
|
386 |
this.addChild(topLeft);
|
|
387 |
|
|
388 |
|
|
389 |
bottomRight = new Widget(this);
|
|
390 |
bottomRight.setPreferredLocation(new Point(-BORDER_SIZE, -BORDER_SIZE));
|
|
391 |
this.addChild(bottomRight);
|
|
392 |
|
|
393 |
slotLayer = new LayerWidget(this);
|
|
394 |
this.addChild(slotLayer);
|
|
395 |
|
|
396 |
connectionLayer = new LayerWidget(this);
|
|
397 |
this.addChild(connectionLayer);
|
|
398 |
|
|
399 |
LayerWidget selectionLayer = new LayerWidget(this);
|
|
400 |
this.addChild(selectionLayer);
|
|
401 |
|
|
402 |
this.setLayout(LayoutFactory.createAbsoluteLayout());
|
|
403 |
|
|
404 |
this.getActions().addAction(hoverAction);
|
|
405 |
zoomAction = new BoundedZoomAction(1.1, false);
|
|
406 |
zoomAction.setMaxFactor(ZOOM_MAX_FACTOR);
|
|
407 |
zoomAction.setMinFactor(ZOOM_MIN_FACTOR);
|
|
408 |
this.getActions().addAction(ActionFactory.createMouseCenteredZoomAction(1.1));
|
|
409 |
panAction = new ExtendedPanAction();
|
|
410 |
this.getActions().addAction(panAction);
|
|
411 |
this.getActions().addAction(ActionFactory.createPopupMenuAction(popupMenuProvider));
|
|
412 |
|
|
413 |
LayerWidget selectLayer = new LayerWidget(this);
|
|
414 |
this.addChild(selectLayer);
|
|
415 |
this.getActions().addAction(ActionFactory.createRectangularSelectAction(rectangularSelectDecorator, selectLayer, rectangularSelectProvider));
|
|
416 |
|
1497
|
417 |
blockWidgets = new HashMap<InputBlock, BlockWidget>();
|
766
|
418 |
|
|
419 |
boolean b = this.getUndoRedoEnabled();
|
|
420 |
this.setUndoRedoEnabled(false);
|
|
421 |
this.setNewModel(model);
|
|
422 |
this.setUndoRedoEnabled(b);
|
|
423 |
}
|
|
424 |
|
|
425 |
private void selectionUpdated() {
|
|
426 |
getModel().setSelectedNodes(this.getSelectedNodes());
|
|
427 |
addUndo();
|
|
428 |
}
|
|
429 |
|
|
430 |
public DiagramViewModel getModel() {
|
|
431 |
return model;
|
|
432 |
}
|
|
433 |
|
|
434 |
public void setRealZoomFactor(double d) {
|
|
435 |
this.realZoomFactor = d;
|
|
436 |
}
|
|
437 |
|
|
438 |
public double getRealZoomFactor() {
|
|
439 |
if (realZoomFactor == 0.0) {
|
|
440 |
return getZoomFactor();
|
|
441 |
} else {
|
|
442 |
return realZoomFactor;
|
|
443 |
}
|
|
444 |
}
|
|
445 |
|
|
446 |
public JScrollPane getScrollPane() {
|
|
447 |
return scrollPane;
|
|
448 |
}
|
|
449 |
|
|
450 |
public boolean isAllVisible() {
|
|
451 |
return getModel().getHiddenNodes().size() == 0;
|
|
452 |
}
|
|
453 |
|
|
454 |
public Action createGotoAction(final Figure f) {
|
|
455 |
final DiagramScene diagramScene = this;
|
|
456 |
Action a = new AbstractAction() {
|
|
457 |
|
|
458 |
public void actionPerformed(ActionEvent e) {
|
|
459 |
diagramScene.gotoFigure(f);
|
|
460 |
}
|
|
461 |
};
|
|
462 |
|
|
463 |
a.setEnabled(true);
|
|
464 |
a.putValue(Action.SMALL_ICON, new ColorIcon(f.getColor()));
|
|
465 |
String name = f.getLines()[0];
|
|
466 |
|
|
467 |
name += " (";
|
|
468 |
|
|
469 |
if (f.getCluster() != null) {
|
|
470 |
name += "B" + f.getCluster().toString();
|
|
471 |
}
|
|
472 |
if (!this.getFigureWidget(f).isVisible()) {
|
|
473 |
if (f.getCluster() != null) {
|
|
474 |
name += ", ";
|
|
475 |
}
|
|
476 |
name += "hidden";
|
|
477 |
}
|
|
478 |
name += ")";
|
|
479 |
a.putValue(Action.NAME, name);
|
|
480 |
return a;
|
|
481 |
}
|
|
482 |
|
|
483 |
public void setNewModel(DiagramViewModel model) {
|
|
484 |
if (this.model != null) {
|
|
485 |
this.model.getDiagramChangedEvent().removeListener(this);
|
|
486 |
this.model.getViewPropertiesChangedEvent().removeListener(this);
|
|
487 |
}
|
|
488 |
this.model = model;
|
|
489 |
|
|
490 |
if (this.model == null) {
|
|
491 |
this.modelCopy = null;
|
|
492 |
} else {
|
|
493 |
this.modelCopy = this.model.copy();
|
|
494 |
}
|
|
495 |
|
|
496 |
model.getDiagramChangedEvent().addListener(this);
|
|
497 |
model.getViewPropertiesChangedEvent().addListener(this);
|
|
498 |
|
|
499 |
update();
|
|
500 |
}
|
|
501 |
|
|
502 |
private void update() {
|
|
503 |
|
|
504 |
/*if (startLabel != null) {
|
|
505 |
// Animate fade-out
|
|
506 |
final LabelWidget labelWidget = this.startLabel;
|
|
507 |
labelWidget.setVisible(true);
|
|
508 |
RequestProcessor.getDefault().post(new Runnable() {
|
|
509 |
public void run() {
|
|
510 |
final int Sleep = 200;
|
|
511 |
final int Progress = 10;
|
|
512 |
for (int i = 0; i < 255 / Progress + 1; i++) {
|
|
513 |
try {
|
|
514 |
SwingUtilities.invokeAndWait(new Runnable() {
|
|
515 |
public void run() {
|
|
516 |
Color c = labelWidget.getForeground();
|
|
517 |
int v = c.getRed();
|
|
518 |
v += Progress;
|
|
519 |
if (v > 255) {
|
|
520 |
v = 255;
|
|
521 |
}
|
|
522 |
labelWidget.setForeground(new Color(v, v, v, 255 - v));
|
|
523 |
labelWidget.getScene().validate();
|
|
524 |
}
|
|
525 |
});
|
|
526 |
} catch (InterruptedException ex) {
|
|
527 |
} catch (InvocationTargetException ex) {
|
|
528 |
}
|
|
529 |
try {
|
|
530 |
Thread.sleep(Sleep);
|
|
531 |
} catch (InterruptedException ex) {
|
|
532 |
}
|
|
533 |
}
|
|
534 |
labelWidget.setVisible(false);
|
|
535 |
DiagramScene.this.scrollChangeListener.unregister(labelWidget);
|
|
536 |
}
|
|
537 |
}, 1000);
|
|
538 |
startLabel = null;
|
|
539 |
}*/
|
|
540 |
|
|
541 |
slotLayer.removeChildren();
|
|
542 |
mainLayer.removeChildren();
|
|
543 |
blockLayer.removeChildren();
|
|
544 |
|
|
545 |
blockWidgets.clear();
|
1497
|
546 |
figureWidgets = new HashMap<Figure, FigureWidget>();
|
|
547 |
slotWidgets = new HashMap<Slot, SlotWidget>();
|
|
548 |
connectionWidgets = new HashMap<Connection, ConnectionWidget>();
|
766
|
549 |
|
|
550 |
WidgetAction selectAction = new ExtendedSelectAction(selectProvider);
|
|
551 |
Diagram d = getModel().getDiagramToView();
|
|
552 |
|
|
553 |
if (getModel().getShowBlocks()) {
|
|
554 |
Scheduler s = Lookup.getDefault().lookup(Scheduler.class);
|
|
555 |
Collection<InputBlock> newBlocks = new ArrayList<InputBlock>(s.schedule(d.getGraph()));
|
|
556 |
d.schedule(newBlocks);
|
|
557 |
}
|
|
558 |
|
|
559 |
for (Figure f : d.getFigures()) {
|
|
560 |
FigureWidget w = new FigureWidget(f, this, mainLayer);
|
|
561 |
w.getActions().addAction(selectAction);
|
|
562 |
w.getActions().addAction(hoverAction);
|
|
563 |
w.getActions().addAction(ActionFactory.createPopupMenuAction(w));
|
|
564 |
w.getActions().addAction(new DoubleClickAction(w));
|
|
565 |
w.setVisible(false);
|
|
566 |
|
|
567 |
figureWidgets.put(f, w);
|
|
568 |
|
|
569 |
for (InputSlot s : f.getInputSlots()) {
|
|
570 |
SlotWidget sw = new InputSlotWidget(s, this, slotLayer, w);
|
|
571 |
slotWidgets.put(s, sw);
|
|
572 |
sw.getActions().addAction(selectAction);
|
|
573 |
}
|
|
574 |
|
|
575 |
for (OutputSlot s : f.getOutputSlots()) {
|
|
576 |
SlotWidget sw = new OutputSlotWidget(s, this, slotLayer, w);
|
|
577 |
slotWidgets.put(s, sw);
|
|
578 |
sw.getActions().addAction(selectAction);
|
|
579 |
}
|
|
580 |
}
|
|
581 |
|
|
582 |
if (getModel().getShowBlocks()) {
|
|
583 |
for (InputBlock bn : d.getGraph().getBlocks()) {
|
|
584 |
BlockWidget w = new BlockWidget(this, d, bn);
|
|
585 |
w.setVisible(false);
|
|
586 |
blockWidgets.put(bn, w);
|
|
587 |
blockLayer.addChild(w);
|
|
588 |
}
|
|
589 |
}
|
|
590 |
|
|
591 |
this.smallUpdate(true);
|
|
592 |
|
|
593 |
}
|
|
594 |
|
|
595 |
private void smallUpdate(boolean relayout) {
|
|
596 |
|
|
597 |
this.updateHiddenNodes(model.getHiddenNodes(), relayout);
|
|
598 |
boolean b = this.getUndoRedoEnabled();
|
|
599 |
this.setUndoRedoEnabled(false);
|
|
600 |
this.setSelection(getModel().getSelectedNodes());
|
|
601 |
this.setUndoRedoEnabled(b);
|
|
602 |
this.validate();
|
|
603 |
}
|
|
604 |
|
|
605 |
private boolean isVisible(Connection c) {
|
|
606 |
FigureWidget w1 = figureWidgets.get(c.getInputSlot().getFigure());
|
|
607 |
FigureWidget w2 = figureWidgets.get(c.getOutputSlot().getFigure());
|
|
608 |
|
|
609 |
if (w1.isVisible() && w2.isVisible()) {
|
|
610 |
return true;
|
|
611 |
}
|
|
612 |
|
|
613 |
return false;
|
|
614 |
}
|
|
615 |
|
|
616 |
private void relayout(Set<Widget> oldVisibleWidgets) {
|
|
617 |
|
|
618 |
Diagram diagram = getModel().getDiagramToView();
|
|
619 |
|
|
620 |
HashSet<Figure> figures = new HashSet<Figure>();
|
|
621 |
|
|
622 |
for (Figure f : diagram.getFigures()) {
|
|
623 |
FigureWidget w = figureWidgets.get(f);
|
|
624 |
if (w.isVisible()) {
|
|
625 |
figures.add(f);
|
|
626 |
}
|
|
627 |
}
|
|
628 |
|
|
629 |
HashSet<Connection> edges = new HashSet<Connection>();
|
|
630 |
|
|
631 |
for (Connection c : diagram.getConnections()) {
|
|
632 |
if (isVisible(c)) {
|
|
633 |
edges.add(c);
|
|
634 |
}
|
|
635 |
}
|
|
636 |
|
|
637 |
if (getModel().getShowBlocks()) {
|
|
638 |
HierarchicalClusterLayoutManager m = new HierarchicalClusterLayoutManager(OldHierarchicalLayoutManager.Combine.SAME_OUTPUTS);
|
|
639 |
HierarchicalLayoutManager manager = new HierarchicalLayoutManager(HierarchicalLayoutManager.Combine.SAME_OUTPUTS);
|
|
640 |
manager.setMaxLayerLength(9);
|
|
641 |
manager.setMinLayerDifference(3);
|
|
642 |
m.setManager(manager);
|
|
643 |
m.setSubManager(new HierarchicalLayoutManager(HierarchicalLayoutManager.Combine.SAME_OUTPUTS));
|
|
644 |
m.doLayout(new LayoutGraph(edges, figures));
|
|
645 |
|
|
646 |
} else {
|
|
647 |
HierarchicalLayoutManager manager = new HierarchicalLayoutManager(HierarchicalLayoutManager.Combine.SAME_OUTPUTS);
|
|
648 |
manager.setMaxLayerLength(10);
|
|
649 |
manager.doLayout(new LayoutGraph(edges, figures));
|
|
650 |
}
|
|
651 |
|
|
652 |
int maxX = -BORDER_SIZE;
|
|
653 |
int maxY = -BORDER_SIZE;
|
|
654 |
for (Figure f : diagram.getFigures()) {
|
|
655 |
FigureWidget w = figureWidgets.get(f);
|
|
656 |
if (w.isVisible()) {
|
|
657 |
Point p = f.getPosition();
|
|
658 |
Dimension d = f.getSize();
|
|
659 |
maxX = Math.max(maxX, p.x + d.width);
|
|
660 |
maxY = Math.max(maxY, p.y + d.height);
|
|
661 |
}
|
|
662 |
}
|
|
663 |
|
|
664 |
for (Connection c : diagram.getConnections()) {
|
|
665 |
List<Point> points = c.getControlPoints();
|
|
666 |
FigureWidget w1 = figureWidgets.get((Figure) c.getTo().getVertex());
|
|
667 |
FigureWidget w2 = figureWidgets.get((Figure) c.getFrom().getVertex());
|
|
668 |
if (w1.isVisible() && w2.isVisible()) {
|
|
669 |
for (Point p : points) {
|
|
670 |
if (p != null) {
|
|
671 |
maxX = Math.max(maxX, p.x);
|
|
672 |
maxY = Math.max(maxY, p.y);
|
|
673 |
}
|
|
674 |
}
|
|
675 |
}
|
|
676 |
}
|
|
677 |
|
|
678 |
if (getModel().getShowBlocks()) {
|
|
679 |
for (Block b : diagram.getBlocks()) {
|
|
680 |
BlockWidget w = blockWidgets.get(b.getInputBlock());
|
|
681 |
if (w != null && w.isVisible()) {
|
|
682 |
Rectangle r = b.getBounds();
|
|
683 |
maxX = Math.max(maxX, r.x + r.width);
|
|
684 |
maxY = Math.max(maxY, r.y + r.height);
|
|
685 |
}
|
|
686 |
}
|
|
687 |
}
|
|
688 |
|
|
689 |
bottomRight.setPreferredLocation(new Point(maxX + BORDER_SIZE, maxY + BORDER_SIZE));
|
|
690 |
int offx = 0;
|
|
691 |
int offy = 0;
|
|
692 |
int curWidth = maxX + 2 * BORDER_SIZE;
|
|
693 |
int curHeight = maxY + 2 * BORDER_SIZE;
|
|
694 |
|
|
695 |
Rectangle bounds = this.getScrollPane().getBounds();
|
|
696 |
if (curWidth < bounds.width) {
|
|
697 |
offx = (bounds.width - curWidth) / 2;
|
|
698 |
}
|
|
699 |
|
|
700 |
if (curHeight < bounds.height) {
|
|
701 |
offy = (bounds.height - curHeight) / 2;
|
|
702 |
}
|
|
703 |
|
|
704 |
final int offx2 = offx;
|
|
705 |
final int offy2 = offy;
|
|
706 |
|
|
707 |
SceneAnimator animator = this.getSceneAnimator();
|
|
708 |
connectionLayer.removeChildren();
|
|
709 |
int visibleFigureCount = 0;
|
|
710 |
for (Figure f : diagram.getFigures()) {
|
|
711 |
if (figureWidgets.get(f).isVisible()) {
|
|
712 |
visibleFigureCount++;
|
|
713 |
}
|
|
714 |
}
|
|
715 |
|
|
716 |
for (Figure f : diagram.getFigures()) {
|
|
717 |
for (OutputSlot s : f.getOutputSlots()) {
|
|
718 |
SceneAnimator anim = animator;
|
|
719 |
if (visibleFigureCount > ANIMATION_LIMIT) {
|
|
720 |
anim = null;
|
|
721 |
}
|
|
722 |
processOutputSlot(s, s.getConnections(), 0, null, null, offx2, offy2, anim);
|
|
723 |
}
|
|
724 |
}
|
|
725 |
|
|
726 |
for (Figure f : diagram.getFigures()) {
|
|
727 |
FigureWidget w = figureWidgets.get(f);
|
|
728 |
if (w.isVisible()) {
|
|
729 |
Point p = f.getPosition();
|
|
730 |
Point p2 = new Point(p.x + offx2, p.y + offy2);
|
|
731 |
Rectangle r = new Rectangle(p.x + offx2, p.y + offy2, f.getSize().width, f.getSize().height);
|
|
732 |
if (oldVisibleWidgets.contains(w)) {
|
|
733 |
if (visibleFigureCount > ANIMATION_LIMIT) {
|
|
734 |
w.setPreferredLocation(p2);
|
|
735 |
} else {
|
|
736 |
animator.animatePreferredLocation(w, p2);
|
|
737 |
}
|
|
738 |
} else {
|
|
739 |
w.setPreferredLocation(p2);
|
|
740 |
}
|
|
741 |
}
|
|
742 |
}
|
|
743 |
|
|
744 |
if (getModel().getShowBlocks()) {
|
|
745 |
for (Block b : diagram.getBlocks()) {
|
|
746 |
BlockWidget w = blockWidgets.get(b.getInputBlock());
|
|
747 |
if (w != null && w.isVisible()) {
|
|
748 |
Point location = new Point(b.getBounds().x + offx2, b.getBounds().y + offy2);
|
|
749 |
Rectangle r = new Rectangle(location.x, location.y, b.getBounds().width, b.getBounds().height);
|
|
750 |
if (oldVisibleWidgets.contains(w)) {
|
|
751 |
if (visibleFigureCount > ANIMATION_LIMIT) {
|
|
752 |
w.setPreferredBounds(r);
|
|
753 |
} else {
|
|
754 |
animator.animatePreferredBounds(w, r);
|
|
755 |
}
|
|
756 |
} else {
|
|
757 |
w.setPreferredBounds(r);
|
|
758 |
}
|
|
759 |
}
|
|
760 |
}
|
|
761 |
}
|
|
762 |
}
|
|
763 |
private final Point specialNullPoint = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
|
|
764 |
|
|
765 |
private void processOutputSlot(OutputSlot s, List<Connection> connections, int controlPointIndex, Point lastPoint, LineWidget predecessor, int offx, int offy, SceneAnimator animator) {
|
|
766 |
Map<Point, List<Connection>> pointMap = new HashMap<Point, List<Connection>>();
|
|
767 |
|
|
768 |
for (Connection c : connections) {
|
|
769 |
|
|
770 |
if (!isVisible(c)) {
|
|
771 |
continue;
|
|
772 |
}
|
|
773 |
|
|
774 |
List<Point> controlPoints = c.getControlPoints();
|
|
775 |
if (controlPointIndex >= controlPoints.size()) {
|
|
776 |
continue;
|
|
777 |
}
|
|
778 |
|
|
779 |
Point cur = controlPoints.get(controlPointIndex);
|
|
780 |
if (cur == null) {
|
|
781 |
cur = specialNullPoint;
|
|
782 |
} else if (controlPointIndex == 0 && !s.getShowName()) {
|
|
783 |
cur = new Point(cur.x, cur.y - SLOT_OFFSET);
|
|
784 |
} else if (controlPointIndex == controlPoints.size() - 1 && !c.getInputSlot().getShowName()) {
|
|
785 |
cur = new Point(cur.x, cur.y + SLOT_OFFSET);
|
|
786 |
}
|
|
787 |
|
|
788 |
if (pointMap.containsKey(cur)) {
|
|
789 |
pointMap.get(cur).add(c);
|
|
790 |
} else {
|
|
791 |
List<Connection> newList = new ArrayList<Connection>(2);
|
|
792 |
newList.add(c);
|
|
793 |
pointMap.put(cur, newList);
|
|
794 |
}
|
|
795 |
|
|
796 |
}
|
|
797 |
|
|
798 |
for (Point p : pointMap.keySet()) {
|
|
799 |
List<Connection> connectionList = pointMap.get(p);
|
|
800 |
|
|
801 |
boolean isBold = false;
|
|
802 |
boolean isDashed = true;
|
|
803 |
|
|
804 |
for (Connection c : connectionList) {
|
|
805 |
|
|
806 |
if (c.getStyle() == Connection.ConnectionStyle.BOLD) {
|
|
807 |
isBold = true;
|
|
808 |
}
|
|
809 |
|
|
810 |
if (c.getStyle() != Connection.ConnectionStyle.DASHED) {
|
|
811 |
isDashed = false;
|
|
812 |
}
|
|
813 |
}
|
|
814 |
|
|
815 |
LineWidget newPredecessor = predecessor;
|
|
816 |
if (p == specialNullPoint) {
|
|
817 |
|
|
818 |
} else if (lastPoint == specialNullPoint) {
|
|
819 |
|
|
820 |
} else if (lastPoint != null) {
|
|
821 |
Point p1 = new Point(lastPoint.x + offx, lastPoint.y + offy);
|
|
822 |
Point p2 = new Point(p.x + offx, p.y + offy);
|
|
823 |
LineWidget w = new LineWidget(this, s, connectionList, p1, p2, predecessor, animator, isBold, isDashed);
|
|
824 |
newPredecessor = w;
|
|
825 |
connectionLayer.addChild(w);
|
|
826 |
w.getActions().addAction(hoverAction);
|
|
827 |
}
|
|
828 |
|
|
829 |
processOutputSlot(s, connectionList, controlPointIndex + 1, p, newPredecessor, offx, offy, animator);
|
|
830 |
}
|
|
831 |
}
|
|
832 |
|
|
833 |
private void clearSelection() {
|
|
834 |
if (selectedWidgets.size() == 0) {
|
|
835 |
return;
|
|
836 |
}
|
|
837 |
for (FigureWidget w : selectedWidgets) {
|
|
838 |
assert w.getState().isSelected();
|
|
839 |
w.setState(w.getState().deriveSelected(false));
|
|
840 |
content.remove(w.getNode());
|
|
841 |
}
|
|
842 |
selectedWidgets.clear();
|
|
843 |
}
|
|
844 |
|
|
845 |
public Lookup getLookup() {
|
|
846 |
return lookup;
|
|
847 |
}
|
|
848 |
|
|
849 |
public void gotoFigures(final List<Figure> figures) {
|
|
850 |
Rectangle overall = null;
|
|
851 |
showFigures(figures);
|
|
852 |
for (Figure f : figures) {
|
|
853 |
|
|
854 |
FigureWidget fw = getFigureWidget(f);
|
|
855 |
if (fw != null) {
|
|
856 |
Rectangle r = fw.getBounds();
|
|
857 |
Point p = fw.getLocation();
|
|
858 |
Rectangle r2 = new Rectangle(p.x, p.y, r.width, r.height);
|
|
859 |
|
|
860 |
if (overall == null) {
|
|
861 |
overall = r2;
|
|
862 |
} else {
|
|
863 |
overall = overall.union(r2);
|
|
864 |
}
|
|
865 |
}
|
|
866 |
}
|
|
867 |
if (overall != null) {
|
|
868 |
centerRectangle(overall);
|
|
869 |
}
|
|
870 |
}
|
|
871 |
|
|
872 |
private Point calcCenter(Rectangle r) {
|
|
873 |
|
|
874 |
Point center = new Point((int) r.getCenterX(), (int) r.getCenterY());
|
|
875 |
center.x -= getScrollPane().getViewport().getViewRect().width / 2;
|
|
876 |
center.y -= getScrollPane().getViewport().getViewRect().height / 2;
|
|
877 |
|
|
878 |
// Ensure to be within area
|
|
879 |
center.x = Math.max(0, center.x);
|
|
880 |
center.x = Math.min(getScrollPane().getViewport().getViewSize().width - getScrollPane().getViewport().getViewRect().width, center.x);
|
|
881 |
center.y = Math.max(0, center.y);
|
|
882 |
center.y = Math.min(getScrollPane().getViewport().getViewSize().height - getScrollPane().getViewport().getViewRect().height, center.y);
|
|
883 |
|
|
884 |
return center;
|
|
885 |
}
|
|
886 |
|
|
887 |
private void centerRectangle(Rectangle r) {
|
|
888 |
|
|
889 |
if (getScrollPane().getViewport().getViewRect().width == 0 || getScrollPane().getViewport().getViewRect().height == 0) {
|
|
890 |
return;
|
|
891 |
}
|
|
892 |
|
|
893 |
Rectangle r2 = new Rectangle(r.x, r.y, r.width, r.height);
|
|
894 |
r2 = convertSceneToView(r2);
|
|
895 |
|
|
896 |
double factorX = (double) r2.width / (double) getScrollPane().getViewport().getViewRect().width;
|
|
897 |
double factorY = (double) r2.height / (double) getScrollPane().getViewport().getViewRect().height;
|
|
898 |
double factor = Math.max(factorX, factorY);
|
|
899 |
if (factor >= 1.0) {
|
|
900 |
Point p = getScrollPane().getViewport().getViewPosition();
|
|
901 |
setZoomFactor(getZoomFactor() / factor);
|
|
902 |
r2.x /= factor;
|
|
903 |
r2.y /= factor;
|
|
904 |
r2.width /= factor;
|
|
905 |
r2.height /= factor;
|
|
906 |
getScrollPane().getViewport().setViewPosition(calcCenter(r2));
|
|
907 |
} else {
|
|
908 |
getScrollPane().getViewport().setViewPosition(calcCenter(r2));
|
|
909 |
}
|
|
910 |
}
|
|
911 |
|
|
912 |
private void addToSelection(Figure f) {
|
|
913 |
FigureWidget w = getFigureWidget(f);
|
|
914 |
addToSelection(w);
|
|
915 |
}
|
|
916 |
|
|
917 |
private void addToSelection(FigureWidget w) {
|
|
918 |
assert !selectedWidgets.contains(w);
|
|
919 |
selectedWidgets.add(w);
|
|
920 |
content.add(w.getNode());
|
|
921 |
w.setState(w.getState().deriveSelected(true));
|
|
922 |
}
|
|
923 |
|
|
924 |
private void setSelection(Set<Integer> nodes) {
|
|
925 |
clearSelection();
|
|
926 |
for (Figure f : getModel().getDiagramToView().getFigures()) {
|
|
927 |
if (doesIntersect(f.getSource().getSourceNodesAsSet(), nodes)) {
|
|
928 |
addToSelection(f);
|
|
929 |
}
|
|
930 |
}
|
|
931 |
selectionUpdated();
|
|
932 |
this.validate();
|
|
933 |
}
|
|
934 |
|
|
935 |
public void setSelection(Collection<Figure> list) {
|
|
936 |
clearSelection();
|
|
937 |
for (Figure f : list) {
|
|
938 |
addToSelection(f);
|
|
939 |
}
|
|
940 |
|
|
941 |
selectionUpdated();
|
|
942 |
this.validate();
|
|
943 |
}
|
|
944 |
|
|
945 |
public Set<Figure> getSelectedFigures() {
|
|
946 |
Set<Figure> result = new HashSet<Figure>();
|
|
947 |
for (Widget w : selectedWidgets) {
|
|
948 |
if (w instanceof FigureWidget) {
|
|
949 |
FigureWidget fw = (FigureWidget) w;
|
|
950 |
if (fw.getState().isSelected()) {
|
|
951 |
result.add(fw.getFigure());
|
|
952 |
}
|
|
953 |
}
|
|
954 |
}
|
|
955 |
return result;
|
|
956 |
}
|
|
957 |
|
|
958 |
public Set<Integer> getSelectedNodes() {
|
|
959 |
Set<Integer> result = new HashSet<Integer>();
|
|
960 |
for (Widget w : selectedWidgets) {
|
|
961 |
if (w instanceof FigureWidget) {
|
|
962 |
FigureWidget fw = (FigureWidget) w;
|
|
963 |
if (fw.getState().isSelected()) {
|
|
964 |
result.addAll(fw.getFigure().getSource().getSourceNodesAsSet());
|
|
965 |
}
|
|
966 |
}
|
|
967 |
}
|
|
968 |
return result;
|
|
969 |
}
|
|
970 |
|
|
971 |
private UndoRedo.Manager getUndoRedoManager() {
|
|
972 |
if (undoRedoManager == null) {
|
|
973 |
undoRedoManager = new UndoRedo.Manager();
|
|
974 |
undoRedoManager.setLimit(UNDOREDO_LIMIT);
|
|
975 |
}
|
|
976 |
|
|
977 |
return undoRedoManager;
|
|
978 |
}
|
|
979 |
|
|
980 |
public UndoRedo getUndoRedo() {
|
|
981 |
return getUndoRedoManager();
|
|
982 |
}
|
|
983 |
|
|
984 |
private boolean isVisible(Figure f) {
|
|
985 |
for (Integer n : f.getSource().getSourceNodesAsSet()) {
|
|
986 |
if (getModel().getHiddenNodes().contains(n)) {
|
|
987 |
return false;
|
|
988 |
}
|
|
989 |
}
|
|
990 |
return true;
|
|
991 |
}
|
|
992 |
|
|
993 |
private boolean doesIntersect(Set s1, Set s2) {
|
|
994 |
if (s1.size() > s2.size()) {
|
|
995 |
Set tmp = s1;
|
|
996 |
s1 = s2;
|
|
997 |
s2 = tmp;
|
|
998 |
}
|
|
999 |
|
|
1000 |
for (Object o : s1) {
|
|
1001 |
if (s2.contains(o)) {
|
|
1002 |
return true;
|
|
1003 |
}
|
|
1004 |
}
|
|
1005 |
|
|
1006 |
return false;
|
|
1007 |
}
|
|
1008 |
|
|
1009 |
public void showNot(final Set<Integer> nodes) {
|
|
1010 |
updateHiddenNodes(nodes, true);
|
|
1011 |
}
|
|
1012 |
|
|
1013 |
public void showOnly(final Set<Integer> nodes) {
|
|
1014 |
HashSet<Integer> allNodes = new HashSet<Integer>(getModel().getGraphToView().getGroup().getAllNodes());
|
|
1015 |
allNodes.removeAll(nodes);
|
|
1016 |
updateHiddenNodes(allNodes, true);
|
|
1017 |
}
|
|
1018 |
|
|
1019 |
private void updateHiddenNodes(Set<Integer> newHiddenNodes, boolean doRelayout) {
|
|
1020 |
|
|
1021 |
Set<InputBlock> visibleBlocks = new HashSet<InputBlock>();
|
|
1022 |
|
|
1023 |
Diagram diagram = getModel().getDiagramToView();
|
|
1024 |
assert diagram != null;
|
|
1025 |
|
|
1026 |
Set<Widget> oldVisibleWidgets = new HashSet<Widget>();
|
|
1027 |
|
|
1028 |
for (Figure f : diagram.getFigures()) {
|
|
1029 |
FigureWidget w = figureWidgets.get(f);
|
|
1030 |
if (w.isVisible()) {
|
|
1031 |
oldVisibleWidgets.add(w);
|
|
1032 |
}
|
|
1033 |
}
|
|
1034 |
|
|
1035 |
if (getModel().getShowBlocks()) {
|
|
1036 |
for (InputBlock b : diagram.getGraph().getBlocks()) {
|
|
1037 |
BlockWidget w = blockWidgets.get(b);
|
|
1038 |
if (w.isVisible()) {
|
|
1039 |
oldVisibleWidgets.add(w);
|
|
1040 |
}
|
|
1041 |
}
|
|
1042 |
}
|
|
1043 |
|
|
1044 |
for (Figure f : diagram.getFigures()) {
|
|
1045 |
boolean hiddenAfter = doesIntersect(f.getSource().getSourceNodesAsSet(), newHiddenNodes);
|
|
1046 |
|
|
1047 |
FigureWidget w = this.figureWidgets.get(f);
|
|
1048 |
w.setBoundary(false);
|
|
1049 |
if (!hiddenAfter) {
|
|
1050 |
// Figure is shown
|
|
1051 |
w.setVisible(true);
|
|
1052 |
for (InputNode n : f.getSource().getSourceNodes()) {
|
|
1053 |
visibleBlocks.add(diagram.getGraph().getBlock(n));
|
|
1054 |
}
|
|
1055 |
} else {
|
|
1056 |
// Figure is hidden
|
|
1057 |
w.setVisible(false);
|
|
1058 |
}
|
|
1059 |
}
|
|
1060 |
|
|
1061 |
if (getModel().getShowNodeHull()) {
|
|
1062 |
List<FigureWidget> boundaries = new ArrayList<FigureWidget>();
|
|
1063 |
for (Figure f : diagram.getFigures()) {
|
|
1064 |
FigureWidget w = this.figureWidgets.get(f);
|
|
1065 |
if (!w.isVisible()) {
|
|
1066 |
Set<Figure> set = new HashSet<Figure>(f.getPredecessorSet());
|
|
1067 |
set.addAll(f.getSuccessorSet());
|
|
1068 |
|
|
1069 |
boolean b = false;
|
|
1070 |
for (Figure neighbor : set) {
|
|
1071 |
FigureWidget neighborWidget = figureWidgets.get(neighbor);
|
|
1072 |
if (neighborWidget.isVisible()) {
|
|
1073 |
b = true;
|
|
1074 |
break;
|
|
1075 |
}
|
|
1076 |
}
|
|
1077 |
|
|
1078 |
if (b) {
|
|
1079 |
w.setBoundary(true);
|
|
1080 |
for (InputNode n : f.getSource().getSourceNodes()) {
|
|
1081 |
visibleBlocks.add(diagram.getGraph().getBlock(n));
|
|
1082 |
}
|
|
1083 |
boundaries.add(w);
|
|
1084 |
}
|
|
1085 |
}
|
|
1086 |
}
|
|
1087 |
|
|
1088 |
for (FigureWidget w : boundaries) {
|
|
1089 |
if (w.isBoundary()) {
|
|
1090 |
w.setVisible(true);
|
|
1091 |
}
|
|
1092 |
}
|
|
1093 |
}
|
|
1094 |
|
|
1095 |
if (getModel().getShowBlocks()) {
|
|
1096 |
for (InputBlock b : diagram.getGraph().getBlocks()) {
|
|
1097 |
|
|
1098 |
boolean visibleAfter = visibleBlocks.contains(b);
|
|
1099 |
|
|
1100 |
BlockWidget w = blockWidgets.get(b);
|
|
1101 |
if (visibleAfter) {
|
|
1102 |
// Block must be shown
|
|
1103 |
w.setVisible(true);
|
|
1104 |
} else {
|
|
1105 |
// Block must be hidden
|
|
1106 |
w.setVisible(false);
|
|
1107 |
}
|
|
1108 |
}
|
|
1109 |
}
|
|
1110 |
|
|
1111 |
getModel().setHiddenNodes(newHiddenNodes);
|
|
1112 |
if (doRelayout) {
|
|
1113 |
relayout(oldVisibleWidgets);
|
|
1114 |
}
|
|
1115 |
this.validate();
|
|
1116 |
addUndo();
|
|
1117 |
}
|
|
1118 |
|
|
1119 |
private void showFigures(Collection<Figure> f) {
|
|
1120 |
HashSet<Integer> newHiddenNodes = new HashSet<Integer>(getModel().getHiddenNodes());
|
|
1121 |
for (Figure fig : f) {
|
|
1122 |
newHiddenNodes.removeAll(fig.getSource().getSourceNodesAsSet());
|
|
1123 |
}
|
|
1124 |
updateHiddenNodes(newHiddenNodes, true);
|
|
1125 |
}
|
|
1126 |
|
|
1127 |
private void showFigure(Figure f) {
|
|
1128 |
HashSet<Integer> newHiddenNodes = new HashSet<Integer>(getModel().getHiddenNodes());
|
|
1129 |
newHiddenNodes.removeAll(f.getSource().getSourceNodesAsSet());
|
|
1130 |
updateHiddenNodes(newHiddenNodes, true);
|
|
1131 |
}
|
|
1132 |
|
|
1133 |
public void showAll(final Collection<Figure> f) {
|
|
1134 |
showFigures(f);
|
|
1135 |
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
public void show(final Figure f) {
|
|
1139 |
showFigure(f);
|
|
1140 |
}
|
|
1141 |
|
|
1142 |
public void gotoFigure(final Figure f) {
|
|
1143 |
|
|
1144 |
if (!isVisible(f)) {
|
|
1145 |
showFigure(f);
|
|
1146 |
}
|
|
1147 |
|
|
1148 |
FigureWidget fw = getFigureWidget(f);
|
|
1149 |
if (fw != null) {
|
|
1150 |
Rectangle r = fw.getBounds();
|
|
1151 |
Point p = fw.getLocation();
|
|
1152 |
centerRectangle(new Rectangle(p.x, p.y, r.width, r.height));
|
|
1153 |
|
|
1154 |
// Select figure
|
|
1155 |
clearSelection();
|
|
1156 |
addToSelection(fw);
|
|
1157 |
selectionUpdated();
|
|
1158 |
}
|
|
1159 |
}
|
|
1160 |
|
|
1161 |
public JPopupMenu createPopupMenu() {
|
|
1162 |
JPopupMenu menu = new JPopupMenu();
|
|
1163 |
for (Action a : actions) {
|
|
1164 |
if (a == null) {
|
|
1165 |
menu.addSeparator();
|
|
1166 |
} else {
|
|
1167 |
menu.add(a);
|
|
1168 |
}
|
|
1169 |
}
|
|
1170 |
return menu;
|
|
1171 |
}
|
|
1172 |
|
|
1173 |
private static class DiagramUndoRedo extends AbstractUndoableEdit implements ChangedListener<DiagramViewModel> {
|
|
1174 |
|
|
1175 |
private DiagramViewModel oldModel;
|
|
1176 |
private DiagramViewModel newModel;
|
|
1177 |
private Point oldScrollPosition;
|
|
1178 |
private DiagramScene scene;
|
|
1179 |
|
|
1180 |
public DiagramUndoRedo(DiagramScene scene, Point oldScrollPosition, DiagramViewModel oldModel, DiagramViewModel newModel) {
|
|
1181 |
assert oldModel != null;
|
|
1182 |
assert newModel != null;
|
|
1183 |
this.oldModel = oldModel;
|
|
1184 |
this.newModel = newModel;
|
|
1185 |
this.scene = scene;
|
|
1186 |
this.oldScrollPosition = oldScrollPosition;
|
|
1187 |
}
|
|
1188 |
|
|
1189 |
@Override
|
|
1190 |
public void redo() throws CannotRedoException {
|
|
1191 |
super.redo();
|
|
1192 |
boolean b = scene.getUndoRedoEnabled();
|
|
1193 |
scene.setUndoRedoEnabled(false);
|
|
1194 |
scene.getModel().getViewChangedEvent().addListener(this);
|
|
1195 |
scene.getModel().setData(newModel);
|
|
1196 |
scene.getModel().getViewChangedEvent().removeListener(this);
|
|
1197 |
scene.setUndoRedoEnabled(b);
|
|
1198 |
}
|
|
1199 |
|
|
1200 |
@Override
|
|
1201 |
public void undo() throws CannotUndoException {
|
|
1202 |
super.undo();
|
|
1203 |
boolean b = scene.getUndoRedoEnabled();
|
|
1204 |
scene.setUndoRedoEnabled(false);
|
|
1205 |
scene.getModel().getViewChangedEvent().addListener(this);
|
|
1206 |
scene.getModel().setData(oldModel);
|
|
1207 |
scene.getModel().getViewChangedEvent().removeListener(this);
|
|
1208 |
|
|
1209 |
SwingUtilities.invokeLater(new Runnable() {
|
|
1210 |
|
|
1211 |
public void run() {
|
|
1212 |
scene.setScrollPosition(oldScrollPosition);
|
|
1213 |
}
|
|
1214 |
});
|
|
1215 |
|
|
1216 |
scene.setUndoRedoEnabled(b);
|
|
1217 |
}
|
|
1218 |
|
|
1219 |
public void changed(DiagramViewModel source) {
|
|
1220 |
scene.getModel().getViewChangedEvent().removeListener(this);
|
|
1221 |
if (oldModel.getSelectedNodes().equals(newModel.getHiddenNodes())) {
|
|
1222 |
scene.smallUpdate(false);
|
|
1223 |
} else {
|
|
1224 |
scene.smallUpdate(true);
|
|
1225 |
}
|
|
1226 |
}
|
|
1227 |
}
|
|
1228 |
private boolean undoRedoEnabled = true;
|
|
1229 |
|
|
1230 |
public void setUndoRedoEnabled(boolean b) {
|
|
1231 |
this.undoRedoEnabled = b;
|
|
1232 |
}
|
|
1233 |
|
|
1234 |
public boolean getUndoRedoEnabled() {
|
|
1235 |
return undoRedoEnabled;
|
|
1236 |
}
|
|
1237 |
|
|
1238 |
public void changed(DiagramViewModel source) {
|
|
1239 |
assert source == model : "Receive only changed event from current model!";
|
|
1240 |
assert source != null;
|
|
1241 |
update();
|
|
1242 |
}
|
|
1243 |
|
|
1244 |
private void addUndo() {
|
|
1245 |
|
|
1246 |
DiagramViewModel newModelCopy = model.copy();
|
|
1247 |
|
|
1248 |
if (undoRedoEnabled) {
|
|
1249 |
this.getUndoRedoManager().undoableEditHappened(new UndoableEditEvent(this, new DiagramUndoRedo(this, this.getScrollPosition(), modelCopy, newModelCopy)));
|
|
1250 |
}
|
|
1251 |
|
|
1252 |
this.modelCopy = newModelCopy;
|
|
1253 |
}
|
|
1254 |
}
|