001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.editors;
020:
021: import java.io.*;
022: import java.util.*;
023:
024: import javax.swing.*;
025:
026: import org.openharmonise.commons.net.*;
027: import org.openharmonise.him.configuration.*;
028: import org.openharmonise.him.editors.filefilters.*;
029: import org.openharmonise.him.harmonise.*;
030: import org.openharmonise.him.window.*;
031: import org.openharmonise.vfs.*;
032: import org.openharmonise.vfs.status.*;
033:
034: /**
035: * The file asset editor handles the binary assets types.
036: *
037: * @author Matthew Large
038: * @version $Revision: 1.1 $
039: *
040: */
041: public class FileAssetEditor extends GenericEditor {
042:
043: private boolean m_bResourceCreated = false;
044:
045: /**
046: *
047: */
048: public FileAssetEditor() {
049: super ();
050: }
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see com.simulacramedia.contentmanager.editors.Editor#createNew(java.lang.String,
056: * com.simulacramedia.vfs.AbstractVirtualFileSystem)
057: */
058: public PathStatusWrapper createNew(String sPath,
059: AbstractVirtualFileSystem vfs) {
060: ResourceStatusWrapper statusWrapper = new ResourceStatusWrapper(
061: null, new VFSStatus());
062:
063: VirtualFile vfFile = new VirtualFile(sPath);
064:
065: JFileChooser chooser = getChooser(sPath);
066:
067: int returnVal = chooser.showOpenDialog(DisplayManager
068: .getInstance().getMainWindow());
069: if (returnVal == JFileChooser.APPROVE_OPTION) {
070: File fFile = chooser.getSelectedFile();
071: File currentFile = chooser.getCurrentDirectory();
072: ConfigStore.getInstance().setProperty("ASSET_PATH",
073: currentFile.getPath());
074:
075: String sFilename = fFile.getName();
076: if (sFilename.indexOf(".") > -1) {
077: String sExt = sFilename.substring(sFilename
078: .indexOf(".") + 1);
079:
080: List mimes = MimeTypeMapping.getMimeTypes(sExt);
081: if (mimes.size() > 0) {
082: String sMimeType = (String) mimes.get(0);
083: vfs.getVirtualFileSystemView().setContentType(
084: vfFile, sMimeType);
085:
086: byte[] bytes = new byte[new Long(fFile.length())
087: .intValue()];
088:
089: BufferedInputStream buff;
090: try {
091: buff = new BufferedInputStream(
092: new FileInputStream(fFile));
093: buff.read(bytes);
094:
095: vfFile.setContent(bytes);
096:
097: statusWrapper = vfs.addVirtualFile(sPath,
098: vfFile);
099:
100: vfFile = vfs.getVirtualFile(sPath)
101: .getResource();
102: if (statusWrapper.getStatus().isOK()) {
103: this .m_bResourceCreated = true;
104: //return new PathStatusWrapper(super.createWorkingFile(vfFile), statusWrapper.getStatus());
105: return new PathStatusWrapper(null,
106: statusWrapper.getStatus());
107: }
108: } catch (FileNotFoundException e) {
109: e.printStackTrace();
110: } catch (IOException e) {
111: e.printStackTrace();
112: }
113: }
114: }
115: }
116:
117: statusWrapper.getStatus()
118: .setStatusLevel(StatusData.LEVEL_ERROR);
119: return new PathStatusWrapper(null, statusWrapper.getStatus());
120: }
121:
122: /**
123: * @param path
124: * @return
125: */
126: private JFileChooser getChooser(String sPath) {
127: JFileChooser chooser = new JFileChooser();
128: String sValue = ConfigStore.getInstance().getPropertyValue(
129: "ASSET_PATH");
130: if (sValue != null && sValue != "") {
131: chooser.setCurrentDirectory(new File(sValue));
132: }
133:
134: if (sPath.startsWith(HarmonisePaths.PATH_IMAGES)) {
135: chooser.setFileFilter(new ImageFilter());
136: } else if (sPath.startsWith(HarmonisePaths.PATH_PDF)) {
137: chooser.setFileFilter(new PDFFilter());
138: } else if (sPath.startsWith(HarmonisePaths.PATH_FLASH)) {
139: chooser.setFileFilter(new FlashFilter());
140: } else if (sPath.startsWith(HarmonisePaths.PATH_MOVIES)) {
141: chooser.setFileFilter(new MovieFilter());
142: } else if (sPath.startsWith(HarmonisePaths.PATH_AUDIO)) {
143: chooser.setFileFilter(new AudioFilter());
144: } else if (sPath.startsWith(HarmonisePaths.PATH_OFFICE)) {
145: chooser.setFileFilter(new OfficeFilter());
146: }
147: return chooser;
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see com.simulacramedia.contentmanager.editors.Editor#export(java.lang.String,
154: * com.simulacramedia.vfs.AbstractVirtualFileSystem)
155: */
156: public StatusData export(String sPath, AbstractVirtualFileSystem vfs) {
157: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
158: String sFilename = super .getFileName(vfFile);
159:
160: String sTempPath = m_sEditFilePath + sFilename;
161: File fTempFile = new File(sTempPath);
162:
163: JFileChooser chooser = new JFileChooser();
164: chooser.setSelectedFile(fTempFile);
165:
166: if (sPath.startsWith(HarmonisePaths.PATH_IMAGES)) {
167: chooser.setFileFilter(new ImageFilter());
168: } else if (sPath.startsWith(HarmonisePaths.PATH_PDF)) {
169: chooser.setFileFilter(new PDFFilter());
170: } else if (sPath.startsWith(HarmonisePaths.PATH_FLASH)) {
171: chooser.setFileFilter(new FlashFilter());
172: } else if (sPath.startsWith(HarmonisePaths.PATH_MOVIES)) {
173: chooser.setFileFilter(new MovieFilter());
174: }
175:
176: int returnVal = chooser.showSaveDialog(DisplayManager
177: .getInstance().getMainWindow());
178: if (returnVal == JFileChooser.APPROVE_OPTION) {
179: File fFile = chooser.getSelectedFile();
180: super .createWorkingFile(vfFile, fFile);
181: }
182:
183: return new VFSStatus();
184: }
185:
186: /*
187: * (non-Javadoc)
188: *
189: * @see com.simulacramedia.contentmanager.editors.Editor#hasResourceBeenCreated()
190: */
191: public boolean hasResourceBeenCreated() {
192: return this .m_bResourceCreated;
193: }
194:
195: public PathStatusWrapper upload(String sPath,
196: AbstractVirtualFileSystem vfs) {
197: VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
198:
199: JFileChooser chooser = getChooser(sPath);
200:
201: String sValue = ConfigStore.getInstance().getPropertyValue(
202: "ASSET_PATH");
203: if (sValue != null && sValue != "") {
204: chooser.setCurrentDirectory(new File(sValue));
205: }
206: int returnVal = chooser.showOpenDialog(DisplayManager
207: .getInstance().getMainWindow());
208: if (returnVal == JFileChooser.APPROVE_OPTION) {
209: File fFile = chooser.getSelectedFile();
210: File currentFile = chooser.getCurrentDirectory();
211: ConfigStore.getInstance().setProperty("ASSET_PATH",
212: currentFile.getPath());
213:
214: String sFilename = fFile.getName();
215: if (sFilename.indexOf(".") > -1) {
216: String sExt = sFilename.substring(sFilename
217: .indexOf(".") + 1);
218:
219: List mimes = MimeTypeMapping.getMimeTypes(sExt);
220: if (mimes.size() > 0) {
221: String sMimeType = (String) mimes.get(0);
222: vfs.getVirtualFileSystemView().setContentType(
223: vfFile, sMimeType);
224:
225: byte[] bytes = new byte[new Long(fFile.length())
226: .intValue()];
227:
228: BufferedInputStream buff;
229: try {
230: buff = new BufferedInputStream(
231: new FileInputStream(fFile));
232: buff.read(bytes);
233:
234: vfFile.setContent(bytes);
235:
236: return new PathStatusWrapper(super
237: .createWorkingFile(vfFile),
238: new VFSStatus());
239: } catch (FileNotFoundException e) {
240: e.printStackTrace();
241: } catch (IOException e) {
242: e.printStackTrace();
243: }
244: }
245: }
246: }
247: return new PathStatusWrapper(null, new VFSStatus());
248: }
249:
250: }
|