001: /*
002: * WbFilePicker.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.beans.PropertyChangeEvent;
015: import java.beans.PropertyChangeListener;
016: import java.io.File;
017: import javax.swing.JFileChooser;
018: import javax.swing.SwingUtilities;
019: import javax.swing.filechooser.FileFilter;
020: import workbench.gui.WbSwingUtilities;
021: import workbench.log.LogMgr;
022: import workbench.resource.Settings;
023: import workbench.util.ExceptionUtil;
024: import workbench.util.StringUtil;
025:
026: /**
027: *
028: * @author support@sql-workbench.net
029: */
030: public class WbFilePicker extends javax.swing.JPanel {
031: private String lastDir;
032: private FileFilter fileFilter;
033: private boolean allowMultiple;
034: private File[] selectedFiles;
035: private String lastDirProperty = null;
036:
037: public WbFilePicker() {
038: initComponents();
039: }
040:
041: public void setTextFieldPropertyName(String name) {
042: this .tfFilename.setName(name);
043: }
044:
045: public void setTextfieldTooltip(String text) {
046: tfFilename.setToolTipText(text);
047: }
048:
049: public void setButtonTooltip(String text) {
050: selectFileButton.setToolTipText(text);
051: }
052:
053: public void setLastDirProperty(String prop) {
054: this .lastDirProperty = prop;
055: this .lastDir = Settings.getInstance().getProperty(prop, null);
056: }
057:
058: public void setEnabled(boolean flag) {
059: super .setEnabled(flag);
060: this .tfFilename.setEnabled(flag);
061: this .selectFileButton.setEnabled(flag);
062: }
063:
064: /** This method is called from within the constructor to
065: * initialize the form.
066: * WARNING: Do NOT modify this code. The content of this method is
067: * always regenerated by the Form Editor.
068: */
069: // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
070: private void initComponents() {
071: java.awt.GridBagConstraints gridBagConstraints;
072:
073: tfFilename = new StringPropertyEditor();
074: selectFileButton = new FlatButton();
075:
076: setLayout(new java.awt.GridBagLayout());
077:
078: tfFilename.setColumns(10);
079: tfFilename.setHorizontalAlignment(javax.swing.JTextField.LEFT);
080: tfFilename.setName("library"); // NOI18N
081: tfFilename.addMouseListener(new TextComponentMouseListener());
082: gridBagConstraints = new java.awt.GridBagConstraints();
083: gridBagConstraints.gridx = 0;
084: gridBagConstraints.gridy = 0;
085: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
086: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
087: gridBagConstraints.weightx = 1.0;
088: gridBagConstraints.weighty = 1.0;
089: add(tfFilename, gridBagConstraints);
090:
091: selectFileButton.setText("...");
092: selectFileButton.setMaximumSize(new java.awt.Dimension(22, 22));
093: selectFileButton.setMinimumSize(new java.awt.Dimension(22, 22));
094: selectFileButton
095: .setPreferredSize(new java.awt.Dimension(22, 22));
096: selectFileButton
097: .addActionListener(new java.awt.event.ActionListener() {
098: public void actionPerformed(
099: java.awt.event.ActionEvent evt) {
100: selectFileButtonActionPerformed(evt);
101: }
102: });
103: gridBagConstraints = new java.awt.GridBagConstraints();
104: gridBagConstraints.gridx = 1;
105: gridBagConstraints.gridy = 0;
106: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
107: gridBagConstraints.weighty = 1.0;
108: gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
109: add(selectFileButton, gridBagConstraints);
110: }// </editor-fold>//GEN-END:initComponents
111:
112: private void selectFileButtonActionPerformed(
113: java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectFileButtonActionPerformed
114: {//GEN-HEADEREND:event_selectFileButtonActionPerformed
115: try {
116: JFileChooser jf = new JFileChooser();
117: jf.setMultiSelectionEnabled(allowMultiple);
118: if (this .lastDir != null) {
119: jf.setCurrentDirectory(new File(this .lastDir));
120: }
121: if (this .fileFilter != null) {
122: jf.setFileFilter(this .fileFilter);
123: }
124: int answer = jf.showOpenDialog(SwingUtilities
125: .getWindowAncestor(this ));
126: if (answer == JFileChooser.APPROVE_OPTION) {
127: if (this .allowMultiple) {
128: this .selectedFiles = jf.getSelectedFiles();
129: } else {
130: this .selectedFiles = new File[1];
131: this .selectedFiles[0] = jf.getSelectedFile();
132: }
133:
134: StringBuilder path = new StringBuilder(
135: this .selectedFiles.length * 100);
136: for (int i = 0; i < this .selectedFiles.length; i++) {
137: if (this .selectedFiles.length > 1 && i > 0) {
138: path.append(StringUtil.getPathSeparator());
139: }
140: path.append(this .selectedFiles[i].getAbsolutePath()
141: .trim());
142: }
143: String newValue = path.toString();
144: String oldValue = tfFilename.getText();
145: this .tfFilename.setText(newValue);
146: if (this .lastDirProperty != null) {
147: Settings.getInstance().setProperty(lastDirProperty,
148: selectedFiles[0].getParent());
149: }
150: this .firePropertyChange("filename", oldValue, newValue);
151: }
152: } catch (Throwable e) {
153: LogMgr.logError(
154: "WbFilePicker.selectFileButtonActionPerformed()",
155: "Error selecting file", e);
156: WbSwingUtilities.showErrorMessage(ExceptionUtil
157: .getDisplay(e));
158: }
159: }//GEN-LAST:event_selectFileButtonActionPerformed
160:
161: public String getFilename() {
162: return tfFilename.getText();
163: }
164:
165: public void setFilename(String name) {
166: this .tfFilename.setText(name != null ? name : "");
167: this .tfFilename.setCaretPosition(0);
168: }
169:
170: public File getSelectedFile() {
171: if (this .selectedFiles == null) {
172: return null;
173: }
174: return this .selectedFiles[0];
175: }
176:
177: public File[] getSelectedFiles() {
178: if (!this .allowMultiple) {
179: return null;
180: }
181: return this .selectedFiles;
182: }
183:
184: public void setAllowMultiple(boolean flag) {
185: this .allowMultiple = flag;
186: }
187:
188: public void setFileFilter(FileFilter f) {
189: this .fileFilter = f;
190: }
191:
192: // Variables declaration - do not modify//GEN-BEGIN:variables
193: private javax.swing.JButton selectFileButton;
194: private javax.swing.JTextField tfFilename;
195: // End of variables declaration//GEN-END:variables
196:
197: }
|