KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ModeListEntry


1 /*
2  * ModeListEntry.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: ModeListEntry.java,v 1.2 2003/07/26 16:21:16 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import gnu.regexp.RE;
25 import gnu.regexp.REException;
26 import java.lang.reflect.Method JavaDoc;
27
28 public final class ModeListEntry
29 {
30     private final int id;
31     private final String JavaDoc displayName;
32     private final String JavaDoc className;
33     private final boolean selectable;
34     private final String JavaDoc defaultFiles;
35     private Mode mode;
36
37     public ModeListEntry(int id, String JavaDoc displayName, String JavaDoc className,
38         boolean selectable, String JavaDoc defaultFiles)
39     {
40         this.id = id;
41         this.displayName = displayName;
42         this.className = className;
43         this.selectable = selectable;
44         this.defaultFiles = defaultFiles;
45     }
46
47     public final int getId()
48     {
49         return id;
50     }
51
52     public final String JavaDoc getDisplayName()
53     {
54         return displayName;
55     }
56
57     public final String JavaDoc getClassName()
58     {
59         return className;
60     }
61
62     public final boolean isSelectable()
63     {
64         return selectable;
65     }
66
67     public Mode getMode(boolean create)
68     {
69         if (mode == null && create) {
70             if (className != null) {
71                 try {
72                     Class JavaDoc c = Class.forName("org.armedbear.j.".concat(className));
73                     Method JavaDoc method = c.getMethod("getMode", new Class JavaDoc[0]);
74                     mode = (Mode) method.invoke(null, new Object JavaDoc[0]);
75                 }
76                 catch (Throwable JavaDoc t) {
77                     Log.error(t);
78                 }
79             }
80         }
81         return mode;
82     }
83
84     public boolean accepts(String JavaDoc filename)
85     {
86         if (defaultFiles == null)
87             return false;
88         final String JavaDoc key = className.concat(".").concat(Property.FILES.key());
89         final String JavaDoc userFiles = Editor.preferences().getStringProperty(key);
90         RE filesRE = null;
91         if (userFiles != null) {
92             if (userFiles.trim().length() == 0)
93                 return false;
94             try {
95                 filesRE = new RE(userFiles, RE.REG_ICASE);
96             }
97             catch (REException e) {
98                 Log.error(e);
99             }
100         } else {
101             try {
102                 filesRE = new RE(defaultFiles, RE.REG_ICASE);
103             }
104             catch (REException e) {
105                 Log.error(e);
106             }
107         }
108         if (filesRE != null && filesRE.isMatch(filename))
109             return true;
110         else
111             return false;
112     }
113
114     public String JavaDoc toString()
115     {
116         return displayName;
117     }
118 }
119
Popular Tags