/jEdit/tags/jedit-4-0-pre4/org/gjt/sp/jedit/options/StyleOptionPane.java

# · Java · 452 lines · 337 code · 51 blank · 64 comment · 38 complexity · 281f2f268c9f14b358df7abaa3b1dfbd MD5 · raw file

  1. /*
  2. * StyleOptionPane.java - Style 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.EnhancedDialog;
  34. import org.gjt.sp.jedit.*;
  35. //}}}
  36. //{{{ StyleOptionPane class
  37. /**
  38. * Style option pane.
  39. * @author Slava Pestov
  40. * @version $Id: StyleOptionPane.java 3905 2001-11-23 09:08:49Z spestov $
  41. */
  42. public class StyleOptionPane extends AbstractOptionPane
  43. {
  44. public static final EmptyBorder noFocusBorder = new EmptyBorder(1,1,1,1);
  45. //{{{ StyleOptionPane constructor
  46. public StyleOptionPane()
  47. {
  48. super("style");
  49. }
  50. //}}}
  51. //{{{ Protected members
  52. //{{{ _init() method
  53. protected void _init()
  54. {
  55. setLayout(new BorderLayout());
  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. StyleOptionPane.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(13);
  117. addStyleChoice("options.style.comment1Style","view.style.comment1");
  118. addStyleChoice("options.style.comment2Style","view.style.comment2");
  119. addStyleChoice("options.style.literal1Style","view.style.literal1");
  120. addStyleChoice("options.style.literal2Style","view.style.literal2");
  121. addStyleChoice("options.style.labelStyle","view.style.label");
  122. addStyleChoice("options.style.keyword1Style","view.style.keyword1");
  123. addStyleChoice("options.style.keyword2Style","view.style.keyword2");
  124. addStyleChoice("options.style.keyword3Style","view.style.keyword3");
  125. addStyleChoice("options.style.functionStyle","view.style.function");
  126. addStyleChoice("options.style.markupStyle","view.style.markup");
  127. addStyleChoice("options.style.operatorStyle","view.style.operator");
  128. addStyleChoice("options.style.digitStyle","view.style.digit");
  129. addStyleChoice("options.style.invalidStyle","view.style.invalid");
  130. MiscUtilities.quicksort(styleChoices,new MiscUtilities.StringCompare());
  131. } //}}}
  132. //{{{ getColumnCount() method
  133. public int getColumnCount()
  134. {
  135. return 2;
  136. } //}}}
  137. //{{{ getRowCount() method
  138. public int getRowCount()
  139. {
  140. return styleChoices.size();
  141. } //}}}
  142. //{{{ getValueAt() method
  143. public Object getValueAt(int row, int col)
  144. {
  145. StyleChoice ch = (StyleChoice)styleChoices.elementAt(row);
  146. switch(col)
  147. {
  148. case 0:
  149. return ch.label;
  150. case 1:
  151. return ch.style;
  152. default:
  153. return null;
  154. }
  155. } //}}}
  156. //{{{ setValueAt() method
  157. public void setValueAt(Object value, int row, int col)
  158. {
  159. StyleChoice ch = (StyleChoice)styleChoices.elementAt(row);
  160. if(col == 1)
  161. ch.style = (SyntaxStyle)value;
  162. fireTableRowsUpdated(row,row);
  163. } //}}}
  164. //{{{ getColumnName() method
  165. public String getColumnName(int index)
  166. {
  167. switch(index)
  168. {
  169. case 0:
  170. return jEdit.getProperty("options.style.object");
  171. case 1:
  172. return jEdit.getProperty("options.style.style");
  173. default:
  174. return null;
  175. }
  176. } //}}}
  177. //{{{ save() method
  178. public void save()
  179. {
  180. for(int i = 0; i < styleChoices.size(); i++)
  181. {
  182. StyleChoice ch = (StyleChoice)styleChoices
  183. .elementAt(i);
  184. jEdit.setProperty(ch.property,
  185. GUIUtilities.getStyleString(ch.style));
  186. }
  187. } //}}}
  188. //{{{ addStyleChoice() method
  189. private void addStyleChoice(String label, String property)
  190. {
  191. styleChoices.addElement(new StyleChoice(jEdit.getProperty(label),
  192. property,
  193. GUIUtilities.parseStyle(jEdit.getProperty(property),
  194. "Dialog",12)));
  195. } //}}}
  196. //{{{ StyleChoice class
  197. static class StyleChoice
  198. {
  199. String label;
  200. String property;
  201. SyntaxStyle style;
  202. StyleChoice(String label, String property, SyntaxStyle style)
  203. {
  204. this.label = label;
  205. this.property = property;
  206. this.style = style;
  207. }
  208. // for sorting
  209. public String toString()
  210. {
  211. return label;
  212. }
  213. } //}}}
  214. //{{{ StyleRenderer class
  215. static class StyleRenderer extends JLabel
  216. implements TableCellRenderer
  217. {
  218. //{{{ StyleRenderer constructor
  219. public StyleRenderer()
  220. {
  221. setOpaque(true);
  222. setBorder(StyleOptionPane.noFocusBorder);
  223. setText("Hello World");
  224. } //}}}
  225. //{{{ getTableCellRendererComponent() method
  226. public Component getTableCellRendererComponent(
  227. JTable table,
  228. Object value,
  229. boolean isSelected,
  230. boolean cellHasFocus,
  231. int row,
  232. int col)
  233. {
  234. if (value != null)
  235. {
  236. SyntaxStyle style = (SyntaxStyle)value;
  237. setForeground(style.getForegroundColor());
  238. if (style.getBackgroundColor() != null)
  239. setBackground(style.getBackgroundColor());
  240. else
  241. {
  242. // this part sucks
  243. setBackground(jEdit.getColorProperty(
  244. "view.bgColor"));
  245. }
  246. setFont(style.getFont());
  247. }
  248. setBorder((cellHasFocus) ? UIManager.getBorder(
  249. "Table.focusCellHighlightBorder")
  250. : StyleOptionPane.noFocusBorder);
  251. return this;
  252. } //}}}
  253. } //}}}
  254. } //}}}
  255. //{{{ StyleEditor class
  256. class StyleEditor extends EnhancedDialog implements ActionListener
  257. {
  258. //{{{ StyleEditor constructor
  259. StyleEditor(Component comp, SyntaxStyle style)
  260. {
  261. super(JOptionPane.getFrameForComponent(comp),
  262. jEdit.getProperty("style-editor.title"),true);
  263. JPanel content = new JPanel(new BorderLayout());
  264. content.setBorder(new EmptyBorder(12,12,12,12));
  265. setContentPane(content);
  266. GridBagLayout layout = new GridBagLayout();
  267. JPanel panel = new JPanel(layout);
  268. GridBagConstraints cons = new GridBagConstraints();
  269. cons.gridx = cons.gridy = 0;
  270. cons.gridwidth = 2;
  271. cons.gridheight = 1;
  272. cons.fill = GridBagConstraints.BOTH;
  273. cons.weightx = 0.0f;
  274. cons.insets = new Insets(0,0,12,0);
  275. italics = new JCheckBox(jEdit.getProperty("style-editor.italics"));
  276. italics.setSelected(style.getFont().isItalic());
  277. layout.setConstraints(italics,cons);
  278. panel.add(italics);
  279. cons.gridy++;
  280. bold = new JCheckBox(jEdit.getProperty("style-editor.bold"));
  281. bold.setSelected(style.getFont().isBold());
  282. layout.setConstraints(bold,cons);
  283. panel.add(bold);
  284. cons.gridy++;
  285. cons.gridwidth = 1;
  286. Color fg = style.getForegroundColor();
  287. fgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.fgColor"));
  288. fgColorCheckBox.setSelected(fg != null);
  289. fgColorCheckBox.addActionListener(this);
  290. layout.setConstraints(fgColorCheckBox,cons);
  291. panel.add(fgColorCheckBox);
  292. cons.gridx++;
  293. fgColor = new JButton(" ");
  294. fgColor.setEnabled(fg != null);
  295. fgColor.setRequestFocusEnabled(false);
  296. fgColor.addActionListener(this);
  297. fgColor.setMargin(new Insets(0,0,0,0));
  298. if(fg == null)
  299. fgColor.setBackground(jEdit.getColorProperty("view.fgColor"));
  300. else
  301. fgColor.setBackground(fg);
  302. layout.setConstraints(fgColor,cons);
  303. panel.add(fgColor);
  304. cons.gridx = 0;
  305. cons.gridy++;
  306. Color bg = style.getBackgroundColor();
  307. bgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.bgColor"));
  308. bgColorCheckBox.setSelected(bg != null);
  309. bgColorCheckBox.addActionListener(this);
  310. layout.setConstraints(bgColorCheckBox,cons);
  311. panel.add(bgColorCheckBox);
  312. cons.gridx++;
  313. bgColor = new JButton(" ");
  314. bgColor.setEnabled(bg != null);
  315. bgColor.setRequestFocusEnabled(false);
  316. bgColor.addActionListener(this);
  317. bgColor.setMargin(new Insets(0,0,0,0));
  318. if(bg == null)
  319. bgColor.setBackground(jEdit.getColorProperty("view.bgColor"));
  320. else
  321. bgColor.setBackground(bg);
  322. layout.setConstraints(bgColor,cons);
  323. panel.add(bgColor);
  324. content.add(BorderLayout.CENTER,panel);
  325. Box box = new Box(BoxLayout.X_AXIS);
  326. box.add(Box.createGlue());
  327. box.add(ok = new JButton(jEdit.getProperty("common.ok")));
  328. getRootPane().setDefaultButton(ok);
  329. ok.addActionListener(this);
  330. box.add(Box.createHorizontalStrut(6));
  331. box.add(cancel = new JButton(jEdit.getProperty("common.cancel")));
  332. cancel.addActionListener(this);
  333. box.add(Box.createGlue());
  334. content.add(BorderLayout.SOUTH,box);
  335. Dimension screen = getToolkit().getScreenSize();
  336. pack();
  337. setLocationRelativeTo(JOptionPane.getFrameForComponent(comp));
  338. show();
  339. } //}}}
  340. //{{{ actionPerformed() method
  341. public void actionPerformed(ActionEvent evt)
  342. {
  343. Object source = evt.getSource();
  344. if(source == ok)
  345. ok();
  346. else if(source == cancel)
  347. cancel();
  348. else if(source == fgColor || source == bgColor)
  349. {
  350. JButton b = (JButton)source;
  351. Color c = JColorChooser.showDialog(this,
  352. jEdit.getProperty("colorChooser.title"),
  353. b.getBackground());
  354. if(c != null)
  355. b.setBackground(c);
  356. }
  357. else if(source == fgColorCheckBox)
  358. fgColor.setEnabled(fgColorCheckBox.isSelected());
  359. else if(source == bgColorCheckBox)
  360. bgColor.setEnabled(bgColorCheckBox.isSelected());
  361. } //}}}
  362. //{{{ ok() method
  363. public void ok()
  364. {
  365. okClicked = true;
  366. dispose();
  367. } //}}}
  368. //{{{ cancel() method
  369. public void cancel()
  370. {
  371. dispose();
  372. } //}}}
  373. //{{{ getStyle() method
  374. public SyntaxStyle getStyle()
  375. {
  376. if(!okClicked)
  377. return null;
  378. Color foreground = (fgColorCheckBox.isSelected()
  379. ? fgColor.getBackground()
  380. : null);
  381. Color background = (bgColorCheckBox.isSelected()
  382. ? bgColor.getBackground()
  383. : null);
  384. return new SyntaxStyle(foreground,background,
  385. new Font("Dialog",
  386. (italics.isSelected() ? Font.ITALIC : 0)
  387. | (bold.isSelected() ? Font.BOLD : 0),
  388. 12));
  389. } //}}}
  390. //{{{ Private members
  391. private JCheckBox italics;
  392. private JCheckBox bold;
  393. private JCheckBox fgColorCheckBox;
  394. private JButton fgColor;
  395. private JCheckBox bgColorCheckBox;
  396. private JButton bgColor;
  397. private JButton ok;
  398. private JButton cancel;
  399. private boolean okClicked;
  400. //}}}
  401. } //}}}