PageRenderTime 59ms CodeModel.GetById 48ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/Mode.java

#
Java | 264 lines | 127 code | 22 blank | 115 comment | 25 complexity | 8e7df63e9896bb7abeb5b9e126a1bbf6 MD5 | raw file
  1/*
  2 * Mode.java - jEdit editing mode
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 1998, 1999, 2000 Slava Pestov
  7 * Copyright (C) 1999 mike dillon
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 */
 23
 24package org.gjt.sp.jedit;
 25
 26//{{{ Imports
 27import gnu.regexp.*;
 28import java.util.Hashtable;
 29import org.gjt.sp.jedit.syntax.TokenMarker;
 30import org.gjt.sp.util.Log;
 31//}}}
 32
 33/**
 34 * An edit mode defines specific settings for editing some type of file.
 35 * One instance of this class is created for each supported edit mode.
 36 *
 37 * @author Slava Pestov
 38 * @version $Id: Mode.java 3928 2001-12-01 05:48:48Z spestov $
 39 */
 40public class Mode
 41{
 42	//{{{ Mode constructor
 43	/**
 44	 * Creates a new edit mode.
 45	 *
 46	 * @param name The name used in mode listings and to query mode
 47	 * properties
 48	 * @see #getProperty(String)
 49	 */
 50	public Mode(String name)
 51	{
 52		this.name = name;
 53		props = new Hashtable();
 54	} //}}}
 55
 56	//{{{ init()
 57	/**
 58	 * Initializes the edit mode. Should be called after all properties
 59	 * are loaded and set.
 60	 */
 61	public void init()
 62	{
 63		try
 64		{
 65			String filenameGlob = (String)getProperty("filenameGlob");
 66			if(filenameGlob != null && filenameGlob.length() != 0)
 67			{
 68				filenameRE = new RE(MiscUtilities.globToRE(
 69					filenameGlob),RE.REG_ICASE);
 70			}
 71
 72			String firstlineGlob = (String)getProperty("firstlineGlob");
 73			if(firstlineGlob != null && firstlineGlob.length() != 0)
 74			{
 75				firstlineRE = new RE(MiscUtilities.globToRE(
 76					firstlineGlob),RE.REG_ICASE);
 77			}
 78		}
 79		catch(REException re)
 80		{
 81			Log.log(Log.ERROR,this,"Invalid filename/firstline"
 82				+ " globs in mode " + name);
 83			Log.log(Log.ERROR,this,re);
 84		}
 85	} //}}}
 86
 87	//{{{ getTokenMarker() method
 88	/**
 89	 * Returns the token marker specified with
 90	 * <code>setTokenMarker()</code>. Should only be called by
 91	 * <code>TokenMarker.getExternalRuleSet()</code>.
 92	 */
 93	public TokenMarker getTokenMarker()
 94	{
 95		loadIfNecessary();
 96		return marker;
 97	} //}}}
 98
 99	//{{{ setTokenMarker() method
100	/**
101	 * Sets the token marker for this mode. This token marker will be
102	 * cloned to obtain new instances.
103	 * @param marker The new token marker
104	 */
105	public void setTokenMarker(TokenMarker marker)
106	{
107		this.marker = marker;
108	} //}}}
109
110	//{{{ loadIfNecessary() method
111	/**
112	 * Loads the mode from disk if it hasn't been loaded already.
113	 * @since jEdit 2.5pre3
114	 */
115	public void loadIfNecessary()
116	{
117		if(marker == null)
118			jEdit.loadMode(this);
119	} //}}}
120
121	//{{{ getProperty() method
122	/**
123	 * Returns a mode property.
124	 * @param key The property name
125	 *
126	 * @since jEdit 2.2pre1
127	 */
128	public Object getProperty(String key)
129	{
130		String prefix = "mode." + name + ".";
131
132		//if(jEdit.getBooleanProperty(prefix + "customSettings"))
133		//{
134			String property = jEdit.getProperty(prefix + key);
135			if(property != null)
136			{
137				Object value;
138				try
139				{
140					value = new Integer(property);
141				}
142				catch(NumberFormatException nf)
143				{
144					value = property;
145				}
146				return value;
147			}
148		//}
149
150		Object value = props.get(key);
151		if(value != null)
152			return value;
153
154		String global = jEdit.getProperty("buffer." + key);
155		if(global != null)
156		{
157			try
158			{
159				return new Integer(global);
160			}
161			catch(NumberFormatException nf)
162			{
163				return global;
164			}
165		}
166		else
167			return null;
168	} //}}}
169
170	//{{{ getBooleanProperty() method
171	/**
172	 * Returns the value of a boolean property.
173	 * @param key The property name
174	 *
175	 * @since jEdit 2.5pre3
176	 */
177	public boolean getBooleanProperty(String key)
178	{
179		Object value = getProperty(key);
180		if("true".equals(value) || "on".equals(value) || "yes".equals(value))
181			return true;
182		else
183			return false;
184	} //}}}
185
186	//{{{ setProperty() method
187	/**
188	 * Sets a mode property.
189	 * @param key The property name
190	 * @param value The property value
191	 */
192	public void setProperty(String key, Object value)
193	{
194		props.put(key,value);
195	} //}}}
196
197	//{{{ unsetProperty() method
198	/**
199	 * Unsets a mode property.
200	 * @param key The property name
201	 * @since jEdit 3.2pre3
202	 */
203	public void unsetProperty(String key)
204	{
205		props.remove(key);
206	} //}}}
207
208	//{{{ setProperties() method
209	/**
210	 * Should only be called by <code>XModeHandler</code>.
211	 * @since jEdit 4.0pre3
212	 */
213	public void setProperties(Hashtable props)
214	{
215		this.props = props;
216	} //}}}
217
218	//{{{ accept() method
219	/**
220	 * Returns if the edit mode is suitable for editing the specified
221	 * file. The buffer name and first line is checked against the
222	 * file name and first line globs, respectively.
223	 * @param fileName The buffer's name
224	 * @param firstLine The first line of the buffer
225	 *
226	 * @since jEdit 3.2pre3
227	 */
228	public boolean accept(String fileName, String firstLine)
229	{
230		if(filenameRE != null && filenameRE.isMatch(fileName))
231			return true;
232
233		if(firstlineRE != null && firstlineRE.isMatch(firstLine))
234			return true;
235
236		return false;
237	} //}}}
238
239	//{{{ getName() method
240	/**
241	 * Returns the internal name of this edit mode.
242	 */
243	public String getName()
244	{
245		return name;
246	} //}}}
247
248	//{{{ toString() method
249	/**
250	 * Returns a string representation of this edit mode.
251	 */
252	public String toString()
253	{
254		return getClass().getName() + "[" + getName() + "]";
255	} //}}}
256
257	//{{{ Private members
258	private String name;
259	private Hashtable props;
260	private RE firstlineRE;
261	private RE filenameRE;
262	private TokenMarker marker;
263	//}}}
264}