Source Code Cross Referenced for ChooserPreviewer.java in  » Database-Client » squirrel-sql-2.6.5a » net » sourceforge » squirrel_sql » fw » gui » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » squirrel sql 2.6.5a » net.sourceforge.squirrel_sql.fw.gui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.