KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > naming > FileName


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs.filebasedfs.naming;
21
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import org.netbeans.modules.masterfs.providers.ProvidedExtensions;
26 import org.openide.util.Exceptions;
27
28 /**
29  * @author Radek Matous
30  */

31 public class FileName implements FileNaming {
32     private String JavaDoc name;
33     private final FileNaming parent;
34     private Integer JavaDoc id;
35
36     protected FileName(final FileNaming parent, final File JavaDoc file) {
37         this.parent = parent;
38         this.name = parseName(file);
39         id = NamingFactory.createID(file);
40     }
41
42     private static String JavaDoc parseName(final File JavaDoc file) {
43         return (file.getParentFile() == null) ? file.getPath() : file.getName();
44     }
45
46     public boolean rename(String JavaDoc name, ProvidedExtensions.IOHandler handler) {
47         boolean retVal = false;
48         final File JavaDoc f = getFile();
49
50         if (f.exists()) {
51             File JavaDoc newFile = new File JavaDoc(f.getParentFile(), name);
52             if (handler != null) {
53                 try {
54                     handler.handle();
55                     retVal = true;
56                 } catch (IOException JavaDoc ex) {
57                     Exceptions.printStackTrace(ex);
58                 }
59             } else {
60                 retVal = f.renameTo(newFile);
61             }
62             if (retVal) {
63                 this.name = name;
64                 Integer JavaDoc iid = NamingFactory.createID(newFile);
65                 if (!iid.equals(id)) {
66                     id = iid;
67                 }
68             }
69         }
70         FolderName.freeCaches();
71         return retVal;
72     }
73
74     public final boolean rename(final String JavaDoc name) {
75         return rename(name, null);
76     }
77
78     public final boolean isRoot() {
79         return (getParent() == null);
80     }
81
82
83     public File JavaDoc getFile() {
84         final FileNaming parent = this.getParent();
85         return (parent != null) ? new File JavaDoc(parent.getFile(), getName()) : new File JavaDoc(getName());
86     }
87
88
89     public final String JavaDoc getName() {
90         return name;
91     }
92
93     public FileNaming getParent() {
94         return parent;
95     }
96
97     public final Integer JavaDoc getId() {
98         return getId(false);
99     }
100
101     public Integer JavaDoc getId(boolean recompute) {
102         if (recompute) {
103             id = NamingFactory.createID(getFile());
104         }
105         return id;
106     }
107
108     public final boolean equals(final Object JavaDoc obj) {
109         return (obj instanceof FileNaming && obj.hashCode() == hashCode());
110     }
111
112
113     public final String JavaDoc toString() {
114         return getFile().getAbsolutePath();
115     }
116
117     public final int hashCode() {
118         return id.intValue();
119     }
120
121     public boolean isFile() {
122         return true;
123     }
124
125     public boolean isDirectory() {
126         return !isFile();
127     }
128 }
129
Popular Tags