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: package edu.rice.cs.util.swing;
037:
038: import java.io.File;
039:
040: /** This class is a wrapper for a file whose <code>toString</code> method outputs only the last element in the file
041: * path. If it's a file, then it outputs the file name without its parent directories. If it's a directory, then
042: * it outputs the name of that directory only
043: */
044: public class FileDisplay {
045:
046: private File _file;
047: private String _rep;
048: private boolean _repIsDifferent; // if the representation is different from the child string
049: private boolean _isNew;
050:
051: protected FileDisplayManager _fdm;
052:
053: FileDisplay(File f, FileDisplayManager fdm) {
054: this (fdm);
055: _file = f;
056: _rep = formatRep(f);
057: }
058:
059: FileDisplay(File parent, String child, FileDisplayManager fdm) {
060: this (fdm);
061: if (child == null || child.equals("")) {
062: _file = new File(parent, ".");
063: } else {
064: _file = new File(parent, child);
065: }
066: _rep = formatRep(_file);
067: }
068:
069: private FileDisplay(FileDisplayManager fdm) {
070: _fdm = fdm;
071: }
072:
073: public static FileDisplay newFile(File parent,
074: FileDisplayManager fdm) {
075: FileDisplay fd = new FileDisplay(parent, "", fdm);
076: fd._isNew = true;
077: fd._rep = getDefaultNewFileRep();
078: return fd;
079: }
080:
081: public File getParentFile() {
082: return _file.getParentFile();
083: }
084:
085: public File getFile() {
086: return _file;
087: }
088:
089: /**
090: * If the representation of the file is different from the underlying
091: * child string of the path, then the node represented by this file display
092: * cannot be edited. If the user edited the text by giving a new representation,
093: * there is no way to determine what the new child string of the path should be.
094: * However, if the user is creating a new node in the tree, they will be able
095: * to edit it.
096: */
097: public boolean isEditable() {
098: return (_isNew || (_file.canWrite() && _rep.equals(_file
099: .getName())));
100: }
101:
102: public boolean isNew() {
103: return _isNew;
104: }
105:
106: public String getRepresentation() {
107: return _rep;
108: }
109:
110: public final String toString() {
111: return _rep;
112: }
113:
114: protected String formatRep(File file) {
115: return _fdm.getName(file);
116: }
117:
118: protected static String getDefaultNewFileRep() {
119: return "New Folder";
120: }
121:
122: }
|