/jEdit/tags/jedit-4-1-pre4/org/gjt/sp/jedit/options/SyntaxHiliteOptionPane.java

# · Java · 444 lines · 324 code · 55 blank · 65 comment · 27 complexity · 6a032199e9806b1cbdd855b344c699f9 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.event.*;
  27. import javax.swing.table.*;
  28. import javax.swing.*;
  29. import java.awt.event.*;
  30. import java.awt.*;
  31. import java.util.Vector;
  32. import org.gjt.sp.jedit.syntax.SyntaxStyle;
  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 4288 2002-08-02 17:31:58Z spestov $
  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. /* Parse fully */
  58. parseFully = new JCheckBox(jEdit.getProperty(
  59. "options.syntax.parseFully"));
  60. parseFully.setSelected(jEdit.getBooleanProperty("parseFully"));
  61. add(BorderLayout.NORTH,parseFully);
  62. add(BorderLayout.CENTER,createStyleTableScroller());
  63. } //}}}
  64. //{{{ _save() method
  65. protected void _save()
  66. {
  67. jEdit.setBooleanProperty("parseFully",parseFully.isSelected());
  68. styleModel.save();
  69. } //}}}
  70. //}}}
  71. //{{{ Private members
  72. private JCheckBox parseFully;
  73. private StyleTableModel styleModel;
  74. private JTable styleTable;
  75. //{{{ createStyleTableScroller() method
  76. private JScrollPane createStyleTableScroller()
  77. {
  78. styleModel = createStyleTableModel();
  79. styleTable = new JTable(styleModel);
  80. styleTable.setRowSelectionAllowed(false);
  81. styleTable.setColumnSelectionAllowed(false);
  82. styleTable.setCellSelectionEnabled(false);
  83. styleTable.getTableHeader().setReorderingAllowed(false);
  84. styleTable.addMouseListener(new MouseHandler());
  85. TableColumnModel tcm = styleTable.getColumnModel();
  86. TableColumn styleColumn = tcm.getColumn(1);
  87. styleColumn.setCellRenderer(new StyleTableModel.StyleRenderer());
  88. Dimension d = styleTable.getPreferredSize();
  89. d.height = Math.min(d.height,100);
  90. JScrollPane scroller = new JScrollPane(styleTable);
  91. scroller.setPreferredSize(d);
  92. return scroller;
  93. } //}}}
  94. //{{{ createStyleTableModel() method
  95. private StyleTableModel createStyleTableModel()
  96. {
  97. return new StyleTableModel();
  98. } //}}}
  99. //}}}
  100. //{{{ MouseHandler class
  101. class MouseHandler extends MouseAdapter
  102. {
  103. public void mouseClicked(MouseEvent evt)
  104. {
  105. int row = styleTable.rowAtPoint(evt.getPoint());
  106. if(row == -1)
  107. return;
  108. SyntaxStyle style = new StyleEditor(
  109. SyntaxHiliteOptionPane.this,
  110. (SyntaxStyle)styleModel.getValueAt(
  111. row,1)).getStyle();
  112. if(style != null)
  113. styleModel.setValueAt(style,row,1);
  114. }
  115. } //}}}
  116. } //}}}
  117. //{{{ StyleTableModel class
  118. class StyleTableModel extends AbstractTableModel
  119. {
  120. private Vector styleChoices;
  121. //{{{ StyleTableModel constructor
  122. StyleTableModel()
  123. {
  124. styleChoices = new Vector(13);
  125. addStyleChoice("options.syntax.comment1Style","view.style.comment1");
  126. addStyleChoice("options.syntax.comment2Style","view.style.comment2");
  127. addStyleChoice("options.syntax.literal1Style","view.style.literal1");
  128. addStyleChoice("options.syntax.literal2Style","view.style.literal2");
  129. addStyleChoice("options.syntax.labelStyle","view.style.label");
  130. addStyleChoice("options.syntax.keyword1Style","view.style.keyword1");
  131. addStyleChoice("options.syntax.keyword2Style","view.style.keyword2");
  132. addStyleChoice("options.syntax.keyword3Style","view.style.keyword3");
  133. addStyleChoice("options.syntax.functionStyle","view.style.function");
  134. addStyleChoice("options.syntax.markupStyle","view.style.markup");
  135. addStyleChoice("options.syntax.operatorStyle","view.style.operator");
  136. addStyleChoice("options.syntax.digitStyle","view.style.digit");
  137. addStyleChoice("options.syntax.invalidStyle","view.style.invalid");
  138. addStyleChoice("options.syntax.foldLine","view.style.foldLine");
  139. MiscUtilities.quicksort(styleChoices,new MiscUtilities.StringCompare());
  140. } //}}}
  141. //{{{ getColumnCount() method
  142. public int getColumnCount()
  143. {
  144. return 2;
  145. } //}}}
  146. //{{{ getRowCount() method
  147. public int getRowCount()
  148. {
  149. return styleChoices.size();
  150. } //}}}
  151. //{{{ getValueAt() method
  152. public Object getValueAt(int row, int col)
  153. {
  154. StyleChoice ch = (StyleChoice)styleChoices.elementAt(row);
  155. switch(col)
  156. {
  157. case 0:
  158. return ch.label;
  159. case 1:
  160. return ch.style;
  161. default:
  162. return null;
  163. }
  164. } //}}}
  165. //{{{ setValueAt() method
  166. public void setValueAt(Object value, int row, int col)
  167. {
  168. StyleChoice ch = (StyleChoice)styleChoices.elementAt(row);
  169. if(col == 1)
  170. ch.style = (SyntaxStyle)value;
  171. fireTableRowsUpdated(row,row);
  172. } //}}}
  173. //{{{ getColumnName() method
  174. public String getColumnName(int index)
  175. {
  176. switch(index)
  177. {
  178. case 0:
  179. return jEdit.getProperty("options.syntax.object");
  180. case 1:
  181. return jEdit.getProperty("options.syntax.style");
  182. default:
  183. return null;
  184. }
  185. } //}}}
  186. //{{{ save() method
  187. public void save()
  188. {
  189. for(int i = 0; i < styleChoices.size(); i++)
  190. {
  191. StyleChoice ch = (StyleChoice)styleChoices
  192. .elementAt(i);
  193. jEdit.setProperty(ch.property,
  194. GUIUtilities.getStyleString(ch.style));
  195. }
  196. } //}}}
  197. //{{{ addStyleChoice() method
  198. private void addStyleChoice(String label, String property)
  199. {
  200. styleChoices.addElement(new StyleChoice(jEdit.getProperty(label),
  201. property,
  202. GUIUtilities.parseStyle(jEdit.getProperty(property),
  203. "Dialog",12)));
  204. } //}}}
  205. //{{{ StyleChoice class
  206. static class StyleChoice
  207. {
  208. String label;
  209. String property;
  210. SyntaxStyle style;
  211. StyleChoice(String label, String property, SyntaxStyle style)
  212. {
  213. this.label = label;
  214. this.property = property;
  215. this.style = style;
  216. }
  217. // for sorting
  218. public String toString()
  219. {
  220. return label;
  221. }
  222. } //}}}
  223. //{{{ StyleRenderer class
  224. static class StyleRenderer extends JLabel
  225. implements TableCellRenderer
  226. {
  227. //{{{ StyleRenderer constructor
  228. public StyleRenderer()
  229. {
  230. setOpaque(true);
  231. setBorder(SyntaxHiliteOptionPane.noFocusBorder);
  232. setText("Hello World");
  233. } //}}}
  234. //{{{ getTableCellRendererComponent() method
  235. public Component getTableCellRendererComponent(
  236. JTable table,
  237. Object value,
  238. boolean isSelected,
  239. boolean cellHasFocus,
  240. int row,
  241. int col)
  242. {
  243. if (value != null)
  244. {
  245. SyntaxStyle style = (SyntaxStyle)value;
  246. setForeground(style.getForegroundColor());
  247. if (style.getBackgroundColor() != null)
  248. setBackground(style.getBackgroundColor());
  249. else
  250. {
  251. // this part sucks
  252. setBackground(jEdit.getColorProperty(
  253. "view.bgColor"));
  254. }
  255. setFont(style.getFont());
  256. }
  257. setBorder((cellHasFocus) ? UIManager.getBorder(
  258. "Table.focusCellHighlightBorder")
  259. : SyntaxHiliteOptionPane.noFocusBorder);
  260. return this;
  261. } //}}}
  262. } //}}}
  263. } //}}}
  264. //{{{ StyleEditor class
  265. class StyleEditor extends EnhancedDialog implements ActionListener
  266. {
  267. //{{{ StyleEditor constructor
  268. StyleEditor(Component comp, SyntaxStyle style)
  269. {
  270. super(JOptionPane.getFrameForComponent(comp),
  271. jEdit.getProperty("style-editor.title"),true);
  272. JPanel content = new JPanel(new BorderLayout(12,12));
  273. content.setBorder(new EmptyBorder(12,12,12,12));
  274. setContentPane(content);
  275. GridBagLayout layout = new GridBagLayout();
  276. JPanel panel = new JPanel(layout);
  277. GridBagConstraints cons = new GridBagConstraints();
  278. cons.gridx = cons.gridy = 0;
  279. cons.gridwidth = 2;
  280. cons.gridheight = 1;
  281. cons.fill = GridBagConstraints.BOTH;
  282. cons.weightx = 0.0f;
  283. italics = new JCheckBox(jEdit.getProperty("style-editor.italics"));
  284. italics.setSelected(style.getFont().isItalic());
  285. layout.setConstraints(italics,cons);
  286. panel.add(italics);
  287. cons.gridy++;
  288. bold = new JCheckBox(jEdit.getProperty("style-editor.bold"));
  289. bold.setSelected(style.getFont().isBold());
  290. layout.setConstraints(bold,cons);
  291. panel.add(bold);
  292. cons.gridy++;
  293. cons.gridwidth = 1;
  294. Color fg = style.getForegroundColor();
  295. fgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.fgColor"));
  296. fgColorCheckBox.setSelected(fg != null);
  297. fgColorCheckBox.addActionListener(this);
  298. fgColorCheckBox.setBorder(new EmptyBorder(0,0,0,12));
  299. layout.setConstraints(fgColorCheckBox,cons);
  300. panel.add(fgColorCheckBox);
  301. cons.gridx++;
  302. fgColor = new ColorWellButton(fg);
  303. fgColor.setEnabled(fg != null);
  304. layout.setConstraints(fgColor,cons);
  305. panel.add(fgColor);
  306. cons.gridx = 0;
  307. cons.gridy++;
  308. Color bg = style.getBackgroundColor();
  309. bgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.bgColor"));
  310. bgColorCheckBox.setSelected(bg != null);
  311. bgColorCheckBox.addActionListener(this);
  312. bgColorCheckBox.setBorder(new EmptyBorder(0,0,0,12));
  313. layout.setConstraints(bgColorCheckBox,cons);
  314. panel.add(bgColorCheckBox);
  315. cons.gridx++;
  316. bgColor = new ColorWellButton(bg);
  317. bgColor.setEnabled(bg != null);
  318. layout.setConstraints(bgColor,cons);
  319. panel.add(bgColor);
  320. content.add(BorderLayout.CENTER,panel);
  321. Box box = new Box(BoxLayout.X_AXIS);
  322. box.add(Box.createGlue());
  323. box.add(ok = new JButton(jEdit.getProperty("common.ok")));
  324. getRootPane().setDefaultButton(ok);
  325. ok.addActionListener(this);
  326. box.add(Box.createHorizontalStrut(6));
  327. box.add(cancel = new JButton(jEdit.getProperty("common.cancel")));
  328. cancel.addActionListener(this);
  329. box.add(Box.createGlue());
  330. content.add(BorderLayout.SOUTH,box);
  331. Dimension screen = getToolkit().getScreenSize();
  332. pack();
  333. setLocationRelativeTo(JOptionPane.getFrameForComponent(comp));
  334. setResizable(false);
  335. show();
  336. } //}}}
  337. //{{{ actionPerformed() method
  338. public void actionPerformed(ActionEvent evt)
  339. {
  340. Object source = evt.getSource();
  341. if(source == ok)
  342. ok();
  343. else if(source == cancel)
  344. cancel();
  345. else if(source == fgColorCheckBox)
  346. fgColor.setEnabled(fgColorCheckBox.isSelected());
  347. else if(source == bgColorCheckBox)
  348. bgColor.setEnabled(bgColorCheckBox.isSelected());
  349. } //}}}
  350. //{{{ ok() method
  351. public void ok()
  352. {
  353. okClicked = true;
  354. dispose();
  355. } //}}}
  356. //{{{ cancel() method
  357. public void cancel()
  358. {
  359. dispose();
  360. } //}}}
  361. //{{{ getStyle() method
  362. public SyntaxStyle getStyle()
  363. {
  364. if(!okClicked)
  365. return null;
  366. Color foreground = (fgColorCheckBox.isSelected()
  367. ? fgColor.getSelectedColor()
  368. : null);
  369. Color background = (bgColorCheckBox.isSelected()
  370. ? bgColor.getSelectedColor()
  371. : null);
  372. return new SyntaxStyle(foreground,background,
  373. new Font("Dialog",
  374. (italics.isSelected() ? Font.ITALIC : 0)
  375. | (bold.isSelected() ? Font.BOLD : 0),
  376. 12));
  377. } //}}}
  378. //{{{ Private members
  379. private JCheckBox italics;
  380. private JCheckBox bold;
  381. private JCheckBox fgColorCheckBox;
  382. private ColorWellButton fgColor;
  383. private JCheckBox bgColorCheckBox;
  384. private ColorWellButton bgColor;
  385. private JButton ok;
  386. private JButton cancel;
  387. private boolean okClicked;
  388. //}}}
  389. } //}}}