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 javax.swing.event.*;
041: import edu.rice.cs.drjava.config.*;
042: import edu.rice.cs.drjava.*;
043: import edu.rice.cs.util.swing.DirectorySelectorComponent;
044: import edu.rice.cs.util.swing.DirectoryChooser;
045: import java.awt.*;
046: import java.io.File;
047: import javax.swing.filechooser.FileFilter;
048:
049: /** Graphical form of a FileOption.
050: *
051: * @version $Id: DirectoryOptionComponent.java 4255 2007-08-28 19:17:37Z mgricken $
052: */
053: public class DirectoryOptionComponent extends OptionComponent<File>
054: implements OptionConstants {
055:
056: private DirectorySelectorComponent _component;
057:
058: public DirectoryOptionComponent(FileOption opt, String text,
059: Frame parent, DirectoryChooser dc) {
060: super (opt, text, parent);
061:
062: _component = new DirectorySelectorComponent(parent, dc, 30, 10f);
063: _component.setFileField(DrJava.getConfig().getSetting(_option));
064: _component.getFileField().getDocument().addDocumentListener(
065: new DocumentListener() {
066: public void insertUpdate(DocumentEvent e) {
067: notifyChangeListeners();
068: }
069:
070: public void removeUpdate(DocumentEvent e) {
071: notifyChangeListeners();
072: }
073:
074: public void changedUpdate(DocumentEvent e) {
075: notifyChangeListeners();
076: }
077: });
078: }
079:
080: /** Constructor that allows for a tooltip description. */
081: public DirectoryOptionComponent(FileOption opt, String text,
082: Frame parent, String desc, DirectoryChooser dc) {
083: this (opt, text, parent, dc);
084: setDescription(desc);
085: }
086:
087: /** Sets the tooltip description text for this option.
088: * @param description the tooltip text
089: */
090: public void setDescription(String description) {
091: _label.setToolTipText(description);
092: }
093:
094: /** Updates the config object with the new setting.
095: * @return true if the new value is set successfully
096: */
097: public boolean updateConfig() {
098: File componentFile = _component.getFileFromField();
099: File currentFile = DrJava.getConfig().getSetting(_option);
100:
101: if (componentFile != null && !componentFile.equals(currentFile)) {
102: DrJava.getConfig().setSetting(_option, componentFile);
103: } else if (componentFile == null) {
104: DrJava.getConfig()
105: .setSetting(_option, _option.getDefault());
106: }
107:
108: return true;
109: }
110:
111: /** Displays the given value. */
112: public void setValue(File value) {
113: _component.setFileField(value);
114: }
115:
116: /** Return's this OptionComponent's configurable component. */
117: public JComponent getComponent() {
118: return _component;
119: }
120:
121: /** Adds a filter to decide if a directory can be chosen. */
122: public void addChoosableFileFilter(FileFilter filter) {
123: _component.addChoosableFileFilter(filter);
124: }
125: }
|