1 33 34 package edu.rice.cs.drjava.ui; 35 36 import javax.swing.*; 37 import javax.swing.event.*; 38 import javax.swing.border.EmptyBorder ; 39 40 import java.awt.event.*; 41 import java.awt.*; 42 import java.io.IOException ; 43 import java.io.File ; 44 import java.util.Vector ; 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 ; 60 61 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 _projFile; 70 71 private final JButton _okButton; 72 private final JButton _applyButton; 73 private final JButton _cancelButton; 74 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 88 public ProjectPropertiesFrame(MainFrame mf) { 89 super("Project Properties"); 90 91 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 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 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 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 JPanel bottom = new JPanel(); 147 bottom.setBorder(new EmptyBorder (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 setSize(FRAME_WIDTH, FRAME_HEIGHT); 164 MainFrame.setPopupLoc(this, _mainFrame); 165 166 addWindowListener(new WindowAdapter() { 167 public void windowClosing(java.awt.event.WindowEvent e) { cancel(); } 168 }); 169 170 reset(); 171 } 172 173 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 projRoot) { 183 _projRootSelector.setFileField(projRoot); 185 186 final File bd = _model.getBuildDirectory(); 187 final JTextField bdTextField = _buildDirSelector.getFileField(); 188 if (bd == null) bdTextField.setText(""); 189 else _buildDirSelector.setFileField(bd); 190 191 final File wd = _model.getWorkingDirectory(); 192 final JTextField wdTextField = _workDirSelector.getFileField(); 193 if (wd == null) wdTextField.setText(""); 194 else _workDirSelector.setFileField(wd); 195 196 final File 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 207 public boolean saveSettings() { boolean projRootChanged = false; 209 210 File 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 bd = _buildDirSelector.getFileFromField(); 219 if (_buildDirSelector.getFileField().getText().equals("")) bd = null; 220 _model.setBuildDirectory(bd); 221 222 File wd = _workDirSelector.getFileFromField(); 223 if (_workDirSelector.getFileField().getText().equals("")) wd = null; 224 _model.setWorkingDirectory(wd); 225 226 File mc = _mainDocumentSelector.getFileFromField(); 227 if (_mainDocumentSelector.getFileField().getText().equals("")) mc = null; 228 _model.setMainClass(mc); 229 230 Vector <File > extras = _extraClassPathList.getValue(); 231 ClassPathVector cpv = new ClassPathVector(); 232 for (File cf : extras) { cpv.add(cf); } 233 _model.setExtraClassPath(cpv); 234 235 if (projRootChanged) { 237 try { 238 _model.reloadProject(_mainFrame.getCurrentProject(), _mainFrame.gatherProjectDocInfo()); 239 } catch(IOException e) { throw new edu.rice.cs.util.UnexpectedException(e, "I/O error while reloading project"); } 240 } 241 return true; 242 } 243 244 245 private File _getProjRoot() { 246 File projRoot = _mainFrame.getModel().getProjectRoot(); 247 if (projRoot != null) return projRoot; 248 return FileOption.NULL_FILE; 249 } 250 251 252 private File _getBuildDir() { 253 File buildDir = _mainFrame.getModel().getBuildDirectory(); 254 if (buildDir != null) return buildDir; 255 return FileOption.NULL_FILE; 256 } 257 258 259 private File _getWorkDir() { 260 File workDir = _mainFrame.getModel().getWorkingDirectory(); 261 if (workDir != null) return workDir; 262 return FileOption.NULL_FILE; 263 } 264 265 266 private File _getMainFile() { 267 File 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 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 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 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 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 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 () { 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 _projRootSelector = new DirectorySelectorComponent(this, dirChooser, 20, 12f); 397 399 _projRootSelector.getFileField().getDocument().addDocumentListener(_applyListener); 400 401 return _projRootSelector; 402 } 403 404 public JPanel _buildDirectoryPanel() { 405 DirectoryChooser dirChooser = new DirectoryChooser(this); 406 File 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 _buildDirSelector = new DirectorySelectorComponent(this, dirChooser, 20, 12f, false); 414 _buildDirSelector.setFileField(bd); 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 _workDirSelector = new DirectorySelectorComponent(this, dirChooser, 20, 12f); 429 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 apply(Object oc) { 439 _applyButton.setEnabled(true); 440 return null; 441 } 442 }); 443 return _extraClassPathList.getComponent(); 444 } 445 446 public JPanel _mainDocumentSelector() { 447 final File 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 chooser.setCurrentDirectory(projRoot); 456 File mainFile = _getMainFile(); 457 if (mainFile != FileOption.NULL_FILE) chooser.setSelectedFile(mainFile); 458 459 chooser.setApproveButtonText("Select"); 460 461 FileFilter filter = new FileFilter () { 462 public boolean accept(File f) { 463 String 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 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 () { 486 public boolean accept(File f) { return f.getName().endsWith(".jar") || f.isDirectory(); } 487 public String getDescription() { return "Java Archive Files (*.jar)"; } 488 }); 489 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 () { 501 public boolean accept(File f) { return f.getName().endsWith(".jar") || f.isDirectory(); } 502 public String getDescription() { return "Java Archive Files (*.jar)"; } 503 }); 504 return _jarFileSelector; 506 } 507 508 } 513 | Popular Tags |