PageRenderTime 78ms CodeModel.GetById 63ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/Mode.java

#
Java | 284 lines | 137 code | 23 blank | 124 comment | 31 complexity | 0283fffc881cd27e82ecab9617a9fb2c 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 4331 2002-08-29 22:09:26Z 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() method
 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		// Fix for this bug:
 87		// -- Put a mode into the user dir with the same name as one
 88		//    on the system dir.
 89		// -- Reload edit modes.
 90		// -- Old mode from system dir still used for highlighting
 91		//    until jEdit restart.
 92		marker = null;
 93	} //}}}
 94
 95	//{{{ getTokenMarker() method
 96	/**
 97	 * Returns the token marker specified with
 98	 * <code>setTokenMarker()</code>. Should only be called by
 99	 * <code>TokenMarker.getExternalRuleSet()</code>.
100	 */
101	public TokenMarker getTokenMarker()
102	{
103		loadIfNecessary();
104		return marker;
105	} //}}}
106
107	//{{{ setTokenMarker() method
108	/**
109	 * Sets the token marker for this mode. This token marker will be
110	 * cloned to obtain new instances.
111	 * @param marker The new token marker
112	 */
113	public void setTokenMarker(TokenMarker marker)
114	{
115		this.marker = marker;
116	} //}}}
117
118	//{{{ loadIfNecessary() method
119	/**
120	 * Loads the mode from disk if it hasn't been loaded already.
121	 * @since jEdit 2.5pre3
122	 */
123	public void loadIfNecessary()
124	{
125		if(marker == null)
126			jEdit.loadMode(this);
127	} //}}}
128
129	//{{{ getProperty() method
130	/**
131	 * Returns a mode property.
132	 * @param key The property name
133	 *
134	 * @since jEdit 2.2pre1
135	 */
136	public Object getProperty(String key)
137	{
138		String prefix = "mode." + name + ".";
139
140		//if(jEdit.getBooleanProperty(prefix + "customSettings"))
141		//{
142			String property = jEdit.getProperty(prefix + key);
143			if(property != null)
144			{
145				Object value;
146				try
147				{
148					value = new Integer(property);
149				}
150				catch(NumberFormatException nf)
151				{
152					value = property;
153				}
154				return value;
155			}
156		//}
157
158		Object value = props.get(key);
159		if(value != null)
160			return value;
161
162		String global = jEdit.getProperty("buffer." + key);
163		if(global != null)
164		{
165			try
166			{
167				return new Integer(global);
168			}
169			catch(NumberFormatException nf)
170			{
171				return global;
172			}
173		}
174		else
175			return null;
176	} //}}}
177
178	//{{{ getBooleanProperty() method
179	/**
180	 * Returns the value of a boolean property.
181	 * @param key The property name
182	 *
183	 * @since jEdit 2.5pre3
184	 */
185	public boolean getBooleanProperty(String key)
186	{
187		Object value = getProperty(key);
188		if("true".equals(value) || "on".equals(value) || "yes".equals(value))
189			return true;
190		else
191			return false;
192	} //}}}
193
194	//{{{ setProperty() method
195	/**
196	 * Sets a mode property.
197	 * @param key The property name
198	 * @param value The property value
199	 */
200	public void setProperty(String key, Object value)
201	{
202		props.put(key,value);
203	} //}}}
204
205	//{{{ unsetProperty() method
206	/**
207	 * Unsets a mode property.
208	 * @param key The property name
209	 * @since jEdit 3.2pre3
210	 */
211	public void unsetProperty(String key)
212	{
213		props.remove(key);
214	} //}}}
215
216	//{{{ setProperties() method
217	/**
218	 * Should only be called by <code>XModeHandler</code>.
219	 * @since jEdit 4.0pre3
220	 */
221	public void setProperties(Hashtable props)
222	{
223		// need to carry over file name and first line globs because they are
224		// not given to us by the XMode handler, but instead are filled in by
225		// the catalog loader.
226		String filenameGlob = (String)this.props.get("filenameGlob");
227		String firstlineGlob = (String)this.props.get("firstlineGlob");
228		String filename = (String)this.props.get("file");
229		this.props = props;
230		if(filenameGlob != null)
231			props.put("filenameGlob",filenameGlob);
232		if(firstlineGlob != null)
233			props.put("firstlineGlob",firstlineGlob);
234		if(filename != null)
235			props.put("file",filename);
236	} //}}}
237
238	//{{{ accept() method
239	/**
240	 * Returns if the edit mode is suitable for editing the specified
241	 * file. The buffer name and first line is checked against the
242	 * file name and first line globs, respectively.
243	 * @param fileName The buffer's name
244	 * @param firstLine The first line of the buffer
245	 *
246	 * @since jEdit 3.2pre3
247	 */
248	public boolean accept(String fileName, String firstLine)
249	{
250		if(filenameRE != null && filenameRE.isMatch(fileName))
251			return true;
252
253		if(firstlineRE != null && firstlineRE.isMatch(firstLine))
254			return true;
255
256		return false;
257	} //}}}
258
259	//{{{ getName() method
260	/**
261	 * Returns the internal name of this edit mode.
262	 */
263	public String getName()
264	{
265		return name;
266	} //}}}
267
268	//{{{ toString() method
269	/**
270	 * Returns a string representation of this edit mode.
271	 */
272	public String toString()
273	{
274		return getClass().getName() + "[" + getName() + "]";
275	} //}}}
276
277	//{{{ Private members
278	private String name;
279	private Hashtable props;
280	private RE firstlineRE;
281	private RE filenameRE;
282	private TokenMarker marker;
283	//}}}
284}