001: /**
002: * L2FProd.com Common Components 7.3 License.
003: *
004: * Copyright 2005-2007 L2FProd.com
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package com.l2fprod.common.beans.editor;
018:
019: import com.l2fprod.common.swing.ComponentFactory;
020: import com.l2fprod.common.swing.LookAndFeelTweaks;
021: import com.l2fprod.common.swing.PercentLayout;
022: import com.l2fprod.common.swing.UserPreferences;
023: import com.l2fprod.common.util.ResourceManager;
024:
025: import java.awt.datatransfer.DataFlavor;
026: import java.awt.datatransfer.Transferable;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.io.File;
030: import java.util.List;
031:
032: import javax.swing.JButton;
033: import javax.swing.JComponent;
034: import javax.swing.JFileChooser;
035: import javax.swing.JPanel;
036: import javax.swing.JTextField;
037: import javax.swing.TransferHandler;
038:
039: /**
040: * FilePropertyEditor. <br>
041: *
042: */
043: public class FilePropertyEditor extends AbstractPropertyEditor {
044:
045: protected JTextField textfield;
046: private JButton button;
047: private JButton cancelButton;
048:
049: public FilePropertyEditor() {
050: this (true);
051: }
052:
053: public FilePropertyEditor(boolean asTableEditor) {
054: editor = new JPanel(new PercentLayout(PercentLayout.HORIZONTAL,
055: 0)) {
056: public void setEnabled(boolean enabled) {
057: super .setEnabled(enabled);
058: textfield.setEnabled(enabled);
059: button.setEnabled(enabled);
060: cancelButton.setEnabled(enabled);
061: }
062: };
063: ((JPanel) editor).add("*", textfield = new JTextField());
064: ((JPanel) editor).add(button = ComponentFactory.Helper
065: .getFactory().createMiniButton());
066: if (asTableEditor) {
067: textfield.setBorder(LookAndFeelTweaks.EMPTY_BORDER);
068: }
069: button.addActionListener(new ActionListener() {
070: public void actionPerformed(ActionEvent e) {
071: selectFile();
072: }
073: });
074: ((JPanel) editor).add(cancelButton = ComponentFactory.Helper
075: .getFactory().createMiniButton());
076: cancelButton.setText("X");
077: cancelButton.addActionListener(new ActionListener() {
078: public void actionPerformed(ActionEvent e) {
079: selectNull();
080: }
081: });
082: textfield.setTransferHandler(new FileTransferHandler());
083: }
084:
085: class FileTransferHandler extends TransferHandler {
086: public boolean canImport(JComponent comp,
087: DataFlavor[] transferFlavors) {
088: for (int i = 0, c = transferFlavors.length; i < c; i++) {
089: if (transferFlavors[i]
090: .equals(DataFlavor.javaFileListFlavor)) {
091: return true;
092: }
093: }
094: return false;
095: }
096:
097: public boolean importData(JComponent comp, Transferable t) {
098: try {
099: List list = (List) t
100: .getTransferData(DataFlavor.javaFileListFlavor);
101: if (list.size() > 0) {
102: File oldFile = (File) getValue();
103: File newFile = (File) list.get(0);
104: String text = newFile.getAbsolutePath();
105: textfield.setText(text);
106: firePropertyChange(oldFile, newFile);
107: }
108: } catch (Exception e) {
109: e.printStackTrace();
110: }
111: return true;
112: }
113: }
114:
115: public Object getValue() {
116: if ("".equals(textfield.getText().trim())) {
117: return null;
118: } else {
119: return new File(textfield.getText());
120: }
121: }
122:
123: public void setValue(Object value) {
124: if (value instanceof File) {
125: textfield.setText(((File) value).getAbsolutePath());
126: } else {
127: textfield.setText("");
128: }
129: }
130:
131: protected void selectFile() {
132: ResourceManager rm = ResourceManager
133: .all(FilePropertyEditor.class);
134:
135: JFileChooser chooser = UserPreferences.getDefaultFileChooser();
136: chooser.setDialogTitle(rm
137: .getString("FilePropertyEditor.dialogTitle"));
138: chooser.setApproveButtonText(rm
139: .getString("FilePropertyEditor.approveButtonText"));
140: chooser.setApproveButtonMnemonic(rm
141: .getChar("FilePropertyEditor.approveButtonMnemonic"));
142: customizeFileChooser(chooser);
143:
144: if (JFileChooser.APPROVE_OPTION == chooser
145: .showOpenDialog(editor)) {
146: File oldFile = (File) getValue();
147: File newFile = chooser.getSelectedFile();
148: String text = newFile.getAbsolutePath();
149: textfield.setText(text);
150: firePropertyChange(oldFile, newFile);
151: }
152: }
153:
154: /**
155: * Placeholder for subclasses to customize the JFileChooser shown to select a
156: * file.
157: *
158: * @param chooser
159: */
160: protected void customizeFileChooser(JFileChooser chooser) {
161: }
162:
163: protected void selectNull() {
164: Object oldFile = getValue();
165: textfield.setText("");
166: firePropertyChange(oldFile, null);
167: }
168:
169: }
|