PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

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

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