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.ui.options;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.io.File;
047: import java.io.IOException;
048: import java.util.StringTokenizer;
049: import javax.swing.JComboBox;
050: import javax.swing.JFileChooser;
051: import javax.swing.JPanel;
052: import javax.swing.filechooser.FileFilter;
053: import javax.swing.filechooser.FileSystemView;
054: import org.netbeans.modules.cnd.MIMENames;
055: import org.netbeans.modules.cnd.api.compilers.CompilerSet;
056: import org.netbeans.modules.cnd.api.compilers.Tool;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.util.NbBundle;
059:
060: /**
061: * A simple dialog for adding and removing compilers.
062: *
063: * @author Gordon Prieur
064: */
065: public class AddRemoveToolPanel extends JPanel implements
066: ActionListener {
067:
068: AddRemoveListModel model;
069: CompilerSet cs;
070:
071: /** Creates new form AddRemoveToolPanel */
072: public AddRemoveToolPanel(JComboBox jc, CompilerSet cs) {
073: Object o;
074:
075: this .cs = cs;
076: model = new AddRemoveListModel();
077: for (int i = 0; i < jc.getItemCount(); i++) {
078: o = jc.getItemAt(i);
079: if (o instanceof Tool) {
080: model.addElement(o);
081: }
082: }
083:
084: initComponents();
085: }
086:
087: public void actionPerformed(ActionEvent ev) {
088: Object o = ev.getSource();
089: if (o == addButton) {
090: String name = addUserTool();
091: if (name != null && !model.contains(name)) {
092: model.addAddElement(name);
093: compilerList.setSelectedValue(name, true);
094: removeButton.setEnabled(true);
095: }
096: } else if (o == removeButton) {
097: int idx = compilerList.getSelectedIndex();
098: if (idx >= 0) {
099: model.remove(idx);
100: if (model.getSize() > 0) {
101: compilerList
102: .setSelectedIndex(idx < model.getSize() ? idx
103: : (model.getSize() - 1));
104: removeButton.setEnabled(true);
105: } else {
106: removeButton.setEnabled(false);
107: }
108: }
109: }
110: }
111:
112: private String addUserTool() {
113: JFileChooser fc = new FileOnlyChooser(new CompilerSetView(cs
114: .getDirectory()));
115: fc.setDialogType(JFileChooser.FILES_ONLY);
116: fc.setFileFilter(new FileOnlyFilter());
117: int rc = fc.showDialog(this , NbBundle.getMessage(
118: ToolsPanel.class, "LBL_DialogAddButton"));
119: if (rc == JFileChooser.APPROVE_OPTION) {
120: File file = fc.getSelectedFile();
121: String name;
122: String path = file.getAbsolutePath();
123: int pos = path.lastIndexOf(File.separator);
124: if (pos >= 0) {
125: name = path.substring(pos + 1);
126: path = path.substring(0, pos);
127: } else {
128: name = file.getName();
129: }
130:
131: // validate the input
132: StringTokenizer tok = new StringTokenizer(cs.getDirectory());
133: boolean valid = false;
134: while (tok.hasMoreTokens()) {
135: String dir = tok.nextToken();
136: if (dir.equals(path)) {
137: file = new File(dir, name);
138: if (!file.isDirectory() && file.exists()) {
139: return name;
140: }
141: }
142: }
143: }
144: return null;
145: }
146:
147: protected AddRemoveListModel getModel() {
148: return model;
149: }
150:
151: private class FileOnlyChooser extends JFileChooser {
152:
153: public FileOnlyChooser(FileSystemView view) {
154: super (view);
155: }
156:
157: // Correctly implementing this method will get proper icons in the file chooser!
158: // public Icon getIcon(File file) {
159: // FileObject fo = FileUtil.toFileObject(file);
160: // return super.getIcon(file);
161: // }
162:
163: public boolean isTraversible(File f) {
164: return false;
165: }
166:
167: public boolean isDirectorySelectionEnabled() {
168: return false;
169: }
170:
171: public boolean isAcceptAllFileFilterUsed() {
172: return false;
173: }
174: }
175:
176: private class CompilerSetView extends FileSystemView {
177:
178: File[] roots;
179:
180: public CompilerSetView(String dirs) {
181: int pos = dirs.indexOf(File.pathSeparator);
182: if (pos >= 0) {
183: roots = new File[2];
184: roots[0] = new File(dirs.substring(0, pos));
185: roots[1] = new File(dirs.substring(pos + 1));
186: } else {
187: roots = new File[1];
188: roots[0] = new File(dirs);
189: }
190: }
191:
192: public String getSystemDisplayName(File file) {
193: if (file.isDirectory()) {
194: return file.getAbsolutePath();
195: } else {
196: return super .getSystemDisplayName(file);
197: }
198: }
199:
200: public File[] getRoots() {
201: return roots;
202: }
203:
204: public File getDefaultDirectory() {
205: return roots[0];
206: }
207:
208: public File getParentDirectory() {
209: return null;
210: }
211:
212: public File getHomeDirectory() {
213: return getDefaultDirectory();
214: }
215:
216: public Boolean isTraversable(File file) {
217: if (file.isDirectory()) {
218: for (int i = 0; i < roots.length; i++) {
219: if (roots[i].equals(file)) {
220: return Boolean.TRUE;
221: }
222: }
223: }
224: return Boolean.FALSE;
225: }
226:
227: public File createNewFolder(File file) throws IOException {
228: return file;
229: }
230: }
231:
232: private class FileOnlyFilter extends FileFilter {
233:
234: public boolean accept(File file) {
235: if (!file.isDirectory()) {
236: String mimetype = FileUtil.toFileObject(file)
237: .getMIMEType();
238: if (mimetype.startsWith(MIMENames.EXE_MIME_TYPE)
239: || mimetype.equals(MIMENames.SHELL_MIME_TYPE)) {
240: return true;
241: }
242: }
243: return false;
244: }
245:
246: public String getDescription() {
247: return NbBundle.getMessage(ToolsPanel.class,
248: "LBL_ExecutableFilesOnly"); // NOI18N
249: }
250: }
251:
252: /** This method is called from within the constructor to
253: * initialize the form.
254: * WARNING: Do NOT modify this code. The content of this method is
255: * always regenerated by the Form Editor.
256: */
257: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
258: private void initComponents() {
259: compilerListLabel = new javax.swing.JLabel();
260: compilerListScrollPane = new javax.swing.JScrollPane();
261: compilerList = new javax.swing.JList();
262: compilerList.setModel(model);
263: addButton = new javax.swing.JButton();
264: addButton.addActionListener(this );
265: removeButton = new javax.swing.JButton();
266: removeButton.addActionListener(this );
267: if (model.getSize() > 0) {
268: compilerList.setSelectedIndex(0);
269: removeButton.setEnabled(true);
270: } else {
271: removeButton.setEnabled(false);
272: }
273:
274: compilerListLabel
275: .setDisplayedMnemonic(java.util.ResourceBundle
276: .getBundle(
277: "org/netbeans/modules/cnd/ui/options/Bundle")
278: .getString("MNEM_CompilerList").charAt(0));
279: compilerListLabel.setLabelFor(compilerList);
280: compilerListLabel.setText(java.util.ResourceBundle.getBundle(
281: "org/netbeans/modules/cnd/ui/options/Bundle")
282: .getString("LBL_CompilerList"));
283:
284: compilerListScrollPane.setViewportView(compilerList);
285:
286: addButton.setMnemonic(java.util.ResourceBundle.getBundle(
287: "org/netbeans/modules/cnd/ui/options/Bundle")
288: .getString("MNEM_AddToolButton").charAt(0));
289: addButton.setText(java.util.ResourceBundle.getBundle(
290: "org/netbeans/modules/cnd/ui/options/Bundle")
291: .getString("LBL_AddToolButton"));
292: addButton.getAccessibleContext().setAccessibleName(
293: java.util.ResourceBundle.getBundle(
294: "org/netbeans/modules/cnd/ui/options/Bundle")
295: .getString("ACSN_AddToolButton"));
296: addButton.getAccessibleContext().setAccessibleDescription(
297: java.util.ResourceBundle.getBundle(
298: "org/netbeans/modules/cnd/ui/options/Bundle")
299: .getString("ACSD_AddToolButton"));
300:
301: removeButton.setMnemonic(java.util.ResourceBundle.getBundle(
302: "org/netbeans/modules/cnd/ui/options/Bundle")
303: .getString("MNEM_RemoveToolButton").charAt(0));
304: removeButton.setText(java.util.ResourceBundle.getBundle(
305: "org/netbeans/modules/cnd/ui/options/Bundle")
306: .getString("LBL_RemoveToolButton"));
307: removeButton.getAccessibleContext().setAccessibleName(
308: java.util.ResourceBundle.getBundle(
309: "org/netbeans/modules/cnd/ui/options/Bundle")
310: .getString("ACSN_RemoveToolButton"));
311: removeButton.getAccessibleContext().setAccessibleDescription(
312: java.util.ResourceBundle.getBundle(
313: "org/netbeans/modules/cnd/ui/options/Bundle")
314: .getString("ACSD_RemoveToolButton"));
315:
316: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
317: this );
318: this .setLayout(layout);
319: layout
320: .setHorizontalGroup(layout
321: .createParallelGroup(
322: org.jdesktop.layout.GroupLayout.LEADING)
323: .add(
324: layout
325: .createSequentialGroup()
326: .addContainerGap()
327: .add(
328: layout
329: .createParallelGroup(
330: org.jdesktop.layout.GroupLayout.LEADING)
331: .add(
332: org.jdesktop.layout.GroupLayout.TRAILING,
333: layout
334: .createSequentialGroup()
335: .add(
336: compilerListScrollPane,
337: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
338: 227,
339: Short.MAX_VALUE)
340: .addPreferredGap(
341: org.jdesktop.layout.LayoutStyle.RELATED)
342: .add(
343: layout
344: .createParallelGroup(
345: org.jdesktop.layout.GroupLayout.LEADING)
346: .add(
347: addButton,
348: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
349: 83,
350: Short.MAX_VALUE)
351: .add(
352: removeButton)))
353: .add(
354: compilerListLabel,
355: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
356: 259,
357: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
358: .addContainerGap()));
359: layout
360: .setVerticalGroup(layout
361: .createParallelGroup(
362: org.jdesktop.layout.GroupLayout.LEADING)
363: .add(
364: layout
365: .createSequentialGroup()
366: .addContainerGap()
367: .add(compilerListLabel)
368: .addPreferredGap(
369: org.jdesktop.layout.LayoutStyle.RELATED)
370: .add(
371: layout
372: .createParallelGroup(
373: org.jdesktop.layout.GroupLayout.LEADING)
374: .add(
375: layout
376: .createSequentialGroup()
377: .add(
378: addButton,
379: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
380: 29,
381: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
382: .addPreferredGap(
383: org.jdesktop.layout.LayoutStyle.RELATED)
384: .add(
385: removeButton,
386: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
387: 25,
388: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
389: .addContainerGap(
390: 208,
391: Short.MAX_VALUE))
392: .add(
393: compilerListScrollPane,
394: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
395: 272,
396: Short.MAX_VALUE))));
397:
398: layout.linkSize(new java.awt.Component[] { addButton,
399: removeButton },
400: org.jdesktop.layout.GroupLayout.VERTICAL);
401:
402: }// </editor-fold>//GEN-END:initComponents
403:
404: // Variables declaration - do not modify//GEN-BEGIN:variables
405: private javax.swing.JButton addButton;
406: private javax.swing.JList compilerList;
407: private javax.swing.JLabel compilerListLabel;
408: private javax.swing.JScrollPane compilerListScrollPane;
409: private javax.swing.JButton removeButton;
410: // End of variables declaration//GEN-END:variables
411:
412: }
|