View Javadoc

1   // Copyright 2001-2005, FreeHEP.
2   package org.freehep.graphicsio.test;
3   
4   import java.awt.Color;
5   import java.awt.Toolkit;
6   import java.awt.datatransfer.Clipboard;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   import java.awt.event.WindowAdapter;
10  import java.awt.event.WindowEvent;
11  
12  import javax.swing.BorderFactory;
13  import javax.swing.JComponent;
14  import javax.swing.JFrame;
15  import javax.swing.JMenu;
16  import javax.swing.JMenuBar;
17  import javax.swing.JMenuItem;
18  import javax.swing.JOptionPane;
19  import javax.swing.JTabbedPane;
20  import javax.swing.WindowConstants;
21  import javax.swing.border.Border;
22  
23  import org.freehep.util.export.ExportDialog;
24  import org.freehep.util.export.VectorGraphicsTransferable;
25  
26  /**
27   * @author Charles Loomis
28   * @author Mark Donszelmann
29   * @version $Id: TestingFrame.java 8584 2006-08-10 23:06:37Z duns $
30   */
31  public class TestingFrame extends JFrame {
32  
33      private String title;
34  
35      private ExportDialog dialog;
36  
37      private JComponent singlePanel;
38  
39      private JTabbedPane multiPanel;
40  
41      public TestingFrame(String title) {
42  
43          // Title this frame.
44          super(title);
45          this.title = title;
46  
47          // Make this exit when the close button is clicked.
48          setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
49          addWindowListener(new WindowAdapter() {
50              public void windowClosing(WindowEvent e) {
51                  quit();
52              }
53          });
54  
55          // Create the Export dialog.
56          dialog = new ExportDialog();
57  
58          // Make a menu bar
59          JMenuBar menuBar = new JMenuBar();
60  
61          // File Menu
62          JMenu file = new JMenu("File");
63          menuBar.add(file);
64  
65          // Add Export...
66          JMenuItem export = new JMenuItem("Export...");
67          file.add(export);
68          export.addActionListener(new ActionListener() {
69              public void actionPerformed(ActionEvent e) {
70                  export();
71              }
72          });
73  
74          // Add Quit...
75          JMenuItem quit = new JMenuItem("Quit");
76          file.add(quit);
77          quit.addActionListener(new ActionListener() {
78              public void actionPerformed(ActionEvent e) {
79                  quit();
80              }
81          });
82  
83          // Edit Menu
84          JMenu edit = new JMenu("Edit");
85          menuBar.add(edit);
86  
87          // Add Copy
88          JMenuItem copy = new JMenuItem("Copy");
89          edit.add(copy);
90          copy.addActionListener(new ActionListener() {
91              public void actionPerformed(ActionEvent e) {
92                  copy();
93              }
94          });
95  
96          // Add this to the frame.
97          setJMenuBar(menuBar);
98      }
99  
100     public void export() {
101         if (multiPanel != null) {
102             // FIXME the current export dialog does not allow to write multiple
103             // files at once...
104             dialog.showExportDialog(this, "Export...", multiPanel
105                     .getSelectedComponent(), title);
106         } else if (singlePanel != null) {
107             dialog.showExportDialog(this, "Export...", singlePanel, title);
108         } else {
109             System.err.println("Failed to export");
110         }
111     }
112 
113     public void addPanel(JComponent c) {
114         addPanel(null, c);
115     }
116 
117     /**
118      * Add a test panel to the frame.
119      */
120     public void addPanel(String name, JComponent c) {
121 
122         if (name == null) {
123             singlePanel = c;
124             setContentPane(singlePanel);
125         } else {
126             if (multiPanel == null) {
127                 multiPanel = new JTabbedPane();
128                 setContentPane(multiPanel);
129             }
130             multiPanel.addTab(name, c);
131         }
132 
133         // Create a border of white surrounded by black.
134         Border border = BorderFactory.createCompoundBorder(BorderFactory
135                 .createMatteBorder(1, 1, 1, 1, Color.white), BorderFactory
136                 .createMatteBorder(2, 2, 2, 2, Color.green));
137         c.setBorder(border);
138     }
139 
140     public void copy() {
141         Clipboard clipBoard = Toolkit.getDefaultToolkit().getSystemClipboard();
142         VectorGraphicsTransferable transferable = new VectorGraphicsTransferable(
143                 multiPanel == null ? singlePanel : multiPanel
144                         .getSelectedComponent());
145         clipBoard.setContents(transferable, transferable);
146     }
147 
148     public void quit() {
149         int n = JOptionPane.showConfirmDialog(this,
150                 "Do you really want to quit?", "Confirm Quit",
151                 JOptionPane.YES_NO_OPTION);
152 
153         if (n == JOptionPane.YES_OPTION)
154             System.exit(0);
155     }
156 
157 }