PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms 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
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit;
  24. //{{{ Imports
  25. import gnu.regexp.*;
  26. import java.util.Hashtable;
  27. import org.gjt.sp.jedit.syntax.TokenMarker;
  28. import org.gjt.sp.util.Log;
  29. //}}}
  30. /**
  31. * An edit mode defines specific settings for editing some type of file.
  32. * One instance of this class is created for each supported edit mode.
  33. *
  34. * @author Slava Pestov
  35. * @version $Id: Mode.java 3928 2001-12-01 05:48:48Z spestov $
  36. */
  37. public class Mode
  38. {
  39. //{{{ Mode constructor
  40. /**
  41. * Creates a new edit mode.
  42. *
  43. * @param name The name used in mode listings and to query mode
  44. * properties
  45. * @see #getProperty(String)
  46. */
  47. public Mode(String name)
  48. {
  49. this.name = name;
  50. props = new Hashtable();
  51. } //}}}
  52. //{{{ init()
  53. /**
  54. * Initializes the edit mode. Should be called after all properties
  55. * are loaded and set.
  56. */
  57. public void init()
  58. {
  59. try
  60. {
  61. String filenameGlob = (String)getProperty("filenameGlob");
  62. if(filenameGlob != null && filenameGlob.length() != 0)
  63. {
  64. filenameRE = new RE(MiscUtilities.globToRE(
  65. filenameGlob),RE.REG_ICASE);
  66. }
  67. String firstlineGlob = (String)getProperty("firstlineGlob");
  68. if(firstlineGlob != null && firstlineGlob.length() != 0)
  69. {
  70. firstlineRE = new RE(MiscUtilities.globToRE(
  71. firstlineGlob),RE.REG_ICASE);
  72. }
  73. }
  74. catch(REException re)
  75. {
  76. Log.log(Log.ERROR,this,"Invalid filename/firstline"
  77. + " globs in mode " + name);
  78. Log.log(Log.ERROR,this,re);
  79. }
  80. } //}}}
  81. //{{{ getTokenMarker() method
  82. /**
  83. * Returns the token marker specified with
  84. * <code>setTokenMarker()</code>. Should only be called by
  85. * <code>TokenMarker.getExternalRuleSet()</code>.
  86. */
  87. public TokenMarker getTokenMarker()
  88. {
  89. loadIfNecessary();
  90. return marker;
  91. } //}}}
  92. //{{{ setTokenMarker() method
  93. /**
  94. * Sets the token marker for this mode. This token marker will be
  95. * cloned to obtain new instances.
  96. * @param marker The new token marker
  97. */
  98. public void setTokenMarker(TokenMarker marker)
  99. {
  100. this.marker = marker;
  101. } //}}}
  102. //{{{ loadIfNecessary() method
  103. /**
  104. * Loads the mode from disk if it hasn't been loaded already.
  105. * @since jEdit 2.5pre3
  106. */
  107. public void loadIfNecessary()
  108. {
  109. if(marker == null)
  110. jEdit.loadMode(this);
  111. } //}}}
  112. //{{{ getProperty() method
  113. /**
  114. * Returns a mode property.
  115. * @param key The property name
  116. *
  117. * @since jEdit 2.2pre1
  118. */
  119. public Object getProperty(String key)
  120. {
  121. String prefix = "mode." + name + ".";
  122. //if(jEdit.getBooleanProperty(prefix + "customSettings"))
  123. //{
  124. String property = jEdit.getProperty(prefix + key);
  125. if(property != null)
  126. {
  127. Object value;
  128. try
  129. {
  130. value = new Integer(property);
  131. }
  132. catch(NumberFormatException nf)
  133. {
  134. value = property;
  135. }
  136. return value;
  137. }
  138. //}
  139. Object value = props.get(key);
  140. if(value != null)
  141. return value;
  142. String global = jEdit.getProperty("buffer." + key);
  143. if(global != null)
  144. {
  145. try
  146. {
  147. return new Integer(global);
  148. }
  149. catch(NumberFormatException nf)
  150. {
  151. return global;
  152. }
  153. }
  154. else
  155. return null;
  156. } //}}}
  157. //{{{ getBooleanProperty() method
  158. /**
  159. * Returns the value of a boolean property.
  160. * @param key The property name
  161. *
  162. * @since jEdit 2.5pre3
  163. */
  164. public boolean getBooleanProperty(String key)
  165. {
  166. Object value = getProperty(key);
  167. if("true".equals(value) || "on".equals(value) || "yes".equals(value))
  168. return true;
  169. else
  170. return false;
  171. } //}}}
  172. //{{{ setProperty() method
  173. /**
  174. * Sets a mode property.
  175. * @param key The property name
  176. * @param value The property value
  177. */
  178. public void setProperty(String key, Object value)
  179. {
  180. props.put(key,value);
  181. } //}}}
  182. //{{{ unsetProperty() method
  183. /**
  184. * Unsets a mode property.
  185. * @param key The property name
  186. * @since jEdit 3.2pre3
  187. */
  188. public void unsetProperty(String key)
  189. {
  190. props.remove(key);
  191. } //}}}
  192. //{{{ setProperties() method
  193. /**
  194. * Should only be called by <code>XModeHandler</code>.
  195. * @since jEdit 4.0pre3
  196. */
  197. public void setProperties(Hashtable props)
  198. {
  199. this.props = props;
  200. } //}}}
  201. //{{{ accept() method
  202. /**
  203. * Returns if the edit mode is suitable for editing the specified
  204. * file. The buffer name and first line is checked against the
  205. * file name and first line globs, respectively.
  206. * @param fileName The buffer's name
  207. * @param firstLine The first line of the buffer
  208. *
  209. * @since jEdit 3.2pre3
  210. */
  211. public boolean accept(String fileName, String firstLine)
  212. {
  213. if(filenameRE != null && filenameRE.isMatch(fileName))
  214. return true;
  215. if(firstlineRE != null && firstlineRE.isMatch(firstLine))
  216. return true;
  217. return false;
  218. } //}}}
  219. //{{{ getName() method
  220. /**
  221. * Returns the internal name of this edit mode.
  222. */
  223. public String getName()
  224. {
  225. return name;
  226. } //}}}
  227. //{{{ toString() method
  228. /**
  229. * Returns a string representation of this edit mode.
  230. */
  231. public String toString()
  232. {
  233. return getClass().getName() + "[" + getName() + "]";
  234. } //}}}
  235. //{{{ Private members
  236. private String name;
  237. private Hashtable props;
  238. private RE firstlineRE;
  239. private RE filenameRE;
  240. private TokenMarker marker;
  241. //}}}
  242. }