001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021:
022: package gruntspud.style;
023:
024: import gruntspud.Constants;
025: import gruntspud.GruntspudContext;
026: import gruntspud.ui.ColorComboBox;
027: import gruntspud.ui.UIUtil;
028: import gruntspud.ui.icons.ColorIcon;
029: import gruntspud.ui.icons.CompoundIcon;
030: import gruntspud.ui.preferences.AbstractOptionsTab;
031:
032: import java.awt.BorderLayout;
033: import java.awt.Color;
034: import java.awt.Component;
035: import java.awt.Dimension;
036: import java.awt.Font;
037: import java.awt.GridBagConstraints;
038: import java.awt.GridBagLayout;
039: import java.awt.Insets;
040: import java.awt.event.ActionEvent;
041: import java.awt.event.ActionListener;
042: import java.util.Iterator;
043:
044: import javax.swing.BorderFactory;
045: import javax.swing.DefaultListCellRenderer;
046: import javax.swing.JCheckBox;
047: import javax.swing.JLabel;
048: import javax.swing.JList;
049: import javax.swing.JPanel;
050: import javax.swing.JScrollPane;
051: import javax.swing.ListSelectionModel;
052: import javax.swing.event.ChangeEvent;
053: import javax.swing.event.ChangeListener;
054: import javax.swing.event.ListSelectionEvent;
055: import javax.swing.event.ListSelectionListener;
056:
057: /**
058: * Description of the Class
059: *
060: *@author magicthize
061: *@created 26 May 2002
062: */
063: public class TextStyleOptionsTab extends AbstractOptionsTab implements
064: ActionListener, ChangeListener {
065:
066: private ColorComboBox foreground;
067: private ColorComboBox background;
068: private JCheckBox bold, italic;
069: private JList styles;
070: private TextStyleModel model;
071: private boolean adjusting;
072:
073: /**
074: * Constructor for the GlobalOptionsTab object
075: */
076: public TextStyleOptionsTab() {
077: super ("Styles", UIUtil.getCachedIcon(Constants.ICON_TOOL_COLOR));
078: }
079:
080: /**
081: * DOCUMENT ME!
082: *
083: * @param context DOCUMENT ME!
084: */
085: public void init(GruntspudContext context) {
086: super .init(context);
087:
088: setTabToolTipText("Set various color / font styles used in Gruntspud.");
089: setTabLargeIcon(UIUtil
090: .getCachedIcon(Constants.ICON_TOOL_LARGE_COLOR));
091: setLayout(new BorderLayout());
092: setTabMnemonic('s');
093: setTabContext("UI");
094:
095: try {
096: model = (TextStyleModel) context.getTextStyleModel()
097: .clone();
098: } catch (CloneNotSupportedException e1) {
099: }
100: styles = new JList(model);
101: styles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
102: styles.setCellRenderer(new StyleListCellRenderer());
103: styles.getSelectionModel().addListSelectionListener(
104: new ListSelectionListener() {
105: public void valueChanged(ListSelectionEvent lse) {
106: Constants.UI_LOG.debug("Style selected");
107: if (!adjusting) {
108: setSettingsForStyle();
109:
110: }
111: }
112: });
113: JScrollPane stylesScroller = new JScrollPane(styles);
114: JPanel s = new JPanel(new BorderLayout());
115: s.add(stylesScroller, BorderLayout.CENTER);
116: s.setBorder(BorderFactory.createTitledBorder("Styles"));
117:
118: //
119: JPanel e = new JPanel(new GridBagLayout());
120: GridBagConstraints gbc = new GridBagConstraints();
121: gbc.insets = new Insets(3, 3, 3, 3);
122: gbc.anchor = GridBagConstraints.WEST;
123: gbc.fill = GridBagConstraints.HORIZONTAL;
124: gbc.weightx = 0.0;
125: UIUtil.jGridBagAdd(e, new JLabel("Foreground"), gbc, 1);
126: gbc.weightx = 1.0;
127: UIUtil.jGridBagAdd(e, foreground = new ColorComboBox(true),
128: gbc, GridBagConstraints.RELATIVE);
129: foreground.addChangeListener(this );
130: gbc.weightx = 0.0;
131: UIUtil.jGridBagAdd(e, bold = new JCheckBox("Bold"), gbc,
132: GridBagConstraints.REMAINDER);
133: bold.addActionListener(this );
134: bold.setMnemonic('b');
135: gbc.weightx = 0.0;
136: UIUtil.jGridBagAdd(e, new JLabel("Background"), gbc, 1);
137: gbc.weightx = 1.0;
138: UIUtil.jGridBagAdd(e, background = new ColorComboBox(true),
139: gbc, GridBagConstraints.RELATIVE);
140: background.addChangeListener(this );
141: gbc.weightx = 0.0;
142: UIUtil.jGridBagAdd(e, italic = new JCheckBox("Italic"), gbc,
143: GridBagConstraints.REMAINDER);
144: italic.addActionListener(this );
145: italic.setMnemonic('i');
146:
147: add(s, BorderLayout.CENTER);
148: add(e, BorderLayout.SOUTH);
149:
150: if (styles.getModel().getSize() > 0)
151: styles.setSelectedIndex(0);
152: setSettingsForStyle();
153: }
154:
155: public void actionPerformed(ActionEvent evt) {
156: if (!adjusting) {
157: applySettings();
158: }
159: }
160:
161: private void applySettings() {
162: Constants.UI_LOG.debug("Applying settings");
163: TextStyle style = model.getStyleAt(styles.getSelectedIndex());
164: style.setForeground(foreground.getColor());
165: style.setBackground(background.getColor());
166: style.setItalic(italic.isSelected());
167: style.setBold(bold.isSelected());
168: model.updateStyle(style);
169: }
170:
171: private void setSettingsForStyle() {
172: adjusting = true;
173: int idx = styles.getSelectedIndex();
174: if (idx == -1) {
175: foreground.setEnabled(false);
176: background.setEnabled(false);
177: italic.setEnabled(false);
178: bold.setEnabled(false);
179: foreground.setColor(null);
180: background.setColor(null);
181: } else {
182: TextStyle style = model.getStyleAt(idx);
183: foreground.setEnabled(true);
184: background.setEnabled(true);
185: italic.setEnabled(true);
186: bold.setEnabled(true);
187: foreground.setColor(style.getForeground());
188: background.setColor(style.getBackground());
189: italic.setSelected(style.isItalic());
190: bold.setSelected(style.isBold());
191: }
192: adjusting = false;
193: }
194:
195: /**
196: * Description of the Method
197: *
198: *@return Description of the Return Value
199: */
200: public boolean validateTab() {
201: return true;
202: }
203:
204: /**
205: * Description of the Method
206: */
207: public void tabSelected() {
208: }
209:
210: /**
211: * Description of the Method
212: */
213: public void applyTab() {
214: Constants.UI_LOG.debug("Applying new styles");
215: Iterator j = getContext().getTextStyleModel().styles();
216: for (Iterator i = model.styles(); i.hasNext();) {
217: TextStyle s1 = (TextStyle) j.next();
218: TextStyle s2 = (TextStyle) i.next();
219: s1.setBackground(s2.getBackground());
220: s1.setForeground(s2.getForeground());
221: s1.setBold(s2.isBold());
222: s1.setItalic(s2.isItalic());
223: getContext().getTextStyleModel().updateStyle(s1);
224: }
225: }
226:
227: class StyleListCellRenderer extends DefaultListCellRenderer {
228:
229: private Color fg;
230: private ColorIcon backgroundIcon, foregroundIcon;
231: private CompoundIcon icon;
232:
233: public StyleListCellRenderer() {
234: super ();
235: backgroundIcon = new ColorIcon(Color.black, new Dimension(
236: 12, 12), styles.getForeground());
237: foregroundIcon = new ColorIcon(Color.black, new Dimension(
238: 12, 12), styles.getForeground());
239: icon = new CompoundIcon(backgroundIcon, foregroundIcon);
240: icon.setGap(4);
241: icon.setMargin(new Insets(1, 4, 1, 4));
242: setVerticalTextPosition(JLabel.CENTER);
243: setVerticalAlignment(JLabel.CENTER);
244: }
245:
246: public Component getListCellRendererComponent(JList list,
247: Object value, int index, boolean isSelected,
248: boolean cellHasFocus) {
249: super .getListCellRendererComponent(list, value, index,
250: isSelected, cellHasFocus);
251: TextStyle style = (TextStyle) value;
252: Color bgi = style.getBackground();
253: Color fgi = style.getForeground();
254: // if(!isSelected)
255: // setForeground(fgi == null ? styles.getForeground() : fgi);
256: backgroundIcon.setColor(bgi);
257: foregroundIcon.setColor(fgi);
258: setIcon(icon);
259: setFont(getFont().deriveFont(
260: (style.isBold() ? Font.BOLD : 0)
261: + (style.isItalic() ? Font.ITALIC : 0)));
262: return this ;
263: }
264: }
265:
266: /* (non-Javadoc)
267: * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
268: */
269: public void stateChanged(ChangeEvent e) {
270: if (!adjusting) {
271: applySettings();
272: }
273: }
274: }
|