001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.makewizard;
043:
044: import java.awt.Component;
045: import java.awt.GridBagConstraints;
046: import java.awt.GridBagLayout;
047: import java.awt.Insets;
048: import java.awt.event.ActionEvent;
049: import java.awt.event.ActionListener;
050: import java.awt.event.FocusEvent;
051: import java.awt.event.FocusListener;
052: import java.io.File;
053: import java.io.IOException;
054: import javax.swing.JButton;
055: import javax.swing.JDialog;
056: import javax.swing.JFileChooser;
057: import javax.swing.JLabel;
058: import javax.swing.JTextArea;
059: import javax.swing.JTextField;
060: import javax.swing.text.JTextComponent;
061: import org.netbeans.modules.cnd.api.utils.IpeFileSystemView;
062: import org.netbeans.modules.cnd.api.utils.IpeUtils;
063:
064: /**
065: * The FileChooserPanel is used to derive various panels in the Makefile
066: * wizard which have a single text field and file chooser.
067: */
068:
069: public abstract class FileChooserPanel extends MakefileWizardPanel
070: implements FocusListener {
071:
072: /** Serial version number */
073: static final long serialVersionUID = -3638230469770222549L;
074:
075: /** The label of the file/path component */
076: private JLabel label;
077:
078: /** The component for the file/path we are looking for */
079: private JTextField text;
080:
081: /** The component that contains the help information */
082: private JTextArea helpText;
083:
084: /** Store the file (possibly customized) file chooser */
085: protected JFileChooser fc;
086:
087: /** The type of file/path information to return (see flags below) */
088: private int pathType;
089:
090: /** The initial value for pathType */
091: public static final int PATH_NONE = 0;
092:
093: /** Return the file/path as an absolute file */
094: public static final int ABSOLUTE_PATH = 1;
095:
096: /** Return the file/path relative to the currentDirectory */
097: public static final int RELATIVE_PATH = 2;
098:
099: /** Return the file/path from File.getName() */
100: public static final int NAME_ONLY = 3;
101:
102: /** Default constructor */
103: public FileChooserPanel(MakefileWizard wd) {
104: super (wd);
105: init();
106: }
107:
108: /** Create a panel passing in a file chooser */
109: public FileChooserPanel(MakefileWizard wd, JFileChooser fc) {
110: super (wd);
111: this .fc = fc;
112: init();
113: }
114:
115: protected void init() {
116: pathType = PATH_NONE;
117: }
118:
119: public void setPathType(int pathType) {
120: if (pathType == ABSOLUTE_PATH || pathType == RELATIVE_PATH
121: || pathType == NAME_ONLY) {
122: this .pathType = pathType;
123: }
124: }
125:
126: public void create(String labelString) {
127: create(labelString, pathType, null);
128: }
129:
130: public void create(String labelString, int pathType) {
131: create(labelString, pathType, null);
132: }
133:
134: public void create(String labelString, String help) {
135: create(labelString, pathType, help);
136: }
137:
138: public void create(String labelString, final int pathType,
139: String help) {
140: int gridy = 0;
141:
142: setLayout(new GridBagLayout());
143: GridBagConstraints grid = new GridBagConstraints();
144: this .pathType = pathType;
145:
146: // Create the components.
147: // First, the label
148: label = new JLabel(labelString);
149: grid.anchor = GridBagConstraints.NORTHWEST;
150: grid.gridx = 0;
151: grid.gridy = gridy++;
152: grid.gridwidth = GridBagConstraints.REMAINDER;
153: add(label, grid);
154:
155: // Next, the text field
156: text = new JTextField();
157: text.addFocusListener(this );
158: grid.gridy = gridy++;
159: grid.weightx = 1.0;
160: grid.gridwidth = GridBagConstraints.RELATIVE;
161: grid.fill = GridBagConstraints.HORIZONTAL;
162: grid.anchor = GridBagConstraints.WEST;
163: add(text, grid);
164:
165: // Next, the file chooser button
166: JButton chooser = new JButton(getString("BTN_Chooser")); // NOI18N
167: chooser.setMnemonic(getString("MNEM_Chooser").charAt(0)); // NOI18N
168: grid.gridx = 2;
169: grid.weightx = 0.0;
170: grid.gridwidth = GridBagConstraints.REMAINDER;
171: grid.fill = GridBagConstraints.NONE;
172: grid.insets = new Insets(0, 5, 0, 0);
173: add(chooser, grid);
174: chooser.addActionListener(new ActionListener() {
175: public void actionPerformed(ActionEvent evt) {
176: File file;
177: File cwd;
178:
179: if (fc == null) {
180: fc = new JFileChooser();
181: fc.setApproveButtonText(getString("BTN_Approve")); // NOI18N
182: fc.setFileSystemView(new IpeFileSystemView(fc
183: .getFileSystemView()));
184: fc
185: .setDialogTitle(getString("DLG_FILE_CHOOSER_TITLE")); // NOI18N
186: }
187:
188: // See if the user has already typed a directory. If so use it.
189: String cur = IpeUtils.expandPath(text.getText());
190: if (cur.length() > 0
191: && (file = new File(cur)).isDirectory()) {
192: if (cur.charAt(0) == '.')
193: cwd = new File(getMakefileData()
194: .getBaseDirectory()
195: + File.separator + cur);
196: else
197: cwd = file;
198: } else {
199: cwd = new File(getMakefileData().getBaseDirectory());
200: }
201: fc.setCurrentDirectory(cwd);
202:
203: int returnVal = fc.showDialog(FileChooserPanel.this ,
204: null);
205: if (returnVal == JFileChooser.APPROVE_OPTION) {
206: file = fc.getSelectedFile();
207: if (pathType == ABSOLUTE_PATH) {
208: try {
209: text.setText(file.getCanonicalPath());
210: } catch (IOException ex) {
211: text.setText(file.getAbsolutePath());
212: }
213: } else if (pathType == NAME_ONLY) {
214: text.setText(file.getName());
215: } else if (pathType == RELATIVE_PATH) {
216: String path = null;
217: String cwdpath;
218:
219: try {
220: path = file.getAbsolutePath();
221: cwdpath = cwd.getCanonicalPath();
222:
223: if (path.equals(cwdpath)) {
224: text.setText("."); // NOI18N
225: } else if (path.startsWith(cwdpath)) {
226: text.setText(path.substring(cwdpath
227: .length() + 1));
228: } else {
229: text.setText(path);
230: }
231: } catch (IOException ex) {
232: if (path == null) {
233: text.setText(file.getAbsolutePath());
234: } else {
235: text.setText(path);
236: }
237: }
238: }
239: onOk();
240: }
241: }
242: });
243:
244: if (help != null) {
245: grid.anchor = GridBagConstraints.NORTHWEST;
246: grid.gridx = 0;
247: grid.gridy = gridy++;
248: grid.weighty = 1.0;
249: grid.gridheight = GridBagConstraints.REMAINDER;
250: grid.fill = GridBagConstraints.BOTH;
251: grid.insets = new Insets(11, 0, 0, 0);
252: helpText = new JTextArea(help);
253: helpText.getAccessibleContext().setAccessibleName(
254: "ACSN_DirHelp"); // NOI18N
255: helpText.addFocusListener(this );
256: helpText.setEditable(false);
257: helpText.setFocusable(false);
258: helpText.setLineWrap(true);
259: helpText.setWrapStyleWord(true);
260: helpText.setBackground(label.getBackground());
261: add(helpText, grid);
262: } else {
263: grid.gridx = 0;
264: grid.gridy = gridy++;
265: grid.gridwidth = GridBagConstraints.REMAINDER;
266: grid.gridheight = GridBagConstraints.REMAINDER;
267: grid.weightx = 1.0;
268: grid.weighty = 1.0;
269: add(new JLabel(""), grid); // NOI18N
270: }
271: }
272:
273: /** If something is to be done on OK then it must be in a derived class */
274: protected void onOk() {
275: }
276:
277: /** Return the label widget */
278: protected JLabel getLabel() {
279: return label;
280: }
281:
282: /** Return the text widget so its value can be querried */
283: protected JTextField getText() {
284: return text;
285: }
286:
287: public void focusGained(FocusEvent evt) {
288: Component comp = evt.getComponent();
289: if (comp == helpText || comp == text) {
290: ((JTextComponent) comp).selectAll();
291: }
292: }
293:
294: public void focusLost(FocusEvent evt) {
295: // help doesn't always get deselected so set total
296: // selection length to zero.
297: ((JTextComponent) evt.getComponent()).setSelectionEnd(0);
298: }
299:
300: public void addNotify() {
301: super .addNotify();
302: IpeUtils.requestFocus(text);
303: }
304:
305: public void removeNotify() {
306: super .removeNotify();
307:
308: if (fc != null && fc.isShowing()) {
309: Object o = fc.getTopLevelAncestor();
310: if (o != null && o instanceof JDialog) {
311: ((JDialog) o).dispose();
312: }
313: }
314: }
315: }
|