PageRenderTime 23ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/options/BrowserColorsOptionPane.java

#
Java | 343 lines | 246 code | 41 blank | 56 comment | 20 complexity | 92820a63f01ac78191275c048e7276e9 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 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.*;
  32. //}}}
  33. //{{{ BrowserColorsOptionPane class
  34. /**
  35. * Browser color editor.
  36. * @author Slava Pestov
  37. * @version $Id: BrowserColorsOptionPane.java 4288 2002-08-02 17:31:58Z spestov $
  38. */
  39. public class BrowserColorsOptionPane extends AbstractOptionPane
  40. {
  41. //{{{ BrowserColorsOptionPane constructor
  42. public BrowserColorsOptionPane()
  43. {
  44. super("browser.colors");
  45. } //}}}
  46. //{{{ Protected members
  47. //{{{ _init() method
  48. protected void _init()
  49. {
  50. setLayout(new BorderLayout());
  51. colorsModel = new BrowserColorsModel();
  52. colorsTable = new JTable(colorsModel);
  53. colorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  54. colorsTable.getTableHeader().setReorderingAllowed(false);
  55. colorsTable.addMouseListener(new MouseHandler());
  56. colorsTable.getSelectionModel().addListSelectionListener(
  57. new SelectionHandler());
  58. TableColumnModel tcm = colorsTable.getColumnModel();
  59. tcm.getColumn(1).setCellRenderer(new BrowserColorsModel.ColorRenderer());
  60. Dimension d = colorsTable.getPreferredSize();
  61. d.height = Math.min(d.height,200);
  62. JScrollPane scroller = new JScrollPane(colorsTable);
  63. scroller.setPreferredSize(d);
  64. add(BorderLayout.CENTER,scroller);
  65. JPanel buttons = new JPanel();
  66. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  67. buttons.setBorder(new EmptyBorder(6,0,0,0));
  68. buttons.add(Box.createGlue());
  69. add = new JButton(jEdit.getProperty("options.browser.colors.add"));
  70. add.addActionListener(new ActionHandler());
  71. buttons.add(add);
  72. buttons.add(Box.createHorizontalStrut(6));
  73. remove = new JButton(jEdit.getProperty("options.browser.colors.remove"));
  74. remove.addActionListener(new ActionHandler());
  75. buttons.add(remove);
  76. buttons.add(Box.createGlue());
  77. add(BorderLayout.SOUTH,buttons);
  78. updateEnabled();
  79. } //}}}
  80. //{{{ _save() method
  81. protected void _save()
  82. {
  83. colorsModel.save();
  84. } //}}}
  85. //}}}
  86. //{{{ Private members
  87. private BrowserColorsModel colorsModel;
  88. private JTable colorsTable;
  89. private JButton add;
  90. private JButton remove;
  91. //{{{ updateEnabled() method
  92. private void updateEnabled()
  93. {
  94. int selectedRow = colorsTable.getSelectedRow();
  95. remove.setEnabled(selectedRow != -1);
  96. } //}}}
  97. //}}}
  98. //{{{ SelectionHandler class
  99. class SelectionHandler implements ListSelectionListener
  100. {
  101. public void valueChanged(ListSelectionEvent evt)
  102. {
  103. updateEnabled();
  104. }
  105. } //}}}
  106. //{{{ ActionHandler class
  107. class ActionHandler implements ActionListener
  108. {
  109. public void actionPerformed(ActionEvent evt)
  110. {
  111. Object source = evt.getSource();
  112. if(source == add)
  113. {
  114. colorsModel.add();
  115. }
  116. else if(source == remove)
  117. {
  118. int selectedRow = colorsTable.getSelectedRow();
  119. colorsModel.remove(selectedRow);
  120. updateEnabled();
  121. }
  122. }
  123. } //}}}
  124. //{{{ MouseHandler class
  125. class MouseHandler extends MouseAdapter
  126. {
  127. public void mouseClicked(MouseEvent evt)
  128. {
  129. Point p = evt.getPoint();
  130. int row = colorsTable.rowAtPoint(p);
  131. int column = colorsTable.columnAtPoint(p);
  132. if(row == -1 || column != 1)
  133. return;
  134. Color color = JColorChooser.showDialog(
  135. BrowserColorsOptionPane.this,
  136. jEdit.getProperty("colorChooser.title"),
  137. (Color)colorsModel.getValueAt(row,1));
  138. if(color != null)
  139. colorsModel.setValueAt(color,row,1);
  140. }
  141. } //}}}
  142. } //}}}
  143. //{{{ BrowserColorsModel class
  144. class BrowserColorsModel extends AbstractTableModel
  145. {
  146. Vector entries;
  147. //{{{ BrowserColorsModel constructor
  148. BrowserColorsModel()
  149. {
  150. entries = new Vector();
  151. int i = 0;
  152. String glob;
  153. while((glob = jEdit.getProperty("vfs.browser.colors." + i + ".glob")) != null)
  154. {
  155. entries.addElement(new Entry(glob,
  156. jEdit.getColorProperty(
  157. "vfs.browser.colors." + i + ".color",
  158. Color.black)));
  159. i++;
  160. }
  161. } //}}}
  162. //{{{ add() method
  163. void add()
  164. {
  165. entries.addElement(new Entry("",UIManager.getColor("Tree.foreground")));
  166. fireTableRowsInserted(entries.size() - 1,entries.size() - 1);
  167. } //}}}
  168. //{{{ remove() method
  169. void remove(int index)
  170. {
  171. entries.removeElementAt(index);
  172. fireTableRowsDeleted(entries.size(),entries.size());
  173. } //}}}
  174. //{{{ save() method
  175. void save()
  176. {
  177. int i;
  178. for(i = 0; i < entries.size(); i++)
  179. {
  180. Entry entry = (Entry)entries.elementAt(i);
  181. jEdit.setProperty("vfs.browser.colors." + i + ".glob",
  182. entry.glob);
  183. jEdit.setColorProperty("vfs.browser.colors." + i + ".color",
  184. entry.color);
  185. }
  186. jEdit.unsetProperty("vfs.browser.colors." + i + ".glob");
  187. jEdit.unsetProperty("vfs.browser.colors." + i + ".color");
  188. } //}}}
  189. //{{{ getColumnCount() method
  190. public int getColumnCount()
  191. {
  192. return 2;
  193. } //}}}
  194. //{{{ getRowCount() method
  195. public int getRowCount()
  196. {
  197. return entries.size();
  198. } //}}}
  199. //{{{ getValueAt() method
  200. public Object getValueAt(int row, int col)
  201. {
  202. Entry entry = (Entry)entries.elementAt(row);
  203. switch(col)
  204. {
  205. case 0:
  206. return entry.glob;
  207. case 1:
  208. return entry.color;
  209. default:
  210. return null;
  211. }
  212. } //}}}
  213. //{{{ isCellEditable() method
  214. public boolean isCellEditable(int row, int col)
  215. {
  216. return (col == 0);
  217. } //}}}
  218. //{{{ setValueAt() method
  219. public void setValueAt(Object value, int row, int col)
  220. {
  221. Entry entry = (Entry)entries.elementAt(row);
  222. if(col == 0)
  223. entry.glob = (String)value;
  224. else
  225. entry.color = (Color)value;
  226. fireTableRowsUpdated(row,row);
  227. } //}}}
  228. //{{{ getColumnName() method
  229. public String getColumnName(int index)
  230. {
  231. switch(index)
  232. {
  233. case 0:
  234. return jEdit.getProperty("options.browser.colors.glob");
  235. case 1:
  236. return jEdit.getProperty("options.browser.colors.color");
  237. default:
  238. return null;
  239. }
  240. } //}}}
  241. //{{{ getColumnClass() method
  242. public Class getColumnClass(int col)
  243. {
  244. switch(col)
  245. {
  246. case 0:
  247. return String.class;
  248. case 1:
  249. return Color.class;
  250. default:
  251. throw new InternalError();
  252. }
  253. } //}}}
  254. //{{{ Entry class
  255. static class Entry
  256. {
  257. String glob;
  258. Color color;
  259. Entry(String glob, Color color)
  260. {
  261. this.glob = glob;
  262. this.color = color;
  263. }
  264. } //}}}
  265. //{{{ ColorRenderer class
  266. static class ColorRenderer extends JLabel
  267. implements TableCellRenderer
  268. {
  269. //{{{ ColorRenderer constructor
  270. public ColorRenderer()
  271. {
  272. setOpaque(true);
  273. setBorder(SyntaxHiliteOptionPane.noFocusBorder);
  274. } //}}}
  275. //{{{ getTableCellRendererComponent() method
  276. public Component getTableCellRendererComponent(
  277. JTable table,
  278. Object value,
  279. boolean isSelected,
  280. boolean cellHasFocus,
  281. int row,
  282. int col)
  283. {
  284. if (isSelected)
  285. {
  286. setBackground(table.getSelectionBackground());
  287. setForeground(table.getSelectionForeground());
  288. }
  289. else
  290. {
  291. setBackground(table.getBackground());
  292. setForeground(table.getForeground());
  293. }
  294. if (value != null)
  295. setBackground((Color)value);
  296. setBorder((cellHasFocus) ? UIManager.getBorder(
  297. "Table.focusCellHighlightBorder")
  298. : SyntaxHiliteOptionPane.noFocusBorder);
  299. return this;
  300. } //}}}
  301. } //}}}
  302. } //}}}