001: /*
002: * SyntaxHiliteOptionPane.java - Syntax highlighting option pane
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 1999, 2000, 2001 Slava Pestov
007: * Portions copyright (C) 1999 mike dillon
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: */
023:
024: package org.gjt.sp.jedit.options;
025:
026: //{{{ Imports
027: import javax.swing.border.EmptyBorder;
028: import javax.swing.table.*;
029: import javax.swing.*;
030: import java.awt.event.*;
031: import java.awt.*;
032: import java.util.Vector;
033: import java.util.Collections;
034:
035: import org.gjt.sp.jedit.syntax.*;
036: import org.gjt.sp.jedit.gui.ColorWellButton;
037: import org.gjt.sp.jedit.gui.EnhancedDialog;
038: import org.gjt.sp.jedit.gui.StyleEditor;
039: import org.gjt.sp.jedit.*;
040:
041: //}}}
042:
043: //{{{ SyntaxHiliteOptionPane class
044: /**
045: * Style option pane.
046: * @author Slava Pestov
047: * @version $Id: SyntaxHiliteOptionPane.java 10467 2007-09-09 06:51:17Z shlomy $
048: */
049: public class SyntaxHiliteOptionPane extends AbstractOptionPane {
050: public static final EmptyBorder noFocusBorder = new EmptyBorder(1,
051: 1, 1, 1);
052:
053: //{{{ StyleOptionPane constructor
054: public SyntaxHiliteOptionPane() {
055: super ("syntax");
056: }
057:
058: //}}}
059:
060: //{{{ Protected members
061:
062: //{{{ _init() method
063: protected void _init() {
064: setLayout(new BorderLayout(6, 6));
065:
066: add(BorderLayout.CENTER, createStyleTableScroller());
067: } //}}}
068:
069: //{{{ _save() method
070: protected void _save() {
071: styleModel.save();
072: } //}}}
073:
074: //}}}
075:
076: //{{{ Private members
077: private StyleTableModel styleModel;
078: private JTable styleTable;
079:
080: //{{{ createStyleTableScroller() method
081: private JScrollPane createStyleTableScroller() {
082: styleModel = createStyleTableModel();
083: styleTable = new JTable(styleModel);
084: styleTable.setRowSelectionAllowed(false);
085: styleTable.setColumnSelectionAllowed(false);
086: styleTable.setCellSelectionEnabled(false);
087: styleTable.getTableHeader().setReorderingAllowed(false);
088: styleTable.addMouseListener(new MouseHandler());
089: TableColumnModel tcm = styleTable.getColumnModel();
090: TableColumn styleColumn = tcm.getColumn(1);
091: styleColumn
092: .setCellRenderer(new StyleTableModel.StyleRenderer());
093: Dimension d = styleTable.getPreferredSize();
094: d.height = Math.min(d.height, 100);
095: JScrollPane scroller = new JScrollPane(styleTable);
096: scroller.setPreferredSize(d);
097: return scroller;
098: } //}}}
099:
100: //{{{ createStyleTableModel() method
101: private static StyleTableModel createStyleTableModel() {
102: return new StyleTableModel();
103: } //}}}
104:
105: //}}}
106:
107: //{{{ MouseHandler class
108: class MouseHandler extends MouseAdapter {
109: public void mouseClicked(MouseEvent evt) {
110: int row = styleTable.rowAtPoint(evt.getPoint());
111: if (row == -1)
112: return;
113:
114: SyntaxStyle style;
115: SyntaxStyle current = (SyntaxStyle) styleModel.getValueAt(
116: row, 1);
117: String token = (String) styleModel.getValueAt(row, 0);
118: JDialog dialog = GUIUtilities
119: .getParentDialog(SyntaxHiliteOptionPane.this );
120: if (dialog != null)
121: style = new StyleEditor(dialog, current, token)
122: .getStyle();
123: else {
124: View view = GUIUtilities
125: .getView(SyntaxHiliteOptionPane.this );
126: style = new StyleEditor(view, current, token)
127: .getStyle();
128: }
129: if (style != null)
130: styleModel.setValueAt(style, row, 1);
131: }
132: } //}}}
133: } //}}}
134:
135: //{{{ StyleTableModel class
136: class StyleTableModel extends AbstractTableModel {
137: private Vector styleChoices;
138:
139: //{{{ StyleTableModel constructor
140: StyleTableModel() {
141: styleChoices = new Vector(Token.ID_COUNT + 4);
142: // start at 1 not 0 to skip Token.NULL
143: for (int i = 1; i < Token.ID_COUNT; i++) {
144: String tokenName = Token.tokenToString((byte) i);
145: addStyleChoice(tokenName, "view.style."
146: + tokenName.toLowerCase());
147: }
148:
149: addStyleChoice(jEdit.getProperty("options.syntax.foldLine.1"),
150: "view.style.foldLine.1");
151: addStyleChoice(jEdit.getProperty("options.syntax.foldLine.2"),
152: "view.style.foldLine.2");
153: addStyleChoice(jEdit.getProperty("options.syntax.foldLine.3"),
154: "view.style.foldLine.3");
155: addStyleChoice(jEdit.getProperty("options.syntax.foldLine.0"),
156: "view.style.foldLine.0");
157:
158: Collections.sort(styleChoices,
159: new MiscUtilities.StringICaseCompare());
160: } //}}}
161:
162: //{{{ getColumnCount() method
163: public int getColumnCount() {
164: return 2;
165: } //}}}
166:
167: //{{{ getRowCount() method
168: public int getRowCount() {
169: return styleChoices.size();
170: } //}}}
171:
172: //{{{ getValueAt() method
173: public Object getValueAt(int row, int col) {
174: StyleChoice ch = (StyleChoice) styleChoices.elementAt(row);
175: switch (col) {
176: case 0:
177: return ch.label;
178: case 1:
179: return ch.style;
180: default:
181: return null;
182: }
183: } //}}}
184:
185: //{{{ setValueAt() method
186: public void setValueAt(Object value, int row, int col) {
187: StyleChoice ch = (StyleChoice) styleChoices.elementAt(row);
188: if (col == 1)
189: ch.style = (SyntaxStyle) value;
190: fireTableRowsUpdated(row, row);
191: } //}}}
192:
193: //{{{ getColumnName() method
194: public String getColumnName(int index) {
195: switch (index) {
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: } //}}}
204:
205: //{{{ save() method
206: public void save() {
207: for (int i = 0; i < styleChoices.size(); i++) {
208: StyleChoice ch = (StyleChoice) styleChoices.elementAt(i);
209: jEdit.setProperty(ch.property, GUIUtilities
210: .getStyleString(ch.style));
211: }
212: } //}}}
213:
214: //{{{ addStyleChoice() method
215: private void addStyleChoice(String label, String property) {
216: styleChoices.addElement(new StyleChoice(label, property,
217: GUIUtilities.parseStyle(jEdit.getProperty(property),
218: "Dialog", 12)));
219: } //}}}
220:
221: //{{{ StyleChoice class
222: static class StyleChoice {
223: String label;
224: String property;
225: SyntaxStyle style;
226:
227: StyleChoice(String label, String property, SyntaxStyle style) {
228: this .label = label;
229: this .property = property;
230: this .style = style;
231: }
232:
233: // for sorting
234: public String toString() {
235: return label;
236: }
237: } //}}}
238:
239: //{{{ StyleRenderer class
240: static class StyleRenderer extends JLabel implements
241: TableCellRenderer {
242: //{{{ StyleRenderer constructor
243: public StyleRenderer() {
244: setOpaque(true);
245: setBorder(SyntaxHiliteOptionPane.noFocusBorder);
246: setText("Hello World");
247: } //}}}
248:
249: //{{{ getTableCellRendererComponent() method
250: public Component getTableCellRendererComponent(JTable table,
251: Object value, boolean isSelected, boolean cellHasFocus,
252: int row, int col) {
253: if (value != null) {
254: SyntaxStyle style = (SyntaxStyle) value;
255: setForeground(style.getForegroundColor());
256: if (style.getBackgroundColor() != null)
257: setBackground(style.getBackgroundColor());
258: else {
259: // this part sucks
260: setBackground(jEdit
261: .getColorProperty("view.bgColor"));
262: }
263: setFont(style.getFont());
264: }
265:
266: setBorder((cellHasFocus) ? UIManager
267: .getBorder("Table.focusCellHighlightBorder")
268: : SyntaxHiliteOptionPane.noFocusBorder);
269: return this ;
270: } //}}}
271: } //}}}
272: } //}}}
|