001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.mlm.debug.ui;
028:
029: import java.io.File;
030: import java.io.FileReader;
031: import java.util.Enumeration;
032: import java.util.Hashtable;
033:
034: import javax.swing.JFileChooser;
035: import javax.swing.JFrame;
036: import javax.swing.JTextArea;
037: import javax.swing.UIManager;
038: import javax.swing.filechooser.FileFilter;
039:
040: public class UIFile implements Runnable {
041:
042: class ExtensionFileFilter extends FileFilter {
043:
044: //private static String TYPE_UNKNOWN = "Type Unknown";
045: //private static String HIDDEN_FILE = "Hidden File";
046:
047: private Hashtable filters = null;
048: private String description = null;
049: private String fullDescription = null;
050: private boolean useExtensionsInDescription = true;
051:
052: /**
053: * Creates a file filter that accepts files with the given extension.
054: * Example: new ExampleFileFilter("jpg");
055: *
056: * @see #addExtension
057: */
058: public ExtensionFileFilter(String extension) {
059: this (extension, null);
060: }
061:
062: public ExtensionFileFilter(String extension, String description) {
063: this (new String[] { extension }, description);
064: }
065:
066: public ExtensionFileFilter(String[] filters, String description) {
067: this .filters = new Hashtable(filters.length);
068: for (int i = 0; i < filters.length; i++) {
069: // add filters one by one
070: addExtension(filters[i]);
071: }
072: setDescription(description);
073: }
074:
075: /**
076: * Return true if this file should be shown in the directory pane,
077: * false if it shouldn't.
078: *
079: * Files that begin with "." are ignored.
080: *
081: * @see #getExtension
082: * @see FileFilter#accepts
083: */
084: public boolean accept(File f) {
085: if (f != null) {
086: if (f.isDirectory()) {
087: return true;
088: }
089: String extension = getExtension(f);
090: if (extension != null
091: && filters.get(getExtension(f)) != null) {
092: return true;
093: }
094: ;
095: }
096: return false;
097: }
098:
099: /**
100: * Return the extension portion of the file's name .
101: *
102: * @see #getExtension
103: * @see FileFilter#accept
104: */
105: public String getExtension(File f) {
106: if (f != null) {
107: String filename = f.getName();
108: int i = filename.lastIndexOf('.');
109: if (i > 0 && i < filename.length() - 1) {
110: return filename.substring(i + 1).toLowerCase();
111: }
112: ;
113: }
114: return null;
115: }
116:
117: /**
118: * Adds a filetype "dot" extension to filter against.
119: *
120: * For example: the following code will create a filter that filters
121: * out all files except those that end in ".jpg" and ".tif":
122: *
123: * ExampleFileFilter filter = new ExampleFileFilter();
124: * filter.addExtension("jpg");
125: * filter.addExtension("tif");
126: *
127: * Note that the "." before the extension is not needed and will be ignored.
128: */
129: public void addExtension(String extension) {
130: if (filters == null) {
131: filters = new Hashtable(5);
132: }
133: filters.put(extension.toLowerCase(), this );
134: fullDescription = null;
135: }
136:
137: /**
138: * Returns the human readable description of this filter. For
139: * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
140: *
141: * @see setDescription
142: * @see setExtensionListInDescription
143: * @see isExtensionListInDescription
144: * @see FileFilter#getDescription
145: */
146: public String getDescription() {
147: if (fullDescription == null) {
148: if (description == null
149: || isExtensionListInDescription()) {
150: if (description != null) {
151: fullDescription = description;
152: }
153: fullDescription += " (";
154: // build the description from the extension list
155: Enumeration extensions = filters.keys();
156: if (extensions != null) {
157: fullDescription += "."
158: + (String) extensions.nextElement();
159: while (extensions.hasMoreElements()) {
160: fullDescription += ", "
161: + (String) extensions.nextElement();
162: }
163: }
164: fullDescription += ")";
165: } else {
166: fullDescription = description;
167: }
168: }
169: return fullDescription;
170: }
171:
172: /**
173: * Sets the human readable description of this filter. For
174: * example: filter.setDescription("Gif and JPG Images");
175: *
176: * @see setDescription
177: * @see setExtensionListInDescription
178: * @see isExtensionListInDescription
179: */
180: public void setDescription(String description) {
181: this .description = description;
182: fullDescription = null;
183: }
184:
185: /**
186: * Determines whether the extension list (.jpg, .gif, etc) should
187: * show up in the human readable description.
188: *
189: * Only relevent if a description was provided in the constructor
190: * or using setDescription();
191: *
192: * @see getDescription
193: * @see setDescription
194: * @see isExtensionListInDescription
195: */
196: public void setExtensionListInDescription(boolean b) {
197: useExtensionsInDescription = b;
198: fullDescription = null;
199: }
200:
201: /**
202: * Returns whether the extension list (.jpg, .gif, etc) should
203: * show up in the human readable description.
204: *
205: * Only relevent if a description was provided in the constructor
206: * or using setDescription();
207: *
208: * @see getDescription
209: * @see setDescription
210: * @see setExtensionListInDescription
211: */
212: public boolean isExtensionListInDescription() {
213: return useExtensionsInDescription;
214: }
215: }
216:
217: /** Pop-up dialog asking user for filename and display the file.
218: If user cancels dialog or specifies directory instead of file, do nothing.
219: */
220:
221: public void run() {
222: // set to windows look and feel before displaying file dialog
223: try {
224: UIManager.setLookAndFeel(UIManager
225: .getSystemLookAndFeelClassName());
226: } catch (Exception exc) {
227: System.err.println("Error loading L&F: " + exc);
228: }
229: String filename = "";
230: JFileChooser chooser = new JFileChooser(".");
231: ExtensionFileFilter filter = new ExtensionFileFilter("out",
232: "FGI Output Files");
233: //filter.addExtension( "out" );
234: //filter.setDescription( "FGI Output Files" );
235: chooser.setFileFilter(filter);
236: int retval = chooser.showOpenDialog(new JFrame());
237: if (retval == JFileChooser.APPROVE_OPTION) {
238: File theFile = chooser.getSelectedFile();
239: if (theFile != null) {
240: if (!theFile.isDirectory()) {
241: filename = chooser.getSelectedFile()
242: .getAbsolutePath();
243: JTextArea textArea = new JTextArea(
244: loadFile(filename));
245: UIFrame uiFrame = new UIFrame("Output File",
246: textArea, null, false, false, false);
247: }
248: }
249: }
250: }
251:
252: /** Read the file.
253: */
254:
255: public static String loadFile(String filename) {
256: String s = "";
257: File f;
258: char[] buff = new char[50000];
259: FileReader reader;
260:
261: try {
262: f = new File(filename);
263: reader = new FileReader(f);
264: int nch;
265: while ((nch = reader.read(buff, 0, buff.length)) != -1) {
266: s = s + new String(buff, 0, nch);
267: }
268: } catch (java.io.IOException ex) {
269: System.out.println("Could not load file: " + filename + " "
270: + ex.toString());
271: }
272: return s;
273: }
274:
275: }
|