001: /*
002: JSmooth: a VM wrapper toolkit for Windows
003: Copyright (C) 2003 Rodrigo Reyes <reyes@charabia.net>
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the GNU General Public License as published by
007: the Free Software Foundation; either version 2 of the License, or
008: (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public License
016: along with this program; if not, write to the Free Software
017: Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018:
019: */
020:
021: package net.charabia.jsmoothgen.application.gui.util;
022:
023: import javax.swing.*;
024: import java.io.*;
025:
026: /**
027: * Represents a JTextField associated with a button that pops up a
028: * file dialog selector.
029: *
030: */
031:
032: public class FileSelectionTextField extends javax.swing.JPanel {
033: private JButton m_buttonFileSelection;
034: private JFileChooser m_fileChooser;
035: private JTextField m_filename;
036:
037: private File m_basedir = null;
038: private java.util.Vector m_listeners = new java.util.Vector();
039:
040: public interface FileSelected {
041: public void fileSelected(String filename);
042: }
043:
044: public FileSelectionTextField() {
045: m_fileChooser = new javax.swing.JFileChooser();
046: m_filename = new javax.swing.JTextField();
047: m_buttonFileSelection = new javax.swing.JButton();
048:
049: setLayout(new java.awt.BorderLayout());
050:
051: add(m_filename, java.awt.BorderLayout.CENTER);
052:
053: m_buttonFileSelection.setText("...");
054: m_buttonFileSelection
055: .addActionListener(new java.awt.event.ActionListener() {
056: public void actionPerformed(
057: java.awt.event.ActionEvent evt) {
058: buttonFileSelectionActionPerformed(evt);
059: }
060: });
061:
062: m_filename
063: .addActionListener(new java.awt.event.ActionListener() {
064: public void actionPerformed(
065: java.awt.event.ActionEvent evt) {
066: if (m_filename.getText().length() > 0)
067: setFile(new java.io.File(m_filename
068: .getText()));
069: else
070: setFile(null);
071: notifyListeners(m_filename.getText());
072: }
073: });
074: m_filename.addFocusListener(new java.awt.event.FocusAdapter() {
075: public void focusLost(java.awt.event.FocusEvent e) {
076: if (m_filename.getText().length() > 0)
077: setFile(new java.io.File(m_filename.getText()));
078: else
079: setFile(null);
080: // setFile(new java.io.File(m_filename.getText()));
081: notifyListeners(m_filename.getText());
082: }
083: });
084:
085: add(m_buttonFileSelection, java.awt.BorderLayout.EAST);
086: }
087:
088: public void addListener(FileSelected fs) {
089: m_listeners.add(fs);
090: }
091:
092: public void removeListener(FileSelected fs) {
093: m_listeners.remove(fs);
094: }
095:
096: public void notifyListeners(String filename) {
097: for (int i = 0; i < m_listeners.size(); i++) {
098: FileSelected fs = (FileSelected) m_listeners.get(i);
099: fs.fileSelected(filename);
100: }
101: }
102:
103: private void buttonFileSelectionActionPerformed(
104: java.awt.event.ActionEvent evt) {
105: String fname = m_filename.getText().trim();
106: java.io.File cur = new java.io.File(fname);
107: if ((cur.isAbsolute() == false) && (m_basedir != null)) {
108: cur = new File(m_basedir, cur.toString()).getAbsoluteFile();
109: try {
110: cur = cur.getCanonicalFile();
111: } catch (IOException iox) {
112: iox.printStackTrace();
113: }
114: }
115: m_fileChooser.setSelectedFile(cur);
116: if (m_fileChooser.showDialog(this , "Select") == JFileChooser.APPROVE_OPTION) {
117: java.io.File f = m_fileChooser.getSelectedFile();
118: if (m_basedir != null) {
119: File rel = net.charabia.jsmoothgen.application.JSmoothModelPersistency
120: .makePathRelativeIfPossible(m_basedir, f);
121: m_filename.setText(rel.toString());
122: notifyListeners(rel.toString());
123: } else {
124: m_filename.setText(f.getAbsolutePath());
125: notifyListeners(f.getAbsolutePath());
126: }
127: }
128: }
129:
130: public void setFile(java.io.File f) {
131: // System.out.println("Setting File: " + f + " / " + m_basedir);
132: if (f == null) {
133: m_filename.setText("");
134: } else if (m_basedir != null) {
135: try {
136: File rel = net.charabia.jsmoothgen.application.JSmoothModelPersistency
137: .makePathRelativeIfPossible(m_basedir, f);
138: m_filename.setText(rel.toString());
139: } catch (Throwable thr) {
140: m_filename.setText(f.toString());
141: }
142: } else {
143: m_filename.setText(f.toString());
144: }
145: // System.out.println("Filename: " + m_filename.getText());
146: }
147:
148: public java.io.File getFile() {
149: if (m_filename.getText().trim().length() == 0)
150: return null;
151: return new java.io.File(m_filename.getText());
152: }
153:
154: public void setFileChooser(JFileChooser jfc) {
155: m_fileChooser = jfc;
156: }
157:
158: public void setFileSelectionMode(int mode) {
159: m_fileChooser.setFileSelectionMode(mode);
160: }
161:
162: public void setBaseDir(File root) {
163: m_fileChooser.setCurrentDirectory(root);
164: m_basedir = root;
165: }
166:
167: public File getBaseDir() {
168: return m_basedir;
169: }
170:
171: public void setEnabled(boolean b) {
172: m_buttonFileSelection.setEnabled(b);
173: m_filename.setEnabled(b);
174: }
175:
176: }
|