1
14 package demo.layout.hierarchic;
15
16 import demo.view.hierarchy.GroupingDemo;
17 import y.base.DataMap;
18 import y.base.EdgeCursor;
19 import y.base.Node;
20 import y.base.NodeCursor;
21 import y.base.NodeList;
22 import y.layout.hierarchic.IncrementalHierarchicLayouter;
23 import y.layout.hierarchic.incremental.IncrementalHintsFactory;
24 import y.option.ConstraintManager;
25 import y.option.OptionHandler;
26 import y.option.OptionItem;
27 import y.util.Maps;
28 import y.view.Graph2D;
29 import y.view.Graph2DLayoutExecutor;
30 import y.view.Graph2DViewActions;
31
32 import javax.swing.AbstractAction;
33 import javax.swing.Action;
34 import javax.swing.JLabel;
35 import javax.swing.JToolBar;
36 import java.awt.EventQueue;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.util.Locale;
40
41
42
51 public class IncrementalHierarchicGroupDemo extends GroupingDemo {
52
53 IncrementalHierarchicLayouter layouter;
54 OptionHandler groupLayoutOptions;
55
56 public IncrementalHierarchicGroupDemo() {
57
58 layouter = new IncrementalHierarchicLayouter();
60 layouter.setOrthogonallyRouted(true);
61 layouter.setRecursiveGroupLayeringEnabled(false);
62
63 Object[] groupStrategyEnum = {"Global Layering", "Recursive Layering"};
65 Object[] groupAlignmentEnum = {"Top", "Center", "Bottom"};
66 groupLayoutOptions = new OptionHandler("Layout Properties");
67 ConstraintManager cm = new ConstraintManager(groupLayoutOptions);
68 OptionItem gsi = groupLayoutOptions.addEnum("Group Layering Strategy", groupStrategyEnum, 0);
69 OptionItem eci = groupLayoutOptions.addBool("Enable Compact Layering", true);
70 OptionItem gai = groupLayoutOptions.addEnum("Group Alignment", groupAlignmentEnum, 0);
71 cm.setEnabledOnValueEquals(gsi, "Recursive Layering", eci);
72 cm.setEnabledOnValueEquals(gsi, "Recursive Layering", gai);
73 cm.setEnabledOnCondition(cm.createConditionValueEquals(gsi, "Recursive Layering").and(
74 cm.createConditionValueEquals(eci, Boolean.TRUE).inverse()), gai);
75
76 view.fitContent();
77 }
78
79
82 protected void registerViewActions() {
83 super.registerViewActions();
84 view.getCanvasComponent().getActionMap().put(Graph2DViewActions.CLOSE_GROUPS, new CloseGroupsAndLayoutAction());
85 view.getCanvasComponent().getActionMap().put(Graph2DViewActions.OPEN_FOLDERS, new OpenFoldersAndLayoutAction());
86 }
87
88
92 class OpenFoldersAndLayoutAction extends Graph2DViewActions.OpenFoldersAction {
93
94 OpenFoldersAndLayoutAction() {
95 super(IncrementalHierarchicGroupDemo.this.view);
96 }
97
98 public void openFolder(Node folderNode, Graph2D graph) {
99 NodeList children = new NodeList(graph.getHierarchyManager().getInnerGraph(folderNode).nodes());
100 super.openFolder(folderNode, graph);
101 graph.unselectAll();
102 graph.setSelected(folderNode, true);
103 for (NodeCursor nc = children.nodes(); nc.ok(); nc.next()) {
104 graph.setSelected(nc.node(), true);
105 }
106
107 layoutIncrementally();
108
109 graph.unselectAll();
110 graph.setSelected(folderNode, true);
111 graph.updateViews();
112
113 }
114 }
115
116
120 class CloseGroupsAndLayoutAction extends Graph2DViewActions.CloseGroupsAction {
121
122 CloseGroupsAndLayoutAction() {
123 super(IncrementalHierarchicGroupDemo.this.view);
124 }
125
126 public void closeGroup(Node groupNode, Graph2D graph) {
127 super.closeGroup(groupNode, graph);
128 graph.unselectAll();
129 graph.setSelected(groupNode, true);
130 for (EdgeCursor ec = groupNode.edges(); ec.ok(); ec.next()) {
131 graph.setSelected(ec.edge(), true);
132 }
133
134 layoutIncrementally();
135 graph.unselectAll();
136
137 graph.updateViews();
138 }
139 }
140
141
142
145 protected void loadInitialGraph() {
146 loadGraph("resource/IncrementalHierarchicGroupDemo.graphml");
147 }
148
149
152 protected JToolBar createToolBar() {
153 JToolBar toolBar = super.createToolBar();
154 addLayoutActions(toolBar);
155 return toolBar;
156 }
157
158 protected void addLayoutActions(JToolBar toolBar) {
159 final Action incrementalLayoutAction = new AbstractAction(
160 "Incremental", SHARED_LAYOUT_ICON) {
161 public void actionPerformed(ActionEvent e) {
162 layoutIncrementally();
163 }
164 };
165
166 final Action layoutAction = new AbstractAction(
167 "Complete", SHARED_LAYOUT_ICON) {
168 public void actionPerformed(ActionEvent e) {
169 layout();
170 }
171 };
172
173 final Action propertiesAction = new AbstractAction(
174 "Settings...", getIconResource("resource/properties.png")) {
175 public void actionPerformed(ActionEvent e) {
176 final ActionListener layoutListener = new ActionListener() {
177 public void actionPerformed(ActionEvent e) {
178 layout();
179 }
180 };
181 OptionSupport.showDialog(groupLayoutOptions, layoutListener, false, view.getFrame());
182 configureGroupLayout();
183 }
184 };
185
186 toolBar.addSeparator();
187 toolBar.add(new JLabel("Layout: "));
188 toolBar.add(createActionControl(incrementalLayoutAction));
189 toolBar.add(createActionControl(layoutAction));
190 toolBar.add(createActionControl(propertiesAction));
191 }
192
193
196 void configureGroupLayout() {
197 Object gsi = groupLayoutOptions.get("Group Layering Strategy");
198 if ("Recursive Layering".equals(gsi)) {
199 layouter.setRecursiveGroupLayeringEnabled(true);
200 } else if ("Global Layering".equals(gsi)) {
201 layouter.setRecursiveGroupLayeringEnabled(false);
202 }
203
204 layouter.setGroupCompactionEnabled(groupLayoutOptions.getBool("Enable Compact Layering"));
205
206 Object gai = groupLayoutOptions.get("Group Alignment");
207 if ("Top".equals(gai)) {
208 layouter.setGroupAlignmentPolicy(IncrementalHierarchicLayouter.POLICY_ALIGN_GROUPS_TOP);
209 } else if ("Center".equals(gai)) {
210 layouter.setGroupAlignmentPolicy(IncrementalHierarchicLayouter.POLICY_ALIGN_GROUPS_CENTER);
211 }
212 if ("Bottom".equals(gai)) {
213 layouter.setGroupAlignmentPolicy(IncrementalHierarchicLayouter.POLICY_ALIGN_GROUPS_BOTTOM);
214 }
215 }
216
217
220 void layoutIncrementally() {
221 Graph2D graph = view.getGraph2D();
222
223 layouter.setLayoutMode(IncrementalHierarchicLayouter.LAYOUT_MODE_INCREMENTAL);
224
225 DataMap incrementalElements = Maps.createHashedDataMap();
227 final IncrementalHintsFactory ihf = layouter.createIncrementalHintsFactory();
229
230 for (NodeCursor nc = graph.selectedNodes(); nc.ok(); nc.next()) {
231 incrementalElements.set(nc.node(), ihf.createLayerIncrementallyHint(nc.node()));
232 }
233
234 for (EdgeCursor ec = graph.selectedEdges(); ec.ok(); ec.next()) {
235 incrementalElements.set(ec.edge(), ihf.createSequenceIncrementallyHint(ec.edge()));
236 }
237 graph.addDataProvider(IncrementalHierarchicLayouter.INCREMENTAL_HINTS_DPKEY, incrementalElements);
238 try {
239 final Graph2DLayoutExecutor layoutExecutor = new Graph2DLayoutExecutor();
240 layoutExecutor.getLayoutMorpher().setSmoothViewTransform(true);
241 layoutExecutor.doLayout(view, layouter);
242 } finally {
243 graph.removeDataProvider(IncrementalHierarchicLayouter.INCREMENTAL_HINTS_DPKEY);
244 }
245 }
246
247
250 void layout() {
251 layouter.setLayoutMode(IncrementalHierarchicLayouter.LAYOUT_MODE_FROM_SCRATCH);
252 final Graph2DLayoutExecutor layoutExecutor = new Graph2DLayoutExecutor();
253 layoutExecutor.getLayoutMorpher().setSmoothViewTransform(true);
254 layoutExecutor.doLayout(view, layouter);
255 }
256
257
261 public static void main(String[] args) {
262 EventQueue.invokeLater(new Runnable() {
263 public void run() {
264 Locale.setDefault(Locale.ENGLISH);
265 initLnF();
266 (new IncrementalHierarchicGroupDemo()).start();
267 }
268 });
269 }
270 }
271