1 23 24 package org.gjt.sp.jedit.options; 25 26 import javax.swing.border.EmptyBorder ; 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 java.util.Collections ; 34 35 import org.gjt.sp.jedit.syntax.*; 36 import org.gjt.sp.jedit.gui.ColorWellButton; 37 import org.gjt.sp.jedit.gui.EnhancedDialog; 38 import org.gjt.sp.jedit.*; 39 41 47 public class SyntaxHiliteOptionPane extends AbstractOptionPane 48 { 49 public static final EmptyBorder noFocusBorder = new EmptyBorder (1,1,1,1); 50 51 public SyntaxHiliteOptionPane() 53 { 54 super("syntax"); 55 } 56 58 60 protected void _init() 62 { 63 setLayout(new BorderLayout(6,6)); 64 65 add(BorderLayout.CENTER,createStyleTableScroller()); 66 } 68 protected void _save() 70 { 71 styleModel.save(); 72 } 74 76 private StyleTableModel styleModel; 78 private JTable styleTable; 79 80 private JScrollPane createStyleTableScroller() 82 { 83 styleModel = createStyleTableModel(); 84 styleTable = new JTable(styleModel); 85 styleTable.setRowSelectionAllowed(false); 86 styleTable.setColumnSelectionAllowed(false); 87 styleTable.setCellSelectionEnabled(false); 88 styleTable.getTableHeader().setReorderingAllowed(false); 89 styleTable.addMouseListener(new MouseHandler()); 90 TableColumnModel tcm = styleTable.getColumnModel(); 91 TableColumn styleColumn = tcm.getColumn(1); 92 styleColumn.setCellRenderer(new StyleTableModel.StyleRenderer()); 93 Dimension d = styleTable.getPreferredSize(); 94 d.height = Math.min(d.height,100); 95 JScrollPane scroller = new JScrollPane(styleTable); 96 scroller.setPreferredSize(d); 97 return scroller; 98 } 100 private static StyleTableModel createStyleTableModel() 102 { 103 return new StyleTableModel(); 104 } 106 108 class MouseHandler extends MouseAdapter 110 { 111 public void mouseClicked(MouseEvent evt) 112 { 113 int row = styleTable.rowAtPoint(evt.getPoint()); 114 if(row == -1) 115 return; 116 117 SyntaxStyle style = new StyleEditor( 118 SyntaxHiliteOptionPane.this, 119 (SyntaxStyle)styleModel.getValueAt( 120 row,1)).getStyle(); 121 if(style != null) 122 styleModel.setValueAt(style,row,1); 123 } 124 } } 127 class StyleTableModel extends AbstractTableModel 129 { 130 private Vector styleChoices; 131 132 StyleTableModel() 134 { 135 styleChoices = new Vector (Token.ID_COUNT + 4); 136 for(int i = 1; i < Token.ID_COUNT; i++) 138 { 139 String tokenName = Token.tokenToString((byte)i); 140 addStyleChoice(tokenName,"view.style." + tokenName.toLowerCase()); 141 } 142 143 addStyleChoice(jEdit.getProperty("options.syntax.foldLine.1"), 144 "view.style.foldLine.1"); 145 addStyleChoice(jEdit.getProperty("options.syntax.foldLine.2"), 146 "view.style.foldLine.2"); 147 addStyleChoice(jEdit.getProperty("options.syntax.foldLine.3"), 148 "view.style.foldLine.3"); 149 addStyleChoice(jEdit.getProperty("options.syntax.foldLine.0"), 150 "view.style.foldLine.0"); 151 152 Collections.sort(styleChoices,new MiscUtilities.StringICaseCompare()); 153 } 155 public int getColumnCount() 157 { 158 return 2; 159 } 161 public int getRowCount() 163 { 164 return styleChoices.size(); 165 } 167 public Object getValueAt(int row, int col) 169 { 170 StyleChoice ch = (StyleChoice)styleChoices.elementAt(row); 171 switch(col) 172 { 173 case 0: 174 return ch.label; 175 case 1: 176 return ch.style; 177 default: 178 return null; 179 } 180 } 182 public void setValueAt(Object value, int row, int col) 184 { 185 StyleChoice ch = (StyleChoice)styleChoices.elementAt(row); 186 if(col == 1) 187 ch.style = (SyntaxStyle)value; 188 fireTableRowsUpdated(row,row); 189 } 191 public String getColumnName(int index) 193 { 194 switch(index) 195 { 196 case 0: 197 return jEdit.getProperty("options.syntax.object"); 198 case 1: 199 return jEdit.getProperty("options.syntax.style"); 200 default: 201 return null; 202 } 203 } 205 public void save() 207 { 208 for(int i = 0; i < styleChoices.size(); i++) 209 { 210 StyleChoice ch = (StyleChoice)styleChoices 211 .elementAt(i); 212 jEdit.setProperty(ch.property, 213 GUIUtilities.getStyleString(ch.style)); 214 } 215 } 217 private void addStyleChoice(String label, String property) 219 { 220 styleChoices.addElement(new StyleChoice(label, 221 property, 222 GUIUtilities.parseStyle(jEdit.getProperty(property), 223 "Dialog",12))); 224 } 226 static class StyleChoice 228 { 229 String label; 230 String property; 231 SyntaxStyle style; 232 233 StyleChoice(String label, String property, SyntaxStyle style) 234 { 235 this.label = label; 236 this.property = property; 237 this.style = style; 238 } 239 240 public String toString() 242 { 243 return label; 244 } 245 } 247 static class StyleRenderer extends JLabel 249 implements TableCellRenderer 250 { 251 public StyleRenderer() 253 { 254 setOpaque(true); 255 setBorder(SyntaxHiliteOptionPane.noFocusBorder); 256 setText("Hello World"); 257 } 259 public Component getTableCellRendererComponent( 261 JTable table, 262 Object value, 263 boolean isSelected, 264 boolean cellHasFocus, 265 int row, 266 int col) 267 { 268 if (value != null) 269 { 270 SyntaxStyle style = (SyntaxStyle)value; 271 setForeground(style.getForegroundColor()); 272 if (style.getBackgroundColor() != null) 273 setBackground(style.getBackgroundColor()); 274 else 275 { 276 setBackground(jEdit.getColorProperty( 278 "view.bgColor")); 279 } 280 setFont(style.getFont()); 281 } 282 283 setBorder((cellHasFocus) ? UIManager.getBorder( 284 "Table.focusCellHighlightBorder") 285 : SyntaxHiliteOptionPane.noFocusBorder); 286 return this; 287 } } } 291 class StyleEditor extends EnhancedDialog implements ActionListener 293 { 294 StyleEditor(Component comp, SyntaxStyle style) 296 { 297 super(GUIUtilities.getParentDialog(comp), 298 jEdit.getProperty("style-editor.title"),true); 299 300 JPanel content = new JPanel(new BorderLayout(12,12)); 301 content.setBorder(new EmptyBorder (12,12,12,12)); 302 setContentPane(content); 303 304 JPanel panel = new JPanel(new GridLayout(4,2,12,12)); 305 306 italics = new JCheckBox(jEdit.getProperty("style-editor.italics")); 307 italics.setSelected(style.getFont().isItalic()); 308 panel.add(italics); 309 panel.add(new JLabel()); 310 311 bold = new JCheckBox(jEdit.getProperty("style-editor.bold")); 312 bold.setSelected(style.getFont().isBold()); 313 panel.add(bold); 314 panel.add(new JLabel()); 315 316 Color fg = style.getForegroundColor(); 317 318 fgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.fgColor")); 319 fgColorCheckBox.setSelected(fg != null); 320 fgColorCheckBox.addActionListener(this); 321 panel.add(fgColorCheckBox); 322 323 fgColor = new ColorWellButton(fg); 324 fgColor.setEnabled(fg != null); 325 panel.add(fgColor); 326 327 Color bg = style.getBackgroundColor(); 328 bgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.bgColor")); 329 bgColorCheckBox.setSelected(bg != null); 330 bgColorCheckBox.addActionListener(this); 331 panel.add(bgColorCheckBox); 332 333 bgColor = new ColorWellButton(bg); 334 bgColor.setEnabled(bg != null); 335 panel.add(bgColor); 336 337 content.add(BorderLayout.CENTER,panel); 338 339 Box box = new Box(BoxLayout.X_AXIS); 340 box.add(Box.createGlue()); 341 box.add(ok = new JButton(jEdit.getProperty("common.ok"))); 342 getRootPane().setDefaultButton(ok); 343 ok.addActionListener(this); 344 box.add(Box.createHorizontalStrut(6)); 345 box.add(cancel = new JButton(jEdit.getProperty("common.cancel"))); 346 cancel.addActionListener(this); 347 box.add(Box.createGlue()); 348 349 content.add(BorderLayout.SOUTH,box); 350 351 pack(); 352 setLocationRelativeTo(GUIUtilities.getParentDialog(comp)); 353 354 setResizable(false); 355 setVisible(true); 356 } 358 public void actionPerformed(ActionEvent evt) 360 { 361 Object source = evt.getSource(); 362 if(source == ok) 363 ok(); 364 else if(source == cancel) 365 cancel(); 366 else if(source == fgColorCheckBox) 367 fgColor.setEnabled(fgColorCheckBox.isSelected()); 368 else if(source == bgColorCheckBox) 369 bgColor.setEnabled(bgColorCheckBox.isSelected()); 370 } 372 public void ok() 374 { 375 okClicked = true; 376 dispose(); 377 } 379 public void cancel() 381 { 382 dispose(); 383 } 385 public SyntaxStyle getStyle() 387 { 388 if(!okClicked) 389 return null; 390 391 Color foreground = (fgColorCheckBox.isSelected() 392 ? fgColor.getSelectedColor() 393 : null); 394 395 Color background = (bgColorCheckBox.isSelected() 396 ? bgColor.getSelectedColor() 397 : null); 398 399 return new SyntaxStyle(foreground,background, 400 new Font("Dialog", 401 (italics.isSelected() ? Font.ITALIC : 0) 402 | (bold.isSelected() ? Font.BOLD : 0), 403 12)); 404 } 406 private JCheckBox italics; 408 private JCheckBox bold; 409 private JCheckBox fgColorCheckBox; 410 private ColorWellButton fgColor; 411 private JCheckBox bgColorCheckBox; 412 private ColorWellButton bgColor; 413 private JButton ok; 414 private JButton cancel; 415 private boolean okClicked; 416 } | Popular Tags |