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.ColorUtil;
025: import gruntspud.Constants;
026: import gruntspud.GruntspudContext;
027: import gruntspud.StringUtil;
028:
029: import java.awt.Color;
030: import java.io.File;
031: import java.util.HashMap;
032: import java.util.Iterator;
033:
034: import javax.swing.AbstractListModel;
035:
036: public class TextStyleModel extends AbstractListModel implements
037: Cloneable {
038: private HashMap stylesMap = new HashMap();
039: private java.util.List styles = new java.util.ArrayList();
040: private File file;
041: private GruntspudContext context;
042:
043: public Object clone() throws CloneNotSupportedException {
044: TextStyleModel model = new TextStyleModel(context);
045: for (Iterator i = styles.iterator(); i.hasNext();) {
046: TextStyle s = (TextStyle) ((TextStyle) i.next()).clone();
047: model.styles.add(s);
048: }
049: return model;
050: }
051:
052: public TextStyleModel(GruntspudContext context) {
053: this .context = context;
054: }
055:
056: public void addStyle(TextStyle style)
057: throws IllegalArgumentException {
058: TextStyle s = (TextStyle) stylesMap.get(style.getName());
059: if (s != null)
060: throw new IllegalArgumentException("Style with a name of "
061: + style.getName() + " has already been registered.");
062: stylesMap.put(style.getName(), style);
063: styles.add(style);
064:
065: //
066: String key = Constants.OPTIONS_STYLE_PREFIX + style.getName();
067: Color foreground = ColorUtil.getColor(key + ".foreground",
068: style.getForeground(), context);
069: style.setForeground(foreground);
070: Color background = ColorUtil.getColor(key + ".background",
071: style.getBackground(), context);
072: style.setBackground(background);
073: boolean bold = context.getHost().getBooleanProperty(
074: key + ".bold", style.isBold());
075: style.setBold(bold);
076: boolean italic = context.getHost().getBooleanProperty(
077: key + ".italic", style.isItalic());
078: style.setItalic(italic);
079: }
080:
081: public Iterator styles() {
082: return styles.iterator();
083: }
084:
085: public void removeStyle(TextStyle style) {
086: styles.remove(style);
087: stylesMap.remove(style.getName());
088: }
089:
090: public void updateStyle(TextStyle style) {
091: int i = styles.indexOf(style);
092: if (i != -1)
093: fireContentsChanged(this , i, i);
094: }
095:
096: public int getSize() {
097: return styles.size();
098: }
099:
100: public Object getElementAt(int index) {
101: return styles.get(index);
102: }
103:
104: public TextStyle getStyle(String name) {
105: return (TextStyle) stylesMap.get(name);
106: }
107:
108: public TextStyle getStyleAt(int index) {
109: return (TextStyle) getElementAt(index);
110: }
111:
112: public void saveStyles() {
113: for (Iterator i = styles.iterator(); i.hasNext();) {
114: TextStyle style = (TextStyle) i.next();
115: String key = Constants.OPTIONS_STYLE_PREFIX
116: + style.getName();
117: context.getHost().setProperty(
118: key + ".foreground",
119: style.getForeground() == null ? "" : StringUtil
120: .colorToString(style.getForeground()));
121: context.getHost().setProperty(
122: key + ".background",
123: style.getBackground() == null ? "" : StringUtil
124: .colorToString(style.getBackground()));
125: context.getHost().setBooleanProperty(key + ".bold",
126: style.isBold());
127: context.getHost().setBooleanProperty(key + ".italic",
128: style.isItalic());
129: }
130: }
131: }
|