KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > FileAssetEditor


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.editors;
20
21 import java.io.*;
22 import java.util.*;
23
24 import javax.swing.*;
25
26 import org.openharmonise.commons.net.*;
27 import org.openharmonise.him.configuration.*;
28 import org.openharmonise.him.editors.filefilters.*;
29 import org.openharmonise.him.harmonise.*;
30 import org.openharmonise.him.window.*;
31 import org.openharmonise.vfs.*;
32 import org.openharmonise.vfs.status.*;
33
34
35 /**
36  * The file asset editor handles the binary assets types.
37  *
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class FileAssetEditor extends GenericEditor {
43
44     private boolean m_bResourceCreated = false;
45
46     /**
47      *
48      */

49     public FileAssetEditor() {
50         super();
51     }
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String,
57      * com.simulacramedia.vfs.AbstractVirtualFileSystem)
58      */

59     public PathStatusWrapper createNew(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
60         ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(null, new VFSStatus());
61         
62         VirtualFile vfFile = new VirtualFile(sPath);
63
64         JFileChooser chooser = getChooser(sPath);
65
66         int returnVal = chooser.showOpenDialog(DisplayManager.getInstance()
67                 .getMainWindow());
68         if (returnVal == JFileChooser.APPROVE_OPTION) {
69             File fFile = chooser.getSelectedFile();
70             File currentFile = chooser.getCurrentDirectory();
71             ConfigStore.getInstance().setProperty("ASSET_PATH",
72                     currentFile.getPath());
73
74             String JavaDoc sFilename = fFile.getName();
75             if (sFilename.indexOf(".") > -1) {
76                 String JavaDoc sExt = sFilename.substring(sFilename.indexOf(".") + 1);
77
78                 List mimes = MimeTypeMapping.getMimeTypes(sExt);
79                 if (mimes.size() > 0) {
80                     String JavaDoc sMimeType = (String JavaDoc) mimes.get(0);
81                     vfs.getVirtualFileSystemView().setContentType(vfFile,
82                             sMimeType);
83
84                     byte[] bytes = new byte[new Long JavaDoc(fFile.length()).intValue()];
85
86                     BufferedInputStream buff;
87                     try {
88                         buff = new BufferedInputStream(new FileInputStream(
89                                 fFile));
90                         buff.read(bytes);
91
92                         vfFile.setContent(bytes);
93
94                         statusWrapper = vfs.addVirtualFile(sPath, vfFile);
95
96                         vfFile = vfs.getVirtualFile(sPath).getResource();
97                         if(statusWrapper.getStatus().isOK()) {
98                             this.m_bResourceCreated = true;
99                             //return new PathStatusWrapper(super.createWorkingFile(vfFile), statusWrapper.getStatus());
100
return new PathStatusWrapper(null, statusWrapper.getStatus());
101                         }
102                     } catch (FileNotFoundException e) {
103                         e.printStackTrace();
104                     } catch (IOException e) {
105                         e.printStackTrace();
106                     }
107                 }
108             }
109         }
110
111         statusWrapper.getStatus().setStatusLevel(StatusData.LEVEL_ERROR);
112         return new PathStatusWrapper(null, statusWrapper.getStatus());
113     }
114
115     /**
116      * @param path
117      * @return
118      */

119     private JFileChooser getChooser(String JavaDoc sPath) {
120         JFileChooser chooser = new JFileChooser();
121         String JavaDoc sValue = ConfigStore.getInstance()
122                 .getPropertyValue("ASSET_PATH");
123         if (sValue != null && sValue != "") {
124             chooser.setCurrentDirectory(new File(sValue));
125         }
126
127         if (sPath.startsWith(HarmonisePaths.PATH_IMAGES)) {
128             chooser.setFileFilter(new ImageFilter());
129         } else if (sPath.startsWith(HarmonisePaths.PATH_PDF)) {
130             chooser.setFileFilter(new PDFFilter());
131         } else if (sPath.startsWith(HarmonisePaths.PATH_FLASH)) {
132             chooser.setFileFilter(new FlashFilter());
133         } else if (sPath.startsWith(HarmonisePaths.PATH_MOVIES)) {
134             chooser.setFileFilter(new MovieFilter());
135         } else if (sPath.startsWith(HarmonisePaths.PATH_AUDIO)) {
136             chooser.setFileFilter(new AudioFilter());
137         } else if (sPath.startsWith(HarmonisePaths.PATH_OFFICE)) {
138             chooser.setFileFilter(new OfficeFilter());
139         }
140         return chooser;
141     }
142
143     /*
144      * (non-Javadoc)
145      *
146      * @see com.simulacramedia.contentmanager.editors.Editor#export(java.lang.String,
147      * com.simulacramedia.vfs.AbstractVirtualFileSystem)
148      */

149     public StatusData export(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
150         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
151         String JavaDoc sFilename = super.getFileName(vfFile);
152
153         String JavaDoc sTempPath = m_sEditFilePath + sFilename;
154         File fTempFile = new File(sTempPath);
155
156         JFileChooser chooser = new JFileChooser();
157         chooser.setSelectedFile(fTempFile);
158
159         if (sPath.startsWith(HarmonisePaths.PATH_IMAGES)) {
160             chooser.setFileFilter(new ImageFilter());
161         } else if (sPath.startsWith(HarmonisePaths.PATH_PDF)) {
162             chooser.setFileFilter(new PDFFilter());
163         } else if (sPath.startsWith(HarmonisePaths.PATH_FLASH)) {
164             chooser.setFileFilter(new FlashFilter());
165         } else if (sPath.startsWith(HarmonisePaths.PATH_MOVIES)) {
166             chooser.setFileFilter(new MovieFilter());
167         }
168
169         int returnVal = chooser.showSaveDialog(DisplayManager.getInstance()
170                 .getMainWindow());
171         if (returnVal == JFileChooser.APPROVE_OPTION) {
172             File fFile = chooser.getSelectedFile();
173             super.createWorkingFile(vfFile, fFile);
174         }
175         
176         return new VFSStatus();
177     }
178
179     /*
180      * (non-Javadoc)
181      *
182      * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
183      */

184     public boolean hasResourceBeenCreated() {
185         return this.m_bResourceCreated;
186     }
187
188     public PathStatusWrapper upload(String JavaDoc sPath, AbstractVirtualFileSystem vfs) {
189         VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
190
191         JFileChooser chooser = getChooser(sPath);
192         
193         String JavaDoc sValue = ConfigStore.getInstance()
194                 .getPropertyValue("ASSET_PATH");
195         if (sValue != null && sValue != "") {
196             chooser.setCurrentDirectory(new File(sValue));
197         }
198         int returnVal = chooser.showOpenDialog(DisplayManager.getInstance()
199                 .getMainWindow());
200         if (returnVal == JFileChooser.APPROVE_OPTION) {
201             File fFile = chooser.getSelectedFile();
202             File currentFile = chooser.getCurrentDirectory();
203             ConfigStore.getInstance().setProperty("ASSET_PATH",
204                     currentFile.getPath());
205
206             String JavaDoc sFilename = fFile.getName();
207             if (sFilename.indexOf(".") > -1) {
208                 String JavaDoc sExt = sFilename.substring(sFilename.indexOf(".") + 1);
209
210                 List mimes = MimeTypeMapping.getMimeTypes(sExt);
211                 if (mimes.size() > 0) {
212                     String JavaDoc sMimeType = (String JavaDoc) mimes.get(0);
213                     vfs.getVirtualFileSystemView().setContentType(vfFile, sMimeType);
214
215                     byte[] bytes = new byte[new Long JavaDoc(fFile.length())
216                             .intValue()];
217
218                     BufferedInputStream buff;
219                     try {
220                         buff = new BufferedInputStream(new FileInputStream(
221                                 fFile));
222                         buff.read(bytes);
223
224                         vfFile.setContent(bytes);
225
226                         return new PathStatusWrapper(super.createWorkingFile(vfFile), new VFSStatus());
227                     } catch (FileNotFoundException e) {
228                         e.printStackTrace();
229                     } catch (IOException e) {
230                         e.printStackTrace();
231                     }
232                 }
233             }
234         }
235         return new PathStatusWrapper(null, new VFSStatus());
236     }
237
238 }
Popular Tags