1 /* $RCSfile$
2  * $Author jonathan gutow$
3  * $Date Aug 5, 2007 9:19:06 AM $
4  * $Revision$
5  *
6  * Copyright (C) 2005-2007  The Jmol Development Team
7  *
8  * Contact: jmol-developers@lists.sf.net
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 2.1 of the License, or (at your option) any later version.
14  *
15  *  This library is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  *  02110-1301, USA.
24  */
25 package org.openscience.jmol.app.webexport;
26 
27 import java.awt.GridLayout;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.io.FileNotFoundException;
31 import java.io.FileOutputStream;
32 import java.io.PrintStream;
33 
34 import javajs.util.PT;
35 
36 import javax.swing.JButton;
37 import javax.swing.JComponent;
38 import javax.swing.JFileChooser;
39 import javax.swing.JLabel;
40 import javax.swing.JPanel;
41 import javax.swing.JTextField;
42 
43 import org.jmol.viewer.Viewer;
44 
45 public class Test extends JPanel implements ActionListener {
46 
47   /*
48    * old code -- not implemented in Jmol 11.3
49    */
50 
51   private static final long serialVersionUID = 1L;
52   // The constants used to generate panels, etc.
53   JButton StateButton, FileButton, PathButton, movetoTime,
54       StringtoScriptButton;
55   JTextField appletPath;
56   ArrayListTransferHandler arrayListHandler;
57   JFileChooser fc;
58   Viewer vwr;
59 
Test(Viewer vwr)60   Test(Viewer vwr) {
61     this.vwr = vwr;
62   }
63 
64   // Need the panel maker and the action listener.
panel()65   public JComponent panel() {
66 
67     // Create the brief discription text
68     JLabel Description = new JLabel(
69         "Buttons to test getting info from Jmol Application");
70 
71     // For layout purposes, put things in separate panels
72 
73     // Create the state button.
74     StateButton = new JButton("Get Application State...");
75     StateButton.addActionListener(this);
76 
77     // Create Filename Button
78     FileButton = new JButton("Get name of open file...");
79     FileButton.addActionListener(this);
80 
81     // Create Path Button
82     PathButton = new JButton("Get Path to open file...");
83     PathButton.addActionListener(this);
84 
85     // Create the movetoTime Button
86     movetoTime = new JButton("Insert 5 seconds for moveto, rotate and zoom...");
87     movetoTime.addActionListener(this);
88 
89     // Create String to Script Button
90     StringtoScriptButton = new JButton("Save a string as a script");
91     StringtoScriptButton.addActionListener(this);
92 
93     // Combine Three buttons into one panel
94     JPanel ButtonPanel1 = new JPanel();
95     ButtonPanel1.add(StateButton);
96     ButtonPanel1.add(FileButton);
97     ButtonPanel1.add(PathButton);
98 
99     // Next three button in another panel
100     JPanel ButtonPanel2 = new JPanel();
101     ButtonPanel2.add(movetoTime);
102     ButtonPanel2.add(StringtoScriptButton);
103 
104     // Create the overall panel
105     JPanel TestPanel = new JPanel();
106     TestPanel.setLayout(new GridLayout(10, 1));
107 
108     // Add everything to this panel.
109     TestPanel.add(Description);
110     TestPanel.add(ButtonPanel1);
111     TestPanel.add(ButtonPanel2);
112 
113     return (TestPanel);
114   }
115 
116   @Override
actionPerformed(ActionEvent e)117   public void actionPerformed(ActionEvent e) {
118 
119     if (e.getSource() == StateButton) { // Handle getting the State of Jmol...
120       String Str = null;
121       Str = vwr.getStateInfo();
122       if (Str == null) {
123         LogPanel
124             .log("Something didn't work when selecting the State Button in Test module");
125       }
126       LogPanel.log(Str);
127     }
128     if (e.getSource() == FileButton) { // Handle getting the file name...
129       String Str = null;
130       Str = vwr.fm.getFileName();
131       if (Str == null) {
132         LogPanel
133             .log("Something didn't work when selecting the file button in Test module");
134       } else {
135         LogPanel.log(Str);
136       }
137     }
138     if (e.getSource() == PathButton) {// Handle getting the path to the file...
139       String Str = null;
140       Str = vwr.fm.getFullPathName(false);
141       if (Str == null) {
142         LogPanel
143             .log("Something didn't work when selecting the Path button in Test module");
144       } else {
145         LogPanel.log(Str);
146       }
147     }
148     if (e.getSource() == movetoTime) {// Handle getting the path to the file...
149       String statestr = null;
150       statestr = vwr.getStateInfo();
151       if (statestr == null) {
152         LogPanel
153             .log("Something didn't work when reading the state while trying to add a moveto time.");
154       }
155       // Change the state string so that it will work as a script with an
156       // animated moveto...
157       statestr = PT.rep(statestr, "set refreshing false;",
158           "set refreshing true;");
159       statestr = PT.rep(statestr, "moveto /* time, axisAngle */ 0.0",
160           "moveto /* time, axisAngle */ 5.0");
161       LogPanel.log("The state below should have a 5 second moveto time...");
162       LogPanel.log(statestr);
163     }
164     if (e.getSource() == StringtoScriptButton) {
165       String Str = "This is a test string to stand in for the script;";
166       PrintStream out = null;
167       try {
168         out = new PrintStream(new FileOutputStream("Test.scpt"));
169       } catch (FileNotFoundException IOe) {
170         LogPanel.log("Open file error in StringtoScriptButton"); // Pass the
171       }
172       out.print(Str);
173       out.close();
174       LogPanel
175           .log("The file Test.scpt should have been written to the default directory.");
176     }
177   }
178 }
179