Source Code Cross Referenced for FileSelectorStringComponent.java in  » IDE » DrJava » edu » rice » cs » util » swing » 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 » IDE » DrJava » edu.rice.cs.util.swing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*BEGIN_COPYRIGHT_BLOCK
002:         *
003:         * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004:         * All rights reserved.
005:         * 
006:         * Redistribution and use in source and binary forms, with or without
007:         * modification, are permitted provided that the following conditions are met:
008:         *    * Redistributions of source code must retain the above copyright
009:         *      notice, this list of conditions and the following disclaimer.
010:         *    * Redistributions in binary form must reproduce the above copyright
011:         *      notice, this list of conditions and the following disclaimer in the
012:         *      documentation and/or other materials provided with the distribution.
013:         *    * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014:         *      names of its contributors may be used to endorse or promote products
015:         *      derived from this software without specific prior written permission.
016:         * 
017:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018:         * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019:         * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020:         * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021:         * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024:         * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025:         * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026:         * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027:         * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028:         *
029:         * This software is Open Source Initiative approved Open Source Software.
030:         * Open Source Initative Approved is a trademark of the Open Source Initiative.
031:         * 
032:         * This file is part of DrJava.  Download the current version of this project
033:         * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034:         * 
035:         * END_COPYRIGHT_BLOCK*/
036:
037:        package edu.rice.cs.util.swing;
038:
039:        import javax.swing.*;
040:        import javax.swing.filechooser.FileFilter;
041:        import java.awt.*;
042:        import java.awt.event.ActionEvent;
043:        import java.awt.event.ActionListener;
044:        import java.awt.event.FocusListener;
045:        import java.awt.event.FocusEvent;
046:        import java.io.*;
047:
048:        /** Just like FileSelectorComponent, but it converts the file to a different string that gets displayed. */
049:        public class FileSelectorStringComponent extends JPanel {
050:
051:            /** The default number of columns for the text box. */
052:            public static final int DEFAULT_NUM_COLS = 30;
053:
054:            /** The default font size for the text box. */
055:            public static final float DEFAULT_FONT_SIZE = 10f;
056:
057:            /** The parent component of this component. */
058:            protected final Component _parent;
059:
060:            /** Text field with the name of the selected file. */
061:            protected final JTextField _textField;
062:
063:            /**  "..." button to open the file chooser. */
064:            protected final JButton _chooserButton;
065:
066:            /** File chooser to open when clicking the "..." button. */
067:            protected final FileChooser _chooser;
068:
069:            /** The current file */
070:            protected volatile File _file;
071:
072:            /** Creates a new DirectorySelectorStringComponent with default dimensions.
073:             *  @param parent  Parent of this component.
074:             *  @param chooser File chooser to display from the "..." button.  Assumed non-null!
075:             */
076:            public FileSelectorStringComponent(Component parent,
077:                    FileChooser chooser) {
078:                this (parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE);
079:            }
080:
081:            /** Creates a new DirectorySelectorStringComponent.
082:             *  @param parent   Parent of this component.  
083:             *  @param chooser  File chooser to display from the "..." button.  Assumed non-null!
084:             *  @param numCols  Number of columns to display in the text field
085:             *  @param fontSize Font size for the text field
086:             */
087:            public FileSelectorStringComponent(Component parent,
088:                    FileChooser chooser, int numCols, float fontSize) {
089:                _parent = parent;
090:                _chooser = chooser;
091:                _file = null;
092:
093:                _textField = new JTextField(numCols) {
094:                    public Dimension getMaximumSize() {
095:                        return new Dimension(Short.MAX_VALUE, super 
096:                                .getPreferredSize().height);
097:                    }
098:                };
099:                _textField.setFont(_textField.getFont().deriveFont(fontSize));
100:                _textField.setPreferredSize(new Dimension(22, 22));
101:
102:                _chooserButton = new JButton("...");
103:                _chooserButton.addActionListener(new ActionListener() {
104:                    public void actionPerformed(ActionEvent e) {
105:                        _chooseFile();
106:                    }
107:                });
108:                _chooserButton.setMaximumSize(new Dimension(22, 22));
109:                _chooserButton.setMargin(new Insets(0, 5, 0, 5));
110:                // Add components
111:                this .setLayout(new BoxLayout(this , BoxLayout.X_AXIS));
112:                this .add(_textField);
113:                this .add(_chooserButton);
114:            }
115:
116:            public void setEnabled(boolean isEnabled) {
117:                _textField.setEnabled(isEnabled);
118:                _chooserButton.setEnabled(isEnabled);
119:                super .setEnabled(isEnabled);
120:            }
121:
122:            /** Returns the file text field. */
123:            public JTextField getTextField() {
124:                return _textField;
125:            }
126:
127:            /** Returns the file chooser. */
128:            public FileChooser getFileChooser() {
129:                return _chooser;
130:            }
131:
132:            /** Converts a string representation from the text field into a File. */
133:            public File convertStringToFile(String s) {
134:                s = s.trim();
135:                if (s.equals(""))
136:                    return null;
137:                return new File(s);
138:            }
139:
140:            /** Converts a file to the string representation of the text field. */
141:            public String convertFileToString(File f) {
142:                if (f == null)
143:                    return "";
144:                return f.toString();
145:            }
146:
147:            /** Returns the last file that was selected. */
148:            public File getFileFromField() {
149:                // Get the file from the chooser
150:                String newValue = _textField.getText();
151:                File newFile = null;
152:                if (!newValue.equals("")) {
153:                    newFile = convertStringToFile(newValue);
154:                    if (!newFile.isDirectory()
155:                            && !_chooser.isFileSelectionEnabled())
156:                        newFile = newFile.getParentFile();
157:                }
158:
159:                if (newFile != null && !newFile.exists())
160:                    newFile = _file;
161:
162:                return newFile;
163:            }
164:
165:            /** Returns the string in the text field. */
166:            public String getText() {
167:                return _textField.getText();
168:            }
169:
170:            /** Sets the string in the text field. */
171:            public void setText(String s) {
172:                _textField.setText(s);
173:            }
174:
175:            /** Sets the text of the file field to be the given file.
176:             *  @param file File to display in the file field.
177:             */
178:            public void setFileField(File file) {
179:                _file = file;
180:                if (file != null && !file.getPath().equals("")) {
181:                    try {
182:                        _file = file.getCanonicalFile();
183:                    } catch (IOException e) { /* do nothing */
184:                    }
185:                }
186:                resetFileField();
187:            }
188:
189:            public void resetFileField() {
190:                _textField.setText(convertFileToString(_file));
191:                _textField.setCaretPosition(_textField.getText().length());
192:            }
193:
194:            public void setToolTipText(String text) {
195:                super .setToolTipText(text);
196:                _textField.setToolTipText(text);
197:                _chooserButton.setToolTipText(text);
198:            }
199:
200:            /** Adds a filter to decide if a directory can be chosen. */
201:            public void addChoosableFileFilter(FileFilter filter) {
202:                _chooser.addChoosableFileFilter(filter);
203:            }
204:
205:            /** Removes the given filefilter from the chooser */
206:            public void removeChoosableFileFilter(FileFilter filter) {
207:                _chooser.removeChoosableFileFilter(filter);
208:            }
209:
210:            public void clearChoosableFileFilters() {
211:                _chooser.resetChoosableFileFilters();
212:            }
213:
214:            /** Opens the file chooser to select a file, putting the result in the file field. */
215:            private void _chooseFile() {
216:                File f = getFileFromField();
217:                if (f != null && f.exists()) {
218:                    _chooser.setCurrentDirectory(f);
219:                    _chooser.setSelectedFile(f);
220:                }
221:                int returnValue = _chooser.showDialog(_parent, null);
222:                if (returnValue == FileChooser.APPROVE_OPTION) {
223:                    File chosen = _chooser.getSelectedFile();
224:                    if (chosen != null) {
225:                        setFileField(chosen);
226:                    }
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.