/jEdit/tags/jedit-4-3-2/org/gjt/sp/util/SyntaxUtilities.java

# · Java · 230 lines · 137 code · 14 blank · 79 comment · 47 complexity · a77f80027ceecc80ec504fc0d8204dbe MD5 · raw file

  1. /*
  2. * SyntaxUtilities.java - Syntax and styles utility utility functions
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2008 Matthieu Casanova, Slava Pestov
  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 org.gjt.sp.util;
  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.syntax.SyntaxStyle;
  29. import org.gjt.sp.jedit.syntax.Token;
  30. import org.gjt.sp.jedit.IPropertyManager;
  31. //}}}
  32. /**
  33. * Syntax utilities that depends on JDK only and syntax package.
  34. *
  35. * @author Matthieu Casanova
  36. * @version $Id: StandardUtilities.java 9871 2007-06-28 16:33:20Z Vampire0 $
  37. * @since 4.3pre13
  38. */
  39. public class SyntaxUtilities
  40. {
  41. public static IPropertyManager propertyManager;
  42. //{{{ getColorHexString() method
  43. /**
  44. * Converts a color object to its hex value. The hex value
  45. * prefixed is with `#', for example `#ff0088'.
  46. * @param c The color object
  47. * @since jEdit 4.3pre13
  48. */
  49. public static String getColorHexString(Color c)
  50. {
  51. String colString = Integer.toHexString(c.getRGB() & 0xffffff);
  52. return "#000000".substring(0,7 - colString.length()).concat(colString);
  53. } //}}}
  54. //{{{ parseColor() method
  55. /**
  56. * @since jEdit 4.3pre13
  57. */
  58. public static Color parseColor(String name, Color defaultColor)
  59. {
  60. if(name == null || name.length() == 0)
  61. return defaultColor;
  62. else if(name.charAt(0) == '#')
  63. {
  64. try
  65. {
  66. return Color.decode(name);
  67. }
  68. catch(NumberFormatException nf)
  69. {
  70. return defaultColor;
  71. }
  72. }
  73. else if("red".equals(name))
  74. return Color.red;
  75. else if("green".equals(name))
  76. return Color.green;
  77. else if("blue".equals(name))
  78. return Color.blue;
  79. else if("yellow".equals(name))
  80. return Color.yellow;
  81. else if("orange".equals(name))
  82. return Color.orange;
  83. else if("white".equals(name))
  84. return Color.white;
  85. else if("lightGray".equals(name))
  86. return Color.lightGray;
  87. else if("gray".equals(name))
  88. return Color.gray;
  89. else if("darkGray".equals(name))
  90. return Color.darkGray;
  91. else if("black".equals(name))
  92. return Color.black;
  93. else if("cyan".equals(name))
  94. return Color.cyan;
  95. else if("magenta".equals(name))
  96. return Color.magenta;
  97. else if("pink".equals(name))
  98. return Color.pink;
  99. else
  100. return defaultColor;
  101. } //}}}
  102. //{{{ parseStyle() method
  103. /**
  104. * Converts a style string to a style object.
  105. * @param str The style string
  106. * @param family Style strings only specify font style, not font family
  107. * @param size Style strings only specify font style, not font family
  108. * @param color If false, the styles will be monochrome
  109. * @param defaultFgColor Default foreground color (if not specified in style string)
  110. * @exception IllegalArgumentException if the style is invalid
  111. * @since jEdit 4.3pre17
  112. */
  113. public static SyntaxStyle parseStyle(String str, String family, int size,
  114. boolean color, Color defaultFgColor)
  115. throws IllegalArgumentException
  116. {
  117. Color fgColor = defaultFgColor;
  118. Color bgColor = null;
  119. boolean italic = false;
  120. boolean bold = false;
  121. StringTokenizer st = new StringTokenizer(str);
  122. while(st.hasMoreTokens())
  123. {
  124. String s = st.nextToken();
  125. if(s.startsWith("color:"))
  126. {
  127. if(color)
  128. fgColor = parseColor(s.substring(6), Color.black);
  129. }
  130. else if(s.startsWith("bgColor:"))
  131. {
  132. if(color)
  133. bgColor = parseColor(s.substring(8), null);
  134. }
  135. else if(s.startsWith("style:"))
  136. {
  137. for(int i = 6; i < s.length(); i++)
  138. {
  139. if(s.charAt(i) == 'i')
  140. italic = true;
  141. else if(s.charAt(i) == 'b')
  142. bold = true;
  143. else
  144. throw new IllegalArgumentException(
  145. "Invalid style: " + s);
  146. }
  147. }
  148. else
  149. throw new IllegalArgumentException(
  150. "Invalid directive: " + s);
  151. }
  152. return new SyntaxStyle(fgColor,bgColor,
  153. new Font(family,
  154. (italic ? Font.ITALIC : 0) | (bold ? Font.BOLD : 0),
  155. size));
  156. } //}}}
  157. //{{{ parseStyle() method
  158. /**
  159. * Converts a style string to a style object.
  160. * @param str The style string
  161. * @param family Style strings only specify font style, not font family
  162. * @param size Style strings only specify font style, not font family
  163. * @param color If false, the styles will be monochrome
  164. * @exception IllegalArgumentException if the style is invalid
  165. * @since jEdit 4.3pre13
  166. */
  167. public static SyntaxStyle parseStyle(String str, String family, int size,
  168. boolean color)
  169. throws IllegalArgumentException
  170. {
  171. return parseStyle(str, family, size, color, Color.black);
  172. } //}}}
  173. //{{{ loadStyles() methods
  174. /**
  175. * Loads the syntax styles from the properties, giving them the specified
  176. * base font family and size.
  177. * @param family The font family
  178. * @param size The font size
  179. * @since jEdit 4.3pre13
  180. */
  181. public static SyntaxStyle[] loadStyles(String family, int size)
  182. {
  183. return loadStyles(family,size,true);
  184. }
  185. /**
  186. * Loads the syntax styles from the properties, giving them the specified
  187. * base font family and size.
  188. * @param family The font family
  189. * @param size The font size
  190. * @param color If false, the styles will be monochrome
  191. * @since jEdit 4.3pre13
  192. */
  193. public static SyntaxStyle[] loadStyles(String family, int size, boolean color)
  194. {
  195. SyntaxStyle[] styles = new SyntaxStyle[Token.ID_COUNT];
  196. // start at 1 not 0 to skip Token.NULL
  197. for(int i = 1; i < styles.length; i++)
  198. {
  199. try
  200. {
  201. String styleName = "view.style."
  202. + Token.tokenToString((byte)i)
  203. .toLowerCase(Locale.ENGLISH);
  204. styles[i] = parseStyle(
  205. propertyManager.getProperty(styleName),
  206. family,size,color);
  207. }
  208. catch(Exception e)
  209. {
  210. Log.log(Log.ERROR,StandardUtilities.class,e);
  211. }
  212. }
  213. return styles;
  214. } //}}}
  215. private SyntaxUtilities(){}
  216. }