001: /*
002: * ModeListEntry.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: ModeListEntry.java,v 1.2 2003/07/26 16:21:16 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import gnu.regexp.RE;
025: import gnu.regexp.REException;
026: import java.lang.reflect.Method;
027:
028: public final class ModeListEntry {
029: private final int id;
030: private final String displayName;
031: private final String className;
032: private final boolean selectable;
033: private final String defaultFiles;
034: private Mode mode;
035:
036: public ModeListEntry(int id, String displayName, String className,
037: boolean selectable, String defaultFiles) {
038: this .id = id;
039: this .displayName = displayName;
040: this .className = className;
041: this .selectable = selectable;
042: this .defaultFiles = defaultFiles;
043: }
044:
045: public final int getId() {
046: return id;
047: }
048:
049: public final String getDisplayName() {
050: return displayName;
051: }
052:
053: public final String getClassName() {
054: return className;
055: }
056:
057: public final boolean isSelectable() {
058: return selectable;
059: }
060:
061: public Mode getMode(boolean create) {
062: if (mode == null && create) {
063: if (className != null) {
064: try {
065: Class c = Class.forName("org.armedbear.j."
066: .concat(className));
067: Method method = c
068: .getMethod("getMode", new Class[0]);
069: mode = (Mode) method.invoke(null, new Object[0]);
070: } catch (Throwable t) {
071: Log.error(t);
072: }
073: }
074: }
075: return mode;
076: }
077:
078: public boolean accepts(String filename) {
079: if (defaultFiles == null)
080: return false;
081: final String key = className.concat(".").concat(
082: Property.FILES.key());
083: final String userFiles = Editor.preferences()
084: .getStringProperty(key);
085: RE filesRE = null;
086: if (userFiles != null) {
087: if (userFiles.trim().length() == 0)
088: return false;
089: try {
090: filesRE = new RE(userFiles, RE.REG_ICASE);
091: } catch (REException e) {
092: Log.error(e);
093: }
094: } else {
095: try {
096: filesRE = new RE(defaultFiles, RE.REG_ICASE);
097: } catch (REException e) {
098: Log.error(e);
099: }
100: }
101: if (filesRE != null && filesRE.isMatch(filename))
102: return true;
103: else
104: return false;
105: }
106:
107: public String toString() {
108: return displayName;
109: }
110: }
|