001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.ui.config;
038:
039: import javax.swing.*;
040: import edu.rice.cs.drjava.ui.*;
041: import edu.rice.cs.drjava.config.*;
042: import edu.rice.cs.drjava.*;
043: import java.awt.*;
044: import java.awt.event.*;
045: import java.io.File;
046: import javax.swing.filechooser.FileFilter;
047:
048: import java.util.ArrayList;
049:
050: /** Graphical form of a VectorOption for the Extra Classpath/Sourcepath options. Uses a file chooser for each File element.
051: * @version $Id: VectorFileOptionComponent.java 4255 2007-08-28 19:17:37Z mgricken $
052: */
053: public class VectorFileOptionComponent extends
054: VectorOptionComponent<File> implements OptionConstants {
055: private FileFilter _fileFilter;
056: private JFileChooser _jfc;
057:
058: public VectorFileOptionComponent(VectorOption<File> opt,
059: String text, Frame parent) {
060: super (opt, text, parent); // creates all four buttons
061:
062: // set up JFileChooser
063: File workDir = new File(System.getProperty("user.home"));
064:
065: _jfc = new JFileChooser(workDir);
066: _jfc.setDialogTitle("Select");
067: _jfc.setApproveButtonText("Select");
068: _jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
069: _jfc.setMultiSelectionEnabled(true);
070: _fileFilter = ClassPathFilter.ONLY;
071: }
072:
073: /** Constructor that allows for a tooltip description. */
074: public VectorFileOptionComponent(VectorOption<File> opt,
075: String text, Frame parent, String description) {
076: this (opt, text, parent);
077: setDescription(description);
078: }
079:
080: /** Adds buttons to _buttonPanel */
081: protected void _addButtons() {
082: super ._addButtons();
083: _buttonPanel.add(_moveUpButton);
084: _buttonPanel.add(_moveDownButton);
085: }
086:
087: /** Displays the given value. */
088: public void setValue(ArrayList<File> files) {
089: _listModel.clear();
090: for (File f : files)
091: _listModel.addElement(f);
092: }
093:
094: /** Set the file filter for this vector option component. */
095: public void setFileFilter(FileFilter fileFilter) {
096: _fileFilter = fileFilter;
097: }
098:
099: /** Shows a file chooser for adding a file to the element. */
100: public void chooseFile() {
101: File selection = (File) _list.getSelectedValue();
102: if (selection != null) {
103: File parent = selection.getParentFile();
104: if (parent != null) {
105: _jfc.setCurrentDirectory(parent);
106: }
107: }
108:
109: _jfc.setFileFilter(_fileFilter);
110:
111: File[] c = null;
112: int returnValue = _jfc.showDialog(_parent, null);
113: if (returnValue == JFileChooser.APPROVE_OPTION) {
114: c = _jfc.getSelectedFiles();
115: }
116: if (c != null) {
117: for (int i = 0; i < c.length; i++) {
118: _listModel.addElement(c[i]);
119: }
120: }
121: }
122:
123: protected Action _getAddAction() {
124: return new AbstractAction("Add") {
125: public void actionPerformed(ActionEvent ae) {
126: chooseFile();
127: _list.setSelectedIndex(_listModel.getSize() - 1);
128: notifyChangeListeners();
129: }
130: };
131: }
132: }
|