001: /*
002: * Beryl - A web platform based on XML, XSLT and Java
003: * This file is part of the Beryl XML GUI
004: *
005: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011:
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 GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
020: */
021:
022: package org.beryl.gui.widgets;
023:
024: import java.awt.Color;
025: import java.awt.Component;
026: import java.awt.Dimension;
027:
028: import javax.swing.ImageIcon;
029: import javax.swing.JScrollPane;
030: import javax.swing.JTextPane;
031: import javax.swing.event.DocumentEvent;
032: import javax.swing.event.DocumentListener;
033: import javax.swing.text.BadLocationException;
034: import javax.swing.text.Document;
035: import javax.swing.text.Style;
036: import javax.swing.text.StyleConstants;
037: import javax.swing.text.StyleContext;
038: import javax.swing.text.StyleConstants.ColorConstants;
039:
040: import org.beryl.gui.GUIException;
041: import org.beryl.gui.Widget;
042: import org.beryl.gui.WidgetInfo;
043: import org.beryl.gui.model.MapChangeEvent;
044: import org.beryl.gui.model.MapDataModel;
045: import org.beryl.gui.model.ModelChangeEvent;
046:
047: public class TextPane extends Widget {
048: protected static WidgetInfo textPaneInfo = null;
049: private JTextPane textPane = null;
050: private JScrollPane scrollPane = null;
051: private String key = null;
052: private Document document = null;
053: private boolean sendEvents = true;
054: private int counter = 0;
055:
056: static {
057: textPaneInfo = new WidgetInfo(TextPane.class, widgetInfo);
058: textPaneInfo.addProperty("key", "string", "");
059: textPaneInfo.addProperty("verticalScrollBar", "bool",
060: Boolean.FALSE);
061: textPaneInfo.addProperty("horizontalScrollBar", "bool",
062: Boolean.FALSE);
063: };
064:
065: public TextPane(Widget parent, String name) throws GUIException {
066: super (parent, name);
067: textPane = new JTextPane();
068: document = textPane.getDocument();
069: scrollPane = new JScrollPane(textPane);
070:
071: Style def = StyleContext.getDefaultStyleContext().getStyle(
072: StyleContext.DEFAULT_STYLE);
073:
074: Style black = textPane.addStyle("black", def);
075: ColorConstants.setForeground(black, Color.black);
076:
077: Style white = textPane.addStyle("white", def);
078: ColorConstants.setForeground(white, Color.white);
079:
080: Style gray = textPane.addStyle("gray", def);
081: ColorConstants.setForeground(gray, Color.gray);
082:
083: Style blue = textPane.addStyle("blue", def);
084: ColorConstants.setForeground(blue, Color.blue);
085:
086: Style cyan = textPane.addStyle("cyan", def);
087: ColorConstants.setForeground(cyan, Color.cyan);
088:
089: Style yellow = textPane.addStyle("yellow", def);
090: ColorConstants.setForeground(yellow, Color.yellow);
091:
092: Style green = textPane.addStyle("green", def);
093: ColorConstants.setForeground(green, Color.green);
094:
095: Style darkGreen = textPane.addStyle("darkgreen", def);
096: ColorConstants.setForeground(darkGreen, new Color(0, 100, 0));
097:
098: Style red = textPane.addStyle("red", def);
099: ColorConstants.setForeground(red, Color.red);
100: }
101:
102: public void setProperty(String name, Object value)
103: throws GUIException {
104: if ("key".equals(name))
105: key = (String) value;
106: else if ("verticalScrollBar".equals(name))
107: scrollPane
108: .setVerticalScrollBarPolicy(((Boolean) value)
109: .booleanValue() ? JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
110: : JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
111: else if ("horizontalScrollBar".equals(name))
112: scrollPane
113: .setHorizontalScrollBarPolicy(((Boolean) value)
114: .booleanValue() ? JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS
115: : JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
116: else
117: super .setProperty(name, value);
118: }
119:
120: private void reload() {
121: MapDataModel model = getDataModel();
122: if (model != null) {
123: String value = (String) model.getValue(key);
124: if (value != null)
125: textPane.setText(value);
126: }
127: }
128:
129: public void modelChanged(ModelChangeEvent e) throws GUIException {
130: sendEvents = false;
131: try {
132: if (e.getSource() == this ) {
133: /* New data model */
134: reload();
135: } else if (e instanceof MapChangeEvent) {
136: MapChangeEvent event = (MapChangeEvent) e;
137: if (event.getKey() == null) {
138: reload();
139: } else if (event.getKey().equals(key)) {
140: textPane.setText((String) event.getNewValue());
141: }
142: }
143: } finally {
144: sendEvents = true;
145: }
146: }
147:
148: public void finalizeConstruction() {
149: textPane.setPreferredSize(new Dimension(MAX_WIDTH,
150: DEFAULT_HEIGHT));
151: document.addDocumentListener(new DocumentListener() {
152: public void insertUpdate(DocumentEvent e) {
153: changedUpdate(e);
154: }
155:
156: public void removeUpdate(DocumentEvent e) {
157: changedUpdate(e);
158: }
159:
160: public void changedUpdate(DocumentEvent e) {
161: if (sendEvents) {
162: try {
163: sendEvents = false;
164: MapDataModel model = getDataModel();
165: if (model != null && key != null) {
166: String text = null;
167: text = textPane.getText();
168: model.setValue(TextPane.this , key, text);
169: }
170: } catch (GUIException ex) {
171: throw new RuntimeException(ex);
172: } finally {
173: sendEvents = true;
174: }
175: }
176: }
177: });
178: }
179:
180: public void addIcon(ImageIcon icon) throws GUIException {
181: String name = "icon" + counter;
182: Style style = textPane.addStyle(name, null);
183: StyleConstants.setIcon(style, icon);
184: addText(name, name);
185: counter++;
186: }
187:
188: public void addText(String text) throws GUIException {
189: addText(text, null);
190: }
191:
192: public final void addText(String text, String style)
193: throws GUIException {
194: try {
195: document.insertString(document.getLength(), text,
196: style == null ? null : textPane.getStyle(style));
197: } catch (BadLocationException e) {
198: throw new GUIException("Error while inserting text", e);
199: }
200: }
201:
202: public Component getWidget() {
203: return scrollPane;
204: }
205:
206: public Component getRealWidget() {
207: return textPane;
208: }
209:
210: public void setEnabled(boolean enabled) throws GUIException {
211: super .setEnabled(enabled);
212: textPane.setEnabled(enabled);
213: }
214:
215: public WidgetInfo getWidgetInfo() {
216: return textPaneInfo;
217: }
218: }
|