001: package net.sourceforge.squirrel_sql.fw.gui;
002:
003: /*
004: * Copyright (C) 2001-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.Dimension;
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026: import java.io.File;
027: import java.io.FileReader;
028: import java.io.IOException;
029:
030: import javax.swing.ImageIcon;
031: import javax.swing.JComponent;
032: import javax.swing.JFileChooser;
033: import javax.swing.JLabel;
034: import javax.swing.JPanel;
035: import javax.swing.JScrollPane;
036: import javax.swing.JTextArea;
037:
038: import net.sourceforge.squirrel_sql.fw.util.StringManager;
039: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
040: import net.sourceforge.squirrel_sql.fw.util.Utilities;
041:
042: /**
043: * This is a decorator class that can be used in a <TT>JFileChooser</TT>
044: * to preview the contents of a text or image file. If the file name
045: * has a suffix of jpg, jpeg, gif or png this class will attempt to render
046: * it as an image, else it will render it as text.
047: *
048: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
049: */
050: public class ChooserPreviewer extends JComponent {
051: /** Internationalized strings for this class. */
052: private static final StringManager s_stringMgr = StringManagerFactory
053: .getStringManager(ChooserPreviewer.class);
054:
055: /** Default number of bytes to read from file for preview. */
056: private int DFT_BYTES_TO_READ = 2048;
057:
058: /**
059: * Empty panel. Used when cannot display anything in the
060: * preview panel.
061: */
062: private final JPanel _emptyPnl = new JPanel();
063:
064: /** Text area to display the contents of a text file in. */
065: private final JTextArea _textComponent = new JTextArea();
066:
067: /** Label to display the contents of an image file in. */
068: private final JLabel _imageComponent = new JLabel();
069:
070: /** Component currently being displayed in the preview area. */
071: private Component _currentComponent;
072:
073: /** Scrollpane for <TT>_currentComponent</TT>. */
074: private JScrollPane _currentComponentSp;
075:
076: /** <TT>JFileChooser</TT> that this accessory belongs to. */
077: private JFileChooser _chooser;
078:
079: /**
080: * Listener listening to <TT>JFileChooser</TT> for a change
081: * in the selected file.
082: */
083: private ChooserListener _propChangeListener;
084:
085: /**
086: * Default ctor.
087: */
088: public ChooserPreviewer() {
089: super ();
090: createUserInterface();
091: }
092:
093: /**
094: * This accessory is being added to a chooser. Add a listener
095: * to the chooser for when the selected file changes.
096: */
097: public void addNotify() {
098: super .addNotify();
099: cleanup();
100: Component parent = getParent();
101: while (parent != null) {
102: if (parent instanceof JFileChooser) {
103: _chooser = (JFileChooser) parent;
104: break;
105: }
106: parent = parent.getParent();
107: }
108:
109: if (_chooser != null) {
110: _propChangeListener = new ChooserListener();
111: _chooser.addPropertyChangeListener(_propChangeListener);
112: }
113: }
114:
115: /**
116: * This accessory is being removed from a chooser. Remove
117: * the listener from the chooser.
118: */
119: public void removeNotify() {
120: super .removeNotify();
121: cleanup();
122: }
123:
124: /**
125: * Remove listener from the chooser.
126: */
127: protected void cleanup() {
128: if (_chooser != null && _propChangeListener != null) {
129: _chooser.removePropertyChangeListener(_propChangeListener);
130: }
131: _propChangeListener = null;
132: _chooser = null;
133: }
134:
135: /**
136: * The file selected in the <TT>FileChooser</TT> has changed so display
137: * its contents in this previewer.
138: */
139: protected void fileChanged() {
140: Component componentToUse = _emptyPnl;
141:
142: File file = _chooser.getSelectedFile();
143: if (file != null && file.isFile() && file.canRead()) {
144: String suffix = Utilities.getFileNameSuffix(file.getPath())
145: .toLowerCase();
146: if (suffix.equals("gif") || suffix.equals("jpg")
147: || suffix.equals("jpeg") || suffix.equals("png")) {
148: componentToUse = readImageFile(file);
149: } else {
150: componentToUse = readTextFile(file);
151: }
152: }
153:
154: if (componentToUse != _currentComponent) {
155: _currentComponentSp.setViewportView(componentToUse);
156: _currentComponent = componentToUse;
157: }
158: }
159:
160: /**
161: * Read the image from the passed file and return it within
162: * a component.
163: *
164: * @param file The file to be read.
165: *
166: * @return The image component.
167: */
168: protected Component readImageFile(File file) {
169: final ImageIcon icon = new ImageIcon(file.getPath());
170: _imageComponent.setIcon(icon);
171: return _imageComponent;
172: }
173:
174: /**
175: * Read the first portion of the passed file
176: * and place them in the text component. If cannot
177: * read it then clear the text component.
178: *
179: * @param file The file to be read.
180: *
181: * @return The text component
182: */
183: protected Component readTextFile(File file) {
184: StringBuffer buf = new StringBuffer(DFT_BYTES_TO_READ);
185: if (file != null && file.isFile() && file.canRead()) {
186: try {
187: FileReader rdr = new FileReader(file);
188: try {
189: char[] data = new char[DFT_BYTES_TO_READ];
190: int count = rdr.read(data, 0, data.length);
191: if (count != -1) {
192: buf.append(data, 0, count);
193: }
194: } finally {
195: rdr.close();
196: }
197: } catch (IOException ex) {
198: buf = new StringBuffer(s_stringMgr.getString(
199: "ChooserPreviewer.error", ex.toString()));
200: }
201: }
202: _textComponent.setText(buf.toString());
203: _textComponent.setCaretPosition(0);
204: return _textComponent;
205: }
206:
207: /**
208: * Create the User Interface.
209: */
210: private void createUserInterface() {
211: _textComponent.setEditable(false);
212: setLayout(new BorderLayout());
213: _currentComponentSp = new JScrollPane(_textComponent);
214: add(_currentComponentSp, BorderLayout.CENTER);
215: setPreferredSize(new Dimension(250, 0));
216: }
217:
218: /**
219: * Listener to the FileChooser that will preview the seleted file
220: * as it changes in the chooser.
221: */
222: private class ChooserListener implements PropertyChangeListener {
223: public void propertyChange(PropertyChangeEvent evt) {
224: if (evt.getPropertyName().equals(
225: JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
226: ChooserPreviewer.this.fileChanged();
227: }
228: }
229: }
230: }
|