PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/options/SyntaxHiliteOptionPane.java

#
Java | 417 lines | 296 code | 56 blank | 65 comment | 28 complexity | 06022bdffc15c94c3dc498284e4bf0e6 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. * 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.ColorWellButton;
  34. import org.gjt.sp.jedit.gui.EnhancedDialog;
  35. import org.gjt.sp.jedit.*;
  36. //}}}
  37. //{{{ SyntaxHiliteOptionPane class
  38. /**
  39. * Style option pane.
  40. * @author Slava Pestov
  41. * @version $Id: SyntaxHiliteOptionPane.java 5487 2006-06-23 22:58:12Z 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. protected void _init()
  55. {
  56. setLayout(new BorderLayout(6,6));
  57. add(BorderLayout.CENTER,createStyleTableScroller());
  58. } //}}}
  59. //{{{ _save() method
  60. protected void _save()
  61. {
  62. styleModel.save();
  63. } //}}}
  64. //}}}
  65. //{{{ Private members
  66. private StyleTableModel styleModel;
  67. private JTable styleTable;
  68. //{{{ createStyleTableScroller() method
  69. private JScrollPane createStyleTableScroller()
  70. {
  71. styleModel = createStyleTableModel();
  72. styleTable = new JTable(styleModel);
  73. styleTable.setRowSelectionAllowed(false);
  74. styleTable.setColumnSelectionAllowed(false);
  75. styleTable.setCellSelectionEnabled(false);
  76. styleTable.getTableHeader().setReorderingAllowed(false);
  77. styleTable.addMouseListener(new MouseHandler());
  78. TableColumnModel tcm = styleTable.getColumnModel();
  79. TableColumn styleColumn = tcm.getColumn(1);
  80. styleColumn.setCellRenderer(new StyleTableModel.StyleRenderer());
  81. Dimension d = styleTable.getPreferredSize();
  82. d.height = Math.min(d.height,100);
  83. JScrollPane scroller = new JScrollPane(styleTable);
  84. scroller.setPreferredSize(d);
  85. return scroller;
  86. } //}}}
  87. //{{{ createStyleTableModel() method
  88. private static StyleTableModel createStyleTableModel()
  89. {
  90. return new StyleTableModel();
  91. } //}}}
  92. //}}}
  93. //{{{ MouseHandler class
  94. class MouseHandler extends MouseAdapter
  95. {
  96. public void mouseClicked(MouseEvent evt)
  97. {
  98. int row = styleTable.rowAtPoint(evt.getPoint());
  99. if(row == -1)
  100. return;
  101. SyntaxStyle style = new StyleEditor(
  102. SyntaxHiliteOptionPane.this,
  103. (SyntaxStyle)styleModel.getValueAt(
  104. row,1)).getStyle();
  105. if(style != null)
  106. styleModel.setValueAt(style,row,1);
  107. }
  108. } //}}}
  109. } //}}}
  110. //{{{ StyleTableModel class
  111. class StyleTableModel extends AbstractTableModel
  112. {
  113. private Vector styleChoices;
  114. //{{{ StyleTableModel constructor
  115. StyleTableModel()
  116. {
  117. styleChoices = new Vector(Token.ID_COUNT + 4);
  118. // start at 1 not 0 to skip Token.NULL
  119. for(int i = 1; i < Token.ID_COUNT; i++)
  120. {
  121. String tokenName = Token.tokenToString((byte)i);
  122. addStyleChoice(tokenName,"view.style." + tokenName.toLowerCase());
  123. }
  124. addStyleChoice(jEdit.getProperty("options.syntax.foldLine.1"),
  125. "view.style.foldLine.1");
  126. addStyleChoice(jEdit.getProperty("options.syntax.foldLine.2"),
  127. "view.style.foldLine.2");
  128. addStyleChoice(jEdit.getProperty("options.syntax.foldLine.3"),
  129. "view.style.foldLine.3");
  130. addStyleChoice(jEdit.getProperty("options.syntax.foldLine.0"),
  131. "view.style.foldLine.0");
  132. Collections.sort(styleChoices,new MiscUtilities.StringICaseCompare());
  133. } //}}}
  134. //{{{ getColumnCount() method
  135. public int getColumnCount()
  136. {
  137. return 2;
  138. } //}}}
  139. //{{{ getRowCount() method
  140. public int getRowCount()
  141. {
  142. return styleChoices.size();
  143. } //}}}
  144. //{{{ getValueAt() method
  145. public Object getValueAt(int row, int col)
  146. {
  147. StyleChoice ch = (StyleChoice)styleChoices.elementAt(row);
  148. switch(col)
  149. {
  150. case 0:
  151. return ch.label;
  152. case 1:
  153. return ch.style;
  154. default:
  155. return null;
  156. }
  157. } //}}}
  158. //{{{ setValueAt() method
  159. public void setValueAt(Object value, int row, int col)
  160. {
  161. StyleChoice ch = (StyleChoice)styleChoices.elementAt(row);
  162. if(col == 1)
  163. ch.style = (SyntaxStyle)value;
  164. fireTableRowsUpdated(row,row);
  165. } //}}}
  166. //{{{ getColumnName() method
  167. public String getColumnName(int index)
  168. {
  169. switch(index)
  170. {
  171. case 0:
  172. return jEdit.getProperty("options.syntax.object");
  173. case 1:
  174. return jEdit.getProperty("options.syntax.style");
  175. default:
  176. return null;
  177. }
  178. } //}}}
  179. //{{{ save() method
  180. public void save()
  181. {
  182. for(int i = 0; i < styleChoices.size(); i++)
  183. {
  184. StyleChoice ch = (StyleChoice)styleChoices
  185. .elementAt(i);
  186. jEdit.setProperty(ch.property,
  187. GUIUtilities.getStyleString(ch.style));
  188. }
  189. } //}}}
  190. //{{{ addStyleChoice() method
  191. private void addStyleChoice(String label, String property)
  192. {
  193. styleChoices.addElement(new StyleChoice(label,
  194. property,
  195. GUIUtilities.parseStyle(jEdit.getProperty(property),
  196. "Dialog",12)));
  197. } //}}}
  198. //{{{ StyleChoice class
  199. static class StyleChoice
  200. {
  201. String label;
  202. String property;
  203. SyntaxStyle style;
  204. StyleChoice(String label, String property, SyntaxStyle style)
  205. {
  206. this.label = label;
  207. this.property = property;
  208. this.style = style;
  209. }
  210. // for sorting
  211. public String toString()
  212. {
  213. return label;
  214. }
  215. } //}}}
  216. //{{{ StyleRenderer class
  217. static class StyleRenderer extends JLabel
  218. implements TableCellRenderer
  219. {
  220. //{{{ StyleRenderer constructor
  221. public StyleRenderer()
  222. {
  223. setOpaque(true);
  224. setBorder(SyntaxHiliteOptionPane.noFocusBorder);
  225. setText("Hello World");
  226. } //}}}
  227. //{{{ getTableCellRendererComponent() method
  228. public Component getTableCellRendererComponent(
  229. JTable table,
  230. Object value,
  231. boolean isSelected,
  232. boolean cellHasFocus,
  233. int row,
  234. int col)
  235. {
  236. if (value != null)
  237. {
  238. SyntaxStyle style = (SyntaxStyle)value;
  239. setForeground(style.getForegroundColor());
  240. if (style.getBackgroundColor() != null)
  241. setBackground(style.getBackgroundColor());
  242. else
  243. {
  244. // this part sucks
  245. setBackground(jEdit.getColorProperty(
  246. "view.bgColor"));
  247. }
  248. setFont(style.getFont());
  249. }
  250. setBorder((cellHasFocus) ? UIManager.getBorder(
  251. "Table.focusCellHighlightBorder")
  252. : SyntaxHiliteOptionPane.noFocusBorder);
  253. return this;
  254. } //}}}
  255. } //}}}
  256. } //}}}
  257. //{{{ StyleEditor class
  258. class StyleEditor extends EnhancedDialog implements ActionListener
  259. {
  260. //{{{ StyleEditor constructor
  261. StyleEditor(Component comp, SyntaxStyle style)
  262. {
  263. super(GUIUtilities.getParentDialog(comp),
  264. jEdit.getProperty("style-editor.title"),true);
  265. JPanel content = new JPanel(new BorderLayout(12,12));
  266. content.setBorder(new EmptyBorder(12,12,12,12));
  267. setContentPane(content);
  268. JPanel panel = new JPanel(new GridLayout(4,2,12,12));
  269. italics = new JCheckBox(jEdit.getProperty("style-editor.italics"));
  270. italics.setSelected(style.getFont().isItalic());
  271. panel.add(italics);
  272. panel.add(new JLabel());
  273. bold = new JCheckBox(jEdit.getProperty("style-editor.bold"));
  274. bold.setSelected(style.getFont().isBold());
  275. panel.add(bold);
  276. panel.add(new JLabel());
  277. Color fg = style.getForegroundColor();
  278. fgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.fgColor"));
  279. fgColorCheckBox.setSelected(fg != null);
  280. fgColorCheckBox.addActionListener(this);
  281. panel.add(fgColorCheckBox);
  282. fgColor = new ColorWellButton(fg);
  283. fgColor.setEnabled(fg != null);
  284. panel.add(fgColor);
  285. Color bg = style.getBackgroundColor();
  286. bgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.bgColor"));
  287. bgColorCheckBox.setSelected(bg != null);
  288. bgColorCheckBox.addActionListener(this);
  289. panel.add(bgColorCheckBox);
  290. bgColor = new ColorWellButton(bg);
  291. bgColor.setEnabled(bg != null);
  292. panel.add(bgColor);
  293. content.add(BorderLayout.CENTER,panel);
  294. Box box = new Box(BoxLayout.X_AXIS);
  295. box.add(Box.createGlue());
  296. box.add(ok = new JButton(jEdit.getProperty("common.ok")));
  297. getRootPane().setDefaultButton(ok);
  298. ok.addActionListener(this);
  299. box.add(Box.createHorizontalStrut(6));
  300. box.add(cancel = new JButton(jEdit.getProperty("common.cancel")));
  301. cancel.addActionListener(this);
  302. box.add(Box.createGlue());
  303. content.add(BorderLayout.SOUTH,box);
  304. pack();
  305. setLocationRelativeTo(GUIUtilities.getParentDialog(comp));
  306. setResizable(false);
  307. setVisible(true);
  308. } //}}}
  309. //{{{ actionPerformed() method
  310. public void actionPerformed(ActionEvent evt)
  311. {
  312. Object source = evt.getSource();
  313. if(source == ok)
  314. ok();
  315. else if(source == cancel)
  316. cancel();
  317. else if(source == fgColorCheckBox)
  318. fgColor.setEnabled(fgColorCheckBox.isSelected());
  319. else if(source == bgColorCheckBox)
  320. bgColor.setEnabled(bgColorCheckBox.isSelected());
  321. } //}}}
  322. //{{{ ok() method
  323. public void ok()
  324. {
  325. okClicked = true;
  326. dispose();
  327. } //}}}
  328. //{{{ cancel() method
  329. public void cancel()
  330. {
  331. dispose();
  332. } //}}}
  333. //{{{ getStyle() method
  334. public SyntaxStyle getStyle()
  335. {
  336. if(!okClicked)
  337. return null;
  338. Color foreground = (fgColorCheckBox.isSelected()
  339. ? fgColor.getSelectedColor()
  340. : null);
  341. Color background = (bgColorCheckBox.isSelected()
  342. ? bgColor.getSelectedColor()
  343. : null);
  344. return new SyntaxStyle(foreground,background,
  345. new Font("Dialog",
  346. (italics.isSelected() ? Font.ITALIC : 0)
  347. | (bold.isSelected() ? Font.BOLD : 0),
  348. 12));
  349. } //}}}
  350. //{{{ Private members
  351. private JCheckBox italics;
  352. private JCheckBox bold;
  353. private JCheckBox fgColorCheckBox;
  354. private ColorWellButton fgColor;
  355. private JCheckBox bgColorCheckBox;
  356. private ColorWellButton bgColor;
  357. private JButton ok;
  358. private JButton cancel;
  359. private boolean okClicked;
  360. //}}}
  361. } //}}}