001: // This file is part of KeY - Integrated Deductive Software Design
002: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
003: // Universitaet Koblenz-Landau, Germany
004: // Chalmers University of Technology, Sweden
005: //
006: // The KeY system is protected by the GNU General Public License.
007: // See LICENSE.TXT for details.
008: //
009: //
010: package de.uka.ilkd.key.util.install;
011:
012: import java.awt.GridLayout;
013: import java.awt.event.ActionEvent;
014: import java.awt.event.ActionListener;
015: import java.awt.event.FocusEvent;
016: import java.awt.event.FocusListener;
017: import java.io.File;
018:
019: import javax.swing.*;
020:
021: /**
022: * This class creates the GUI element below. The element is used to
023: * enter a path, e.g. where to install or find a program.
024: * -------------------------------------
025: * | text |======Path======| |...| |
026: * -------------------------------------
027: * The component exists of a leading 'text' followed by a text field
028: * where to enter the path and a button '...'. If it is cliecked on
029: * the button a file choser windows comes up that helps to select
030: * the right directory/file.
031: */
032: public class InstallationPathChooser extends JPanel {
033:
034: private JButton fileSelectionButton;
035: private JTextField pathEntryField;
036: private File file;
037:
038: /** event listener */
039: private InstallationPathListener ipListener = new InstallationPathListener();
040: /** file selection mode */
041: private int fileSelectionMode;
042:
043: private String label = "";
044:
045: /** creates an installation path chooser
046: * starts with text followed by a textfield
047: * @param text a String
048: * @param defaultPath the default path of the file
049: * @param mode an int describing the file chooser mode
050: */
051: public InstallationPathChooser(String text, String defaultPath,
052: int mode) {
053: this .fileSelectionMode = mode;
054: this .file = new File(defaultPath);
055: this .label = text;
056: setup(text, defaultPath);
057: }
058:
059: private void setup(String text, String defaultPath) {
060: setLayout(new GridLayout(1, 1));
061:
062: Box slots = Box.createHorizontalBox();
063: JPanel firstSecond = new JPanel(new GridLayout(1, 2));
064:
065: firstSecond.add(new JLabel(text));
066: pathEntryField = new JTextField(defaultPath, 15);
067: pathEntryField.addActionListener(ipListener);
068: pathEntryField.addFocusListener(ipListener);
069: firstSecond.add(pathEntryField);
070: slots.add(firstSecond);
071: fileSelectionButton = new JButton("...");
072:
073: fileSelectionButton.addActionListener(new ActionListener() {
074: public void actionPerformed(ActionEvent e) {
075: if (e.getSource() == fileSelectionButton) {
076: JFileChooser chooser = new JFileChooser(
077: pathEntryField.getText());
078: chooser.setFileSelectionMode(fileSelectionMode);
079: int returnVal = chooser.showOpenDialog(null);
080: if (returnVal == JFileChooser.APPROVE_OPTION) {
081: pathEntryField.setText(chooser
082: .getSelectedFile().getAbsolutePath());
083: }
084: }
085: }
086: });
087: slots.add(fileSelectionButton);
088: add(slots);
089: }
090:
091: public boolean isFile() {
092: return file.isFile();
093: }
094:
095: public boolean isDirectory() {
096: return file.isDirectory();
097: }
098:
099: public String getPath() {
100: return file.getPath();
101: }
102:
103: public String getFile() {
104: return isFile() ? file.getName() : "";
105: }
106:
107: public boolean updateModel() {
108: File nf = new File(pathEntryField.getText());
109: if (nf.exists()
110: && (fileSelectionMode == JFileChooser.FILES_ONLY
111: && !nf.isFile() || (fileSelectionMode == JFileChooser.DIRECTORIES_ONLY && !nf
112: .isDirectory()))) {
113: pathEntryField.selectAll();
114: return false;
115: } else {
116: file = nf;
117: return true;
118: }
119: }
120:
121: public String setPath(String path) {
122: String oldPath = pathEntryField.getText();
123: pathEntryField.setText(path);
124: return oldPath;
125: }
126:
127: public void setEnabled(boolean state) {
128: pathEntryField.setEnabled(state);
129: fileSelectionButton.setEnabled(state);
130: }
131:
132: public String label() {
133: return label;
134: }
135:
136: // static main method
137: public static void main(String[] args) {
138: JFrame testFrame = new JFrame();
139: testFrame.setSize(400, 300);
140: testFrame.getContentPane().add(
141: new InstallationPathChooser("Install Path",
142: "/home/bubel/", JFileChooser.FILES_ONLY));
143: testFrame.setVisible(true);
144: }
145:
146: // inner listener class
147: private class InstallationPathListener implements ActionListener,
148: FocusListener {
149:
150: public void actionPerformed(ActionEvent e) {
151: updateModel();
152: }
153:
154: public void focusLost(FocusEvent e) {
155: updateModel();
156: }
157:
158: public void focusGained(FocusEvent e) {
159: }
160: }
161:
162: }
|