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