View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.examples.tutorial.model;
17  
18  import java.io.File;
19  import java.util.Date;
20  
21  /**
22   * <p>
23   * A simple data class representing a file (or sub directory) in a directory.
24   * </p>
25   * <p>
26   * Objects of this type are used by the model of the table control. Each
27   * instance represents one row in the table. An instance wraps a {@code File}
28   * object and provides bean-style get methods for it. (The API of {@code File}
29   * does not always conform to the Java Beans standard.)
30   * </p>
31   *
32   * @author Oliver Heger
33   * @version $Id: FileData.java 205 2012-01-29 18:29:57Z oheger $
34   */
35  public class FileData implements Comparable<FileData>
36  {
37      /** Stores the wrapped file. */
38      private final File file;
39  
40      /** The icon to be displayed. */
41      private final Object icon;
42  
43      /** The file name. */
44      private final String name;
45  
46      /**
47       * Creates a new instance of {@code FileData} for the specified {@code File}
48       * object.
49       *
50       * @param f the {@code File}
51       * @param icon the icon for this {@code File}
52       */
53      public FileData(File f, Object icon)
54      {
55          file = f;
56          this.icon = icon;
57          name = f.getName();
58      }
59  
60      /**
61       * Creates a new instance of {@code FileData} that does not represent a
62       * file. Objects of this type are used as placeholders if no real data is
63       * available, e.g. if data has to be loaded first.
64       *
65       * @param name the name to be displayed
66       * @param icon an icon
67       */
68      public FileData(String name, Object icon)
69      {
70          file = null;
71          this.icon = icon;
72          this.name = name;
73      }
74  
75      /**
76       * Returns the associated {@code File} object. If this object represents a
77       * special entry which is not associated with a file, result is <b>null</b>.
78       *
79       * @return the associated {@code File} object
80       */
81      public File getFile()
82      {
83          return file;
84      }
85  
86      /**
87       * Returns the icon of this file.
88       *
89       * @return the icon
90       */
91      public Object getIcon()
92      {
93          return icon;
94      }
95  
96      /**
97       * Returns the name of this file.
98       *
99       * @return the file name
100      */
101     public String getName()
102     {
103         return name;
104     }
105 
106     /**
107      * Returns the date this file was modified.
108      *
109      * @return the last modification date (can be <b>null</b>)
110      */
111     public Date getLastModified()
112     {
113         return (file != null) ? new Date(file.lastModified()) : null;
114     }
115 
116     /**
117      * Returns the size of this file.
118      *
119      * @return the size
120      */
121     public long getSize()
122     {
123         return (file == null || !file.isFile()) ? 0L : file.length();
124     }
125 
126     /**
127      * Compares this object with another one. Two instances of {@code FileData}
128      * are equal if they refer to the same file.
129      *
130      * @param obj the object to compare to
131      * @return a flag whether the objects are equal
132      */
133     @Override
134     public boolean equals(Object obj)
135     {
136         if (this == obj)
137         {
138             return true;
139         }
140         if (!(obj instanceof FileData))
141         {
142             return false;
143         }
144 
145         FileData c = (FileData) obj;
146         return ((file == null) ? c.file == null : file.equals(c.file));
147     }
148 
149     /**
150      * Returns a hash code for this object.
151      *
152      * @return a hash code
153      */
154     @Override
155     public int hashCode()
156     {
157         return (file == null) ? 17 : file.hashCode();
158     }
159 
160     /**
161      * Returns a string representation for this object. This string contains the
162      * name and the file stored in this instance.
163      *
164      * @return a string for this object
165      */
166     @Override
167     public String toString()
168     {
169         StringBuilder buf = new StringBuilder();
170         buf.append("FileData [ name = ").append(name);
171         buf.append(", file = ").append(file);
172         buf.append(" ]");
173         return buf.toString();
174     }
175 
176     /**
177      * A special {@code compareTo()} implementation. Files are sorted
178      * alphabetically by their names. Directories are sorted before plain files.
179      *
180      * @param o the other {@code FileData} object
181      * @return a value indicating the order of these objects
182      */
183     @Override
184     public int compareTo(FileData o)
185     {
186         if (file == null)
187         {
188             return (o.file == null) ? 0 : -1;
189         }
190         if (o.file == null)
191         {
192             return 1;
193         }
194 
195         boolean dir1 = file.isDirectory();
196         boolean dir2 = o.file.isDirectory();
197         if (dir1 != dir2)
198         {
199             return dir1 ? -1 : 1;
200         }
201         return getName().compareTo(o.getName());
202     }
203 }