PageRenderTime 52ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-0-pre5/org/gjt/sp/jedit/options/ColorOptionPane.java

#
Java | 298 lines | 214 code | 30 blank | 54 comment | 10 complexity | 3268cec68f9fc5b4c0d49fc4dc85903f 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. * ColorOptionPane.java - Color option pane
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000 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.plaf.metal.MetalLookAndFeel;
  28. import javax.swing.table.*;
  29. import javax.swing.*;
  30. import java.awt.event.*;
  31. import java.awt.*;
  32. import java.util.Vector;
  33. import org.gjt.sp.jedit.syntax.SyntaxStyle;
  34. import org.gjt.sp.jedit.gui.EnhancedDialog;
  35. import org.gjt.sp.jedit.*;
  36. //}}}
  37. //{{{ ColorOptionPane class
  38. /**
  39. * Color option pane.
  40. * @author Slava Pestov
  41. * @version $Id: ColorOptionPane.java 3905 2001-11-23 09:08:49Z spestov $
  42. */
  43. public class ColorOptionPane extends AbstractOptionPane
  44. {
  45. public static final EmptyBorder noFocusBorder = new EmptyBorder(1,1,1,1);
  46. //{{{ ColorOptionPane constructor
  47. public ColorOptionPane()
  48. {
  49. super("color");
  50. } //}}}
  51. //{{{ Protected members
  52. //{{{ _init() method
  53. protected void _init()
  54. {
  55. setLayout(new BorderLayout());
  56. add(BorderLayout.CENTER,createColorTableScroller());
  57. } //}}}
  58. //{{{ _save() method
  59. protected void _save()
  60. {
  61. colorModel.save();
  62. } //}}}
  63. //}}}
  64. //{{{ Private members
  65. private ColorTableModel colorModel;
  66. private JTable colorTable;
  67. //{{{ createColorTableScroller() method
  68. private JScrollPane createColorTableScroller()
  69. {
  70. colorModel = createColorTableModel();
  71. colorTable = new JTable(colorModel);
  72. colorTable.setRowSelectionAllowed(false);
  73. colorTable.setColumnSelectionAllowed(false);
  74. colorTable.setCellSelectionEnabled(false);
  75. colorTable.getTableHeader().setReorderingAllowed(false);
  76. colorTable.addMouseListener(new MouseHandler());
  77. TableColumnModel tcm = colorTable.getColumnModel();
  78. TableColumn colorColumn = tcm.getColumn(1);
  79. colorColumn.setCellRenderer(new ColorTableModel.ColorRenderer());
  80. Dimension d = colorTable.getPreferredSize();
  81. d.height = Math.min(d.height,100);
  82. JScrollPane scroller = new JScrollPane(colorTable);
  83. scroller.setPreferredSize(d);
  84. return scroller;
  85. } //}}}
  86. //{{{ createColorTableModel() method
  87. private ColorTableModel createColorTableModel()
  88. {
  89. return new ColorTableModel();
  90. } //}}}
  91. //}}}
  92. //{{{ MouseHandler class
  93. class MouseHandler extends MouseAdapter
  94. {
  95. public void mouseClicked(MouseEvent evt)
  96. {
  97. int row = colorTable.rowAtPoint(evt.getPoint());
  98. if(row == -1)
  99. return;
  100. Color color = JColorChooser.showDialog(
  101. ColorOptionPane.this,
  102. jEdit.getProperty("colorChooser.title"),
  103. (Color)colorModel.getValueAt(row,1));
  104. if(color != null)
  105. colorModel.setValueAt(color,row,1);
  106. }
  107. } //}}}
  108. } //}}}
  109. //{{{ ColorTableModel class
  110. class ColorTableModel extends AbstractTableModel
  111. {
  112. private Vector colorChoices;
  113. //{{{ ColorTableModel constructor
  114. ColorTableModel()
  115. {
  116. colorChoices = new Vector(16);
  117. addColorChoice("options.color.bgColor","view.bgColor");
  118. addColorChoice("options.color.fgColor","view.fgColor");
  119. addColorChoice("options.color.caretColor","view.caretColor");
  120. addColorChoice("options.color.selectionColor",
  121. "view.selectionColor");
  122. addColorChoice("options.color.foldedLineColor",
  123. "view.foldedLineColor");
  124. addColorChoice("options.color.lineHighlightColor",
  125. "view.lineHighlightColor");
  126. addColorChoice("options.color.bracketHighlightColor",
  127. "view.bracketHighlightColor");
  128. addColorChoice("options.color.eolMarkerColor",
  129. "view.eolMarkerColor");
  130. addColorChoice("options.color.wrapGuideColor",
  131. "view.wrapGuideColor");
  132. addColorChoice("options.color.gutterBgColor",
  133. "view.gutter.bgColor");
  134. addColorChoice("options.color.gutterFgColor",
  135. "view.gutter.fgColor");
  136. addColorChoice("options.color.gutterHighlightColor",
  137. "view.gutter.highlightColor");
  138. addColorChoice("options.color.gutterCurrentLineColor",
  139. "view.gutter.currentLineColor");
  140. addColorChoice("options.color.gutterBracketHighlightColor",
  141. "view.bracketHighlightColor");
  142. addColorChoice("options.color.gutterMarkerColor",
  143. "view.gutter.markerColor");
  144. addColorChoice("options.color.gutterFoldColor",
  145. "view.gutter.foldColor");
  146. addColorChoice("options.color.gutterFocusBorderColor",
  147. "view.gutter.focusBorderColor");
  148. addColorChoice("options.color.gutterNoFocusBorderColor",
  149. "view.gutter.noFocusBorderColor");
  150. MiscUtilities.quicksort(colorChoices,new MiscUtilities.StringCompare());
  151. } //}}}
  152. //{{{ getColumnCount() method
  153. public int getColumnCount()
  154. {
  155. return 2;
  156. } //}}}
  157. //{{{ getRowCount() method
  158. public int getRowCount()
  159. {
  160. return colorChoices.size();
  161. } //}}}
  162. //{{{ getValueAt() method
  163. public Object getValueAt(int row, int col)
  164. {
  165. ColorChoice ch = (ColorChoice)colorChoices.elementAt(row);
  166. switch(col)
  167. {
  168. case 0:
  169. return ch.label;
  170. case 1:
  171. return ch.color;
  172. default:
  173. return null;
  174. }
  175. } //}}}
  176. //{{{ setValueAt() method
  177. public void setValueAt(Object value, int row, int col)
  178. {
  179. ColorChoice ch = (ColorChoice)colorChoices.elementAt(row);
  180. if(col == 1)
  181. ch.color = (Color)value;
  182. fireTableRowsUpdated(row,row);
  183. } //}}}
  184. //{{{ getColumnName() method
  185. public String getColumnName(int index)
  186. {
  187. switch(index)
  188. {
  189. case 0:
  190. return jEdit.getProperty("options.color.object");
  191. case 1:
  192. return jEdit.getProperty("options.color.color");
  193. default:
  194. return null;
  195. }
  196. } //}}}
  197. //{{{ save() method
  198. public void save()
  199. {
  200. for(int i = 0; i < colorChoices.size(); i++)
  201. {
  202. ColorChoice ch = (ColorChoice)colorChoices
  203. .elementAt(i);
  204. jEdit.setColorProperty(ch.property,ch.color);
  205. }
  206. } //}}}
  207. //{{{ addColorChoice() method
  208. private void addColorChoice(String label, String property)
  209. {
  210. colorChoices.addElement(new ColorChoice(jEdit.getProperty(label),
  211. property,jEdit.getColorProperty(property)));
  212. } //}}}
  213. //{{{ ColorChoice class
  214. static class ColorChoice
  215. {
  216. String label;
  217. String property;
  218. Color color;
  219. ColorChoice(String label, String property, Color color)
  220. {
  221. this.label = label;
  222. this.property = property;
  223. this.color = color;
  224. }
  225. // for sorting
  226. public String toString()
  227. {
  228. return label;
  229. }
  230. } //}}}
  231. //{{{ ColorRenderer class
  232. static class ColorRenderer extends JLabel
  233. implements TableCellRenderer
  234. {
  235. //{{{ ColorRenderer constructor
  236. public ColorRenderer()
  237. {
  238. setOpaque(true);
  239. setBorder(StyleOptionPane.noFocusBorder);
  240. } //}}}
  241. //{{{ getTableCellRendererComponent() method
  242. public Component getTableCellRendererComponent(
  243. JTable table,
  244. Object value,
  245. boolean isSelected,
  246. boolean cellHasFocus,
  247. int row,
  248. int col)
  249. {
  250. if (isSelected)
  251. {
  252. setBackground(table.getSelectionBackground());
  253. setForeground(table.getSelectionForeground());
  254. }
  255. else
  256. {
  257. setBackground(table.getBackground());
  258. setForeground(table.getForeground());
  259. }
  260. if (value != null)
  261. setBackground((Color)value);
  262. setBorder((cellHasFocus) ? UIManager.getBorder(
  263. "Table.focusCellHighlightBorder")
  264. : StyleOptionPane.noFocusBorder);
  265. return this;
  266. } //}}}
  267. } //}}}
  268. } //}}}