KickJava   Java API By Example, From Geeks To Geeks.

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


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 edu.rice.cs.util.swing.DirectorySelectorComponent;
37 import edu.rice.cs.util.swing.DirectoryChooser;
38 import edu.rice.cs.drjava.DrJava;
39 import edu.rice.cs.drjava.config.Configuration;
40 import edu.rice.cs.drjava.config.OptionConstants;
41 import edu.rice.cs.util.DirectorySelector;
42 import edu.rice.cs.util.OperationCanceledException;
43
44 import javax.swing.*;
45 import java.io.File JavaDoc;
46
47 /** Manages a dialog box that can select a destination directory for generating Javadoc. The getDirectory method should
48  * be called to show the dialog, using the suggested location for the Javadoc as the "start" file. If the user
49  * modifies the selection once, the user's choice will be remembered and no further suggestions will be used.
50  *
51  * @version $Id: JavadocDialog.java 4077 2007-01-19 23:16:02Z sigma_lmtd $
52  */

53 public class JavadocDialog implements DirectorySelector {
54   /** Parent frame of the dialog. */
55   private final JFrame _frame;
56
57   /** File field and button. */
58   private final DirectorySelectorComponent _selector;
59
60   /** Whether to always prompt for destination. */
61   private final JCheckBox _checkBox;
62
63   /** OptionPane from which to get the results. */
64   private final JOptionPane _optionPane;
65
66   /** Dialog to show. */
67   private final JDialog _dialog;
68
69   /** Whether to use the suggested directory each time the dialog is shown. */
70   private boolean _useSuggestion;
71
72   /** Current suggestion for the destination directory, or null. */
73   private File JavaDoc _suggestedDir;
74
75   /** Creates a new JavadocDialog to show from the given frame.
76    *
77    * @param frame Parent frame of this dialog
78    */

79   public JavadocDialog(JFrame frame) {
80     _frame = frame;
81     _useSuggestion = true;
82     _suggestedDir = null;
83
84     // Create file chooser
85
DirectoryChooser chooser = new DirectoryChooser();
86     chooser.setMultiSelectionEnabled(false);
87     chooser.setApproveButtonText("Select");
88 // chooser.setEditable(true);
89

90     // Create components for dialog
91
String JavaDoc msg = "Select a destination directory for the Javadoc files:";
92     _selector = new DirectorySelectorComponent(_frame, chooser, DirectorySelectorComponent.DEFAULT_NUM_COLS, DirectorySelectorComponent.DEFAULT_FONT_SIZE, false);
93     _checkBox = new JCheckBox("Always Prompt For Destination");
94     Object JavaDoc[] components = new Object JavaDoc[] { msg, _selector, _checkBox };
95
96     _optionPane = new JOptionPane(components,
97                                   JOptionPane.QUESTION_MESSAGE,
98                                   JOptionPane.OK_CANCEL_OPTION);
99     _dialog = _optionPane.createDialog(_frame, "Select Javadoc Destination");
100     chooser.setOwner(_dialog);
101   }
102
103
104   public boolean isRecursive() { return false; }
105   
106   /** Shows the dialog prompting the user for a destination directory in which to generate Javadoc.
107    *
108    * This operation must be executed from the event-handling thread!
109    *
110    * @param start The directory to display in the text box. If null,
111    * the most recent suggested directory (passed in via setSuggestedDir)
112    * is displayed, unless the user has modified a previous suggestion.
113    * @return A directory to use for the Javadoc (which might not exist)
114    * @throws OperationCanceledException if the selection request is canceled
115    */

116   public File JavaDoc getDirectory(File JavaDoc start) throws OperationCanceledException {
117     if (start != null) {
118       // We were given a default - use it.
119
_selector.setFileField(start);
120     }
121     else if (_useSuggestion && (_suggestedDir != null)) {
122       // We weren't given one, so we need to use our suggestion.
123
_selector.setFileField(_suggestedDir);
124     }
125
126     Configuration config = DrJava.getConfig();
127     boolean ask = config.getSetting(OptionConstants.JAVADOC_PROMPT_FOR_DESTINATION).booleanValue();
128
129     if (ask) {
130       // The "always prompt" checkbox should be checked
131
_checkBox.setSelected(true);
132
133       // Prompt the user
134
MainFrame.setPopupLoc(_dialog, _frame);
135       _dialog.setVisible(true);
136
137       // Get result
138
if (!_isPositiveResult()) {
139         throw new OperationCanceledException();
140       }
141
142       // See if the user wants to suppress this dialog in the future.
143
if (!_checkBox.isSelected()) {
144         config.setSetting(OptionConstants.JAVADOC_PROMPT_FOR_DESTINATION,
145                           Boolean.FALSE);
146       }
147
148       // Check if the user disagreed with the suggestion
149
if ((start == null) &&
150           (_useSuggestion && (_suggestedDir != null)) &&
151           !_selector.getFileFromField().equals(_suggestedDir)) {
152         _useSuggestion = false;
153       }
154     }
155     return _selector.getFileFromField();
156   }
157
158   /** Asks the user a yes/no question.
159    * @return true if the user responded affirmatively, false if negatively
160    */

161   public boolean askUser(String JavaDoc message, String JavaDoc title) {
162     int choice = JOptionPane.showConfirmDialog(_frame, message, title, JOptionPane.YES_NO_OPTION);
163     return (choice == JOptionPane.YES_OPTION);
164   }
165
166   /** Warns the user about an error condition. */
167   public void warnUser(String JavaDoc message, String JavaDoc title) {
168     JOptionPane.showMessageDialog(_frame, message, title, JOptionPane.ERROR_MESSAGE);
169   }
170
171   /** Sets the suggested destination directory for Javadoc generation. This directory will be displayed
172    * in the file field if the user has not modified the suggestion in the past.
173    * @param dir Suggested destination directory
174    */

175   public void setSuggestedDir(File JavaDoc dir) { _suggestedDir = dir; }
176
177   /** Sets whether the dialog should use the suggested directory provided
178    * to the getDirectory method as the default location.
179    * @param use Whether to use the suggested directory
180    */

181   public void setUseSuggestion(boolean use) { _useSuggestion = use; }
182
183   /** Returns whether the JOptionPane currently has the OK_OPTION result. */
184   private boolean _isPositiveResult() {
185     Object JavaDoc result = _optionPane.getValue();
186     if ((result != null) && (result instanceof Integer JavaDoc)) {
187       int rc = ((Integer JavaDoc)result).intValue();
188       return rc == JOptionPane.OK_OPTION;
189     }
190     else return false;
191   }
192 }
Popular Tags