PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

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