PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 345 lines | 249 code | 40 blank | 56 comment | 20 complexity | 28e4b7153a4d12d36994159dce093957 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. * BrowserColorsOptionPane.java - Browser colors options panel
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2002 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.options;
  23. //{{{ Imports
  24. import javax.swing.border.EmptyBorder;
  25. import javax.swing.event.*;
  26. import javax.swing.table.*;
  27. import javax.swing.*;
  28. import java.awt.event.*;
  29. import java.awt.*;
  30. import java.util.*;
  31. import org.gjt.sp.jedit.gui.RolloverButton;
  32. import org.gjt.sp.jedit.*;
  33. //}}}
  34. //{{{ BrowserColorsOptionPane class
  35. /**
  36. * Browser color editor.
  37. * @author Slava Pestov
  38. * @version $Id: BrowserColorsOptionPane.java 4393 2002-12-24 17:35:24Z spestov $
  39. */
  40. public class BrowserColorsOptionPane extends AbstractOptionPane
  41. {
  42. //{{{ BrowserColorsOptionPane constructor
  43. public BrowserColorsOptionPane()
  44. {
  45. super("browser.colors");
  46. } //}}}
  47. //{{{ Protected members
  48. //{{{ _init() method
  49. protected void _init()
  50. {
  51. setLayout(new BorderLayout());
  52. colorsModel = new BrowserColorsModel();
  53. colorsTable = new JTable(colorsModel);
  54. colorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  55. colorsTable.getTableHeader().setReorderingAllowed(false);
  56. colorsTable.addMouseListener(new MouseHandler());
  57. colorsTable.getSelectionModel().addListSelectionListener(
  58. new SelectionHandler());
  59. TableColumnModel tcm = colorsTable.getColumnModel();
  60. tcm.getColumn(1).setCellRenderer(new BrowserColorsModel.ColorRenderer());
  61. Dimension d = colorsTable.getPreferredSize();
  62. d.height = Math.min(d.height,200);
  63. JScrollPane scroller = new JScrollPane(colorsTable);
  64. scroller.setPreferredSize(d);
  65. add(BorderLayout.CENTER,scroller);
  66. JPanel buttons = new JPanel();
  67. buttons.setBorder(new EmptyBorder(3,0,0,0));
  68. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  69. ActionHandler actionHandler = new ActionHandler();
  70. add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
  71. add.setToolTipText(jEdit.getProperty("options.browser.colors.add"));
  72. add.addActionListener(actionHandler);
  73. buttons.add(add);
  74. buttons.add(Box.createHorizontalStrut(6));
  75. remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
  76. remove.setToolTipText(jEdit.getProperty("options.browser.colors.remove"));
  77. remove.addActionListener(actionHandler);
  78. buttons.add(remove);
  79. buttons.add(Box.createGlue());
  80. add(BorderLayout.SOUTH,buttons);
  81. updateEnabled();
  82. } //}}}
  83. //{{{ _save() method
  84. protected void _save()
  85. {
  86. colorsModel.save();
  87. } //}}}
  88. //}}}
  89. //{{{ Private members
  90. private BrowserColorsModel colorsModel;
  91. private JTable colorsTable;
  92. private JButton add;
  93. private JButton remove;
  94. //{{{ updateEnabled() method
  95. private void updateEnabled()
  96. {
  97. int selectedRow = colorsTable.getSelectedRow();
  98. remove.setEnabled(selectedRow != -1);
  99. } //}}}
  100. //}}}
  101. //{{{ SelectionHandler class
  102. class SelectionHandler implements ListSelectionListener
  103. {
  104. public void valueChanged(ListSelectionEvent evt)
  105. {
  106. updateEnabled();
  107. }
  108. } //}}}
  109. //{{{ ActionHandler class
  110. class ActionHandler implements ActionListener
  111. {
  112. public void actionPerformed(ActionEvent evt)
  113. {
  114. Object source = evt.getSource();
  115. if(source == add)
  116. {
  117. colorsModel.add();
  118. }
  119. else if(source == remove)
  120. {
  121. int selectedRow = colorsTable.getSelectedRow();
  122. colorsModel.remove(selectedRow);
  123. updateEnabled();
  124. }
  125. }
  126. } //}}}
  127. //{{{ MouseHandler class
  128. class MouseHandler extends MouseAdapter
  129. {
  130. public void mouseClicked(MouseEvent evt)
  131. {
  132. Point p = evt.getPoint();
  133. int row = colorsTable.rowAtPoint(p);
  134. int column = colorsTable.columnAtPoint(p);
  135. if(row == -1 || column != 1)
  136. return;
  137. Color color = JColorChooser.showDialog(
  138. BrowserColorsOptionPane.this,
  139. jEdit.getProperty("colorChooser.title"),
  140. (Color)colorsModel.getValueAt(row,1));
  141. if(color != null)
  142. colorsModel.setValueAt(color,row,1);
  143. }
  144. } //}}}
  145. } //}}}
  146. //{{{ BrowserColorsModel class
  147. class BrowserColorsModel extends AbstractTableModel
  148. {
  149. Vector entries;
  150. //{{{ BrowserColorsModel constructor
  151. BrowserColorsModel()
  152. {
  153. entries = new Vector();
  154. int i = 0;
  155. String glob;
  156. while((glob = jEdit.getProperty("vfs.browser.colors." + i + ".glob")) != null)
  157. {
  158. entries.addElement(new Entry(glob,
  159. jEdit.getColorProperty(
  160. "vfs.browser.colors." + i + ".color",
  161. Color.black)));
  162. i++;
  163. }
  164. } //}}}
  165. //{{{ add() method
  166. void add()
  167. {
  168. entries.addElement(new Entry("",UIManager.getColor("Tree.foreground")));
  169. fireTableRowsInserted(entries.size() - 1,entries.size() - 1);
  170. } //}}}
  171. //{{{ remove() method
  172. void remove(int index)
  173. {
  174. entries.removeElementAt(index);
  175. fireTableRowsDeleted(entries.size(),entries.size());
  176. } //}}}
  177. //{{{ save() method
  178. void save()
  179. {
  180. int i;
  181. for(i = 0; i < entries.size(); i++)
  182. {
  183. Entry entry = (Entry)entries.elementAt(i);
  184. jEdit.setProperty("vfs.browser.colors." + i + ".glob",
  185. entry.glob);
  186. jEdit.setColorProperty("vfs.browser.colors." + i + ".color",
  187. entry.color);
  188. }
  189. jEdit.unsetProperty("vfs.browser.colors." + i + ".glob");
  190. jEdit.unsetProperty("vfs.browser.colors." + i + ".color");
  191. } //}}}
  192. //{{{ getColumnCount() method
  193. public int getColumnCount()
  194. {
  195. return 2;
  196. } //}}}
  197. //{{{ getRowCount() method
  198. public int getRowCount()
  199. {
  200. return entries.size();
  201. } //}}}
  202. //{{{ getValueAt() method
  203. public Object getValueAt(int row, int col)
  204. {
  205. Entry entry = (Entry)entries.elementAt(row);
  206. switch(col)
  207. {
  208. case 0:
  209. return entry.glob;
  210. case 1:
  211. return entry.color;
  212. default:
  213. return null;
  214. }
  215. } //}}}
  216. //{{{ isCellEditable() method
  217. public boolean isCellEditable(int row, int col)
  218. {
  219. return (col == 0);
  220. } //}}}
  221. //{{{ setValueAt() method
  222. public void setValueAt(Object value, int row, int col)
  223. {
  224. Entry entry = (Entry)entries.elementAt(row);
  225. if(col == 0)
  226. entry.glob = (String)value;
  227. else
  228. entry.color = (Color)value;
  229. fireTableRowsUpdated(row,row);
  230. } //}}}
  231. //{{{ getColumnName() method
  232. public String getColumnName(int index)
  233. {
  234. switch(index)
  235. {
  236. case 0:
  237. return jEdit.getProperty("options.browser.colors.glob");
  238. case 1:
  239. return jEdit.getProperty("options.browser.colors.color");
  240. default:
  241. return null;
  242. }
  243. } //}}}
  244. //{{{ getColumnClass() method
  245. public Class getColumnClass(int col)
  246. {
  247. switch(col)
  248. {
  249. case 0:
  250. return String.class;
  251. case 1:
  252. return Color.class;
  253. default:
  254. throw new InternalError();
  255. }
  256. } //}}}
  257. //{{{ Entry class
  258. static class Entry
  259. {
  260. String glob;
  261. Color color;
  262. Entry(String glob, Color color)
  263. {
  264. this.glob = glob;
  265. this.color = color;
  266. }
  267. } //}}}
  268. //{{{ ColorRenderer class
  269. static class ColorRenderer extends JLabel
  270. implements TableCellRenderer
  271. {
  272. //{{{ ColorRenderer constructor
  273. public ColorRenderer()
  274. {
  275. setOpaque(true);
  276. setBorder(SyntaxHiliteOptionPane.noFocusBorder);
  277. } //}}}
  278. //{{{ getTableCellRendererComponent() method
  279. public Component getTableCellRendererComponent(
  280. JTable table,
  281. Object value,
  282. boolean isSelected,
  283. boolean cellHasFocus,
  284. int row,
  285. int col)
  286. {
  287. if (isSelected)
  288. {
  289. setBackground(table.getSelectionBackground());
  290. setForeground(table.getSelectionForeground());
  291. }
  292. else
  293. {
  294. setBackground(table.getBackground());
  295. setForeground(table.getForeground());
  296. }
  297. if (value != null)
  298. setBackground((Color)value);
  299. setBorder((cellHasFocus) ? UIManager.getBorder(
  300. "Table.focusCellHighlightBorder")
  301. : SyntaxHiliteOptionPane.noFocusBorder);
  302. return this;
  303. } //}}}
  304. } //}}}
  305. } //}}}