001: /* Copyright 2006-2007 David N. Welton
002:
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014: */
015:
016: package heclbuilder;
017:
018: import jarhack.JarHack;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.InputStream;
023: import javax.swing.JFileChooser;
024: import javax.swing.JOptionPane;
025:
026: import org.jdesktop.layout.GroupLayout;
027: import org.jdesktop.layout.GroupLayout;
028: import org.jdesktop.layout.LayoutStyle;
029:
030: /**
031: * <code>HeclBuilderGui</code> -- a gui for the creation of J2ME Hecl
032: * apps. Utilizes an internal copy of the relevant (MIDP 1.0 or 2.0)
033: * Hecl.jar as a sort of "template" that is combined with a
034: * user-supplied script.
035: *
036: * @author <a href="mailto:davidw@dedasys.com">David N. Welton</a>
037: * @version 1.0
038: */
039: public class HeclBuilderGui extends javax.swing.JFrame {
040:
041: private javax.swing.JLabel createLabel;
042: private javax.swing.JButton editButton;
043: private javax.swing.JLabel outputLabel;
044: private javax.swing.JMenu mainMenu;
045: private javax.swing.JMenuBar mainMenuBar;
046: private javax.swing.JMenuItem menuQuit;
047: private javax.swing.JLabel midletLabel;
048: private javax.swing.JTextField midletTextField;
049: private javax.swing.JButton outputSelectButton;
050: private javax.swing.JTextField outputTextField;
051: private javax.swing.JButton runButton;
052: private javax.swing.JFileChooser scriptFileChooser;
053: private javax.swing.JFileChooser outputDirChooser;
054: private javax.swing.JTextField scriptTextField;
055: private javax.swing.JButton selectScriptButton;
056: private javax.swing.JLabel selectLabel;
057:
058: private javax.swing.JRadioButton selectMidp10;
059: private javax.swing.JRadioButton selectMidp20;
060: private javax.swing.ButtonGroup selectMidp;
061:
062: /**
063: * Creates a new <code>HeclBuilderGui</code> instance.
064: *
065: */
066: public HeclBuilderGui() {
067: initComponents();
068: }
069:
070: /**
071: * The <code>initComponents</code> method creates all the GUI
072: * elements.
073: *
074: */
075: private void initComponents() {
076: selectLabel = new javax.swing.JLabel();
077: midletLabel = new javax.swing.JLabel();
078: outputLabel = new javax.swing.JLabel();
079: createLabel = new javax.swing.JLabel();
080: scriptTextField = new javax.swing.JTextField();
081: midletTextField = new javax.swing.JTextField();
082: selectScriptButton = new javax.swing.JButton();
083: runButton = new javax.swing.JButton();
084: editButton = new javax.swing.JButton();
085: outputTextField = new javax.swing.JTextField();
086: outputSelectButton = new javax.swing.JButton();
087: mainMenuBar = new javax.swing.JMenuBar();
088: mainMenu = new javax.swing.JMenu();
089: menuQuit = new javax.swing.JMenuItem();
090:
091: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
092: setTitle("HeclBuilder");
093: selectLabel.setText("Select a script");
094:
095: midletLabel.setText("MIDlet name");
096:
097: createLabel.setText("Create .jar and .jad files");
098:
099: scriptTextField.setColumns(40);
100: scriptTextField
101: .addCaretListener(new javax.swing.event.CaretListener() {
102: public void caretUpdate(
103: javax.swing.event.CaretEvent evt) {
104: scriptTextFieldCaretUpdate(evt);
105: }
106: });
107:
108: midletTextField
109: .addCaretListener(new javax.swing.event.CaretListener() {
110: public void caretUpdate(
111: javax.swing.event.CaretEvent evt) {
112: midletTextFieldCaretUpdate(evt);
113: }
114: });
115:
116: selectScriptButton.setText("...");
117: selectScriptButton
118: .addActionListener(new java.awt.event.ActionListener() {
119: public void actionPerformed(
120: java.awt.event.ActionEvent evt) {
121: selectScriptButtonActionPerformed(evt);
122: }
123: });
124:
125: selectMidp10 = new javax.swing.JRadioButton("MIDP 1.0", true);
126: selectMidp20 = new javax.swing.JRadioButton("MIDP 2.0");
127: selectMidp = new javax.swing.ButtonGroup();
128: selectMidp.add(selectMidp10);
129: selectMidp.add(selectMidp20);
130:
131: runButton.setText("Create .jar/.jad files");
132: runButton
133: .setToolTipText("Select a script and MIDlet name to activate this button");
134: runButton.setEnabled(false);
135: runButton
136: .addActionListener(new java.awt.event.ActionListener() {
137: public void actionPerformed(
138: java.awt.event.ActionEvent evt) {
139: runButtonActionPerformed(evt);
140: }
141: });
142:
143: runButton.getAccessibleContext().setAccessibleDescription("");
144:
145: editButton.setText("View/Edit");
146: editButton
147: .addActionListener(new java.awt.event.ActionListener() {
148: public void actionPerformed(
149: java.awt.event.ActionEvent evt) {
150: editButtonActionPerformed(evt);
151: }
152: });
153:
154: outputLabel.setText("Output directory");
155:
156: outputTextField.setText(System.getProperty("user.home"));
157:
158: outputSelectButton.setText("...");
159: outputSelectButton
160: .addActionListener(new java.awt.event.ActionListener() {
161: public void actionPerformed(
162: java.awt.event.ActionEvent evt) {
163: outputSelectActionPerformed(evt);
164: }
165: });
166:
167: mainMenu.setText("File");
168: menuQuit.setText("Quit");
169: menuQuit.addActionListener(new java.awt.event.ActionListener() {
170: public void actionPerformed(java.awt.event.ActionEvent evt) {
171: menuQuitActionPerformed(evt);
172: }
173: });
174:
175: mainMenu.add(menuQuit);
176:
177: mainMenuBar.add(mainMenu);
178:
179: setJMenuBar(mainMenuBar);
180:
181: /* This layout manager comes from here:
182: * https://swing-layout.dev.java.net/ */
183:
184: GroupLayout layout = new GroupLayout(getContentPane());
185: getContentPane().setLayout(layout);
186:
187: layout.setAutocreateGaps(true);
188: layout.setAutocreateContainerGaps(true);
189:
190: GroupLayout.SequentialGroup hGroup = layout
191: .createSequentialGroup();
192: hGroup.add(layout.createParallelGroup().add(selectLabel).add(
193: midletLabel).add(outputLabel).add(createLabel));
194: hGroup.add(layout.createParallelGroup().add(scriptTextField)
195: .add(midletTextField).add(outputTextField).add(
196: layout.createSequentialGroup()
197: .add(selectMidp10).add(selectMidp20)));
198:
199: hGroup.add(layout.createParallelGroup().add(
200: layout.createSequentialGroup().add(selectScriptButton)
201: .add(editButton)).add(outputSelectButton).add(
202: runButton));
203:
204: layout.setHorizontalGroup(hGroup);
205:
206: GroupLayout.SequentialGroup vGroup = layout
207: .createSequentialGroup();
208: vGroup.add(layout.createParallelGroup(GroupLayout.BASELINE)
209: .add(selectLabel).add(scriptTextField).add(
210: selectScriptButton).add(editButton));
211: vGroup.add(layout.createParallelGroup(GroupLayout.BASELINE)
212: .add(midletLabel).add(midletTextField));
213: vGroup.add(layout.createParallelGroup(GroupLayout.BASELINE)
214: .add(outputLabel).add(outputTextField).add(
215: outputSelectButton));
216: vGroup.add(layout.createParallelGroup(GroupLayout.BASELINE)
217: .add(createLabel).add(selectMidp10).add(selectMidp20)
218: .add(runButton));
219: layout.setVerticalGroup(vGroup);
220:
221: pack();
222: }
223:
224: /**
225: * The <code>runButtonActionPerformed</code> method runs JarHack
226: * to actually create the jar/jad files where they are supposed to
227: * be.
228: *
229: * @param evt a <code>java.awt.event.ActionEvent</code> value
230: */
231: private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {
232: String hecljar = null;
233: if (selectMidp10.isSelected()) {
234: hecljar = "/jars/cldc1.0-midp1.0/Hecl.jar";
235: } else {
236: hecljar = "/jars/cldc1.1-midp2.0/Hecl.jar";
237: }
238: InputStream in = this .getClass().getResourceAsStream(hecljar);
239: String scriptfile = scriptTextField.getText();
240: String newname = midletTextField.getText();
241: String outfile = outputTextField.getText() + File.separatorChar
242: + newname + ".jar";
243: try {
244: JarHack.substHecl(in, outfile, newname, scriptfile);
245: JarHack.createJadForJar(outfile, newname);
246: } catch (Exception e) {
247: JOptionPane.showMessageDialog(this , "Problem creating "
248: + outfile + "\n" + e.toString(),
249: "Hecl Builder Error", JOptionPane.ERROR_MESSAGE);
250: }
251: }
252:
253: private void scriptTextFieldCaretUpdate(
254: javax.swing.event.CaretEvent evt) {
255: midletTextFieldCaretUpdate(evt);
256: }
257:
258: private void midletTextFieldCaretUpdate(
259: javax.swing.event.CaretEvent evt) {
260: String midletName = midletTextField.getText();
261: String scriptName = scriptTextField.getText();
262: if (midletName.length() > 0 && scriptName.length() > 0) {
263: runButton.setEnabled(true);
264: }
265: }
266:
267: private void menuQuitActionPerformed(java.awt.event.ActionEvent evt) {
268: System.exit(1);
269: }
270:
271: private void selectScriptButtonActionPerformed(
272: java.awt.event.ActionEvent evt) {
273: scriptFileChooser = new JFileChooser(System
274: .getProperty("user.dir"));
275: scriptFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
276: int retval = scriptFileChooser.showOpenDialog(this );
277: if (retval == JFileChooser.APPROVE_OPTION) {
278: File file = scriptFileChooser.getSelectedFile();
279: scriptTextField.setText(file.toString());
280: }
281: }
282:
283: private void editButtonActionPerformed(
284: java.awt.event.ActionEvent evt) {
285: String filename = null;
286: File fl = new File(scriptTextField.getText());
287:
288: if (fl.exists()) {
289: filename = fl.toString();
290: } else {
291: if (JOptionPane.showConfirmDialog(this , "The file " + fl
292: + " does not exist. Create it?") != JOptionPane.YES_OPTION) {
293: return;
294: }
295: filename = fl.toString();
296: }
297: HeclEditor he = new HeclEditor(filename);
298: he.setVisible(true);
299: }
300:
301: private void outputSelectActionPerformed(
302: java.awt.event.ActionEvent evt) {
303: outputDirChooser = new JFileChooser(outputTextField.getText());
304: outputDirChooser
305: .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
306: int retval = outputDirChooser.showOpenDialog(this );
307: if (retval == JFileChooser.APPROVE_OPTION) {
308: File file = outputDirChooser.getSelectedFile();
309: outputTextField.setText(file.toString());
310: }
311: }
312:
313: public static void main(String args[]) {
314: java.awt.EventQueue.invokeLater(new Runnable() {
315: public void run() {
316: new HeclBuilderGui().setVisible(true);
317: }
318: });
319: }
320: }
|