1 22 23 package org.gjt.sp.jedit.options; 24 25 import javax.swing.border.EmptyBorder ; 27 import javax.swing.event.*; 28 import javax.swing.table.*; 29 import javax.swing.*; 30 import java.awt.event.*; 31 import java.awt.*; 32 import java.util.*; 33 import org.gjt.sp.jedit.gui.RolloverButton; 34 import org.gjt.sp.jedit.*; 35 37 43 public class BrowserColorsOptionPane extends AbstractOptionPane 44 { 45 public BrowserColorsOptionPane() 47 { 48 super("browser.colors"); 49 } 51 53 protected void _init() 55 { 56 setLayout(new BorderLayout()); 57 58 colorsModel = new BrowserColorsModel(); 59 colorsTable = new JTable(colorsModel); 60 colorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 61 colorsTable.getTableHeader().setReorderingAllowed(false); 62 colorsTable.addMouseListener(new MouseHandler()); 63 colorsTable.getSelectionModel().addListSelectionListener( 64 new SelectionHandler()); 65 TableColumnModel tcm = colorsTable.getColumnModel(); 66 tcm.getColumn(1).setCellRenderer(new BrowserColorsModel.ColorRenderer()); 67 Dimension d = colorsTable.getPreferredSize(); 68 d.height = Math.min(d.height,200); 69 JScrollPane scroller = new JScrollPane(colorsTable); 70 scroller.setPreferredSize(d); 71 add(BorderLayout.CENTER,scroller); 72 73 JPanel buttons = new JPanel(); 74 buttons.setBorder(new EmptyBorder (3,0,0,0)); 75 buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS)); 76 ActionHandler actionHandler = new ActionHandler(); 77 add = new RolloverButton(GUIUtilities.loadIcon("Plus.png")); 78 add.setToolTipText(jEdit.getProperty("common.add")); 79 add.addActionListener(actionHandler); 80 buttons.add(add); 81 buttons.add(Box.createHorizontalStrut(6)); 82 remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png")); 83 remove.setToolTipText(jEdit.getProperty("common.remove")); 84 remove.addActionListener(actionHandler); 85 buttons.add(remove); 86 buttons.add(Box.createHorizontalStrut(6)); 87 moveUp = new RolloverButton(GUIUtilities.loadIcon("ArrowU.png")); 88 moveUp.setToolTipText(jEdit.getProperty("common.moveUp")); 89 moveUp.addActionListener(actionHandler); 90 buttons.add(moveUp); 91 buttons.add(Box.createHorizontalStrut(6)); 92 moveDown = new RolloverButton(GUIUtilities.loadIcon("ArrowD.png")); 93 moveDown.setToolTipText(jEdit.getProperty("common.moveDown")); 94 moveDown.addActionListener(actionHandler); 95 buttons.add(moveDown); 96 buttons.add(Box.createGlue()); 97 98 add(BorderLayout.SOUTH,buttons); 99 100 updateEnabled(); 101 } 103 protected void _save() 105 { 106 colorsModel.save(); 107 } 109 111 private BrowserColorsModel colorsModel; 113 private JTable colorsTable; 114 private JButton add; 115 private JButton remove; 116 private JButton moveUp; 117 private JButton moveDown; 118 119 private void updateEnabled() 121 { 122 int selectedRow = colorsTable.getSelectedRow(); 123 remove.setEnabled(selectedRow != -1); 124 moveUp.setEnabled(selectedRow > 0); 125 moveUp.setEnabled(selectedRow != -1 && selectedRow != 126 colorsModel.getRowCount()); 127 } 129 private void setSelectedRow(int row) 131 { 132 colorsTable.getSelectionModel().setSelectionInterval(row,row); 133 colorsTable.scrollRectToVisible(colorsTable.getCellRect(row,0,true)); 134 } 136 138 class SelectionHandler implements ListSelectionListener 140 { 141 public void valueChanged(ListSelectionEvent evt) 142 { 143 updateEnabled(); 144 } 145 } 147 class ActionHandler implements ActionListener 149 { 150 public void actionPerformed(ActionEvent evt) 151 { 152 Object source = evt.getSource(); 153 if(source == add) 154 { 155 colorsModel.add(); 156 } 157 else if(source == remove) 158 { 159 int selectedRow = colorsTable.getSelectedRow(); 160 colorsModel.remove(selectedRow); 161 updateEnabled(); 162 } 163 else if(source == moveUp) 164 { 165 int selectedRow = colorsTable.getSelectedRow(); 166 if(selectedRow != 0) 167 { 168 colorsModel.moveUp(selectedRow); 169 setSelectedRow(selectedRow - 1); 170 } 171 updateEnabled(); 172 } 173 else if(source == moveDown) 174 { 175 int selectedRow = colorsTable.getSelectedRow(); 176 if(selectedRow != colorsTable.getRowCount() - 1) 177 { 178 colorsModel.moveDown(selectedRow); 179 setSelectedRow(selectedRow + 1); 180 } 181 updateEnabled(); 182 } 183 } 184 } 186 class MouseHandler extends MouseAdapter 188 { 189 public void mouseClicked(MouseEvent evt) 190 { 191 Point p = evt.getPoint(); 192 int row = colorsTable.rowAtPoint(p); 193 int column = colorsTable.columnAtPoint(p); 194 if(row == -1 || column != 1) 195 return; 196 197 Color color = JColorChooser.showDialog( 198 BrowserColorsOptionPane.this, 199 jEdit.getProperty("colorChooser.title"), 200 (Color)colorsModel.getValueAt(row,1)); 201 if(color != null) 202 colorsModel.setValueAt(color,row,1); 203 } 204 } } 207 class BrowserColorsModel extends AbstractTableModel 209 { 210 BrowserColorsModel() 212 { 213 entries = new ArrayList(); 214 215 int i = 0; 216 String glob; 217 while((glob = jEdit.getProperty("vfs.browser.colors." + i + ".glob")) != null) 218 { 219 entries.add(new Entry(glob, 220 jEdit.getColorProperty( 221 "vfs.browser.colors." + i + ".color", 222 Color.black))); 223 i++; 224 } 225 } 227 void add() 229 { 230 entries.add(new Entry("",UIManager.getColor("Tree.foreground"))); 231 fireTableRowsInserted(entries.size() - 1,entries.size() - 1); 232 } 234 void remove(int index) 236 { 237 entries.remove(index); 238 fireTableRowsDeleted(entries.size(),entries.size()); 239 } 241 public void moveUp(int index) 243 { 244 Object obj = entries.get(index); 245 entries.remove(index); 246 entries.add(index - 1,obj); 247 fireTableRowsUpdated(index - 1,index); 248 } 250 public void moveDown(int index) 252 { 253 Object obj = entries.get(index); 254 entries.remove(index); 255 entries.add(index + 1,obj); 256 fireTableRowsUpdated(index,index + 1); 257 } 259 void save() 261 { 262 int i; 263 for(i = 0; i < entries.size(); i++) 264 { 265 Entry entry = (Entry)entries.get(i); 266 jEdit.setProperty("vfs.browser.colors." + i + ".glob", 267 entry.glob); 268 jEdit.setColorProperty("vfs.browser.colors." + i + ".color", 269 entry.color); 270 } 271 jEdit.unsetProperty("vfs.browser.colors." + i + ".glob"); 272 jEdit.unsetProperty("vfs.browser.colors." + i + ".color"); 273 } 275 public int getColumnCount() 277 { 278 return 2; 279 } 281 public int getRowCount() 283 { 284 return entries.size(); 285 } 287 public Object getValueAt(int row, int col) 289 { 290 Entry entry = (Entry)entries.get(row); 291 292 switch(col) 293 { 294 case 0: 295 return entry.glob; 296 case 1: 297 return entry.color; 298 default: 299 return null; 300 } 301 } 303 public boolean isCellEditable(int row, int col) 305 { 306 return (col == 0); 307 } 309 public void setValueAt(Object value, int row, int col) 311 { 312 Entry entry = (Entry)entries.get(row); 313 314 if(col == 0) 315 entry.glob = (String )value; 316 else 317 entry.color = (Color)value; 318 319 fireTableRowsUpdated(row,row); 320 } 322 public String getColumnName(int index) 324 { 325 switch(index) 326 { 327 case 0: 328 return jEdit.getProperty("options.browser.colors.glob"); 329 case 1: 330 return jEdit.getProperty("options.browser.colors.color"); 331 default: 332 return null; 333 } 334 } 336 public Class getColumnClass(int col) 338 { 339 switch(col) 340 { 341 case 0: 342 return String .class; 343 case 1: 344 return Color.class; 345 default: 346 throw new InternalError (); 347 } 348 } 350 private ArrayList entries; 351 352 static class Entry 354 { 355 String glob; 356 Color color; 357 358 Entry(String glob, Color color) 359 { 360 this.glob = glob; 361 this.color = color; 362 } 363 } 365 static class ColorRenderer extends JLabel 367 implements TableCellRenderer 368 { 369 public ColorRenderer() 371 { 372 setOpaque(true); 373 setBorder(SyntaxHiliteOptionPane.noFocusBorder); 374 } 376 public Component getTableCellRendererComponent( 378 JTable table, 379 Object value, 380 boolean isSelected, 381 boolean cellHasFocus, 382 int row, 383 int col) 384 { 385 if (isSelected) 386 { 387 setBackground(table.getSelectionBackground()); 388 setForeground(table.getSelectionForeground()); 389 } 390 else 391 { 392 setBackground(table.getBackground()); 393 setForeground(table.getForeground()); 394 } 395 396 if (value != null) 397 setBackground((Color)value); 398 399 setBorder((cellHasFocus) ? UIManager.getBorder( 400 "Table.focusCellHighlightBorder") 401 : SyntaxHiliteOptionPane.noFocusBorder); 402 return this; 403 } } } | Popular Tags |