PageRenderTime 54ms CodeModel.GetById 42ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 285 lines | 140 code | 24 blank | 121 comment | 33 complexity | 55603d576877f54d09770d4cea853605 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 java.util.Hashtable;
 28import java.util.regex.Pattern;
 29import java.util.regex.PatternSyntaxException;
 30import org.gjt.sp.jedit.syntax.TokenMarker;
 31import org.gjt.sp.util.Log;
 32//}}}
 33
 34/**
 35 * An edit mode defines specific settings for editing some type of file.
 36 * One instance of this class is created for each supported edit mode.
 37 *
 38 * @author Slava Pestov
 39 * @version $Id: Mode.java 5443 2006-06-18 18:51:40Z vanza $
 40 */
 41public class Mode
 42{
 43	//{{{ Mode constructor
 44	/**
 45	 * Creates a new edit mode.
 46	 *
 47	 * @param name The name used in mode listings and to query mode
 48	 * properties
 49	 * @see #getProperty(String)
 50	 */
 51	public Mode(String name)
 52	{
 53		this.name = name;
 54		props = new Hashtable();
 55	} //}}}
 56
 57	//{{{ init() method
 58	/**
 59	 * Initializes the edit mode. Should be called after all properties
 60	 * are loaded and set.
 61	 */
 62	public void init()
 63	{
 64		try
 65		{
 66			String filenameGlob = (String)getProperty("filenameGlob");
 67			if(filenameGlob != null && filenameGlob.length() != 0)
 68			{
 69				filenameRE = Pattern.compile(MiscUtilities.globToRE(filenameGlob),
 70							     Pattern.CASE_INSENSITIVE);
 71			}
 72
 73			String firstlineGlob = (String)getProperty("firstlineGlob");
 74			if(firstlineGlob != null && firstlineGlob.length() != 0)
 75			{
 76				firstlineRE = Pattern.compile(MiscUtilities.globToRE(firstlineGlob),
 77							      Pattern.CASE_INSENSITIVE);
 78			}
 79		}
 80		catch(PatternSyntaxException re)
 81		{
 82			Log.log(Log.ERROR,this,"Invalid filename/firstline"
 83				+ " globs in mode " + name);
 84			Log.log(Log.ERROR,this,re);
 85		}
 86
 87		// Fix for this bug:
 88		// -- Put a mode into the user dir with the same name as one
 89		//    on the system dir.
 90		// -- Reload edit modes.
 91		// -- Old mode from system dir still used for highlighting
 92		//    until jEdit restart.
 93		marker = null;
 94	} //}}}
 95
 96	//{{{ getTokenMarker() method
 97	/**
 98	 * Returns the token marker for this mode.
 99	 */
100	public TokenMarker getTokenMarker()
101	{
102		loadIfNecessary();
103		return marker;
104	} //}}}
105
106	//{{{ setTokenMarker() method
107	/**
108	 * Sets the token marker for this mode.
109	 * @param marker The new token marker
110	 */
111	public void setTokenMarker(TokenMarker marker)
112	{
113		this.marker = marker;
114	} //}}}
115
116	//{{{ loadIfNecessary() method
117	/**
118	 * Loads the mode from disk if it hasn't been loaded already.
119	 * @since jEdit 2.5pre3
120	 */
121	public void loadIfNecessary()
122	{
123		if(marker == null)
124			jEdit.loadMode(this);
125	} //}}}
126
127	//{{{ getProperty() method
128	/**
129	 * Returns a mode property.
130	 * @param key The property name
131	 *
132	 * @since jEdit 2.2pre1
133	 */
134	public Object getProperty(String key)
135	{
136		String prefix = "mode." + name + ".";
137
138		//if(jEdit.getBooleanProperty(prefix + "customSettings"))
139		//{
140			String property = jEdit.getProperty(prefix + key);
141			if(property != null)
142			{
143				Object value;
144				try
145				{
146					value = new Integer(property);
147				}
148				catch(NumberFormatException nf)
149				{
150					value = property;
151				}
152				return value;
153			}
154		//}
155
156		Object value = props.get(key);
157		if(value != null)
158			return value;
159
160		String global = jEdit.getProperty("buffer." + key);
161		if(global != null)
162		{
163			try
164			{
165				return new Integer(global);
166			}
167			catch(NumberFormatException nf)
168			{
169				return global;
170			}
171		}
172		else
173			return null;
174	} //}}}
175
176	//{{{ getBooleanProperty() method
177	/**
178	 * Returns the value of a boolean property.
179	 * @param key The property name
180	 *
181	 * @since jEdit 2.5pre3
182	 */
183	public boolean getBooleanProperty(String key)
184	{
185		Object value = getProperty(key);
186		if("true".equals(value) || "on".equals(value) || "yes".equals(value))
187			return true;
188		else
189			return false;
190	} //}}}
191
192	//{{{ setProperty() method
193	/**
194	 * Sets a mode property.
195	 * @param key The property name
196	 * @param value The property value
197	 */
198	public void setProperty(String key, Object value)
199	{
200		props.put(key,value);
201	} //}}}
202
203	//{{{ unsetProperty() method
204	/**
205	 * Unsets a mode property.
206	 * @param key The property name
207	 * @since jEdit 3.2pre3
208	 */
209	public void unsetProperty(String key)
210	{
211		props.remove(key);
212	} //}}}
213
214	//{{{ setProperties() method
215	/**
216	 * Should only be called by <code>XModeHandler</code>.
217	 * @since jEdit 4.0pre3
218	 */
219	public void setProperties(Hashtable props)
220	{
221		if(props == null)
222			props = new Hashtable();
223
224		// need to carry over file name and first line globs because they are
225		// not given to us by the XMode handler, but instead are filled in by
226		// the catalog loader.
227		String filenameGlob = (String)this.props.get("filenameGlob");
228		String firstlineGlob = (String)this.props.get("firstlineGlob");
229		String filename = (String)this.props.get("file");
230		this.props = props;
231		if(filenameGlob != null)
232			props.put("filenameGlob",filenameGlob);
233		if(firstlineGlob != null)
234			props.put("firstlineGlob",firstlineGlob);
235		if(filename != null)
236			props.put("file",filename);
237	} //}}}
238
239	//{{{ accept() method
240	/**
241	 * Returns if the edit mode is suitable for editing the specified
242	 * file. The buffer name and first line is checked against the
243	 * file name and first line globs, respectively.
244	 * @param fileName The buffer's name
245	 * @param firstLine The first line of the buffer
246	 *
247	 * @since jEdit 3.2pre3
248	 */
249	public boolean accept(String fileName, String firstLine)
250	{
251		if(filenameRE != null && filenameRE.matcher(fileName).matches())
252			return true;
253
254		if(firstlineRE != null && firstlineRE.matcher(firstLine).matches())
255			return true;
256
257		return false;
258	} //}}}
259
260	//{{{ getName() method
261	/**
262	 * Returns the internal name of this edit mode.
263	 */
264	public String getName()
265	{
266		return name;
267	} //}}}
268
269	//{{{ toString() method
270	/**
271	 * Returns a string representation of this edit mode.
272	 */
273	public String toString()
274	{
275		return name;
276	} //}}}
277
278	//{{{ Private members
279	private String name;
280	private Hashtable props;
281	private Pattern firstlineRE;
282	private Pattern filenameRE;
283	private TokenMarker marker;
284	//}}}
285}