View Javadoc

1   // Copyright 2000-2007, FreeHEP.
2   package org.freehep.graphicsio.test;
3   
4   import java.awt.Component;
5   import java.awt.Dimension;
6   import java.io.File;
7   import java.lang.reflect.Constructor;
8   import java.lang.reflect.InvocationTargetException;
9   import java.util.ArrayList;
10  import java.util.List;
11  import java.util.Properties;
12  
13  import javax.swing.JComponent;
14  import javax.swing.JPanel;
15  
16  import org.freehep.graphics2d.VectorGraphics;
17  import org.freehep.graphicsio.ImageGraphics2D;
18  import org.freehep.swing.Headless;
19  import org.freehep.util.UserProperties;
20  
21  /**
22   * @author Mark Donszelmann
23   * @version $Id: TestingPanel.java 12626 2007-06-08 22:23:13Z duns $
24   */
25  public class TestingPanel extends JPanel {
26  
27      public static final int width = 600;
28      public static final int height = 600;
29      
30      protected String[] args;
31  
32      protected VectorGraphics graphics;
33  
34      protected List/* <String> */names = new ArrayList();
35  
36      protected List/* <JComponent> */pages = new ArrayList();
37  
38      public TestingPanel(String[] args) throws Exception {
39          this.args = args;
40          if ((args != null) && (args.length != 0)) {
41              if (args[0].equals(ImageGraphics2D.class.getName())) {
42                  if (args.length != 3) {
43                      System.err.println("Usage: Test... "
44                              + ImageGraphics2D.class + " format OutputFile");
45                      System.exit(1);
46                  }
47              } else {
48                  if (args.length != 2) {
49                      System.err
50                              .println("Usage: Test... VectorGraphicsClassName OutputFile");
51                      System.exit(1);
52                  }
53              }
54          }
55      }
56  
57      protected void addPage(String name, JComponent c) {
58          names.add(name);
59          pages.add(c);
60      }
61  
62      public void runTest() throws Exception {
63          runTest(null);
64      }
65  
66      public void runTest(int width, int height) throws Exception {
67          runTest(width, height, null);
68      }
69  
70      public void runTest(Properties options) throws Exception {
71          runTest(width, height, options);
72      }
73  
74      public void runTest(int width, int height, Properties options)
75              throws Exception {
76  
77          setPreferredSize(new Dimension(width, height));
78  
79          if ((args == null) || (args.length == 0)) {
80              // Create a new frame to hold everything.
81              TestingFrame frame = new TestingFrame(getClass().toString());
82  
83              if (names.size() > 0) {
84                  for (int i = 0; i < names.size(); i++) {
85                      frame.addPanel((String) names.get(i), (JComponent) pages
86                              .get(i));
87                  }
88                  frame.setSize(width, height);
89              } else {
90                  // Create a new instance of this class and add it to the frame.
91                  frame.addPanel(this);
92                  frame.pack();
93              }
94  
95              // Give the frame a size and make it visible.
96              frame.setVisible(true);
97          } else {
98              // run with -Djava.awt.headless=true
99              Headless headless = new Headless(this);
100             headless.pack();
101             headless.setVisible(true);
102 
103             if (args[0].equals(ImageGraphics2D.class.getName())) {
104                 File file = new File(args[2]);
105                 graphics = new ImageGraphics2D(file, this, args[1]);
106             } else {
107                 try {
108                     File file = new File(args[1]);
109 //                    System.err.println(file);
110                     Class cls = Class.forName(args[0]);
111                     Constructor constructor = cls.getConstructor(new Class[] {
112                             File.class, Component.class });
113                     graphics = (VectorGraphics) constructor
114                             .newInstance(new Object[] { file, this });
115                 } catch (ClassNotFoundException e) {
116                     System.out.println("Cannot find class: " + args[0]);
117                     throw e;
118                 } catch (ClassCastException e) {
119                     System.out.println("Class: " + args[0]
120                             + " does not extend VectorGraphics");
121                     throw e;
122                 } catch (NoSuchMethodException e) {
123                     System.out.println("Class: " + args[0]
124                             + " does not have constructor(File, Component)");
125                     throw e;
126                 } catch (InvocationTargetException e) {
127                     System.out.println(e.getTargetException());
128                     e.getTargetException().printStackTrace();
129                     throw e;
130                 }
131             }
132 
133             // FIXME we should also set the properties in interactive mode
134             UserProperties user = (options == null) ? new UserProperties()
135                     : new UserProperties(options);
136 
137             // FIXME enable these when we can know if we run interactively or
138             // from ANT
139             // user.setProperty(AbstractVectorGraphicsIO.EMIT_WARNINGS, true);
140             // user.setProperty(AbstractVectorGraphicsIO.EMIT_ERRORS, true);
141 
142             // Text Antialiasing seems to produce something time dependent
143             user.setProperty(ImageGraphics2D.ANTIALIAS_TEXT, false);
144             // user.setProperty(JAVAGraphics2D.PACKAGE_NAME,
145             // "org.freehep.graphicsio.java.test");
146 
147             // FIXME FREEHEP-219, we need something here to write multi-page
148             // documents...
149 
150             graphics.setProperties(user);
151 
152             graphics.setDeviceIndependent(true);
153             graphics.startExport();
154             print(graphics);
155             graphics.endExport();
156         }
157     }
158 }