001: package org.xorm.tools;
002:
003: import javax.swing.*;
004: import javax.swing.filechooser.*;
005: import javax.swing.event.*;
006: import java.awt.*;
007: import java.awt.event.*;
008: import java.io.*;
009: import java.util.*;
010:
011: import org.xorm.XORM;
012:
013: public class Wizard extends JFrame implements ActionListener {
014: public static final String PREFERENCES_FILENAME;
015:
016: static {
017: PREFERENCES_FILENAME = System.getProperty("user.home")
018: + System.getProperty("file.separator") + ".xorm-wizard";
019: }
020:
021: // Menus
022: protected JMenuItem fileNew, fileOpen, fileSave, fileSaveAs,
023: fileQuit;
024: protected ArrayList fileList = new ArrayList();
025: protected JMenuItem helpHome, helpGuide, helpTutorial;
026: protected JMenuItem toolsGenerateDB, toolsGenerateJDO,
027: toolsGenerateSource;
028:
029: protected File propertiesFile;
030: protected Properties properties;
031: protected Properties preferences;
032: protected JTabbedPane tabs;
033:
034: public Wizard() {
035: super ("XORM " + XORM.getVersion() + " - Setup Wizard");
036:
037: properties = new Properties();
038: properties.setProperty(
039: "javax.jdo.PersistenceManagerFactoryClass",
040: "org.xorm.InterfaceManagerFactory");
041:
042: loadPreferences();
043:
044: setupMenus();
045: setupTabs();
046: }
047:
048: public void actionPerformed(ActionEvent e) {
049: Object source = e.getSource();
050: if (source == fileQuit) {
051: if (doCheckExit()) {
052: savePreferences();
053: System.exit(0);
054: }
055: } else if (source == fileNew) {
056: //doCheckSave();
057: doFileNew();
058: } else if (source == fileOpen) {
059: //doCheckSave();
060: doFileOpen();
061: } else if (source == fileSave) {
062: doFileSave();
063: } else if (source == fileSaveAs) {
064: File file = chooseFile("Save As", "Save properties file");
065: if (file != null) {
066: propertiesFile = file;
067: doFileSave();
068: fileSave.setEnabled(true);
069: setTitle("XORM " + XORM.getVersion() + " - "
070: + file.getName());
071: }
072: } else if (source == helpHome) {
073: BrowserControl.displayURL("http://xorm.org/");
074: } else if (source == helpGuide) {
075: BrowserControl.displayURL("http://xorm.org/guide.html");
076: } else if (source == helpTutorial) {
077: BrowserControl.displayURL("http://xorm.org/tutorial.html");
078: } else if (fileList.contains(source)) {
079: propertiesFile = new File(((JMenuItem) source).getText());
080: openProperties();
081: }
082: }
083:
084: protected File chooseFile(String action, String description) {
085: final JFileChooser fc = new JFileChooser(preferences
086: .getProperty("lastDir"));
087: fc.setFileFilter(new javax.swing.filechooser.FileFilter() {
088: public boolean accept(File file) {
089: return file.isDirectory()
090: || file.getName().endsWith(".properties");
091: }
092:
093: public String getDescription() {
094: return "Properties Files";
095: }
096: });
097: fc.setDialogTitle(description);
098: int returnVal = fc.showDialog(this , action);
099: if (returnVal != fc.APPROVE_OPTION) {
100: return null;
101: }
102: preferences.setProperty("lastDir", fc.getSelectedFile()
103: .getParent().toString());
104: return fc.getSelectedFile();
105: }
106:
107: protected void doFileOpen() {
108: File file = chooseFile("Open", "Open properties file");
109: if (file == null)
110: return;
111: propertiesFile = file;
112: openProperties();
113: }
114:
115: protected void openProperties() {
116: setTitle("XORM " + XORM.getVersion() + " - "
117: + propertiesFile.getName());
118:
119: String oldRecent = preferences.getProperty("recentFiles", "");
120: if (oldRecent.indexOf(propertiesFile.toString()) == -1) {
121: if (oldRecent.split(",").length > 6) {
122: oldRecent = oldRecent.substring(0, oldRecent
123: .lastIndexOf(','));
124: }
125: preferences.setProperty("recentFiles", propertiesFile
126: .toString()
127: + "," + oldRecent);
128: }
129:
130: properties = new Properties();
131: try {
132: properties.load(new FileInputStream(propertiesFile));
133: } catch (IOException e) {
134: e.printStackTrace();
135: }
136:
137: changeProperties();
138: fileSave.setEnabled(true);
139: }
140:
141: // Notifies each tab of change to the properties source
142: protected void changeProperties() {
143: int len = tabs.getTabCount();
144: for (int i = 0; i < len; i++) {
145: ((PropertyEditor) tabs.getComponentAt(i))
146: .resetProperties(properties);
147: }
148: }
149:
150: protected void setupTabs() {
151: tabs = new JTabbedPane();
152:
153: String[] installedJars = System.getProperty("java.class.path")
154: .split(System.getProperty("path.separator"));
155:
156: PropertyEditor editor = new PropertyEditor(properties);
157: editor.addClassProperty(
158: "javax.jdo.option.ConnectionDriverName",
159: "JDBC Driver Class", installedJars,
160: java.sql.Driver.class);
161: editor.addProperty("javax.jdo.option.ConnectionURL",
162: "Connection URL");
163: editor.addProperty("javax.jdo.option.ConnectionUserName",
164: "User Name");
165: editor.addProperty("javax.jdo.option.ConnectionPassword",
166: "Password");
167: editor.addProperty("javax.jdo.option.MinPool",
168: "Connection Pool Minimum Size");
169: editor.addProperty("javax.jdo.option.MaxPool",
170: "Connection Pool Maximum Size");
171: tabs.addTab("Connection", null, editor,
172: "Edit standard connection properties");
173:
174: editor = new PropertyEditor(properties);
175: editor.addFileProperty("org.xorm.datastore.database",
176: "Database descriptor file", ".xml");
177: editor.addClassProperty(
178: "org.xorm.datastore.ConnectionInfoClass",
179: "Datastore Type", installedJars,
180: org.xorm.datastore.ConnectionInfo.class);
181: tabs.addTab("Datastore", null, editor,
182: "Edit custom datastore options");
183:
184: editor = new PropertyEditor(properties);
185: editor.addClassProperty("org.xorm.cache.DataCacheClass",
186: "Data Cache Class", installedJars,
187: org.xorm.cache.DataCache.class);
188: editor.addClassProperty("org.xorm.FetchGroupManagerClass",
189: "Fetch Group Manager Class", installedJars,
190: org.xorm.FetchGroupManager.class);
191: editor.addBooleanProperty("org.xorm.options.ValidateXML",
192: "Validate JDO and database XML");
193: editor.addBooleanProperty(
194: "org.xorm.options.ThreadLocalTransactions",
195: "Use thread local transactions");
196: tabs.addTab("Options", null, editor,
197: "Edit XORM specific options");
198:
199: JPanel throwaway = new JPanel();
200: throwaway.add(tabs);
201: setContentPane(throwaway);
202: show();
203: }
204:
205: private void setupMenus() {
206: JMenuItem item;
207:
208: JMenuBar menuBar = new JMenuBar();
209: setJMenuBar(menuBar);
210:
211: JMenu fileMenu = new JMenu("File");
212: fileMenu.setMnemonic(KeyEvent.VK_F);
213: menuBar.add(fileMenu);
214:
215: fileNew = new JMenuItem("New");
216: fileNew.setMnemonic(KeyEvent.VK_N);
217: fileNew.addActionListener(this );
218: fileMenu.add(fileNew);
219:
220: fileOpen = new JMenuItem("Open...");
221: fileOpen.setMnemonic(KeyEvent.VK_O);
222: fileOpen.addActionListener(this );
223: fileMenu.add(fileOpen);
224:
225: fileSave = new JMenuItem("Save");
226: fileSave.setMnemonic(KeyEvent.VK_S);
227: fileSave.addActionListener(this );
228: fileSave.setEnabled(false);
229: fileMenu.add(fileSave);
230:
231: fileSaveAs = new JMenuItem("Save As...");
232: fileSaveAs.setMnemonic(KeyEvent.VK_A);
233: fileSaveAs.addActionListener(this );
234: fileMenu.add(fileSaveAs);
235:
236: fileQuit = new JMenuItem("Quit");
237: fileQuit.setMnemonic(KeyEvent.VK_Q);
238: fileQuit.addActionListener(this );
239: fileMenu.add(fileQuit);
240:
241: String s = preferences.getProperty("recentFiles");
242: if (s != null) {
243: fileMenu.addSeparator();
244: String[] recent = s.split(",");
245: for (int i = 0; i < recent.length; i++) {
246: item = new JMenuItem(recent[i]);
247: //setMnemonic(KeyEvent.VK_);
248: item.addActionListener(this );
249: fileMenu.add(item);
250: fileList.add(item);
251: }
252: }
253:
254: JMenu toolsMenu = new JMenu("Tools");
255: toolsMenu.setMnemonic(KeyEvent.VK_T);
256: menuBar.add(toolsMenu);
257:
258: toolsGenerateDB = new JMenuItem("Generate Database XML");
259: toolsGenerateDB.addActionListener(this );
260: toolsMenu.add(toolsGenerateDB);
261: toolsGenerateJDO = new JMenuItem("Generate JDO Metadata");
262: toolsGenerateJDO.addActionListener(this );
263: toolsMenu.add(toolsGenerateJDO);
264: toolsGenerateSource = new JMenuItem("Generate Java Source Code");
265: toolsGenerateSource.addActionListener(this );
266: toolsMenu.add(toolsGenerateSource);
267:
268: JMenu helpMenu = new JMenu("Help");
269: helpMenu.setMnemonic(KeyEvent.VK_H);
270: menuBar.add(helpMenu);
271:
272: helpHome = new JMenuItem("About XORM");
273: helpHome.addActionListener(this );
274: helpMenu.add(helpHome);
275:
276: helpGuide = new JMenuItem("User's Guide");
277: helpGuide.addActionListener(this );
278: helpMenu.add(helpGuide);
279:
280: helpTutorial = new JMenuItem("Tutorial");
281: helpTutorial.addActionListener(this );
282: helpMenu.add(helpTutorial);
283: }
284:
285: public void doFileNew() {
286: properties = new Properties();
287: properties.setProperty(
288: "javax.jdo.PersistenceManagerFactoryClass",
289: "org.xorm.InterfaceManagerFactory");
290: fileSave.setEnabled(false);
291: changeProperties();
292: }
293:
294: public void doFileSave() {
295: try {
296: FileOutputStream out = new FileOutputStream(propertiesFile);
297: properties.store(out, "XORM " + XORM.getVersion()
298: + " properties (auto-generated)");
299: } catch (IOException e) {
300: e.printStackTrace();
301: }
302: }
303:
304: public boolean doCheckExit() {
305: return true;
306: }
307:
308: private void loadPreferences() {
309: preferences = new Properties();
310: try {
311: FileInputStream prefs = new FileInputStream(
312: PREFERENCES_FILENAME);
313: preferences.load(prefs);
314: } catch (FileNotFoundException e) {
315: preferences.setProperty("lastDir", ".");
316: } catch (IOException e) {
317: e.printStackTrace();
318: }
319:
320: }
321:
322: private void savePreferences() {
323: try {
324: preferences.store(
325: new FileOutputStream(PREFERENCES_FILENAME),
326: "XORM Wizard preferences");
327: } catch (IOException e) {
328: e.printStackTrace();
329: }
330: }
331:
332: public static void main(String[] argv) throws Exception {
333: String nativeLNF = UIManager.getSystemLookAndFeelClassName();
334: if (nativeLNF != null) {
335: UIManager.setLookAndFeel(nativeLNF);
336: }
337:
338: final Wizard wizard = new Wizard();
339: wizard.addWindowListener(new WindowAdapter() {
340: public void windowClosing(WindowEvent e) {
341: wizard.savePreferences();
342: System.exit(0);
343: }
344: });
345:
346: wizard.pack();
347: wizard.setVisible(true);
348: }
349: }
|