View Javadoc
1   //
2   // $Id$
3   //
4   // jupload - A file upload applet.
5   //
6   // Copyright 2015 The JUpload Team
7   //
8   // Created: 6 févr. 2015
9   // Creator: etienne_sf
10  // Last modified: $Date$
11  //
12  // This program is free software; you can redistribute it and/or modify
13  // it under the terms of the GNU General Public License as published by
14  // the Free Software Foundation; either version 2 of the License, or
15  // (at your option) any later version.
16  //
17  // This program is distributed in the hope that it will be useful,
18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  // GNU General Public License for more details.
21  //
22  // You should have received a copy of the GNU General Public License
23  // along with this program; if not, write to the Free Software
24  // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  
26  package wjhk.jupload2.gui.filepanel.treeview;
27  
28  import java.io.File;
29  import java.io.InputStream;
30  import java.util.ArrayList;
31  import java.util.Date;
32  import java.util.List;
33  
34  import javax.swing.tree.TreeModel;
35  import javax.swing.tree.TreePath;
36  
37  import wjhk.jupload2.exception.JUploadException;
38  import wjhk.jupload2.exception.JUploadIOException;
39  import wjhk.jupload2.filedata.FileData;
40  import wjhk.jupload2.gui.filepanel.FilePanelFlatDataModel2;
41  import wjhk.jupload2.upload.helper.ByteArrayEncoder;
42  
43  /**
44   * @author etienne_sf
45   */
46  public class FileDataNode implements TreeFileDataNode {
47  
48      /** The mode which manages this node. Used to fire events, when nodes are changed */
49      MyTreeTableModel<TreeFileDataNode> treeModel = null;
50  
51      /**
52       * The flat list file is still responsible to manage the final list of files to download. So each
53       * {@link FileDataNode} creation (as a child of FolderNode) must trigger adding a row into the flat list. This is
54       * done through its TableModel, this attribute.
55       * 
56       * @see #addChild(File)
57       */
58      FilePanelFlatDataModel2 flatModel = null;
59  
60      /** The {@link FileData} instance to which all calls to the {@link FileData} interface are delegated. */
61      FileData fileData = null;
62  
63      /**
64       * Contains the parent of this node, in the hierarchy. May be null, if this node has not been attached to a hierachy
65       * yet.
66       */
67      TreeFileDataNode parent = null;
68  
69      /**
70       * @param fileData The {@link FileData} instance to which all calls to the {@link FileData} interface are delegated
71       */
72      public FileDataNode(FileData fileData) {
73          this.fileData = fileData;
74          fileData.setTreeFileDataNode(this);
75      }
76  
77      /** @see TreeFileDataNode#getTotalChildCount() */
78      public int getTotalChildCount() {
79          return 0;
80      }
81  
82      public List<MyTreeNode> getChildren() {
83          return new ArrayList<MyTreeNode>(0);
84      }
85  
86      public void appendFileProperties(ByteArrayEncoder bae, int index) throws JUploadIOException {
87          fileData.appendFileProperties(bae, index);
88      }
89  
90      public void beforeUpload(String uploadFileRoot) throws JUploadException {
91          fileData.beforeUpload(uploadFileRoot);
92      }
93  
94      public long getUploadLength() {
95          return fileData.getUploadLength();
96      }
97  
98      public void afterUpload() {
99          fileData.afterUpload();
100     }
101 
102     public InputStream getInputStream() throws JUploadException {
103         return fileData.getInputStream();
104     }
105 
106     public String getFileName() {
107         return fileData.getFileName();
108     }
109 
110     public String getFileExtension() {
111         return fileData.getFileExtension();
112     }
113 
114     public long getFileLength() {
115         return fileData.getFileLength();
116     }
117 
118     public Date getLastModified() {
119         return fileData.getLastModified();
120     }
121 
122     public boolean getUploadFlag() {
123         return fileData.getUploadFlag();
124     }
125 
126     public void setUploadFlag(boolean uploadFlag) {
127         if (fileData.getUploadFlag() != uploadFlag) {
128             // Let's update the FileData
129             fileData.setUploadFlag(uploadFlag);
130             if (treeModel != null) {
131                 treeModel.fireTreeNodesChanged(this, treeModel.getTreePath((TreeFileDataNode) getParent()),
132                         FolderNode.getIntArray((getParent().getChildren()).indexOf(this)),
133                         FolderNode.getItemArray((TreeFileDataNode) this));
134             }
135         }
136     }
137 
138     public String getDirectory() {
139         return fileData.getDirectory();
140     }
141 
142     public String getMD5() throws JUploadException {
143         return fileData.getMD5();
144     }
145 
146     public String getMimeType() {
147         return fileData.getMimeType();
148     }
149 
150     public boolean canRead() {
151         return fileData.canRead();
152     }
153 
154     public File getFile() {
155         throw new IllegalAccessError("Internal error: getFile is deprecated and should not be called from "
156                 + this.getClass().getName());
157     }
158 
159     public String getRelativeDir() {
160         return fileData.getRelativeDir();
161     }
162 
163     public String getAbsolutePath() {
164         return FolderNode.getAbsolutePath(this);
165     }
166 
167     public boolean isPreparedForUpload() {
168         return fileData.isPreparedForUpload();
169     }
170 
171     public int getChildCount() {
172         return 0;
173     }
174 
175     public MyTreeNode getChild(int index) {
176         return null;
177     }
178 
179     public MyTreeNode getChild(String name) {
180         // There are no Children, here...
181         return null;
182     }
183 
184     public MyTreeNode getParent() {
185         return this.parent;
186     }
187 
188     /** @see MyTreeNode#setParent(MyTreeNode) */
189     public void setParent(MyTreeNode parent) {
190         this.parent = (TreeFileDataNode) parent;
191     }
192 
193     /** @see MyTreeNode#setTreeModel(TreeModel) */
194     @SuppressWarnings("unchecked")
195     public void setTreeModel(TreeModel model) {
196         this.treeModel = (MyTreeTableModel<TreeFileDataNode>) model;
197     }
198 
199     /** {@inheritDoc} */
200     public void setFlatModel(FilePanelFlatDataModel2 flatModel) {
201         this.flatModel = flatModel;
202     }
203 
204     public void removeChild(MyTreeNode child) {
205         throw new IllegalArgumentException(this.getClass().getName() + " can't have children");
206     }
207 
208     public boolean isLeaf() {
209         return true;
210     }
211 
212     /**
213      * Important: this method is called by the JTree class, as the 'main' name for the node.
214      * 
215      * @see java.lang.Object#toString()
216      */
217     @Override
218     public String toString() {
219         return getFileName();
220     }
221 
222     public TreeFileDataNode getTreeFileDataNode() {
223         return this;
224     }
225 
226     public void setTreeFileDataNode(TreeFileDataNode node) {
227         throw new IllegalStateException("setTreeFileDataNode may not be called againts a " + this.getClass().getName());
228     }
229 
230     public TreePath getTreePath() {
231         if (parent == null) {
232             return new TreePath(this);
233         } else {
234             return parent.getTreePath().pathByAddingChild(this);
235         }
236     }
237 }