1
28 package demo.layout.component;
29
30 import demo.view.DemoBase;
31 import y.layout.ComponentLayouter;
32 import y.view.Graph2DLayoutExecutor;
33 import y.view.ShapeNodeRealizer;
34
35 import javax.swing.AbstractAction;
36 import javax.swing.JComboBox;
37 import javax.swing.JComponent;
38 import javax.swing.JLabel;
39 import javax.swing.JMenu;
40 import javax.swing.JMenuBar;
41 import javax.swing.JSlider;
42 import javax.swing.JToolBar;
43 import javax.swing.SwingConstants;
44 import javax.swing.event.ChangeEvent;
45 import javax.swing.event.ChangeListener;
46 import java.awt.Color;
47 import java.awt.Component;
48 import java.awt.Dimension;
49 import java.awt.EventQueue;
50 import java.awt.event.ActionEvent;
51 import java.awt.event.ActionListener;
52 import java.awt.event.ComponentAdapter;
53 import java.awt.event.ComponentEvent;
54 import java.net.URL;
55 import java.util.Locale;
56
57
61 public class ComponentLayouterDemo extends DemoBase {
62 private byte style;
63 private int spacing;
64
65
68 public ComponentLayouterDemo() {
69 this(null);
70 }
71
72
75 public ComponentLayouterDemo(final String helpFilePath) {
76 addHelpPane(helpFilePath);
77 loadGraph("resource/graphs_sample.graphml");
78 }
79
80
83 protected void registerViewActions() {
84 super.registerViewActions();
85 view.addComponentListener(new ComponentAdapter() {
86 public void componentResized(ComponentEvent e) {
87 layout();
88 }
89 });
90 }
91
92
95 protected JComponent createHelpPane(URL helpURL) {
96 final JComponent helpPane = super.createHelpPane(helpURL);
97 helpPane.setPreferredSize(new Dimension(300, 250));
98 return helpPane;
99 }
100
101
104 protected void configureDefaultRealizers() {
105 final ShapeNodeRealizer nr = new ShapeNodeRealizer(ShapeNodeRealizer.RECT);
106 nr.setFillColor(Color.ORANGE);
107 nr.removeLabel(0);
108 view.getGraph2D().setDefaultNodeRealizer(nr);
109 }
110
111
114 protected JToolBar createToolBar() {
115 final JToolBar bar = super.createToolBar();
116 bar.addSeparator();
117 bar.add(createActionControl(new AbstractAction("Layout", SHARED_LAYOUT_ICON) {
118 public void actionPerformed(ActionEvent e) {
119 layout();
120 }
121 }));
122 bar.addSeparator();
123 bar.add(new JLabel(" Style: "));
124 bar.add(createStyleComboBox());
125 bar.add(new JLabel(" Spacing: "));
126 bar.add(createSpacingSlider());
127 return bar;
128 }
129
130
133 private Component createStyleComboBox() {
134 final JComboBox comboBox = new JComboBox(new Object[]{
135 "Multiple Rows",
136 "Single Row",
137 "Single Column",
138 "Packed Rectangle",
139 "Compact Rectangle",
140 "Packed Circle",
141 "Compact Circle",
142 "Nested Rows",
143 "Compact Nested Rows",
144 "Width Constrained Nested Rows",
145 "Height Constrained Nested Rows",
146 "Width Constrained Compact Nested Rows",
147 "Height Constrained Compact Nested Rows"});
148 final byte[] styles = new byte[]{
149 ComponentLayouter.STYLE_ROWS,
150 ComponentLayouter.STYLE_SINGLE_ROW,
151 ComponentLayouter.STYLE_SINGLE_COLUMN,
152 ComponentLayouter.STYLE_PACKED_RECTANGLE,
153 ComponentLayouter.STYLE_PACKED_COMPACT_RECTANGLE,
154 ComponentLayouter.STYLE_PACKED_CIRCLE,
155 ComponentLayouter.STYLE_PACKED_COMPACT_CIRCLE,
156 ComponentLayouter.STYLE_MULTI_ROWS,
157 ComponentLayouter.STYLE_MULTI_ROWS_COMPACT,
158 ComponentLayouter.STYLE_MULTI_ROWS_WIDTH_CONSTRAINED,
159 ComponentLayouter.STYLE_MULTI_ROWS_HEIGHT_CONSTRAINED,
160 ComponentLayouter.STYLE_MULTI_ROWS_WIDTH_CONSTRAINED_COMPACT,
161 ComponentLayouter.STYLE_MULTI_ROWS_HEIGHT_CONSTRAINED_COMPACT};
162 comboBox.setMaximumSize(comboBox.getPreferredSize());
163 comboBox.addActionListener(new ActionListener() {
164 public void actionPerformed(ActionEvent e) {
165 style = styles[comboBox.getSelectedIndex()];
166 layout();
167 }
168 });
169 style = styles[0];
170 return comboBox;
171 }
172
173
176 private Component createSpacingSlider() {
177 spacing = 10;
178 final JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 10, 100, spacing);
179 slider.setMaximumSize(new Dimension(200, 100));
180 slider.addChangeListener(new ChangeListener() {
181 public void stateChanged(ChangeEvent e) {
182 if (!slider.getValueIsAdjusting()) {
183 spacing = slider.getValue();
184 layout();
185 }
186 }
187 });
188 return slider;
189 }
190
191
194 protected JMenuBar createMenuBar() {
195 final JMenuBar menuBar = super.createMenuBar();
196 JMenu menu = new JMenu("Sample Graphs");
197 menuBar.add(menu);
198 menu.add(new AbstractAction("Nodes") {
199 public void actionPerformed(ActionEvent e) {
200 loadGraph("resource/nodes_sample.graphml");
201 layout();
202 }
203 });
204 menu.add(new AbstractAction("Graphs") {
205 public void actionPerformed(ActionEvent e) {
206 loadGraph("resource/graphs_sample.graphml");
207 layout();
208 }
209 });
210 return menuBar;
211 }
212
213
216 private void layout() {
217 final ComponentLayouter layouter = new ComponentLayouter();
218 layouter.setStyle(style);
219 layouter.setComponentSpacing(spacing);
220 layouter.setPreferredLayoutSize(view.getWidth(), view.getHeight());
221 new Graph2DLayoutExecutor().doLayout(view, layouter);
222 }
223
224 public static void main(String[] args) {
225 EventQueue.invokeLater(new Runnable() {
226 public void run() {
227 Locale.setDefault(Locale.ENGLISH);
228 initLnF();
229 new ComponentLayouterDemo("resource/componentlayouterhelp.html").start("Component Layouter Demo");
230 }
231 });
232 }
233 }
234