KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > ProjectPropertiesFrame


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import javax.swing.*;
37 import javax.swing.event.*;
38 import javax.swing.border.EmptyBorder JavaDoc;
39
40 import java.awt.event.*;
41 import java.awt.*;
42 import java.io.IOException JavaDoc;
43 import java.io.File JavaDoc;
44 import java.util.Vector JavaDoc;
45
46 import edu.rice.cs.drjava.DrJava;
47 import edu.rice.cs.drjava.model.SingleDisplayModel;
48 import edu.rice.cs.drjava.config.*;
49 import edu.rice.cs.drjava.ui.config.*;
50
51 import edu.rice.cs.plt.io.IOUtil;
52 import edu.rice.cs.util.ClassPathVector;
53 import edu.rice.cs.util.swing.FileSelectorComponent;
54 import edu.rice.cs.util.swing.DirectorySelectorComponent;
55 import edu.rice.cs.util.swing.DirectoryChooser;
56 import edu.rice.cs.util.swing.FileChooser;
57 import edu.rice.cs.util.swing.Utilities;
58
59 import javax.swing.filechooser.FileFilter JavaDoc;
60
61 /** A frame for setting Project Preferences */
62 public class ProjectPropertiesFrame extends JFrame {
63
64   private static final int FRAME_WIDTH = 503;
65   private static final int FRAME_HEIGHT = 318;
66
67   private MainFrame _mainFrame;
68   private SingleDisplayModel _model;
69   private File JavaDoc _projFile;
70
71   private final JButton _okButton;
72   private final JButton _applyButton;
73   private final JButton _cancelButton;
74   // private JButton _saveSettingsButton;
75
private JPanel _mainPanel;
76
77   private DirectorySelectorComponent _projRootSelector;
78   private DirectorySelectorComponent _buildDirSelector;
79   private DirectorySelectorComponent _workDirSelector;
80   private FileSelectorComponent _mainDocumentSelector;
81
82   private FileSelectorComponent _jarFileSelector;
83   private FileSelectorComponent _manifestFileSelector;
84
85   private VectorFileOptionComponent _extraClassPathList;
86
87   /** Constructs project properties frame for a new project and displays it. Assumes that a project is active. */
88   public ProjectPropertiesFrame(MainFrame mf) {
89     super("Project Properties");
90
91     // Utilities.show("ProjectPropertiesFrame(" + mf + ", " + projFile + ")");
92

93     _mainFrame = mf;
94     _model = _mainFrame.getModel();
95     _projFile = _model.getProjectFile();
96     _mainPanel= new JPanel();
97     
98     Action okAction = new AbstractAction("OK") {
99       public void actionPerformed(ActionEvent e) {
100         // Always apply and save settings
101
boolean successful = true;
102         successful = saveSettings();
103         if (successful) ProjectPropertiesFrame.this.setVisible(false);
104         reset();
105       }
106     };
107     _okButton = new JButton(okAction);
108
109     Action applyAction = new AbstractAction("Apply") {
110       public void actionPerformed(ActionEvent e) {
111         // Always save settings
112
saveSettings();
113         reset();
114       }
115     };
116     _applyButton = new JButton(applyAction);
117
118     Action cancelAction = new AbstractAction("Cancel") {
119       public void actionPerformed(ActionEvent e) { cancel(); }
120     };
121     _cancelButton = new JButton(cancelAction);
122     
123     init();
124   }
125
126   /** Initializes the components in this frame. */
127   private void init() {
128     _setupPanel(_mainPanel);
129     JScrollPane scrollPane = new JScrollPane(_mainPanel);
130     Container cp = getContentPane();
131     
132     GridBagLayout cpLayout = new GridBagLayout();
133     GridBagConstraints c = new GridBagConstraints();
134     cp.setLayout(cpLayout);
135     
136     c.fill = GridBagConstraints.BOTH;
137     c.anchor = GridBagConstraints.NORTH;
138     c.gridwidth = GridBagConstraints.REMAINDER;
139     c.gridheight = GridBagConstraints.RELATIVE;
140     c.weightx = 1.0;
141     c.weighty = 1.0;
142     cpLayout.setConstraints(scrollPane, c);
143     cp.add(scrollPane);
144     
145     // Add buttons
146
JPanel bottom = new JPanel();
147     bottom.setBorder(new EmptyBorder JavaDoc(5,5,5,5));
148     bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
149     bottom.add(Box.createHorizontalGlue());
150     bottom.add(_applyButton);
151     bottom.add(_okButton);
152     bottom.add(_cancelButton);
153     bottom.add(Box.createHorizontalGlue());
154
155     c.fill = GridBagConstraints.NONE;
156     c.anchor = GridBagConstraints.SOUTH;
157     c.gridheight = GridBagConstraints.REMAINDER;
158     c.weighty = 0.0;
159     cpLayout.setConstraints(bottom, c);
160     cp.add(bottom);
161
162     // Set all dimensions ----
163
setSize(FRAME_WIDTH, FRAME_HEIGHT);
164     MainFrame.setPopupLoc(this, _mainFrame);
165
166     addWindowListener(new WindowAdapter() {
167       public void windowClosing(java.awt.event.WindowEvent JavaDoc e) { cancel(); }
168     });
169
170     reset();
171   }
172
173   /** Resets the frame and hides it. */
174   public void cancel() {
175     reset();
176     _applyButton.setEnabled(false);
177     ProjectPropertiesFrame.this.setVisible(false);
178   }
179
180   public void reset() { reset(_model.getProjectRoot()); }
181
182   private void reset(File JavaDoc projRoot) {
183 // Utilities.show("reset(" + projRoot + ")");
184
_projRootSelector.setFileField(projRoot);
185
186     final File JavaDoc bd = _model.getBuildDirectory();
187     final JTextField bdTextField = _buildDirSelector.getFileField();
188     if (bd == null) bdTextField.setText("");
189     else _buildDirSelector.setFileField(bd);
190
191     final File JavaDoc wd = _model.getWorkingDirectory();
192     final JTextField wdTextField = _workDirSelector.getFileField();
193     if (wd == null) wdTextField.setText("");
194     else _workDirSelector.setFileField(wd);
195
196     final File JavaDoc mc = _model.getMainClass();
197     final JTextField mcTextField = _mainDocumentSelector.getFileField();
198     if (mc == null) mcTextField.setText("");
199     else _mainDocumentSelector.setFileField(mc);
200
201     ClassPathVector cp = _model.getExtraClassPath();
202     _extraClassPathList.setValue(cp.asFileVector());
203     _applyButton.setEnabled(false);
204   }
205
206   /** Caches the settings in the global model */
207   public boolean saveSettings() {//throws IOException {
208
boolean projRootChanged = false;
209
210     File JavaDoc pr = _projRootSelector.getFileFromField();
211     if (_projRootSelector.getFileField().getText().equals("")) { pr = null; } else {
212       if (!pr.equals(_model.getProjectRoot())) {
213         _model.setProjectRoot(pr);
214         projRootChanged = true;
215       }
216     }
217
218     File JavaDoc bd = _buildDirSelector.getFileFromField();
219     if (_buildDirSelector.getFileField().getText().equals("")) bd = null;
220     _model.setBuildDirectory(bd);
221
222     File JavaDoc wd = _workDirSelector.getFileFromField();
223     if (_workDirSelector.getFileField().getText().equals("")) wd = null;
224     _model.setWorkingDirectory(wd);
225
226     File JavaDoc mc = _mainDocumentSelector.getFileFromField();
227     if (_mainDocumentSelector.getFileField().getText().equals("")) mc = null;
228     _model.setMainClass(mc);
229
230     Vector JavaDoc<File JavaDoc> extras = _extraClassPathList.getValue();
231     ClassPathVector cpv = new ClassPathVector();
232     for (File JavaDoc cf : extras) { cpv.add(cf); }
233     _model.setExtraClassPath(cpv);
234
235     // _mainFrame.saveProject();
236
if (projRootChanged) {
237       try {
238         _model.reloadProject(_mainFrame.getCurrentProject(), _mainFrame.gatherProjectDocInfo());
239       } catch(IOException JavaDoc e) { throw new edu.rice.cs.util.UnexpectedException(e, "I/O error while reloading project"); }
240     }
241     return true;
242   }
243
244   /** Returns the current project root in the project profile. */
245   private File JavaDoc _getProjRoot() {
246     File JavaDoc projRoot = _mainFrame.getModel().getProjectRoot();
247     if (projRoot != null) return projRoot;
248     return FileOption.NULL_FILE;
249   }
250
251   /** Returns the current build directory in the project profile. */
252   private File JavaDoc _getBuildDir() {
253     File JavaDoc buildDir = _mainFrame.getModel().getBuildDirectory();
254     if (buildDir != null) return buildDir;
255     return FileOption.NULL_FILE;
256   }
257
258   /** Returns the current working directory in the project profile (FileOption.NULL_FILE if none is set) */
259   private File JavaDoc _getWorkDir() {
260     File JavaDoc workDir = _mainFrame.getModel().getWorkingDirectory();
261     if (workDir != null) return workDir;
262     return FileOption.NULL_FILE;
263   }
264
265   /** Returns the current working directory in the project profile (FileOption.NULL_FILE if none is set) */
266   private File JavaDoc _getMainFile() {
267     File JavaDoc mainFile = _mainFrame.getModel().getMainClass();
268     if (mainFile != null) return mainFile;
269     return FileOption.NULL_FILE;
270   }
271
272   private void _setupPanel(JPanel panel) {
273     GridBagLayout gridbag = new GridBagLayout();
274     GridBagConstraints c = new GridBagConstraints();
275     panel.setLayout(gridbag);
276     c.fill = GridBagConstraints.HORIZONTAL;
277     Insets labelInsets = new Insets(5, 10, 0, 0);
278     Insets compInsets = new Insets(5, 5, 0, 10);
279
280     // Project Root
281

282     c.weightx = 0.0;
283     c.gridwidth = 1;
284     c.insets = labelInsets;
285
286     JLabel prLabel = new JLabel("Project Root");
287     prLabel.setToolTipText("<html>The root directory for the project source files .<br>"+
288     "If not specified, the parent directory of the project file.</html>");
289     gridbag.setConstraints(prLabel, c);
290
291     panel.add(prLabel);
292     c.weightx = 1.0;
293     c.gridwidth = GridBagConstraints.REMAINDER;
294     c.insets = compInsets;
295
296     JPanel prPanel = _projRootPanel();
297     gridbag.setConstraints(prPanel, c);
298     panel.add(prPanel);
299
300     // Build Directory
301

302     c.weightx = 0.0;
303     c.gridwidth = 1;
304     c.insets = labelInsets;
305
306     JLabel bdLabel = new JLabel("Build Directory");
307     bdLabel.setToolTipText("<html>The directory the class files will be compiled into.<br>"+
308         "If not specified, the class files will be compiled into<br>"+
309     "the same directory as their corresponding source files</html>");
310     gridbag.setConstraints(bdLabel, c);
311
312     panel.add(bdLabel);
313     c.weightx = 1.0;
314     c.gridwidth = GridBagConstraints.REMAINDER;
315     c.insets = compInsets;
316
317     JPanel bdPanel = _buildDirectoryPanel();
318     gridbag.setConstraints(bdPanel, c);
319     panel.add(bdPanel);
320
321     // Working Directory
322

323     c.weightx = 0.0;
324     c.gridwidth = 1;
325     c.insets = labelInsets;
326
327     JLabel wdLabel = new JLabel("Working Directory");
328     wdLabel.setToolTipText("<html>The root directory for relative path names.</html>");
329     gridbag.setConstraints(wdLabel, c);
330
331     panel.add(wdLabel);
332     c.weightx = 1.0;
333     c.gridwidth = GridBagConstraints.REMAINDER;
334     c.insets = compInsets;
335
336     JPanel wdPanel = _workDirectoryPanel();
337     gridbag.setConstraints(wdPanel, c);
338     panel.add(wdPanel);
339
340     // Main Document file
341

342     c.weightx = 0.0;
343     c.gridwidth = 1;
344     c.insets = labelInsets;
345
346     JLabel classLabel = new JLabel("Main Document");
347     classLabel.setToolTipText("<html>The project document containing the<br>" +
348     "<code>main</code>method for the entire project</html>");
349     gridbag.setConstraints(classLabel, c);
350     panel.add(classLabel);
351
352     c.weightx = 1.0;
353     c.gridwidth = GridBagConstraints.REMAINDER;
354     c.insets = compInsets;
355
356     JPanel mainClassPanel = _mainDocumentSelector();
357     gridbag.setConstraints(mainClassPanel, c);
358     panel.add(mainClassPanel);
359
360     c.weightx = 0.0;
361     c.gridwidth = 1;
362     c.insets = labelInsets;
363
364     // ExtraProjectClasspaths
365
JLabel extrasLabel = new JLabel("Extra Classpath");
366     extrasLabel.setToolTipText("<html>The list of extra classpaths to load with the project.<br>"+
367         "This may include either JAR files or directories. Any<br>"+
368         "classes defined in these classpath locations will be <br>"+
369         "visible in the interactions pane and also accessible <br>"+
370     "by the compiler when compiling the project.</html>");
371     gridbag.setConstraints(extrasLabel, c);
372     panel.add(extrasLabel);
373
374     c.weightx = 1.0;
375     c.gridwidth = GridBagConstraints.REMAINDER;
376     c.insets = compInsets;
377
378     Component extrasComponent = _extraClassPathComponent();
379     gridbag.setConstraints(extrasComponent, c);
380     panel.add(extrasComponent);
381   }
382   
383    private DocumentListener _applyListener = new DocumentListener() {
384       public void insertUpdate(DocumentEvent e) { setEnabled(); }
385       public void removeUpdate(DocumentEvent e) { setEnabled(); }
386       public void changedUpdate(DocumentEvent e) { setEnabled(); }
387       private void setEnabled() { Utilities.invokeLater(new Runnable JavaDoc() { public void run() { _applyButton.setEnabled(true); } }); }
388    };
389
390   public JPanel _projRootPanel() {
391     DirectoryChooser dirChooser = new DirectoryChooser(this);
392     dirChooser.setSelectedFile(_getProjRoot());
393     dirChooser.setDialogTitle("Select Project Root Folder");
394     dirChooser.setApproveButtonText("Select");
395 // dirChooser.setEditable(true);
396
_projRootSelector = new DirectorySelectorComponent(this, dirChooser, 20, 12f);
397     //toReturn.add(_buildDirSelector, BorderLayout.EAST);
398

399     _projRootSelector.getFileField().getDocument().addDocumentListener(_applyListener);
400
401     return _projRootSelector;
402   }
403
404   public JPanel _buildDirectoryPanel() {
405     DirectoryChooser dirChooser = new DirectoryChooser(this);
406     File JavaDoc bd = _getBuildDir();
407     if (bd == null || bd == FileOption.NULL_FILE) bd = _getProjRoot();
408     dirChooser.setSelectedFile(bd);
409     dirChooser.setDialogTitle("Select Build Directory");
410     dirChooser.setApproveButtonText("Select");
411 // dirChooser.setEditable(true);
412
// (..., false); since build directory does not have to exist
413
_buildDirSelector = new DirectorySelectorComponent(this, dirChooser, 20, 12f, false);
414     _buildDirSelector.setFileField(bd); // the file field is used as the initial file selection
415
//toReturn.add(_buildDirSelector, BorderLayout.EAST);
416

417     _buildDirSelector.getFileField().getDocument().addDocumentListener(_applyListener);
418
419     return _buildDirSelector;
420   }
421
422   public JPanel _workDirectoryPanel() {
423     DirectoryChooser dirChooser = new DirectoryChooser(this);
424     dirChooser.setSelectedFile(_getWorkDir());
425     dirChooser.setDialogTitle("Select Working Directory");
426     dirChooser.setApproveButtonText("Select");
427 // dirChooser.setEditable(true);
428
_workDirSelector = new DirectorySelectorComponent(this, dirChooser, 20, 12f);
429     //toReturn.add(_buildDirSelector, BorderLayout.EAST);
430

431     _workDirSelector.getFileField().getDocument().addDocumentListener(_applyListener);
432     return _workDirSelector;
433   }
434
435   public Component _extraClassPathComponent() {
436     _extraClassPathList = new VectorFileOptionComponent(null, "Extra Project Classpaths", this);
437     _extraClassPathList.addChangeListener(new OptionComponent.ChangeListener() {
438       public Object JavaDoc apply(Object JavaDoc oc) {
439         _applyButton.setEnabled(true);
440         return null;
441       }
442     });
443     return _extraClassPathList.getComponent();
444   }
445
446   public JPanel _mainDocumentSelector() {
447     final File JavaDoc projRoot = _getProjRoot();
448
449     FileChooser chooser = new FileChooser(projRoot);
450     chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
451     chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
452
453     chooser.setDialogTitle("Select Main Document");
454 // Utilities.show("Main Document Root is: " + root);
455
chooser.setCurrentDirectory(projRoot);
456     File JavaDoc mainFile = _getMainFile();
457     if (mainFile != FileOption.NULL_FILE) chooser.setSelectedFile(mainFile);
458
459     chooser.setApproveButtonText("Select");
460
461     FileFilter JavaDoc filter = new FileFilter JavaDoc() {
462       public boolean accept(File JavaDoc f) {
463         String JavaDoc name = f.getName();
464         return IOUtil.isMember(f, projRoot) && (f.isDirectory() ||
465             (name.endsWith(".java") || name.endsWith(".dj0") || name.endsWith(".dj1") || name.endsWith(".dj2")));
466       }
467       public String JavaDoc getDescription() { return "Java & DrJava Files (*.java, *.dj0, *.dj1, *.dj2) in project"; }
468     };
469
470     chooser.addChoosableFileFilter(filter);
471     _mainDocumentSelector = new FileSelectorComponent(this, chooser, 20, 12f);
472
473     _mainDocumentSelector.getFileField().getDocument().addDocumentListener(_applyListener);
474     return _mainDocumentSelector;
475   }
476
477   public JPanel _manifestFileSelector() {
478     JFileChooser fileChooser = new JFileChooser(_getProjRoot().getParentFile());
479     fileChooser.setDialogTitle("Select Output jar File");
480     fileChooser.setApproveButtonText("Select");
481     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
482     fileChooser.setMultiSelectionEnabled(false);
483     _manifestFileSelector = new FileSelectorComponent(this, fileChooser, 20, 12f);
484
485     _manifestFileSelector.setFileFilter(new FileFilter JavaDoc() {
486       public boolean accept(File JavaDoc f) { return f.getName().endsWith(".jar") || f.isDirectory(); }
487       public String JavaDoc getDescription() { return "Java Archive Files (*.jar)"; }
488     });
489     //toReturn.add(_buildDirSelector, BorderLayout.EAST);
490
return _manifestFileSelector;
491   }
492
493   public JPanel _jarFileSelector() {
494     JFileChooser fileChooser = new JFileChooser(_getProjRoot());
495     fileChooser.setDialogTitle("Select Manifest File");
496     fileChooser.setApproveButtonText("Select");
497     fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
498     fileChooser.setMultiSelectionEnabled(false);
499     _jarFileSelector = new FileSelectorComponent(this, fileChooser, 20, 12f);
500     _jarFileSelector.setFileFilter(new FileFilter JavaDoc() {
501       public boolean accept(File JavaDoc f) { return f.getName().endsWith(".jar") || f.isDirectory(); }
502       public String JavaDoc getDescription() { return "Java Archive Files (*.jar)"; }
503     });
504     //toReturn.add(_buildDirSelector, BorderLayout.EAST);
505
return _jarFileSelector;
506   }
507
508 //public void setVisible(boolean vis) {
509
//super.setVisible(vis);
510
//reset();
511
//}
512
}
513
Popular Tags