001: package net.xoetrope.builder.editor.dialog;
002:
003: import java.io.File;
004: import java.util.prefs.Preferences;
005:
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.awt.event.ItemEvent;
009: import java.awt.event.ItemListener;
010: import java.awt.event.KeyEvent;
011: import java.awt.event.KeyListener;
012: import javax.swing.JDialog;
013: import javax.swing.JFileChooser;
014: import javax.swing.JOptionPane;
015: import javax.swing.event.ChangeEvent;
016: import javax.swing.event.ChangeListener;
017:
018: import net.xoetrope.builder.editor.XEditorProject;
019: import net.xoetrope.builder.editor.XEditorUtilities;
020: import net.xoetrope.builder.editor.XuiDefaults;
021: import net.xoetrope.builder.editor.XuiEditor;
022: import net.xoetrope.swing.XButton;
023: import net.xoetrope.swing.XCheckbox;
024: import net.xoetrope.swing.XComboBox;
025: import net.xoetrope.swing.XEdit;
026: import net.xoetrope.swing.XPanel;
027: import net.xoetrope.xui.XPage;
028: import net.xoetrope.xui.helper.BuddyHelper;
029: import net.xoetrope.xui.style.XStyleFactory;
030:
031: /**
032: * <p>A new project dialog class</p>
033: * <p>Copyright: Copyright (c) Xoetrope Ltd., 1998-2003</p>
034: * $Revision: 1.19 $
035: */
036: public class XNewProjectDialog extends JDialog implements
037: ActionListener, ItemListener, ChangeListener, KeyListener {
038: private XButton btnCreate, btnCancel;
039: private boolean returnValue = false;
040: private XEdit txtLocation, txtPackage, txtName, txtWidth,
041: txtHeight, txtInitialScreenName, txtAppTitle;
042: private XButton btnFindLocation, editFramesBtn;
043: private XComboBox cmbRes;
044: private XCheckbox chkFrameset, chkSwing;
045: private XEditorProject currentProject;
046: private XFramesetEditor frameEditor;
047: private String frameNames[] = { "banner", "", "Welcome", "",
048: "navPanel" };
049: private String frameSizes[];
050:
051: public XNewProjectDialog(XEditorProject project) {
052: currentProject = project;
053:
054: setModal(true);
055: setTitle("New Project");
056: setSize(328, 288);
057: setLocation(100, 100);
058:
059: Preferences prefs = Preferences
060: .userNodeForPackage(XuiEditor.class);
061:
062: getContentPane().setLayout(null);
063: XStyleFactory factory = new XStyleFactory(
064: XPage.XUI_SWING_PACKAGE);
065: factory.setParentComponent(getContentPane());
066:
067: XPanel mainPanel = (XPanel) factory.addComponent(XPage.PANEL,
068: 5, 5, 325, 215, "");
069: XPanel pnl = (XPanel) factory.addComponent(XPage.PANEL, 5, 215,
070: 325, 40, "");
071:
072: factory.setParentComponent(mainPanel);
073: BuddyHelper buddy = new BuddyHelper(factory);
074: txtName = (XEdit) buddy.addComponent(XPage.EDIT, 5, 105, 5,
075: 300, 20, "Project name :", "NewProject", null);
076: txtLocation = (XEdit) buddy.addComponent(XPage.EDIT, 5, 105,
077: 30, 275, 20, "Project location :", prefs.get(
078: "ProjectPath", XuiDefaults.ProjectPath), null);
079: btnFindLocation = (XButton) factory.addComponent(XPage.BUTTON,
080: 285, 30, 20, 20, "...");
081: txtPackage = (XEdit) buddy.addComponent(XPage.EDIT, 5, 105, 55,
082: 300, 20, "Package name :", prefs.get("ProjectPackage",
083: XuiDefaults.ProjectPath), null);
084: cmbRes = (XComboBox) buddy.addComponent(XPage.COMBO, 5, 105,
085: 80, 195, 20, "Screen size :", prefs.get(
086: "ProjectPackage", XuiDefaults.ProjectPath),
087: null);
088: txtWidth = (XEdit) factory.addComponent(XPage.EDIT, 210, 80,
089: 45, 20, "");
090: txtHeight = (XEdit) factory.addComponent(XPage.EDIT, 260, 80,
091: 45, 20, "");
092: txtInitialScreenName = (XEdit) buddy.addComponent(XPage.EDIT,
093: 5, 105, 105, 300, 20, "Initial screen name :",
094: "Welcome", null);
095: txtAppTitle = (XEdit) buddy.addComponent(XPage.EDIT, 5, 105,
096: 130, 300, 20, "Application title :",
097: "Another XUI Project", null);
098: chkFrameset = (XCheckbox) factory.addComponent(XPage.CHECK,
099: 101, 152, 130, 20, "Create a frameset", null);
100: editFramesBtn = (XButton) factory.addComponent(XPage.BUTTON,
101: 240, 155, 65, 20, "Edit...");
102: chkSwing = (XCheckbox) factory.addComponent(XPage.CHECK, 101,
103: 174, 130, 20, "Use JFC/Swing", null);
104: chkSwing.setAlignmentX(chkFrameset.LEFT_ALIGNMENT);
105: chkSwing.setSelected(true);
106: chkFrameset.setAlignmentX(chkFrameset.LEFT_ALIGNMENT);
107: editFramesBtn.setEnabled(false);
108:
109: factory.setParentComponent(pnl);
110: btnCancel = (XButton) factory.addComponent(XPage.BUTTON, 105,
111: 10, 95, 20, "Cancel");
112: btnCreate = (XButton) factory.addComponent(XPage.BUTTON, 210,
113: 10, 95, 20, "OK");
114:
115: addHandlers();
116:
117: cmbRes.addItem("240 x 320");
118: cmbRes.addItem("320 x 240");
119: cmbRes.addItem("640 x 480");
120: cmbRes.addItem("800 x 600");
121: cmbRes.addItem("1024 x 768");
122: cmbRes.addItem("1280 x 1024");
123: cmbRes.addItem("1400 x 1050");
124: cmbRes.setSelectedIndex(2);
125:
126: XEditorUtilities.setDefaultFont(this );
127: }
128:
129: private void addHandlers() {
130: btnCreate.addActionListener(this );
131: btnCancel.addActionListener(this );
132:
133: btnFindLocation.addActionListener(this );
134: editFramesBtn.addActionListener(this );
135:
136: cmbRes.addItemListener(this );
137: chkFrameset.addChangeListener(this );
138:
139: txtInitialScreenName.addKeyListener(this );
140: }
141:
142: /**
143: * Change the text for the width and height of the application upon startup
144: * and when the combobox is changed.
145: */
146: public void changeRes() {
147: String res = (String) cmbRes.getSelectedItem();
148: int pos = res.indexOf("x");
149: txtWidth.setText(res.substring(0, pos - 1));
150: txtHeight.setText(res.substring(pos + 2));
151: frameSizes = null;
152: repaint();
153: }
154:
155: /**
156: * Display a new JFileChooser so as to allow the user to select the location
157: * of their new project. If a location is selected set the text of the path.
158: */
159: public void findLocation() {
160: File start;
161: if (txtLocation.getText().length() == 0)
162: start = new File(System.getProperty("user.home"));
163: else
164: start = new File(txtLocation.getText());
165: JFileChooser chooser = new JFileChooser(start);
166: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
167: int i = chooser.showOpenDialog(this );
168: if (i == 0) {
169: txtLocation.setText(chooser.getSelectedFile()
170: .getAbsolutePath());
171: }
172: }
173:
174: /**
175: * When the create button is clicked get the instance of the Project and
176: * reset it. Set the paths of the project, copy the default files for the
177: * project. Change the necessary values of the properties file with the
178: * values filled in on the dialog. Get the Project to reload the screens and
179: * set the dialog to invisible.
180: * @return true if the project is created successfully
181: */
182: public boolean createProject() {
183: Preferences prefs = Preferences
184: .userNodeForPackage(XuiEditor.class);
185: String projectFilePath = txtLocation.getText()
186: + File.separatorChar
187: + prefs.get("ProjectFile", XuiDefaults.ProjectFile);
188: File projectFile = new File(projectFilePath);
189: if (projectFile.exists()) {
190: JOptionPane.showMessageDialog(this ,
191: "A project already exists on the selected path");
192: return false;
193: }
194:
195: prefs.put("ProjectPath", txtLocation.getText());
196: prefs.put("ProjectPackage", txtPackage.getText());
197:
198: currentProject.createNewProject(txtLocation.getText(),
199: txtPackage.getText(), txtWidth.getText(), txtHeight
200: .getText(), frameSizes, txtInitialScreenName
201: .getText(), txtAppTitle.getText(), chkFrameset
202: .isSelected(), chkSwing.isSelected(),
203: frameNames);
204:
205: return true;
206: }
207:
208: /**
209: * Set the returnValue and set the JDialog to invisible.
210: */
211: public void cancel() {
212: returnValue = false;
213: hide();
214: }
215:
216: public void itemStateChanged(ItemEvent evt) {
217: changeRes();
218: }
219:
220: /**
221: * Respond to the button clicks
222: * @param evt
223: */
224: public void actionPerformed(ActionEvent evt) {
225: Object source = evt.getSource();
226: if (btnCreate == source) {
227: if (validateInputs())
228: if (createProject()) {
229: returnValue = true;
230: hide();
231: }
232: } else if (btnCancel == source)
233: cancel();
234: else if (btnFindLocation == source)
235: findLocation();
236: else if (source == editFramesBtn) {
237: frameEditor = new XFramesetEditor(frameNames, frameSizes,
238: true, Integer.parseInt(txtWidth.getText()), Integer
239: .parseInt(txtHeight.getText()));
240: frameEditor.setLocationRelativeTo(getParent());
241: frameEditor.show();
242: frameSizes = frameEditor.getFrameSizes();
243: if (frameEditor.getResult())
244: frameNames = frameEditor.getFrameNames();
245: }
246: }
247:
248: public void stateChanged(ChangeEvent e) {
249: if (e.getSource() == chkFrameset)
250: editFramesBtn.setEnabled(chkFrameset.isSelected());
251: }
252:
253: /**
254: * Check that the inputs are valid.
255: * @return true if the inputs are OK, otherwise return false
256: */
257: private boolean validateInputs() {
258: if (txtName.getText().trim().compareTo("") == 0) {
259: JOptionPane.showMessageDialog(this ,
260: "A project name must be specified");
261: return false;
262: }
263: if (txtLocation.getText().trim().compareTo("") == 0) {
264: JOptionPane.showMessageDialog(this ,
265: "A valid location must be specified");
266: return false;
267: }
268:
269: // Check that a file path hasn't been added instead of a package name
270: String packageName = txtPackage.getText().trim();
271: if ((packageName.indexOf(':') >= 0)
272: || (packageName.indexOf('/') >= 0)
273: || (packageName.indexOf('\\') >= 0)
274: || (packageName.indexOf(' ') >= 0)) {
275: JOptionPane.showMessageDialog(this ,
276: "A valid (Java) package name must be specified");
277: return false;
278: }
279:
280: if (!(new File(txtLocation.getText())).exists()) {
281: int rc = JOptionPane
282: .showConfirmDialog(
283: null,
284: "The project directory does not exist. Do you want it to be created?",
285: "Create directory",
286: JOptionPane.INFORMATION_MESSAGE,
287: JOptionPane.YES_NO_CANCEL_OPTION);
288: if (rc == JOptionPane.YES_OPTION)
289: XEditorUtilities.createDir(txtLocation.getText());
290:
291: if (!(new File(txtLocation.getText())).exists()) {
292: JOptionPane.showMessageDialog(this ,
293: "A valid location must be specified");
294: return false;
295: }
296: }
297: if ((txtWidth.getText().trim().compareTo("") == 0)
298: || (txtHeight.getText().trim().compareTo("") == 0)) {
299: JOptionPane.showMessageDialog(this ,
300: "A valid dimension must be specified");
301: return false;
302: }
303: if (txtAppTitle.getText().trim().compareTo("") == 0) {
304: JOptionPane.showMessageDialog(this ,
305: "An application title must be specified");
306: return false;
307: }
308: return true;
309: }
310:
311: public boolean showDlg() {
312: show();
313: return returnValue;
314: }
315:
316: public String getProjectPath() {
317: return txtLocation.getText();
318: }
319:
320: public void keyTyped(KeyEvent ke) {
321: if (ke.getSource().equals(txtInitialScreenName))
322: frameNames[3] = txtInitialScreenName.getText();
323: }
324:
325: public void keyReleased(KeyEvent ke) {
326: }
327:
328: public void keyPressed(KeyEvent ke) {
329: }
330:
331: }
|