source: release-kits/wirk3/bin/launch4j/src/net/sf/launch4j/formimpl/MainFrame.java@ 15023

Last change on this file since 15023 was 15023, checked in by oranfry, 16 years ago

did the bulk of the work on wirk3

File size: 10.6 KB
Line 
1/*
2 Launch4j (http://launch4j.sourceforge.net/)
3 Cross-platform Java application wrapper for creating Windows native executables.
4
5 Copyright (C) 2004, 2006 Grzegorz Kowal
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22/*
23 * Created on 2005-05-09
24 */
25package net.sf.launch4j.formimpl;
26
27import java.awt.BorderLayout;
28import java.awt.Dimension;
29import java.awt.Toolkit;
30import java.awt.event.ActionEvent;
31import java.awt.event.ActionListener;
32import java.awt.event.WindowAdapter;
33import java.awt.event.WindowEvent;
34import java.io.File;
35
36import javax.swing.ImageIcon;
37import javax.swing.JButton;
38import javax.swing.JFileChooser;
39import javax.swing.JFrame;
40import javax.swing.JOptionPane;
41import javax.swing.JToolBar;
42import javax.swing.UIManager;
43
44import com.jgoodies.plaf.Options;
45import com.jgoodies.plaf.plastic.PlasticXPLookAndFeel;
46
47import foxtrot.Task;
48import foxtrot.Worker;
49
50import net.sf.launch4j.Builder;
51import net.sf.launch4j.BuilderException;
52import net.sf.launch4j.ExecException;
53import net.sf.launch4j.FileChooserFilter;
54import net.sf.launch4j.Log;
55import net.sf.launch4j.Main;
56import net.sf.launch4j.Util;
57import net.sf.launch4j.binding.BindingException;
58import net.sf.launch4j.binding.InvariantViolationException;
59import net.sf.launch4j.config.Config;
60import net.sf.launch4j.config.ConfigPersister;
61import net.sf.launch4j.config.ConfigPersisterException;
62
63/**
64 * @author Copyright (C) 2005 Grzegorz Kowal
65 */
66public class MainFrame extends JFrame {
67 private static MainFrame _instance;
68
69 private final JToolBar _toolBar;
70 private final JButton _runButton;
71 private final ConfigFormImpl _configForm;
72 private final JFileChooser _fileChooser = new JFileChooser();
73 private File _outfile;
74 private boolean _saved = false;
75
76 public static void createInstance() {
77 try {
78 Toolkit.getDefaultToolkit().setDynamicLayout(true);
79 System.setProperty("sun.awt.noerasebackground","true"); //$NON-NLS-1$ //$NON-NLS-2$
80
81 // JGoodies
82 Options.setDefaultIconSize(new Dimension(16, 16)); // menu icons
83 Options.setUseNarrowButtons(false);
84 Options.setPopupDropShadowEnabled(true);
85
86 UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
87 _instance = new MainFrame();
88 } catch (Exception e) {
89 System.err.println(e);
90 }
91 }
92
93 public static MainFrame getInstance() {
94 return _instance;
95 }
96
97 public MainFrame() {
98 showConfigName(null);
99 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
100 addWindowListener(new MainFrameListener());
101 setGlassPane(new GlassPane(this));
102 _fileChooser.setFileFilter(new FileChooserFilter(
103 Messages.getString("MainFrame.config.files"), //$NON-NLS-1$
104 new String[] {".xml", ".cfg"})); //$NON-NLS-1$ //$NON-NLS-2$
105
106 _toolBar = new JToolBar();
107 _toolBar.setFloatable(false);
108 _toolBar.setRollover(true);
109 addButton("images/new.png", //$NON-NLS-1$
110 Messages.getString("MainFrame.new.config"), //$NON-NLS-1$
111 new NewActionListener());
112 addButton("images/open.png", //$NON-NLS-1$
113 Messages.getString("MainFrame.open.config"), //$NON-NLS-1$
114 new OpenActionListener());
115 addButton("images/save.png", //$NON-NLS-1$
116 Messages.getString("MainFrame.save.config"), //$NON-NLS-1$
117 new SaveActionListener());
118 _toolBar.addSeparator();
119 addButton("images/build.png", //$NON-NLS-1$
120 Messages.getString("MainFrame.build.wrapper"), //$NON-NLS-1$
121 new BuildActionListener());
122 _runButton = addButton("images/run.png", //$NON-NLS-1$
123 Messages.getString("MainFrame.test.wrapper"), //$NON-NLS-1$
124 new RunActionListener());
125 setRunEnabled(false);
126 _toolBar.addSeparator();
127 addButton("images/info.png", //$NON-NLS-1$
128 Messages.getString("MainFrame.about.launch4j"), //$NON-NLS-1$
129 new AboutActionListener());
130
131 _configForm = new ConfigFormImpl();
132 getContentPane().setLayout(new BorderLayout());
133 getContentPane().add(_toolBar, BorderLayout.NORTH);
134 getContentPane().add(_configForm, BorderLayout.CENTER);
135 pack();
136 Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
137 Dimension fr = getSize();
138 fr.height += 100;
139 setBounds((scr.width - fr.width) / 2, (scr.height - fr.height) / 2,
140 fr.width, fr.height);
141 setVisible(true);
142 }
143
144 private JButton addButton(String iconPath, String tooltip, ActionListener l) {
145 ImageIcon icon = new ImageIcon(MainFrame.class.getClassLoader().getResource(iconPath));
146 JButton b = new JButton(icon);
147 b.setToolTipText(tooltip);
148 b.addActionListener(l);
149 _toolBar.add(b);
150 return b;
151 }
152
153 public void info(String text) {
154 JOptionPane.showMessageDialog(this,
155 text,
156 Main.PROGRAM_NAME,
157 JOptionPane.INFORMATION_MESSAGE);
158 }
159
160 public void warn(String text) {
161 JOptionPane.showMessageDialog(this,
162 text,
163 Main.PROGRAM_NAME,
164 JOptionPane.WARNING_MESSAGE);
165 }
166
167 public void warn(InvariantViolationException e) {
168 e.getBinding().markInvalid();
169 warn(e.getMessage());
170 e.getBinding().markValid();
171 }
172
173 private boolean isModified() {
174 return (!_configForm.isModified())
175 || JOptionPane.showConfirmDialog(MainFrame.this,
176 Messages.getString("MainFrame.discard.changes"), //$NON-NLS-1$
177 Messages.getString("MainFrame.confirm"), //$NON-NLS-1$
178 JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION;
179 }
180
181 private boolean save() {
182 // XXX
183 try {
184 _configForm.get(ConfigPersister.getInstance().getConfig());
185 if (_fileChooser.showSaveDialog(MainFrame.this) == JOptionPane.YES_OPTION) {
186 File f = _fileChooser.getSelectedFile();
187 if (!f.getPath().endsWith(".xml")) { //$NON-NLS-1$
188 f = new File(f.getPath() + ".xml"); //$NON-NLS-1$
189 }
190 ConfigPersister.getInstance().save(f);
191 _saved = true;
192 showConfigName(f);
193 return true;
194 }
195 return false;
196 } catch (InvariantViolationException ex) {
197 warn(ex);
198 return false;
199 } catch (BindingException ex) {
200 warn(ex.getMessage());
201 return false;
202 } catch (ConfigPersisterException ex) {
203 warn(ex.getMessage());
204 return false;
205 }
206 }
207
208 private void showConfigName(File config) {
209 setTitle(Main.PROGRAM_NAME + " - " //$NON-NLS-1$
210 + (config != null ? config.getName()
211 : Messages.getString("MainFrame.untitled"))); //$NON-NLS-1$
212 }
213
214 private void setRunEnabled(boolean enabled) {
215 if (!enabled) {
216 _outfile = null;
217 }
218 _runButton.setEnabled(enabled);
219 }
220
221 private void clearConfig() {
222 ConfigPersister.getInstance().createBlank();
223 _configForm.clear(ConfigPersister.getInstance().getConfig());
224 }
225
226 private class MainFrameListener extends WindowAdapter {
227 public void windowOpened(WindowEvent e) {
228 clearConfig();
229 }
230
231 public void windowClosing(WindowEvent e) {
232 if (isModified()) {
233 System.exit(0);
234 }
235 }
236 }
237
238 private class NewActionListener implements ActionListener {
239 public void actionPerformed(ActionEvent e) {
240 if (isModified()) {
241 clearConfig();
242 }
243 _saved = false;
244 showConfigName(null);
245 setRunEnabled(false);
246 }
247 }
248
249 private class OpenActionListener implements ActionListener {
250 public void actionPerformed(ActionEvent e) {
251 try {
252 if (isModified()
253 && _fileChooser.showOpenDialog(MainFrame.this) == JOptionPane.YES_OPTION) {
254 final File f = _fileChooser.getSelectedFile();
255 if (f.getPath().endsWith(".xml")) { //$NON-NLS-1$
256 ConfigPersister.getInstance().load(f);
257 _saved = true;
258 } else {
259 ConfigPersister.getInstance().loadVersion1(f);
260 _saved = false;
261 }
262 _configForm.put(ConfigPersister.getInstance().getConfig());
263 showConfigName(f);
264 setRunEnabled(false);
265 }
266 } catch (ConfigPersisterException ex) {
267 warn(ex.getMessage());
268 }
269 }
270 }
271
272 private class SaveActionListener implements ActionListener {
273 public void actionPerformed(ActionEvent e) {
274 save();
275 }
276 }
277
278 private class BuildActionListener implements ActionListener {
279 public void actionPerformed(ActionEvent e) {
280 final Log log = Log.getSwingLog(_configForm.getLogTextArea());
281 try {
282 if ((!_saved || _configForm.isModified())
283 && !save()) {
284 return;
285 }
286 log.clear();
287 ConfigPersister.getInstance().getConfig().checkInvariants();
288 Builder b = new Builder(log);
289 _outfile = b.build();
290 setRunEnabled(ConfigPersister.getInstance().getConfig()
291 .getHeaderType() == Config.GUI_HEADER // TODO fix console app test
292 && (Util.WINDOWS_OS || !ConfigPersister.getInstance()
293 .getConfig().isDontWrapJar()));
294 } catch (InvariantViolationException ex) {
295 setRunEnabled(false);
296 ex.setBinding(_configForm.getBinding(ex.getProperty())); // XXX
297 warn(ex);
298 } catch (BuilderException ex) {
299 setRunEnabled(false);
300 log.append(ex.getMessage());
301 }
302 }
303 }
304
305 private class RunActionListener implements ActionListener {
306 public void actionPerformed(ActionEvent e) {
307 try {
308 getGlassPane().setVisible(true);
309 Worker.post(new Task() {
310 public Object run() throws ExecException {
311 Log log = Log.getSwingLog(_configForm.getLogTextArea());
312 log.clear();
313 String path = _outfile.getPath();
314 if (Util.WINDOWS_OS) {
315 log.append(Messages.getString("MainFrame.executing") + path); //$NON-NLS-1$
316 Util.exec(path, log);
317 } else {
318 log.append(Messages.getString("MainFrame.jar.integrity.test") + path); //$NON-NLS-1$
319 Util.exec("java -jar " + path, log); //$NON-NLS-1$
320 }
321 return null;
322 }
323 });
324 } catch (Exception ex) {
325 // XXX errors logged by exec
326 } finally {
327 getGlassPane().setVisible(false);
328 }
329 };
330 }
331
332 private class AboutActionListener implements ActionListener {
333 public void actionPerformed(ActionEvent e) {
334 info(Main.PROGRAM_DESCRIPTION
335 + "The following projects are used by launch4j...\n\n" //$NON-NLS-1$
336 + "MinGW binutils (http://www.mingw.org/)\n" //$NON-NLS-1$
337 + "Commons BeanUtils (http://jakarta.apache.org/commons/beanutils/)\n" //$NON-NLS-1$
338 + "Commons Logging (http://jakarta.apache.org/commons/logging/)\n" //$NON-NLS-1$
339 + "XStream (http://xstream.codehaus.org/)\n" //$NON-NLS-1$
340 + "JGoodies Forms (http://www.jgoodies.com/freeware/forms/)\n" //$NON-NLS-1$
341 + "JGoodies Looks (http://www.jgoodies.com/freeware/looks/)\n" //$NON-NLS-1$
342 + "Foxtrot (http://foxtrot.sourceforge.net/)\n" //$NON-NLS-1$
343 + "Nuvola Icon Theme (http://www.icon-king.com)"); //$NON-NLS-1$
344 }
345 }
346}
Note: See TracBrowser for help on using the repository browser.