001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application.gui;
022:
023: import javax.swing.*;
024: import java.awt.event.*;
025: import java.awt.*;
026: import net.charabia.jsmoothgen.skeleton.*;
027: import java.util.*;
028: import net.charabia.jsmoothgen.application.gui.util.*;
029: import net.charabia.jsmoothgen.application.*;
030: import java.util.prefs.*;
031:
032: public class Main extends JFrame {
033: final static public SkeletonList SKELETONS = new SkeletonList(
034: new java.io.File("skeletons"));
035: final static public String VERSION = "@{VERSION}@";
036: final static public String RELEASEINFO = "@{RELEASEINFO}@";
037:
038: final static public ResourceBundle TEXTS = PropertyResourceBundle
039: .getBundle("locale.Texts");
040:
041: static public Main MAIN;
042:
043: private MasterPanel m_panel;
044:
045: private javax.swing.JFileChooser m_projectFileChooser = new JFileChooser();
046: private RecentFileMenu m_recentFiles = null;
047:
048: private Main() {
049: Splash splash = new Splash(this , "/icons/splash.png", false);
050: splash.setVersion(VERSION);
051: splash.show();
052:
053: m_projectFileChooser
054: .addChoosableFileFilter(new SimpleFileFilter("jsmooth",
055: "JSmooth Project Files"));
056:
057: getContentPane().setLayout(new BorderLayout());
058: m_panel = new MasterPanel();
059: getContentPane().add(BorderLayout.CENTER, m_panel);
060:
061: setupMenus();
062: setupToolBar();
063:
064: addWindowListener(new java.awt.event.WindowAdapter() {
065: public void windowClosing(java.awt.event.WindowEvent evt) {
066: EXIT.actionPerformed(null);
067: }
068: });
069:
070: setTitle("Untitled");
071: loadWindowSettings();
072: splash.dispose();
073: }
074:
075: private void setupMenus() {
076: JMenuBar bar = new JMenuBar();
077: setJMenuBar(bar);
078:
079: JMenu menu = new JMenu(local("MENU_SYSTEM"));
080: menu.add(new JMenuItem(NEW));
081: menu.addSeparator();
082: menu.add(new JMenuItem(OPEN));
083: menu.add(new JMenuItem(SAVE));
084: menu.add(new JMenuItem(SAVE_AS));
085: menu.addSeparator();
086: JMenu recentfiles = new JMenu(Main.local("MENU_RECENT"));
087: m_recentFiles = new RecentFileMenu(recentfiles, 5, Main.class,
088: new RecentFileMenu.Action() {
089: public void action(String path) {
090: if (m_panel.openFile(new java.io.File(path)))
091: setTitle(path);
092: }
093: });
094:
095: menu.add(recentfiles);
096: menu.addSeparator();
097: menu.add(new JMenuItem(EXIT));
098: bar.add(menu);
099:
100: menu = new JMenu(local("MENU_PROJECT"));
101: menu.add(new JMenuItem(COMPILE));
102: menu.add(new JMenuItem(RUNEXE));
103: bar.add(menu);
104:
105: menu = new JMenu(local("MENU_HELP"));
106: menu.add(new JMenuItem(ABOUT));
107: bar.add(menu);
108: }
109:
110: private void setupToolBar() {
111: JToolBar bar = new JToolBar();
112: bar.add(NEW);
113: bar.addSeparator();
114: bar.add(OPEN);
115: bar.add(SAVE);
116: bar.add(SAVE_AS);
117: bar.addSeparator();
118: bar.add(COMPILE);
119: bar.add(RUNEXE);
120: bar.addSeparator();
121: bar.add(ABOUT);
122: getContentPane().add(BorderLayout.NORTH, bar);
123: }
124:
125: public static String local(String key) {
126: try {
127: String value = Main.TEXTS.getString(key);
128: return value;
129: } catch (Exception exc) {
130: }
131: return "[" + key + "]";
132: }
133:
134: private final Icon ICON_NEW = new javax.swing.ImageIcon(getClass()
135: .getResource("/icons/stock_new.png"));
136: public final Action NEW = new AbstractAction(local("NEW"), ICON_NEW) {
137: public void actionPerformed(ActionEvent e) {
138: m_panel.newModel();
139: setTitle("Untitled");
140: }
141: };
142:
143: private final Icon ICON_OPEN = new javax.swing.ImageIcon(getClass()
144: .getResource("/icons/stock_open.png"));
145: public final Action OPEN = new AbstractAction(local("OPEN"),
146: ICON_OPEN) {
147: public void actionPerformed(ActionEvent e) {
148: if (m_projectFileChooser.showOpenDialog(Main.this ) == JFileChooser.APPROVE_OPTION) {
149: java.io.File f = m_projectFileChooser.getSelectedFile();
150: if (m_panel.openFile(f)) {
151: // TODO
152: m_recentFiles.add(f.getAbsolutePath());
153: setTitle(f.getAbsolutePath());
154: }
155:
156: // if (openDirect(m_projectFileChooser.getSelectedFile()))
157: // m_recentMenuManager.add(m_projectFileChooser.getSelectedFile().getAbsolutePath());
158: }
159: }
160: };
161:
162: private final Icon ICON_SAVE = new javax.swing.ImageIcon(getClass()
163: .getResource("/icons/stock_save.png"));
164: public final Action SAVE = new AbstractAction(local("SAVE"),
165: ICON_SAVE) {
166: public void actionPerformed(ActionEvent e) {
167: if (m_panel.getProjectFile() == null)
168: SAVE_AS.actionPerformed(e);
169: else {
170: m_panel.save();
171: }
172: }
173: };
174:
175: private final Icon ICON_SAVE_AS = new javax.swing.ImageIcon(
176: getClass().getResource("/icons/stock_save_as.png"));
177: public final Action SAVE_AS = new AbstractAction(local("SAVE_AS"),
178: ICON_SAVE_AS) {
179: public void actionPerformed(ActionEvent e) {
180: if (m_projectFileChooser.showSaveDialog(Main.this ) == JFileChooser.APPROVE_OPTION) {
181: if ((m_panel.getModel() != null)
182: && (m_panel.getProjectFile() != null))
183: m_panel.getModel().normalizePaths(
184: m_panel.getProjectFile().getParentFile(),
185: false);
186:
187: java.io.File nf = m_projectFileChooser
188: .getSelectedFile();
189: String suf = getSuffix(nf);
190: if ("jsmooth".equalsIgnoreCase(suf) == false) {
191: nf = new java.io.File(nf.toString() + ".jsmooth");
192: }
193: if (m_panel.getModel() != null)
194: m_panel.getModel().normalizePaths(
195: nf.getParentFile(), true);
196:
197: m_panel.setProjectFile(nf);
198: setTitle(nf.getAbsolutePath());
199: m_panel.save();
200: m_recentFiles.add(nf.getAbsolutePath());
201: }
202:
203: }
204: };
205:
206: private final Icon ICON_EXIT = new javax.swing.ImageIcon(getClass()
207: .getResource("/icons/stock_exit-16.png"));
208: public final Action EXIT = new AbstractAction(local("EXIT"),
209: ICON_EXIT) {
210: public void actionPerformed(ActionEvent e) {
211: m_recentFiles.savePrefs();
212: saveWindowSettings();
213: if (m_panel.getProjectFile() != null) {
214: m_panel.save();
215: }
216: System.exit(0);
217: }
218: };
219:
220: private final Icon ICON_COMPILE = new javax.swing.ImageIcon(
221: getClass().getResource("/icons/stock_autopilot-24.png"));
222: public final Action COMPILE = new AbstractAction(local("COMPILE"),
223: ICON_COMPILE) {
224: public void actionPerformed(ActionEvent e) {
225: m_panel.fireUpdateModel();
226:
227: SAVE.actionPerformed(e);
228: if (m_panel.getProjectFile() == null)
229: return;
230:
231: final ExeCompiler.CompilerRunner compiler = m_panel
232: .getCompiler();
233:
234: CompilationDialog dia = new CompilationDialog(Main.this ,
235: true);
236: dia.setTitle(Main.local("COMPILATION_DIALOG_TITLE"));
237: dia.pack();
238:
239: if (compiler != null) {
240: dia.setCompiler(compiler.getCompiler());
241: dia.compile(compiler);
242: // return dia.getResult();
243: } else {
244: dia
245: .setNewState(100,
246: "Error, compiler couldn't be created. Error description should follow:");
247: Vector v = m_panel.getLastErrors();
248: for (Iterator i = v.iterator(); i.hasNext();) {
249: dia.setNewState(100, "- " + (i.next().toString()));
250: }
251: dia.setVisible(true);
252: }
253: }
254: };
255:
256: private final Icon ICON_RUNEXE = new javax.swing.ImageIcon(
257: getClass().getResource("/icons/stock_next.png"));
258: public final Action RUNEXE = new AbstractAction(local("RUNEXE"),
259: ICON_RUNEXE) {
260: public void actionPerformed(ActionEvent e) {
261: m_panel.runexe();
262: }
263: };
264:
265: private final Icon ICON_ABOUT = new javax.swing.ImageIcon(
266: getClass().getResource("/icons/stock_about.png"));
267: public final Action ABOUT = new AbstractAction(local("ABOUT"),
268: ICON_ABOUT) {
269: public void actionPerformed(ActionEvent e) {
270: AboutBox ab = new AboutBox(Main.this , true);
271: ab.setVersion(Main.VERSION + " (" + Main.RELEASEINFO + ")");
272: ab.setVisible(true);
273: }
274: };
275:
276: private String getSuffix(java.io.File f) {
277: String fstr = f.getAbsolutePath();
278: int lastDot = fstr.lastIndexOf('.');
279: if ((lastDot >= 0) && ((lastDot + 1) < fstr.length())) {
280: return fstr.substring(lastDot + 1);
281: }
282: return "";
283: }
284:
285: public void setTitle(String title) {
286: super .setTitle("JSmooth " + Main.VERSION + ": " + title);
287: }
288:
289: public void saveWindowSettings() {
290: Preferences prefs = Preferences.systemNodeForPackage(this
291: .getClass());
292: prefs.putInt("window-state", this .getExtendedState());
293: // setExtendedState(NORMAL);
294:
295: prefs.putInt("window-x", (int) this .getLocation().getX());
296: prefs.putInt("window-y", (int) this .getLocation().getY());
297:
298: prefs.putInt("window-width", (int) this .getWidth());
299: prefs.putInt("window-height", (int) this .getHeight());
300:
301: }
302:
303: public void loadWindowSettings() {
304: Preferences prefs = Preferences.systemNodeForPackage(this
305: .getClass());
306: this .setExtendedState(prefs
307: .getInt("window-state", Frame.NORMAL));
308:
309: if (prefs.getInt("window-x", -1) > 0) {
310: this .setLocation(prefs.getInt("window-x", 10), prefs
311: .getInt("window-y", 10));
312: int w = prefs.getInt("window-width", 500);
313: int h = prefs.getInt("window-height", 400);
314: if (w <= 0)
315: w = 400;
316: if (h <= 0)
317: h = 400;
318: this .setSize(w, h);
319: setExtendedState(prefs.getInt("window-state", NORMAL));
320: } else {
321: this .setSize(500, 400);
322: setExtendedState(prefs.getInt("window-state", NORMAL));
323: }
324: }
325:
326: public static void main(String args[]) {
327: System.out.println("Running JSmooth...");
328: try {
329: UIManager.setLookAndFeel(UIManager
330: .getSystemLookAndFeelClassName());
331: } catch (Exception e) {
332: e.printStackTrace();
333: }
334:
335: Main.MAIN = new Main();
336:
337: if (args.length > 0) {
338: java.io.File f = new java.io.File(args[0]);
339: if (f.exists()) {
340: Main.MAIN.m_panel.openFile(f);
341: Main.MAIN.setTitle(f.toString());
342: } else {
343: JOptionPane.showMessageDialog(null, Main.MAIN
344: .local("GENERAL_CANTOPENFILE"), Main.MAIN
345: .local("GENERAL_ERROR_LABEL"),
346: JOptionPane.ERROR_MESSAGE);
347: }
348: }
349:
350: Main.MAIN.setVisible(true);
351: }
352: }
|