KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > swing > FileDisplay


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project:
4  * http://sourceforge.net/projects/drjava/ or http://www.drjava.org/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2003 JavaPLT group at Rice University (javaplt@rice.edu)
9  * All rights reserved.
10  *
11  * Developed by: Java Programming Languages Team
12  * Rice University
13  * http://www.cs.rice.edu/~javaplt/
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal with the Software without restriction, including without
18  * limitation the rights to use, copy, modify, merge, publish, distribute,
19  * sublicense, and/or sell copies of the Software, and to permit persons to
20  * whom the Software is furnished to do so, subject to the following
21  * conditions:
22  *
23  * - Redistributions of source code must retain the above copyright
24  * notice, this list of conditions and the following disclaimers.
25  * - Redistributions in binary form must reproduce the above copyright
26  * notice, this list of conditions and the following disclaimers in the
27  * documentation and/or other materials provided with the distribution.
28  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the
29  * names of its contributors may be used to endorse or promote products
30  * derived from this Software without specific prior written permission.
31  * - Products derived from this software may not be called "DrJava" nor
32  * use the term "DrJava" as part of their names without prior written
33  * permission from the JavaPLT group. For permission, write to
34  * javaplt@rice.edu.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
39  * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
40  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
41  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
42  * OTHER DEALINGS WITH THE SOFTWARE.
43  *
44  END_COPYRIGHT_BLOCK*/

45
46 package edu.rice.cs.util.swing;
47
48 import java.io.File JavaDoc;
49
50 /** This class is a wrapper for a file whose <code>toString</code> method outputs only the last element in the file
51   * path. If it's a file, then it outputs the file name without its parent directories. If it's a directory, then
52   * it outputs the name of that directory only
53   */

54 public class FileDisplay {
55   
56   private File JavaDoc _file;
57   private String JavaDoc _rep;
58   private boolean _repIsDifferent; // if the representation is different from the child string
59
private boolean _isNew;
60   
61   protected FileDisplayManager _fdm;
62   
63   FileDisplay(File JavaDoc f, FileDisplayManager fdm) {
64     this(fdm);
65     _file = f;
66     _rep = formatRep(f);
67   }
68   
69   FileDisplay(File JavaDoc parent, String JavaDoc child, FileDisplayManager fdm) {
70     this(fdm);
71     if (child == null || child.equals("")) {
72       _file = new File JavaDoc(parent, ".");
73     }
74     else {
75       _file = new File JavaDoc(parent, child);
76     }
77     _rep = formatRep(_file);
78   }
79   
80   private FileDisplay(FileDisplayManager fdm) {
81     _fdm = fdm;
82   }
83   
84   public static FileDisplay newFile(File JavaDoc parent, FileDisplayManager fdm) {
85     FileDisplay fd = new FileDisplay(parent, "", fdm);
86     fd._isNew = true;
87     fd._rep = getDefaultNewFileRep();
88     return fd;
89   }
90   
91   public File JavaDoc getParentFile() { return _file.getParentFile(); }
92   
93   public File JavaDoc getFile() { return _file; }
94   
95   /**
96    * If the representation of the file is different from the underlying
97    * child string of the path, then the node represented by this file display
98    * cannot be edited. If the user edited the text by giving a new representation,
99    * there is no way to determine what the new child string of the path should be.
100    * However, if the user is creating a new node in the tree, they will be able
101    * to edit it.
102    */

103   public boolean isEditable() { return (_isNew || (_file.canWrite() && _rep.equals(_file.getName()))); }
104   
105   public boolean isNew() { return _isNew; }
106   
107   public String JavaDoc getRepresentation() { return _rep; }
108   
109   public final String JavaDoc toString() { return _rep; }
110   
111   protected String JavaDoc formatRep(File JavaDoc file) {
112     return _fdm.getName(file);
113   }
114     
115   protected static String JavaDoc getDefaultNewFileRep() {
116     return "New Folder";
117   }
118   
119   
120 }
Popular Tags