001: package com.xoetrope.carousel.testpilot;
002:
003: import java.io.File;
004:
005: import java.awt.AWTEvent;
006: import java.awt.FileDialog;
007: import java.awt.Toolkit;
008: import java.awt.event.AWTEventListener;
009: import java.awt.event.WindowEvent;
010:
011: import net.xoetrope.swing.XButton;
012: import net.xoetrope.swing.XEdit;
013: import net.xoetrope.swing.XPanel;
014: import net.xoetrope.swing.XRadioButton;
015: import net.xoetrope.swing.XScrollableMetaContent;
016: import net.xoetrope.data.XDataSource;
017: import net.xoetrope.xui.XPage;
018: import net.xoetrope.xui.XPageManager;
019: import net.xoetrope.xui.XProjectManager;
020: import net.xoetrope.xui.data.XBaseModel;
021: import net.xoetrope.xui.data.XModel;
022: import java.util.prefs.Preferences;
023: import net.xoetrope.swing.XDialog;
024: import net.xoetrope.swing.XTextArea;
025: import net.xoetrope.swing.XLabel;
026:
027: /**
028: * Support for starting an application for a new session or from saved data
029: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
030: * the GNU Public License (GPL), please see license.txt for more details. If
031: * you make commercial use of this software you must purchase a commercial
032: * license from Xoetrope.</p>
033: * <p> $Revision: 1.2 $</p>
034: */
035: public class ApplicationConfig extends XPage implements
036: AWTEventListener {
037: private XButton btnProceed;
038: private XRadioButton radRecord, radRun;
039: private XPanel pnlRun, pnlRecord;
040: private XEdit txtFile;
041: private XTextArea lblDesc;
042: private XLabel txtLabel;
043:
044: private XButton btnLocate, btnLocateHome;
045: private XEdit mainClassEdit, argumentsEdit, classpathEdit,
046: homepathEdit;
047: private String delay = "100";
048: private Preferences prefs;
049:
050: /**
051: * The running application Frame object
052: */
053: public static Object mainClassObject;
054:
055: public ApplicationConfig() {
056: super ();
057: prefs = Preferences.userNodeForPackage(ApplicationConfig.class);
058: Toolkit.getDefaultToolkit().addAWTEventListener(this ,
059: AWTEvent.WINDOW_EVENT_MASK);
060: }
061:
062: /**
063: * Show the previous page
064: */
065: public void goBack() {
066: if (wasMouseClicked()) {
067: savePreferences();
068: pageMgr.showPrevious();
069: }
070: }
071:
072: /**
073: * Show the recording options
074: */
075: public void showOption() {
076: txtLabel.setEnabled(radRun.isSelected());
077: btnLocate.setEnabled(radRun.isSelected());
078: txtFile.setEnabled(radRun.isSelected());
079: lblDesc.setEnabled(radRun.isSelected());
080:
081: // pnlRecord.setVisible( radRecord.getState() );
082: savePreferences();
083: btnProceed.setEnabled(true);
084: }
085:
086: /**
087: * Go to the next page.<UL>
088: * <LI>The new application launcher page, or</LI>
089: * <LI>The script selection page</LI>
090: * </UL>
091: */
092: public void nextPage() {
093: if (wasMouseClicked()) {
094: savePreferences();
095: // Only invoke if an application is not already running
096: if (mainClassObject == null) {
097: ApplicationLauncher ai = new ApplicationLauncher(false,
098: getMainClass(), getClasspath(), getArguments(),
099: getHomeDir());
100: ai.start();
101: }
102:
103: if (radRecord.isSelected()) {
104: try {
105: pageMgr.showPage("tpApplicationRecorder");
106: } catch (Exception ex) {
107: ex.printStackTrace();
108: }
109: } else if (radRun.isSelected()) {
110: pageMgr.showPage("tpApplicationRunner");
111: }
112:
113: setMainClass();
114: }
115: }
116:
117: public void locateScript() {
118: if (wasMouseClicked()) {
119: FileDialog fd = new FileDialog(UnitApplet.getApplet()
120: .getFrame(),
121: "Please locate the script you want to run!");
122: fd.setVisible(true);
123: XDataSource modelDataSource = new XDataSource(project);
124: if (fd.getFile() != null) {
125: try {
126: File fis = new File(fd.getDirectory()
127: + fd.getFile());
128: txtFile.setText(fd.getDirectory() + fd.getFile());
129: XModel baseModel = project.getModel()
130: .reset(project);
131: ApplicationScriptConverter.convertTableForOpen(
132: project, fis);
133: XBaseModel desc = (XBaseModel) baseModel
134: .get("application/settings/desc");
135: if (desc.get() != null)
136: lblDesc.setText(desc.get().toString());
137: lblDesc.repaint();
138: lblDesc.setVisible(true);
139: XBaseModel settingsNode = (XBaseModel) baseModel
140: .get("application/settings");
141: mainClassEdit.setText(settingsNode
142: .getValueAsString("mainClass"));
143: argumentsEdit.setText(settingsNode
144: .getValueAsString("arguments"));
145: classpathEdit.setText(settingsNode
146: .getValueAsString("classpath"));
147: homepathEdit.setText(settingsNode
148: .getValueAsString("home"));
149: } catch (Exception ex) {
150: ex.printStackTrace();
151: }
152: }
153: }
154: }
155:
156: public void locateHome() {
157: if (wasMouseClicked()) {
158: FileDialog fd = new FileDialog(UnitApplet.getApplet()
159: .getFrame(),
160: "Please locate the script you want to run!");
161: fd.setVisible(true);
162: if (fd.getFile() != null) {
163: try {
164: homepathEdit.setText(fd.getDirectory());
165: } catch (Exception ex) {
166: ex.printStackTrace();
167: }
168: }
169: }
170: }
171:
172: public String getCommandLine() {
173: return "c://jdk1.5.0//bin//java -classpath " + getClasspath()
174: + " " + getMainClass() + " " + getArguments();
175: }
176:
177: public String getDelay() {
178: return delay;
179: }
180:
181: public String getMainClass() {
182: return mainClassEdit.getText().trim();
183: }
184:
185: public String getArguments() {
186: return argumentsEdit.getText().trim();
187: }
188:
189: public String getClasspath() {
190: return classpathEdit.getText().trim();
191: }
192:
193: public String getHomeDir() {
194: return homepathEdit.getText().trim();
195: }
196:
197: public void pageCreated() {
198: btnProceed = (XButton) findComponent("btnProceed");
199: radRecord = (XRadioButton) findComponent("radRecord");
200: radRun = (XRadioButton) findComponent("radRun");
201: pnlRun = (XPanel) findComponent("pnlRun");
202: pnlRecord = (XPanel) findComponent("pnlRecord");
203: txtFile = (XEdit) findComponent("txtFile");
204: lblDesc = (XTextArea) findComponent("lblDesc");
205:
206: mainClassEdit = (XEdit) findComponent("mainClassEdit");
207: argumentsEdit = (XEdit) findComponent("argumentsEdit");
208: classpathEdit = (XEdit) findComponent("classpathEdit");
209: homepathEdit = (XEdit) findComponent("homepathEdit");
210: btnLocateHome = (XButton) findComponent("btnLocateHome");
211: btnLocate = (XButton) findComponent("btnLocate");
212: txtLabel = (XLabel) findComponent("txtLabel");
213:
214: String prefsText = prefs.get("mainClass",
215: "net.xoetrope.swing.XApplet");
216: mainClassEdit.setText(prefsText);
217:
218: prefsText = prefs.get("arguments", "startup.properties");
219: argumentsEdit.setText(prefsText);
220:
221: prefsText = prefs.get("classpath",
222: ".;./lib/XuiCore.jar;./resources;./pages;./classes");
223: classpathEdit.setText(prefsText);
224:
225: prefsText = prefs.get("homepath", "C:/Xui");
226: homepathEdit.setText(prefsText);
227:
228: prefsText = prefs.get("delay", "0");
229: delay = prefsText;
230: }
231:
232: public void pageActivated() {
233: txtLabel.setEnabled(radRun.isSelected());
234: btnLocate.setEnabled(radRun.isSelected());
235: txtFile.setEnabled(radRun.isSelected());
236: lblDesc.setEnabled(radRun.isSelected());
237: }
238:
239: private void savePreferences() {
240: prefs.put("mainClass", mainClassEdit.getText());
241: prefs.put("arguments", argumentsEdit.getText());
242: prefs.put("classpath", classpathEdit.getText());
243: prefs.put("homepath", homepathEdit.getText());
244: prefs.put("delay", delay);
245: }
246:
247: /**
248: * Invoked when an event is dispatched in the AWT.
249: */
250: public void eventDispatched(AWTEvent event) {
251: try {
252: // Filter the event source.
253: Object source = event.getSource();
254: if ((source == mainClassObject)
255: && (event.getID() == WindowEvent.WINDOW_CLOSED))
256: mainClassObject = null;
257: else if ((source instanceof FileDialog)
258: || (source.getClass().getName().indexOf(
259: "net.xoetrope") > -1))
260: ;
261: else if (source != UnitApplet.getApplet().getFrame()) {
262: mainClassObject = event.getSource();
263: setMainClass();
264: }
265: } catch (Exception e) {
266: e.printStackTrace();
267: }
268: }
269:
270: /**
271: * Set the main class for dependant pages.
272: */
273: private void setMainClass() {
274: ScriptPage recorder = (ScriptPage) pageMgr.getPage(radRun
275: .isSelected() ? "tpApplicationRunner"
276: : "tpApplicationRecorder");
277: if (recorder != null)
278: recorder.setMainClassObject(mainClassObject);
279: }
280: }
|