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 net.charabia.jsmoothgen.application.*;
024: import net.charabia.jsmoothgen.skeleton.*;
025: import net.charabia.jsmoothgen.application.gui.util.*;
026: import javax.swing.*;
027: import java.awt.event.*;
028: import java.awt.*;
029: import java.io.File;
030: import java.util.*;
031: import com.l2fprod.common.swing.*;
032:
033: import se.datadosen.component.RiverLayout;
034:
035: public class MasterPanel extends JPanel {
036: private JButtonBar m_leftBar = new JButtonBar(JButtonBar.VERTICAL);
037: private ButtonGroup m_leftGroup = new ButtonGroup();
038: private JPanel m_mainpanel = new JPanel();
039: private JScrollBar m_mainpanelVBar;
040: private JScrollPane m_scrollpane;
041:
042: // private ResourceBundle m_texts;
043:
044: private Vector m_displayedElements = new Vector();
045:
046: private JSmoothModelBean m_model = new JSmoothModelBean();
047: private java.io.File m_modelLocation = null;
048:
049: private EditorPool m_edPool = new EditorPool();
050:
051: private String m_currentPanelName = "";
052:
053: private Object[] m_skelElements = {
054: new net.charabia.jsmoothgen.application.gui.editors.SkeletonChooser(),
055: new net.charabia.jsmoothgen.application.gui.editors.SkeletonPropertiesEditor()
056: // new net.charabia.jsmoothgen.application.gui.editors.SkeletonProperties()
057: };
058:
059: private Object[] m_execElements = {
060: "GUI_LABEL_EXECUTABLE_SETTINGS",
061: new net.charabia.jsmoothgen.application.gui.editors.ExecutableName(),
062: new net.charabia.jsmoothgen.application.gui.editors.ExecutableIcon(),
063: new net.charabia.jsmoothgen.application.gui.editors.CurrentDirectory() };
064:
065: private Object[] m_appElements = {
066: "GUI_LABEL_APPLICATION_SETTINGS",
067: new net.charabia.jsmoothgen.application.gui.editors.MainClass(),
068: new net.charabia.jsmoothgen.application.gui.editors.ApplicationArguments(),
069: "GUI_LABEL_EMBEDDEDJAR_SETTINGS",
070: new net.charabia.jsmoothgen.application.gui.editors.EmbeddedJar(),
071: new net.charabia.jsmoothgen.application.gui.editors.ClassPath() };
072:
073: private Object[] m_jvmSelElements = {
074: "GUI_LABEL_JAVA_VERSION",
075: new net.charabia.jsmoothgen.application.gui.editors.MinVersion(),
076: new net.charabia.jsmoothgen.application.gui.editors.MaxVersion(),
077: "GUI_LABEL_BUNDLEDJRE",
078: new net.charabia.jsmoothgen.application.gui.editors.JVMBundle(),
079: new net.charabia.jsmoothgen.application.gui.editors.JVMSearchSequence() };
080:
081: private Object[] m_jvmCfgElements = {
082: "GUI_LABEL_MEMORYSETTINGS",
083: new net.charabia.jsmoothgen.application.gui.editors.MaxMemoryHeap(),
084: new net.charabia.jsmoothgen.application.gui.editors.InitialMemoryHeap(),
085: new net.charabia.jsmoothgen.application.gui.editors.JavaProperties() };
086:
087: private Object[] m_jsInfo = { new net.charabia.jsmoothgen.application.gui.editors.JSmoothInfo() };
088:
089: public MasterPanel() {
090: setLayout(new BorderLayout());
091: add(BorderLayout.WEST,
092: m_scrollpane = new JScrollPane(m_leftBar));
093: JScrollPane scp = new JScrollPane(m_mainpanel);
094: m_mainpanelVBar = scp.getVerticalScrollBar();
095: add(BorderLayout.CENTER, scp);
096: scp
097: .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
098: // add(BorderLayout.CENTER, m_mainpanel);
099: m_mainpanel.setLayout(new RiverLayout());
100:
101: addAction("Welcome", "/icons/stock_form-properties.png",
102: m_jsInfo);
103: addAction("Skeleton", "/icons/stock_new-template.png",
104: m_skelElements);
105: addAction("Executable", "/icons/stock_autopilot-24.png",
106: m_execElements);
107: addAction("Application", "/icons/stock_form-image-control.png",
108: m_appElements);
109: addAction("JVM Selection", "/icons/stock_search.png",
110: m_jvmSelElements);
111: addAction("JVM Configuration",
112: "/icons/stock_form-properties.png", m_jvmCfgElements);
113:
114: setupPanel(m_jsInfo);
115: }
116:
117: private String getLocaleText(String key) {
118: try {
119: String value = Main.TEXTS.getString(key);
120: return value;
121: } catch (Exception exc) {
122: }
123: return key;
124: }
125:
126: private void addAction(final String name, String iconloc,
127: final Object[] els) {
128: final Action a = new AbstractAction(name, new ImageIcon(
129: getClass().getResource(iconloc))) {
130: public void actionPerformed(ActionEvent e) {
131: if (m_currentPanelName.equals(name))
132: return;
133: setupPanel(els);
134: m_currentPanelName = name;
135: }
136: };
137:
138: JToggleButton jtb = new JToggleButton(a);
139: m_leftGroup.add(jtb);
140: m_leftBar.add(jtb);
141: }
142:
143: public void setupPanel(Object[] els) {
144: fireUpdateModel();
145: detachAll();
146:
147: m_mainpanel.removeAll();
148: m_displayedElements.removeAllElements();
149:
150: // System.out.println("Adding " + els);
151:
152: if (els == null)
153: return;
154:
155: JPanel pgroup = null;
156:
157: for (int i = 0; i < els.length; i++) {
158: if (els[i] instanceof Editor) {
159: // Editor ed = m_edPool.getInstance(els[i]);
160: Editor ed = (Editor) els[i];
161: if (ed.needsBigSpace() && (pgroup != null)) {
162: m_mainpanel.add("br hfill", pgroup);
163: pgroup = null;
164: }
165:
166: if (pgroup == null) {
167: pgroup = new JPanel();
168: pgroup.setLayout(new RiverLayout());
169: javax.swing.border.TitledBorder title = BorderFactory
170: .createTitledBorder(
171: BorderFactory
172: .createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED),
173: "");
174: }
175:
176: if (ed.needsFullSpace()) {
177: pgroup.add("left hfill vfill", ed);
178: String gc = "br hfill vfill";
179: m_mainpanel.add(gc, ed);
180: ed.setMaximumSize(new java.awt.Dimension(300, 300));
181: m_mainpanel.add("tab", new JLabel("..."));
182: pgroup = null;
183: // m_scrollpane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
184: //m_scrollpane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
185: } else if (ed.needsBigSpace()) {
186: javax.swing.border.TitledBorder title = BorderFactory
187: .createTitledBorder(
188: BorderFactory
189: .createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED),
190: getLocaleText(ed.getLabel()));
191: pgroup.setBorder(title);
192: pgroup.add("left vtop", new HelpButton(
193: getLocaleText(ed.getDescription())));
194: pgroup.add("tab left hfill", ed);
195: String gc = "br hfill";
196: if (i + 1 >= els.length)
197: gc = "p hfill vfill";
198: m_mainpanel.add(gc, pgroup);
199: pgroup = null;
200: } else {
201: if (ed.useDescription()) {
202: pgroup.add("br left", new JLabel(
203: getLocaleText(ed.getLabel())));
204: pgroup.add("tab", new HelpButton(
205: getLocaleText(ed.getDescription())));
206: pgroup.add("tab hfill", ed);
207: } else {
208: pgroup.add("left hfill", ed);
209: }
210: }
211:
212: m_displayedElements.add(ed);
213: } else if (els[i] instanceof String) {
214: // System.out.println("TITLE: " + els[i]);
215: if (pgroup != null) {
216: m_mainpanel.add("br hfill", pgroup);
217: pgroup = null;
218: }
219:
220: pgroup = new JPanel();
221: pgroup.setLayout(new RiverLayout());
222:
223: javax.swing.border.TitledBorder title = BorderFactory
224: .createTitledBorder(
225: BorderFactory
226: .createEtchedBorder(javax.swing.border.EtchedBorder.LOWERED),
227: getLocaleText((String) els[i]));
228: pgroup.setBorder(title);
229: }
230:
231: // try {
232:
233: // } catch (Exception exc)
234: // {
235: // exc.printStackTrace();
236: // }
237: }
238: if (pgroup != null) {
239: m_mainpanel.add("p hfill", pgroup);
240: }
241: m_mainpanelVBar.setValue(0);
242: attachAll();
243:
244: validate();
245: repaint();
246: }
247:
248: public void fireUpdateModel() {
249: for (Iterator i = m_displayedElements.iterator(); i.hasNext();) {
250: Editor ed = (Editor) i.next();
251: ed.updateModel();
252: }
253: }
254:
255: public void fireModelChanged() {
256: for (Iterator i = m_displayedElements.iterator(); i.hasNext();) {
257: Editor ed = (Editor) i.next();
258: ed.dataChanged();
259: }
260: }
261:
262: private void detachAll() {
263: for (Iterator i = m_displayedElements.iterator(); i.hasNext();) {
264: Editor ed = (Editor) i.next();
265:
266: if (ed instanceof JSmoothModelBean.Listener)
267: m_model.removeListener((JSmoothModelBean.Listener) ed);
268: if (ed instanceof JSmoothModelBean.SkeletonChangedListener)
269: m_model
270: .removeSkeletonChangedListener((JSmoothModelBean.SkeletonChangedListener) ed);
271:
272: ed.detach();
273: }
274: }
275:
276: private void attachAll() {
277: // System.out.println("Attaching all with " + m_modelLocation + ": " + m_model);
278: for (Iterator i = m_displayedElements.iterator(); i.hasNext();) {
279: Editor ed = (Editor) i.next();
280:
281: File basedir = null;
282: if (m_modelLocation != null)
283: basedir = m_modelLocation.getParentFile();
284:
285: ed.attach(m_model, basedir);
286: if (ed instanceof JSmoothModelBean.Listener)
287: m_model.addListener((JSmoothModelBean.Listener) ed);
288: if (ed instanceof JSmoothModelBean.SkeletonChangedListener)
289: m_model
290: .addSkeletonChangedListener((JSmoothModelBean.SkeletonChangedListener) ed);
291:
292: ed.dataChanged();
293: }
294: }
295:
296: public void newModel() {
297: JSmoothModelBean bean = new JSmoothModelBean();
298: newModel(bean, null);
299: }
300:
301: public void newModel(JSmoothModelBean bean, java.io.File location) {
302: detachAll();
303:
304: m_model = bean;
305: m_modelLocation = location;
306:
307: attachAll();
308: }
309:
310: public boolean openFile(java.io.File f) {
311: m_modelLocation = f;
312:
313: try {
314: JSmoothModelBean model = JSmoothModelPersistency
315: .load(m_modelLocation);
316: newModel(model, f);
317: return true;
318: } catch (java.io.IOException iox) {
319: iox.printStackTrace();
320: return false;
321: }
322: }
323:
324: public boolean save() {
325: if (m_modelLocation == null)
326: return false;
327: try {
328: fireUpdateModel();
329: m_model.normalizePaths(m_modelLocation.getParentFile(),
330: true);
331: JSmoothModelPersistency.save(m_modelLocation, m_model);
332: // System.out.println("saving model " + m_model);
333: // fireModelChanged();
334: return true;
335: } catch (java.io.IOException iox) {
336: iox.printStackTrace();
337: }
338: return false;
339: }
340:
341: private Vector m_lastErrors = new Vector();
342:
343: public Vector getLastErrors() {
344: return m_lastErrors;
345: }
346:
347: public ExeCompiler.CompilerRunner getCompiler() {
348: fireUpdateModel();
349: m_model.normalizePaths(m_modelLocation.getParentFile());
350: m_lastErrors.removeAllElements();
351:
352: SkeletonBean skel = Main.SKELETONS.getSkeleton(m_model
353: .getSkeletonName());
354: if (skel == null) {
355: m_lastErrors.add(Main.local("UNKNOWN_SKEL"));
356: return null;
357: }
358:
359: File skelroot = Main.SKELETONS.getDirectory(skel);
360: File basedir = m_modelLocation.getParentFile();
361: File exedir = basedir;
362:
363: try {
364: File out = null;
365: if (new File(m_model.getExecutableName()).isAbsolute() == false)
366: out = new File(exedir, m_model.getExecutableName());
367: else
368: out = new File(m_model.getExecutableName());
369:
370: // System.out.println("out = "+ out.getAbsolutePath());
371: ExeCompiler compiler = new ExeCompiler();
372: ExeCompiler.CompilerRunner runner = compiler.getRunnable(
373: skelroot, skel, basedir, m_model, out);
374: return runner;
375: } catch (Exception exc) {
376: exc.printStackTrace();
377: m_lastErrors.add(exc.getMessage());
378: return null;
379: }
380: }
381:
382: public void runexe() {
383: fireUpdateModel();
384:
385: try {
386: File basedir = m_modelLocation.getParentFile();
387: File f = new File(basedir, m_model.getExecutableName());
388: String[] cmd = new String[] { f.getAbsolutePath() };
389:
390: // System.out.println("RUNNING " + cmd[0] + " @ " + basedir);
391: CommandRunner.run(cmd, f.getParentFile());
392: } catch (Exception exc) {
393: exc.printStackTrace();
394: }
395: }
396:
397: public java.io.File getProjectFile() {
398: return m_modelLocation;
399: }
400:
401: public void setProjectFile(java.io.File prjfile) {
402: m_modelLocation = prjfile;
403: }
404:
405: public JSmoothModelBean getModel() {
406: return m_model;
407: }
408:
409: public static void main(String args[]) {
410: try {
411: UIManager.setLookAndFeel(UIManager
412: .getSystemLookAndFeelClassName());
413: } catch (Exception e) {
414: }
415: JFrame test = new JFrame("test");
416: test.getContentPane().add(new MasterPanel());
417: test.pack();
418: test.setVisible(true);
419: }
420: }
|