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.Component;
025: import java.awt.Graphics;
026: import java.awt.event.MouseAdapter;
027: import java.awt.event.MouseEvent;
028: import java.util.HashMap;
029:
030: import javax.swing.Icon;
031: import javax.swing.ImageIcon;
032: import javax.swing.JTabbedPane;
033:
034: import org.beryl.gui.GUIEvent;
035: import org.beryl.gui.GUIEventListener;
036: import org.beryl.gui.GUIException;
037: import org.beryl.gui.ImageIconFactory;
038: import org.beryl.gui.Widget;
039: import org.beryl.gui.WidgetInfo;
040:
041: public class TabbedPane extends Widget {
042: protected static WidgetInfo tabbedInfo = null;
043: private JTabbedPane pane = null;
044: private HashMap paneInfo = null;
045: private boolean isConstructed = false;
046: private ImageIcon closeIcon = null;
047:
048: static {
049: tabbedInfo = new WidgetInfo(TabbedPane.class, widgetInfo);
050: tabbedInfo.addEvent("rightclick");
051: tabbedInfo.addEvent("close");
052: try {
053: tabbedInfo.addProperty("closeicon", "icon",
054: ImageIconFactory.getIcon("remove_tab"));
055: } catch (Exception e) {
056: throw new RuntimeException(e);
057: }
058: }
059:
060: private static class CloseIcon implements Icon {
061: private Icon icon;
062: private int x = 0;
063: private int y = 0;
064: private int height = 10;
065: private int width = 10;
066:
067: public CloseIcon(ImageIcon icon) {
068: this .icon = icon;
069:
070: if (icon != null) {
071: height = icon.getIconHeight();
072: width = icon.getIconWidth();
073: }
074: }
075:
076: public int getIconHeight() {
077: return height;
078: }
079:
080: public int getIconWidth() {
081: return width;
082: }
083:
084: public void paintIcon(Component c, Graphics g, int x, int y) {
085: this .x = x;
086: this .y = y;
087:
088: if (icon != null) {
089: icon.paintIcon(c, g, x, y + 1);
090: } else {
091: g.drawRect(x, y + 1, width, height);
092: }
093: }
094:
095: public boolean contains(int x2, int y2) {
096: if (!(x2 >= x) || !(x2 <= x + width))
097: return false;
098: if (!(y2 >= y) || !(y2 <= y + height))
099: return false;
100: return true;
101: }
102: }
103:
104: public TabbedPane(Widget parent, String name) throws GUIException {
105: super (parent, name);
106: pane = new JTabbedPane();
107: paneInfo = new HashMap();
108: }
109:
110: public void addChild(Widget widget, Object constraint)
111: throws GUIException {
112: String name = widget.getName();
113: if (constraint != null)
114: throw new GUIException(
115: "Anchors not supported inside a tabbed pane");
116: if (!(widget instanceof Panel))
117: throw new GUIException(
118: "Only panels can be added to a tabbed pane");
119:
120: String title = (String) paneInfo.get(name + ".title");
121: if (title == null)
122: title = "(unnamed)";
123:
124: Icon icon = (Icon) paneInfo.get(name + ".icon");
125: if (icon == null && closeIcon != null)
126: icon = new CloseIcon(closeIcon);
127: pane.addTab(title, icon, widget.getWidget(), (String) paneInfo
128: .get(name + ".tooltip"));
129: addChild(widget);
130: }
131:
132: public void removeChildWidget(Widget widget) throws GUIException {
133: pane.remove(widget.getWidget());
134: super .removeChildWidget(widget);
135: }
136:
137: public Widget getSelectedWidget() {
138: return getChild(pane.getSelectedIndex());
139: }
140:
141: public void setSelectedWidget(Widget widget) {
142: pane.setSelectedIndex(getChildIndex(widget));
143: }
144:
145: public void setProperty(String name, Object value)
146: throws GUIException {
147: if (name.endsWith(".title") || name.endsWith(".icon")
148: || name.endsWith(".tooltip")) {
149: String widgetName = name
150: .substring(0, name.lastIndexOf('.'));
151:
152: paneInfo.put(name, value);
153: if (isConstructed) {
154: Widget widget = getWidget(widgetName);
155: if (widget != null) {
156: int index = pane.indexOfComponent(widget
157: .getWidget());
158: if (index == -1)
159: throw new GUIException(
160: "That widget is not a child of the tabbed pane");
161: pane.removeTabAt(index);
162:
163: String title = (String) paneInfo.get(widgetName
164: + ".title");
165: if (title == null)
166: title = "(unnamed)";
167: Icon icon = (Icon) paneInfo.get(widgetName
168: + ".icon");
169: if (icon == null && closeIcon != null)
170: icon = new CloseIcon(closeIcon);
171: pane.insertTab(title, icon, widget.getWidget(),
172: (String) paneInfo.get(widgetName
173: + ".tooltip"), index);
174: } else {
175: throw new GUIException("A tab named [" + widgetName
176: + "] was not found");
177: }
178: }
179: } else if (name.equals("closeicon")) {
180: closeIcon = (ImageIcon) value;
181: } else {
182: super .setProperty(name, value);
183: }
184: }
185:
186: public void addListener(String event, final String name,
187: final GUIEventListener listener) throws GUIException {
188: if ("rightclick".equals(event)) {
189: pane.addMouseListener(new MouseAdapter() {
190: public void mousePressed(MouseEvent e) {
191: if (e.isPopupTrigger()) {
192: if (pane.getSelectedIndex() >= 0) {
193: listener.eventOccured(new GUIEvent(
194: TabbedPane.this , name, e));
195: }
196: }
197: }
198:
199: public void mouseReleased(MouseEvent e) {
200: mousePressed(e);
201: }
202: });
203: } else if ("close".equals(event)) {
204: pane.addMouseListener(new MouseAdapter() {
205: public void mouseClicked(MouseEvent e) {
206: int i = pane.getSelectedIndex();
207:
208: if (i == -1 || e.isPopupTrigger())
209: return;
210:
211: if (pane.getIconAt(i) instanceof CloseIcon) {
212: CloseIcon icon = (CloseIcon) pane.getIconAt(i);
213:
214: if (icon != null
215: && icon.contains(e.getX(), e.getY())) {
216: listener.eventOccured(new GUIEvent(
217: TabbedPane.this , name, e));
218: }
219: }
220: }
221: });
222: } else {
223: super .addListener(event, name, listener);
224: }
225: }
226:
227: public void finalizeConstruction() throws GUIException {
228: isConstructed = true;
229: }
230:
231: public Component getWidget() {
232: return pane;
233: }
234:
235: public WidgetInfo getWidgetInfo() {
236: return tabbedInfo;
237: }
238: }
|