/plugins/ModeHighlighting/tags/mh0_1/modelighting/StyleSettings.java

# · Java · 374 lines · 232 code · 39 blank · 103 comment · 34 complexity · 6b7e7c67781786367fd7c4b757ac8402 MD5 · raw file

  1. /*
  2. * StyleSettings.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2011 Evan Wright
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package modelighting;
  23. //{{{ Imports
  24. import java.awt.Color;
  25. import java.awt.Font;
  26. import java.util.Locale;
  27. import java.util.StringTokenizer;
  28. import org.gjt.sp.jedit.jEdit;
  29. import org.gjt.sp.jedit.GUIUtilities;
  30. import org.gjt.sp.jedit.syntax.SyntaxStyle;
  31. import org.gjt.sp.jedit.syntax.Token;
  32. import org.gjt.sp.util.Log;
  33. import org.gjt.sp.util.SyntaxUtilities;
  34. //}}}
  35. public class StyleSettings
  36. {
  37. //{{{ loadStyleSet() methods
  38. /**
  39. * Loads a style set from the properties
  40. * @param mode The mode which owns the style set, or null for a global style set.
  41. * @param name The name of the style set
  42. * @since jEdit 4.5pre1
  43. */
  44. public static StyleSet loadStyleSet(String mode, String name)
  45. {
  46. SyntaxStyle defaultStyle = loadDefaultStyle();
  47. return loadStyleSet(mode, name, defaultStyle);
  48. }
  49. /**
  50. * Loads a style set from the properties, with the given defaults
  51. * @param mode The mode which owns the style set, or null for a global style set.
  52. * @param name The name of the style set
  53. * @param defaultStyle The default style
  54. * @since jEdit 4.5pre1
  55. */
  56. public static StyleSet loadStyleSet(String mode, String name, SyntaxStyle defaultStyle)
  57. {
  58. Font defaultFont = defaultStyle.getFont();
  59. String defaultFamily = defaultFont.getFamily();
  60. int defaultSize = defaultFont.getSize();
  61. Color defaultColor = defaultStyle.getForegroundColor();
  62. SyntaxStyle[] tokenStyles = new SyntaxStyle[Token.ID_COUNT];
  63. for (int i = 1; i < Token.ID_COUNT; i++)
  64. {
  65. String propertyName = getTokenPropertyName(mode, name, (byte)i);
  66. String styleString = jEdit.getProperty(propertyName);
  67. try
  68. {
  69. tokenStyles[i] = SyntaxUtilities.
  70. parseStyle(styleString, defaultFamily, defaultSize, true);
  71. }
  72. catch (Exception e)
  73. {
  74. Log.log(Log.ERROR, StyleSettings.class,
  75. "Error loading style set: " + e);
  76. tokenStyles[i] = defaultStyle;
  77. }
  78. }
  79. tokenStyles[Token.NULL] = defaultStyle;
  80. SyntaxStyle[] foldLineStyles = new SyntaxStyle[StyleSet.FOLD_LEVELS + 1];
  81. for (int level = 0; level <= StyleSet.FOLD_LEVELS; level++)
  82. {
  83. String propertyName = getFoldLinePropertyName(mode, name, level);
  84. String styleString = jEdit.getProperty(propertyName);
  85. try
  86. {
  87. foldLineStyles[level] = SyntaxUtilities.
  88. parseStyle(styleString, defaultFamily, defaultSize, true);
  89. }
  90. catch (Exception e)
  91. {
  92. Log.log(Log.ERROR, StyleSettings.class,
  93. "Error loading style set: " + e);
  94. foldLineStyles[level] = defaultStyle;
  95. }
  96. }
  97. return new StyleSet(mode, name, tokenStyles, foldLineStyles);
  98. } //}}}
  99. //{{{ getActiveStyleSetName() method
  100. /**
  101. * Gets the name of the active style for the given mode.
  102. * @since jEdit 4.5pre1
  103. */
  104. public static String getActiveStyleSetName(String mode)
  105. {
  106. return jEdit.getProperty(getActiveStyleSetPropertyName(mode));
  107. } //}}}
  108. //{{{ setActiveStyleSet() method
  109. public static void setActiveStyleSet(String mode, String styleSet)
  110. {
  111. String propertyName = getActiveStyleSetPropertyName(mode);
  112. if (styleSet == null)
  113. {
  114. jEdit.unsetProperty(propertyName);
  115. }
  116. else
  117. {
  118. jEdit.setProperty(propertyName, styleSet);
  119. }
  120. } //}}}
  121. //{{{ createStyleSet() method
  122. public static void createStyleSet(String mode, String name, StyleSet styleSet)
  123. {
  124. // Add the new style set to the list of style sets for this mode
  125. String propertyName = getStyleSetListPropertyName(mode);
  126. String oldProperty = jEdit.getProperty(propertyName);
  127. if (oldProperty == null || oldProperty.equals(""))
  128. {
  129. jEdit.setProperty(propertyName, name);
  130. }
  131. else
  132. {
  133. jEdit.setProperty(propertyName, oldProperty + "," + name);
  134. }
  135. // Save the new style set to the properties
  136. String styleProperty, value;
  137. for (int i = 0; i <= StyleSet.FOLD_LEVELS; i++)
  138. {
  139. styleProperty = getFoldLinePropertyName(
  140. styleSet.getModeName(),
  141. styleSet.getName(),
  142. i);
  143. value = GUIUtilities.getStyleString(styleSet.getFoldLineStyle(i));
  144. jEdit.setProperty(styleProperty, value);
  145. }
  146. for (int i = 0; i < Token.ID_COUNT; i++)
  147. {
  148. styleProperty = getTokenPropertyName(
  149. styleSet.getModeName(),
  150. styleSet.getName(),
  151. (byte)i);
  152. value = GUIUtilities.getStyleString(styleSet.getTokenStyle((byte)i));
  153. jEdit.setProperty(styleProperty, value);
  154. }
  155. } //}}}
  156. //{{{ deleteStyleSet() method
  157. /**
  158. * Deletes a style set from the properties
  159. */
  160. public static void deleteStyleSet(String mode, String name)
  161. {
  162. // Get the old list of style set names
  163. String propertyName = getStyleSetListPropertyName(mode);
  164. String oldProperty = jEdit.getProperty(propertyName);
  165. String[] styleSetList = oldProperty.split(",");
  166. if (styleSetList.length == 0 || name == null)
  167. {
  168. Log.log(Log.ERROR, StyleSettings.class, "Trying to delete non-existent style set");
  169. return;
  170. }
  171. if (styleSetList.length == 1)
  172. {
  173. // If we're deleting the last style set, just get rid of the property
  174. if (!styleSetList[0].equals(name))
  175. {
  176. Log.log(Log.ERROR, StyleSettings.class, "Trying to delete non-existent style set");
  177. return;
  178. }
  179. jEdit.unsetProperty(propertyName);
  180. }
  181. else
  182. {
  183. // Otherwise, rebuild the comma-delimited list of style set names, excluding the
  184. // one we're deleting
  185. String newProperty;
  186. if (styleSetList[0].equals(name))
  187. {
  188. newProperty = "";
  189. }
  190. else
  191. {
  192. newProperty = styleSetList[0];
  193. }
  194. for (int i = 1; i < styleSetList.length; i++)
  195. {
  196. if (!styleSetList[i].equals(name))
  197. {
  198. if (newProperty.equals(""))
  199. {
  200. newProperty = styleSetList[i];
  201. }
  202. else
  203. {
  204. newProperty = newProperty + "," + styleSetList[i];
  205. }
  206. }
  207. }
  208. jEdit.setProperty(propertyName, newProperty);
  209. }
  210. // If we delete the active style set for this mode, set the active style
  211. // set to global default
  212. String active = getActiveStyleSetName(mode);
  213. if (name.equals(active))
  214. {
  215. String activePropertyName = getActiveStyleSetPropertyName(mode);
  216. jEdit.unsetProperty(activePropertyName);
  217. }
  218. // Also clear all of the other properties for this style set
  219. for (int i = 1; i < Token.ID_COUNT; i++)
  220. {
  221. String tokenPropertyName = getTokenPropertyName(mode, name, (byte)i);
  222. jEdit.unsetProperty(tokenPropertyName);
  223. }
  224. for (int i = 0; i <= StyleSet.FOLD_LEVELS; i++)
  225. {
  226. String foldPropertyName = getFoldLinePropertyName(mode, name, i);
  227. jEdit.unsetProperty(foldPropertyName);
  228. }
  229. } //}}}
  230. //{{{ setTokenStyle() method
  231. public static void setTokenStyle(String mode, String name, byte tokenType, SyntaxStyle style)
  232. {
  233. String propertyName = getTokenPropertyName(mode, name, tokenType);
  234. jEdit.setProperty(propertyName, GUIUtilities.getStyleString(style));
  235. } //}}}
  236. //{{{ setFoldLineStyle() method
  237. public static void setFoldLineStyle(String mode, String name, int level, SyntaxStyle style)
  238. {
  239. String propertyName = getFoldLinePropertyName(mode, name, level);
  240. jEdit.setProperty(propertyName, GUIUtilities.getStyleString(style));
  241. } //}}}
  242. //{{{ loadStyleSetNames() method
  243. /**
  244. * Loads the names of the style sets available for the given mode, or the names of
  245. * the global style sets if the <code>mode</code> parameter is null
  246. * @param mode The mode whose style set names to load
  247. * @since jEdit 4.5pre1
  248. */
  249. public static String[] loadStyleSetNames(String mode)
  250. {
  251. String styleSetList = jEdit.getProperty(
  252. getStyleSetListPropertyName(mode));
  253. if (styleSetList == null || styleSetList.trim().equals(""))
  254. {
  255. return new String[] {};
  256. }
  257. else
  258. {
  259. // The style-set-list property is a comma-delimited list of style set names
  260. return styleSetList.split(",");
  261. }
  262. } //}}}
  263. //{{{ loadDefaultStyle() method
  264. /**
  265. * Loads the default style from the properties.
  266. * @since jEdit 4.5pre1
  267. */
  268. public static SyntaxStyle loadDefaultStyle()
  269. {
  270. Color defaultColor = jEdit.getColorProperty("view.fgColor", Color.black);
  271. Font defaultFont = jEdit.getFontProperty("view.font");
  272. return new SyntaxStyle(defaultColor, null, defaultFont);
  273. } //}}}
  274. //{{{ Private members
  275. private static final String PREFIX = "mode-highlighting.";
  276. //{{{ getTokenPropertyName() method
  277. /**
  278. * Gets the property name for the a given token type in a given style set.
  279. * @param mode The mode which owns the style set, or null for a global style set
  280. * @param name The name of the style set
  281. * @param tokenType The type of token - one of the constants defined in the {@link Token}
  282. * class
  283. * @since jEdit 4.5pre1
  284. */
  285. private static String getTokenPropertyName(String mode, String name, byte tokenType)
  286. {
  287. if (name.indexOf("global/") == 0)
  288. {
  289. mode = null;
  290. }
  291. String tokenName = Token.tokenToString(tokenType).toLowerCase(Locale.ENGLISH);
  292. String modeName = (mode == null) ? "all" : mode;
  293. return PREFIX + modeName + ".style-sets." + name + "." + tokenName;
  294. } //}}}
  295. //{{{ getFoldLinePropertyName() method
  296. /**
  297. * Gets the property name associated to a given fold level in a given style set
  298. * @param mode The mode which owns the style set, or null for a global style set
  299. * @param name The name of the style set
  300. * @param level The fold level - must be in the range 0 to StyleSet.FOLD_LEVELS
  301. * @since jEdit 4.5pre1
  302. */
  303. private static String getFoldLinePropertyName(String mode, String name, int level)
  304. {
  305. if(name.indexOf("global/") == 0)
  306. {
  307. mode = null;
  308. }
  309. String modeName = (mode == null) ? "all" : mode;
  310. return PREFIX + modeName + ".style-sets." + name + ".foldLine." + level;
  311. } //}}}
  312. //{{{ getActiveStyleSetPropertyName() method
  313. /**
  314. * Gets the property name for the active style of a given mode
  315. * @since jEdit 4.5pre1
  316. */
  317. private static String getActiveStyleSetPropertyName(String mode)
  318. {
  319. String modeName = (mode == null) ? "all" : mode;
  320. return PREFIX + modeName + ".active-style";
  321. } //}}}
  322. //{{{ getStyleSetListPropertyName()
  323. /**
  324. * Get the property name associated to the list of style sets for the given mode, or for the
  325. * global style sets if the <code>mode</code> parameter is null
  326. * @param mode The mode whose style list property name to look up
  327. * @since jEdit 4.5pre1
  328. */
  329. private static String getStyleSetListPropertyName(String mode)
  330. {
  331. String modeName = (mode == null) ? "all" : mode;
  332. return PREFIX + modeName + ".style-set-list";
  333. } //}}}
  334. //}}}
  335. }