/jEdit/trunk/org/gjt/sp/jedit/options/SyntaxHiliteOptionPane.java

# · Java · 305 lines · 213 code · 35 blank · 57 comment · 14 complexity · 31ec18932b9a974130130ce08428dc95 MD5 · raw file

  1. /*
  2. * SyntaxHiliteOptionPane.java - Syntax highlighting option pane
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000, 2001 Slava Pestov
  7. * Portions 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.options;
  24. //{{{ Imports
  25. import javax.swing.border.EmptyBorder;
  26. import javax.swing.table.*;
  27. import javax.swing.*;
  28. import java.awt.event.*;
  29. import java.awt.*;
  30. import java.util.Vector;
  31. import java.util.Collections;
  32. import org.gjt.sp.jedit.syntax.*;
  33. import org.gjt.sp.jedit.gui.StyleEditor;
  34. import org.gjt.sp.jedit.*;
  35. import org.gjt.sp.util.StandardUtilities;
  36. //}}}
  37. //{{{ SyntaxHiliteOptionPane class
  38. /**
  39. * Style option pane.
  40. * @author Slava Pestov
  41. * @version $Id: SyntaxHiliteOptionPane.java 14127 2008-12-01 10:10:57Z kpouer $
  42. */
  43. public class SyntaxHiliteOptionPane extends AbstractOptionPane
  44. {
  45. public static final EmptyBorder noFocusBorder = new EmptyBorder(1,1,1,1);
  46. //{{{ StyleOptionPane constructor
  47. public SyntaxHiliteOptionPane()
  48. {
  49. super("syntax");
  50. }
  51. //}}}
  52. //{{{ Protected members
  53. //{{{ _init() method
  54. @Override
  55. protected void _init()
  56. {
  57. setLayout(new BorderLayout(6,6));
  58. add(BorderLayout.CENTER,createStyleTableScroller());
  59. } //}}}
  60. //{{{ _save() method
  61. @Override
  62. protected void _save()
  63. {
  64. styleModel.save();
  65. } //}}}
  66. //}}}
  67. //{{{ Private members
  68. private StyleTableModel styleModel;
  69. private JTable styleTable;
  70. //{{{ createStyleTableScroller() method
  71. private JScrollPane createStyleTableScroller()
  72. {
  73. styleModel = createStyleTableModel();
  74. styleTable = new JTable(styleModel);
  75. styleTable.setRowSelectionAllowed(false);
  76. styleTable.setColumnSelectionAllowed(false);
  77. styleTable.setCellSelectionEnabled(false);
  78. styleTable.getTableHeader().setReorderingAllowed(false);
  79. styleTable.addMouseListener(new MouseHandler());
  80. TableColumnModel tcm = styleTable.getColumnModel();
  81. TableColumn styleColumn = tcm.getColumn(1);
  82. styleColumn.setCellRenderer(new StyleTableModel.StyleRenderer());
  83. Dimension d = styleTable.getPreferredSize();
  84. d.height = Math.min(d.height,100);
  85. JScrollPane scroller = new JScrollPane(styleTable);
  86. scroller.setPreferredSize(d);
  87. return scroller;
  88. } //}}}
  89. //{{{ createStyleTableModel() method
  90. private static StyleTableModel createStyleTableModel()
  91. {
  92. return new StyleTableModel();
  93. } //}}}
  94. //}}}
  95. //{{{ MouseHandler class
  96. private class MouseHandler extends MouseAdapter
  97. {
  98. @Override
  99. public void mouseClicked(MouseEvent evt)
  100. {
  101. int row = styleTable.rowAtPoint(evt.getPoint());
  102. if(row == -1)
  103. return;
  104. SyntaxStyle style;
  105. SyntaxStyle current = (SyntaxStyle)styleModel.getValueAt(row,1);
  106. String token = (String) styleModel.getValueAt(row, 0);
  107. JDialog dialog = GUIUtilities.getParentDialog(
  108. SyntaxHiliteOptionPane.this);
  109. if (dialog != null)
  110. style = new StyleEditor(dialog, current, token).getStyle();
  111. else
  112. {
  113. View view = GUIUtilities.getView(SyntaxHiliteOptionPane.this);
  114. style = new StyleEditor(view, current, token).getStyle();
  115. }
  116. if(style != null)
  117. styleModel.setValueAt(style,row,1);
  118. }
  119. } //}}}
  120. //{{{ StyleTableModel class
  121. private static class StyleTableModel extends AbstractTableModel
  122. {
  123. private final java.util.List<StyleChoice> styleChoices;
  124. //{{{ StyleTableModel constructor
  125. StyleTableModel()
  126. {
  127. styleChoices = new Vector<StyleChoice>(Token.ID_COUNT + 4);
  128. // start at 1 not 0 to skip Token.NULL
  129. for(int i = 1; i < Token.ID_COUNT; i++)
  130. {
  131. String tokenName = Token.tokenToString((byte)i);
  132. addStyleChoice(tokenName,"view.style." + tokenName.toLowerCase());
  133. }
  134. addStyleChoice(jEdit.getProperty("options.syntax.foldLine.1"),
  135. "view.style.foldLine.1");
  136. addStyleChoice(jEdit.getProperty("options.syntax.foldLine.2"),
  137. "view.style.foldLine.2");
  138. addStyleChoice(jEdit.getProperty("options.syntax.foldLine.3"),
  139. "view.style.foldLine.3");
  140. addStyleChoice(jEdit.getProperty("options.syntax.foldLine.0"),
  141. "view.style.foldLine.0");
  142. Collections.sort(styleChoices,new StandardUtilities.StringCompare<StyleChoice>(true));
  143. } //}}}
  144. //{{{ getColumnCount() method
  145. public int getColumnCount()
  146. {
  147. return 2;
  148. } //}}}
  149. //{{{ getRowCount() method
  150. public int getRowCount()
  151. {
  152. return styleChoices.size();
  153. } //}}}
  154. //{{{ getValueAt() method
  155. public Object getValueAt(int row, int col)
  156. {
  157. StyleChoice ch = styleChoices.get(row);
  158. switch(col)
  159. {
  160. case 0:
  161. return ch.label;
  162. case 1:
  163. return ch.style;
  164. default:
  165. return null;
  166. }
  167. } //}}}
  168. //{{{ setValueAt() method
  169. @Override
  170. public void setValueAt(Object value, int row, int col)
  171. {
  172. StyleChoice ch = styleChoices.get(row);
  173. if(col == 1)
  174. ch.style = (SyntaxStyle)value;
  175. fireTableRowsUpdated(row,row);
  176. } //}}}
  177. //{{{ getColumnName() method
  178. @Override
  179. public String getColumnName(int index)
  180. {
  181. switch(index)
  182. {
  183. case 0:
  184. return jEdit.getProperty("options.syntax.object");
  185. case 1:
  186. return jEdit.getProperty("options.syntax.style");
  187. default:
  188. return null;
  189. }
  190. } //}}}
  191. //{{{ save() method
  192. public void save()
  193. {
  194. for(int i = 0; i < styleChoices.size(); i++)
  195. {
  196. StyleChoice ch = styleChoices
  197. .get(i);
  198. jEdit.setProperty(ch.property,
  199. GUIUtilities.getStyleString(ch.style));
  200. }
  201. } //}}}
  202. //{{{ addStyleChoice() method
  203. private void addStyleChoice(String label, String property)
  204. {
  205. styleChoices.add(new StyleChoice(label,
  206. property,
  207. GUIUtilities.parseStyle(jEdit.getProperty(property),
  208. "Dialog",12)));
  209. } //}}}
  210. //{{{ StyleChoice class
  211. private static class StyleChoice
  212. {
  213. private String label;
  214. private String property;
  215. private SyntaxStyle style;
  216. StyleChoice(String label, String property, SyntaxStyle style)
  217. {
  218. this.label = label;
  219. this.property = property;
  220. this.style = style;
  221. }
  222. // for sorting
  223. @Override
  224. public String toString()
  225. {
  226. return label;
  227. }
  228. } //}}}
  229. //{{{ StyleRenderer class
  230. static class StyleRenderer extends JLabel
  231. implements TableCellRenderer
  232. {
  233. //{{{ StyleRenderer constructor
  234. StyleRenderer()
  235. {
  236. setOpaque(true);
  237. setBorder(SyntaxHiliteOptionPane.noFocusBorder);
  238. setText("Hello World");
  239. } //}}}
  240. //{{{ getTableCellRendererComponent() method
  241. public Component getTableCellRendererComponent(
  242. JTable table,
  243. Object value,
  244. boolean isSelected,
  245. boolean cellHasFocus,
  246. int row,
  247. int col)
  248. {
  249. if (value != null)
  250. {
  251. SyntaxStyle style = (SyntaxStyle)value;
  252. setForeground(style.getForegroundColor());
  253. if (style.getBackgroundColor() != null)
  254. setBackground(style.getBackgroundColor());
  255. else
  256. {
  257. // this part sucks
  258. setBackground(jEdit.getColorProperty(
  259. "view.bgColor"));
  260. }
  261. setFont(style.getFont());
  262. }
  263. setBorder(cellHasFocus ? UIManager.getBorder(
  264. "Table.focusCellHighlightBorder")
  265. : SyntaxHiliteOptionPane.noFocusBorder);
  266. return this;
  267. } //}}}
  268. } //}}}
  269. } //}}}
  270. } //}}}