PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms 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
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 java.util.Hashtable;
  26. import java.util.regex.Pattern;
  27. import java.util.regex.PatternSyntaxException;
  28. import org.gjt.sp.jedit.syntax.TokenMarker;
  29. import org.gjt.sp.util.Log;
  30. //}}}
  31. /**
  32. * An edit mode defines specific settings for editing some type of file.
  33. * One instance of this class is created for each supported edit mode.
  34. *
  35. * @author Slava Pestov
  36. * @version $Id: Mode.java 5443 2006-06-18 18:51:40Z vanza $
  37. */
  38. public class Mode
  39. {
  40. //{{{ Mode constructor
  41. /**
  42. * Creates a new edit mode.
  43. *
  44. * @param name The name used in mode listings and to query mode
  45. * properties
  46. * @see #getProperty(String)
  47. */
  48. public Mode(String name)
  49. {
  50. this.name = name;
  51. props = new Hashtable();
  52. } //}}}
  53. //{{{ init() method
  54. /**
  55. * Initializes the edit mode. Should be called after all properties
  56. * are loaded and set.
  57. */
  58. public void init()
  59. {
  60. try
  61. {
  62. String filenameGlob = (String)getProperty("filenameGlob");
  63. if(filenameGlob != null && filenameGlob.length() != 0)
  64. {
  65. filenameRE = Pattern.compile(MiscUtilities.globToRE(filenameGlob),
  66. Pattern.CASE_INSENSITIVE);
  67. }
  68. String firstlineGlob = (String)getProperty("firstlineGlob");
  69. if(firstlineGlob != null && firstlineGlob.length() != 0)
  70. {
  71. firstlineRE = Pattern.compile(MiscUtilities.globToRE(firstlineGlob),
  72. Pattern.CASE_INSENSITIVE);
  73. }
  74. }
  75. catch(PatternSyntaxException re)
  76. {
  77. Log.log(Log.ERROR,this,"Invalid filename/firstline"
  78. + " globs in mode " + name);
  79. Log.log(Log.ERROR,this,re);
  80. }
  81. // Fix for this bug:
  82. // -- Put a mode into the user dir with the same name as one
  83. // on the system dir.
  84. // -- Reload edit modes.
  85. // -- Old mode from system dir still used for highlighting
  86. // until jEdit restart.
  87. marker = null;
  88. } //}}}
  89. //{{{ getTokenMarker() method
  90. /**
  91. * Returns the token marker for this mode.
  92. */
  93. public TokenMarker getTokenMarker()
  94. {
  95. loadIfNecessary();
  96. return marker;
  97. } //}}}
  98. //{{{ setTokenMarker() method
  99. /**
  100. * Sets the token marker for this mode.
  101. * @param marker The new token marker
  102. */
  103. public void setTokenMarker(TokenMarker marker)
  104. {
  105. this.marker = marker;
  106. } //}}}
  107. //{{{ loadIfNecessary() method
  108. /**
  109. * Loads the mode from disk if it hasn't been loaded already.
  110. * @since jEdit 2.5pre3
  111. */
  112. public void loadIfNecessary()
  113. {
  114. if(marker == null)
  115. jEdit.loadMode(this);
  116. } //}}}
  117. //{{{ getProperty() method
  118. /**
  119. * Returns a mode property.
  120. * @param key The property name
  121. *
  122. * @since jEdit 2.2pre1
  123. */
  124. public Object getProperty(String key)
  125. {
  126. String prefix = "mode." + name + ".";
  127. //if(jEdit.getBooleanProperty(prefix + "customSettings"))
  128. //{
  129. String property = jEdit.getProperty(prefix + key);
  130. if(property != null)
  131. {
  132. Object value;
  133. try
  134. {
  135. value = new Integer(property);
  136. }
  137. catch(NumberFormatException nf)
  138. {
  139. value = property;
  140. }
  141. return value;
  142. }
  143. //}
  144. Object value = props.get(key);
  145. if(value != null)
  146. return value;
  147. String global = jEdit.getProperty("buffer." + key);
  148. if(global != null)
  149. {
  150. try
  151. {
  152. return new Integer(global);
  153. }
  154. catch(NumberFormatException nf)
  155. {
  156. return global;
  157. }
  158. }
  159. else
  160. return null;
  161. } //}}}
  162. //{{{ getBooleanProperty() method
  163. /**
  164. * Returns the value of a boolean property.
  165. * @param key The property name
  166. *
  167. * @since jEdit 2.5pre3
  168. */
  169. public boolean getBooleanProperty(String key)
  170. {
  171. Object value = getProperty(key);
  172. if("true".equals(value) || "on".equals(value) || "yes".equals(value))
  173. return true;
  174. else
  175. return false;
  176. } //}}}
  177. //{{{ setProperty() method
  178. /**
  179. * Sets a mode property.
  180. * @param key The property name
  181. * @param value The property value
  182. */
  183. public void setProperty(String key, Object value)
  184. {
  185. props.put(key,value);
  186. } //}}}
  187. //{{{ unsetProperty() method
  188. /**
  189. * Unsets a mode property.
  190. * @param key The property name
  191. * @since jEdit 3.2pre3
  192. */
  193. public void unsetProperty(String key)
  194. {
  195. props.remove(key);
  196. } //}}}
  197. //{{{ setProperties() method
  198. /**
  199. * Should only be called by <code>XModeHandler</code>.
  200. * @since jEdit 4.0pre3
  201. */
  202. public void setProperties(Hashtable props)
  203. {
  204. if(props == null)
  205. props = new Hashtable();
  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.matcher(fileName).matches())
  233. return true;
  234. if(firstlineRE != null && firstlineRE.matcher(firstLine).matches())
  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 name;
  253. } //}}}
  254. //{{{ Private members
  255. private String name;
  256. private Hashtable props;
  257. private Pattern firstlineRE;
  258. private Pattern filenameRE;
  259. private TokenMarker marker;
  260. //}}}
  261. }