PageRenderTime 187ms CodeModel.GetById 170ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/Mode.java

#
Java | 284 lines | 139 code | 24 blank | 121 comment | 33 complexity | 95438328a623f1acab4f8ac5da9efefb 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 4651 2003-04-28 01:35:29Z 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 for this mode.
 98	 */
 99	public TokenMarker getTokenMarker()
100	{
101		loadIfNecessary();
102		return marker;
103	} //}}}
104
105	//{{{ setTokenMarker() method
106	/**
107	 * Sets the token marker for this mode.
108	 * @param marker The new token marker
109	 */
110	public void setTokenMarker(TokenMarker marker)
111	{
112		this.marker = marker;
113	} //}}}
114
115	//{{{ loadIfNecessary() method
116	/**
117	 * Loads the mode from disk if it hasn't been loaded already.
118	 * @since jEdit 2.5pre3
119	 */
120	public void loadIfNecessary()
121	{
122		if(marker == null)
123			jEdit.loadMode(this);
124	} //}}}
125
126	//{{{ getProperty() method
127	/**
128	 * Returns a mode property.
129	 * @param key The property name
130	 *
131	 * @since jEdit 2.2pre1
132	 */
133	public Object getProperty(String key)
134	{
135		String prefix = "mode." + name + ".";
136
137		//if(jEdit.getBooleanProperty(prefix + "customSettings"))
138		//{
139			String property = jEdit.getProperty(prefix + key);
140			if(property != null)
141			{
142				Object value;
143				try
144				{
145					value = new Integer(property);
146				}
147				catch(NumberFormatException nf)
148				{
149					value = property;
150				}
151				return value;
152			}
153		//}
154
155		Object value = props.get(key);
156		if(value != null)
157			return value;
158
159		String global = jEdit.getProperty("buffer." + key);
160		if(global != null)
161		{
162			try
163			{
164				return new Integer(global);
165			}
166			catch(NumberFormatException nf)
167			{
168				return global;
169			}
170		}
171		else
172			return null;
173	} //}}}
174
175	//{{{ getBooleanProperty() method
176	/**
177	 * Returns the value of a boolean property.
178	 * @param key The property name
179	 *
180	 * @since jEdit 2.5pre3
181	 */
182	public boolean getBooleanProperty(String key)
183	{
184		Object value = getProperty(key);
185		if("true".equals(value) || "on".equals(value) || "yes".equals(value))
186			return true;
187		else
188			return false;
189	} //}}}
190
191	//{{{ setProperty() method
192	/**
193	 * Sets a mode property.
194	 * @param key The property name
195	 * @param value The property value
196	 */
197	public void setProperty(String key, Object value)
198	{
199		props.put(key,value);
200	} //}}}
201
202	//{{{ unsetProperty() method
203	/**
204	 * Unsets a mode property.
205	 * @param key The property name
206	 * @since jEdit 3.2pre3
207	 */
208	public void unsetProperty(String key)
209	{
210		props.remove(key);
211	} //}}}
212
213	//{{{ setProperties() method
214	/**
215	 * Should only be called by <code>XModeHandler</code>.
216	 * @since jEdit 4.0pre3
217	 */
218	public void setProperties(Hashtable props)
219	{
220		if(props == null)
221			props = new Hashtable();
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 name;
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}