Revision: 587 Author: allain.lalonde Date: Mon Jul 27 13:48:11 2009 Log: Refactoring the ExampleRunner for clarity. http://code.google.com/p/piccolo2d/source/detail?r=587
Modified: /piccolo2d.java/trunk/examples/src/main/java/edu/umd/cs/piccolo/examples/ExampleRunner.java ======================================= --- /piccolo2d.java/trunk/examples/src/main/java/edu/umd/cs/piccolo/examples/ExampleRunner.java Fri Jan 23 12:27:47 2009 +++ /piccolo2d.java/trunk/examples/src/main/java/edu/umd/cs/piccolo/examples/ExampleRunner.java Mon Jul 27 13:48:11 2009 @@ -37,6 +37,7 @@ import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; +import javax.swing.JOptionPane; import javax.swing.JPanel; import edu.umd.cs.piccolo.util.PDebug; @@ -57,303 +58,63 @@ public void createExampleButtons() { Container c = getContentPane(); - Container p = new JPanel(); - - p = new JPanel(new GridLayout(0, 1)); - c.add(BorderLayout.NORTH, p); - - p.add(new JCheckBox(new AbstractAction("Print Frame Rates to Console") { + JPanel panel = new JPanel(new GridLayout(0, 1)); + c.add(BorderLayout.NORTH, panel); + + panel.add(new JCheckBox(new AbstractAction("Print Frame Rates to Console") { public void actionPerformed(ActionEvent e) { PDebug.debugPrintFrameRate = !PDebug.debugPrintFrameRate; } })); - p.add(new JCheckBox(new AbstractAction("Show Region Managment") { + panel.add(new JCheckBox(new AbstractAction("Show Region Managment") { public void actionPerformed(ActionEvent e) { PDebug.debugRegionManagement = !PDebug.debugRegionManagement; } })); - p.add(new JCheckBox(new AbstractAction("Show Full Bounds") { + panel.add(new JCheckBox(new AbstractAction("Show Full Bounds") { public void actionPerformed(ActionEvent e) { PDebug.debugFullBounds = !PDebug.debugFullBounds; } })); - p = new JPanel(new GridLayout(0, 2)); - c.add(BorderLayout.CENTER, p); - - p.add(new JButton(new AbstractAction("ActivityExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new ActivityExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("AngleNodeExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new AngleNodeExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("BirdsEyeViewExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new BirdsEyeViewExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("CameraExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new CameraExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("CenterExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new CenterExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("ChartLabelExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new ChartLabelExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("ClipExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new ClipExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("CompositeExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new CompositeExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("DynamicExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new DynamicExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("EventHandlerExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new EventHandlerExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("FullScreenNodeExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new FullScreenNodeExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("GraphEditorExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new GraphEditorExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - p.add(new JButton(new AbstractAction("GridExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new GridExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("GroupExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new GroupExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("HandleExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new HandleExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("HierarchyZoomExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new HierarchyZoomExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("KeyEventFocusExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new KeyEventFocusExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("LayoutExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new LayoutExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("LensExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new LensExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("NavigationExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new NavigationExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("NodeCacheExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new NodeCacheExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("NodeEventExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new NodeEventExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("NodeExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new NodeExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("NodeLinkExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new NodeLinkExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("PanToExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new PanToExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("PathExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new PathExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("PositionExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new PositionExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("PositionPathActivityExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new PositionPathActivityExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("PulseExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new PulseExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("ScrollingExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new ScrollingExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("SelectionExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new SelectionExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("SquiggleExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new SquiggleExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("StickyExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new StickyExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("StickyHandleLayerExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new StickyHandleLayerExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("StrokeExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new StrokeExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("TextExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new TextExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("Tooltip Example") { - public void actionPerformed(ActionEvent e) { - PFrame example = new TooltipExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("TwoCanvasExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new TwoCanvasExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); - - p.add(new JButton(new AbstractAction("WaitForActivitiesExample") { - public void actionPerformed(ActionEvent e) { - PFrame example = new WaitForActivitiesExample(); - example.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); - } - })); + panel = new JPanel(new GridLayout(0, 2)); + c.add(BorderLayout.CENTER, panel); + + addExampleButtons(panel, new Class[] { ActivityExample.class, AngleNodeExample.class, + BirdsEyeViewExample.class, CameraExample.class, CenterExample.class, ChartLabelExample.class, + ClipExample.class, CompositeExample.class, DynamicExample.class, EventHandlerExample.class, + FullScreenNodeExample.class, GraphEditorExample.class, GraphEditorExample.class, GridExample.class, + GroupExample.class, HandleExample.class, HierarchyZoomExample.class, KeyEventFocusExample.class, + LayoutExample.class, LensExample.class, NavigationExample.class, NodeCacheExample.class, + NodeEventExample.class, NodeEventExample.class, NodeLinkExample.class, PanToExample.class, + PathExample.class, PositionExample.class, PositionPathActivityExample.class, PulseExample.class, + ScrollingExample.class, SelectionExample.class, SquiggleExample.class, StickyExample.class, + StickyHandleLayerExample.class, StrokeExample.class, TextExample.class, TooltipExample.class, + TwoCanvasExample.class, WaitForActivitiesExample.class }); + } + + private void addExampleButtons(JPanel panel, Class[] exampleClasses) { + for (int i = 0; i < exampleClasses.length; i++) { + panel.add(buildExampleButton(exampleClasses[i])); + } + } + + private JButton buildExampleButton(final Class exampleClass) { + String fullClassName = exampleClass.getName(); + String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1); + return new JButton(new AbstractAction(simpleClassName) { + public void actionPerformed(ActionEvent event) { + try { + PFrame example = (PFrame) exampleClass.newInstance(); + example.setDefaultCloseOperation(PFrame.DISPOSE_ON_CLOSE); + } + catch (Exception e) { + JOptionPane.showMessageDialog(ExampleRunner.this, "A problem was encountered running the example"); + } + } + }); } public static void main(String[] args) { --~--~---------~--~----~------------~-------~--~----~ Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en -~----------~----~----~----~------~----~------~--~---