001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client.gui.editor;
024:
025: import java.io.IOException;
026: import java.lang.reflect.Constructor;
027: import java.util.Enumeration;
028: import java.util.HashMap;
029: import java.util.Properties;
030: import java.util.StringTokenizer;
031: import org.skunk.config.Configurator;
032: import org.skunk.dav.client.DAVFile;
033: import org.skunk.dav.client.gui.ExplorerApp;
034: import org.skunk.swing.text.syntax.FileMode;
035: import org.skunk.swing.text.syntax.SyntaxStyle;
036: import org.skunk.trace.Debug;
037:
038: public abstract class DAVEditorFactory {
039: public static Properties editorProps;
040: public static final String EDITOR_PROPERTIES = "editor.properties";
041: public static final String EXTENSION_PREFIX = "extension-";
042: public static final String EXTENSION_SEPARATOR = "/";
043: private static HashMap extensionHash;
044: private static Configurator configurator;
045:
046: static {
047: editorProps = new Properties();
048: extensionHash = new HashMap();
049: //this will only be adequate while all the editor associations are hard-coded.
050: try {
051: editorProps.load(DAVEditorFactory.class
052: .getResourceAsStream(EDITOR_PROPERTIES));
053: if (Debug.DEBUG)
054: Debug.trace(DAVEditorFactory.class, Debug.DP3,
055: editorProps);
056: makeExtensionHash();
057: } catch (IOException oyVeh) {
058: if (Debug.DEBUG)
059: Debug.trace(DAVEditorFactory.class, Debug.DP1, oyVeh);
060: }
061: //configure the FileMode mode maps
062: configurator = ExplorerApp.getAppContext().getConfigurator();
063: configurator.configure(FileMode.getModeMapContainer());
064: configurator.configure(SyntaxStyle.getDefaultStyleSet());
065: }
066:
067: private static void makeExtensionHash()
068: {
069: for (Enumeration enum=editorProps.keys();enum.hasMoreElements();)
070: {
071: String key=enum.nextElement().toString();
072: if (Debug.DEBUG) Debug.trace(DAVEditorFactory.class, Debug.DP3, "processing key: "+key);
073: if (key.startsWith(EXTENSION_PREFIX))
074: {
075: StringTokenizer st=new StringTokenizer(key.substring(EXTENSION_PREFIX.length()),
076: EXTENSION_SEPARATOR);
077: String classStr=editorProps.getProperty(key);
078: while (st.hasMoreTokens())
079: {
080: extensionHash.put(st.nextToken().toLowerCase().trim(), classStr);
081: }
082: }
083: }
084: if (Debug.DEBUG) Debug.trace(DAVEditorFactory.class, Debug.DP3,
085: "extensionHash initialized: {0}", extensionHash);
086: }
087:
088: public static final DAVEditor getEditor(DAVFile file) {
089: String filename = file.getFileName();
090: int lastDot = filename.lastIndexOf(".") + 1;
091: if (lastDot > 0 && (lastDot) < filename.length()) {
092: String extension = filename.substring(lastDot)
093: .toLowerCase().trim();
094: if (extensionHash.containsKey(extension)) {
095: String editorName = extensionHash.get(extension)
096: .toString();
097: //try to create it through reflection, using the constructor that takes a DAVFile
098: try {
099: Class editorClass = Class.forName(editorName);
100: Constructor editorConstructor = editorClass
101: .getConstructor(new Class[] { DAVFile.class });
102: Object editorObject = editorConstructor
103: .newInstance(new Object[] { file });
104: if (editorObject instanceof DAVEditor) {
105: configurator.configure(editorObject);
106: return (DAVEditor) editorObject;
107: }
108: } catch (Exception e) {
109: if (Debug.DEBUG)
110: Debug.trace(DAVEditorFactory.class, Debug.DP3,
111: e);
112: }
113: }
114: }
115: return getDefaultEditor(file);
116: }
117:
118: public static final DAVEditor getDefaultEditor(DAVFile file) {
119: DAVEditor ed = new SimpleTextEditor(file);
120: configurator.configure(ed);
121: return ed;
122: }
123: }
|