001: /*
002: * SwingML Copyright (C) 2002 SwingML Team
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors:
019: * Ezequiel Cuellar <ecuellar@crosslogic.com>
020: * Robert Morris <robertj@morris.net>
021: * Bram Stieperaere <bramez@users.sourceforge.net>
022: * Marcelo W. Lopez Cremona <marcelo_java@argentina.com>
023: * Hamdi Mohs Yusof <hamdi@medical-online.net>
024: */
025: package org.swingml.view;
026:
027: import java.awt.*;
028: import java.util.*;
029: import java.util.List;
030: import javax.swing.*;
031:
032: import org.swingml.*;
033: import org.swingml.event.*;
034: import org.swingml.model.*;
035: import org.swingml.registry.*;
036: import org.swingml.tablebrowser.ext.*;
037: import org.swingml.treetablebrowser.ext.*;
038: import org.swingml.view.renderer.*;
039:
040: public class RendererUtil {
041:
042: private static final Hashtable DEFAULT_RENDERERES;
043: static {
044: DEFAULT_RENDERERES = new Hashtable();
045: DEFAULT_RENDERERES.put(JPanelModel.class, JPanelRenderer.class);
046: DEFAULT_RENDERERES.put(JMenuBarModel.class,
047: JMenuBarRenderer.class);
048: DEFAULT_RENDERERES.put(JMenuModel.class, JMenuRenderer.class);
049: DEFAULT_RENDERERES.put(JMenuItemModel.class,
050: JMenuItemRenderer.class);
051: DEFAULT_RENDERERES.put(JSeparatorModel.class,
052: JSeparatorRenderer.class);
053: DEFAULT_RENDERERES.put(JDialogModel.class,
054: JDialogRenderer.class);
055: DEFAULT_RENDERERES.put(JFrameModel.class, JFrameRenderer.class);
056: DEFAULT_RENDERERES.put(JTextFieldModel.class,
057: JTextFieldRenderer.class);
058: DEFAULT_RENDERERES.put(JButtonModel.class,
059: JButtonRenderer.class);
060: DEFAULT_RENDERERES.put(JToolBarModel.class,
061: JToolBarRenderer.class);
062: DEFAULT_RENDERERES.put(JTabbedPaneModel.class,
063: JTabbedPaneRenderer.class);
064: DEFAULT_RENDERERES.put(JLabelModel.class, JLabelRenderer.class);
065: DEFAULT_RENDERERES.put(JTreeModel.class, JTreeRenderer.class);
066: DEFAULT_RENDERERES.put(JTableModel.class, JTableRenderer.class);
067: DEFAULT_RENDERERES.put(JComboBoxModel.class,
068: JComboBoxRenderer.class);
069: DEFAULT_RENDERERES.put(JListModel.class, JListRenderer.class);
070: DEFAULT_RENDERERES.put(JCheckBoxModel.class,
071: JCheckBoxRenderer.class);
072: DEFAULT_RENDERERES.put(JRadioButtonModel.class,
073: JRadioButtonRenderer.class);
074: DEFAULT_RENDERERES.put(ButtonGroupModel.class,
075: ButtonGroupRenderer.class);
076: DEFAULT_RENDERERES.put(JTextAreaModel.class,
077: JTextAreaRenderer.class);
078: DEFAULT_RENDERERES.put(JEditorPaneModel.class,
079: JEditorPaneRenderer.class);
080: DEFAULT_RENDERERES.put(JOptionPaneModel.class,
081: JOptionPaneRenderer.class);
082: DEFAULT_RENDERERES.put(JInternalFrameModel.class,
083: JInternalFrameRenderer.class);
084: DEFAULT_RENDERERES.put(JSplitPaneModel.class,
085: JSplitPaneRenderer.class);
086: DEFAULT_RENDERERES.put(GridBagPanelModel.class,
087: GridBagPanelRenderer.class);
088: DEFAULT_RENDERERES.put(GridBagRowModel.class,
089: GridBagRowRenderer.class);
090: DEFAULT_RENDERERES.put(GridBagCellModel.class,
091: GridBagCellRenderer.class);
092: DEFAULT_RENDERERES.put(JDebugModel.class, JDebugRenderer.class);
093: DEFAULT_RENDERERES.put(JPasswordModel.class,
094: JPasswordRenderer.class);
095: DEFAULT_RENDERERES.put(DataStoreModel.class,
096: DataStoreRenderer.class);
097: DEFAULT_RENDERERES.put(JSliderModel.class,
098: JSliderRenderer.class);
099: DEFAULT_RENDERERES.put(JDesktopPaneModel.class,
100: JDesktopPaneRenderer.class);
101: DEFAULT_RENDERERES.put(JFormattedTextFieldModel.class,
102: JFormattedTextFieldRenderer.class);
103: DEFAULT_RENDERERES.put(JScrollPaneModel.class,
104: JScrollPaneRenderer.class);
105: DEFAULT_RENDERERES.put(ErrorHandlerModel.class,
106: ErrorHandlerRenderer.class);
107: // ================= - - - Browser Component Modifications - - -
108: DEFAULT_RENDERERES.put(TableBrowserModel.class,
109: TableBrowserRenderer.class);
110: // -- TreeTable
111: DEFAULT_RENDERERES.put(TreeTableBrowserModel.class,
112: TreeTableBrowserRenderer.class);
113: }
114: private final Hashtable renderers = new Hashtable();;
115:
116: public RendererUtil() {
117: this .renderers.putAll(DEFAULT_RENDERERES);
118: }
119:
120: public Class addRenderer(Class model, Class renderer) {
121: if (model.isAssignableFrom(SwingMLModel.class)) {
122: throw new IllegalArgumentException(model
123: + " is not a subclass of " + SwingMLModel.class);
124: }
125: if (renderer.isAssignableFrom(Renderer.class)) {
126: throw new IllegalArgumentException(renderer
127: + " is not a subclass of " + Renderer.class);
128: }
129: return (Class) this .renderers.put(model, renderer);
130: }
131:
132: public Renderer getRenderer(Object aChild) {
133: // check for an exact match
134: Class rendererClass = (Class) this .renderers.get(aChild
135: .getClass());
136: // look for a superclass?
137: if (rendererClass == null) {
138: for (Enumeration iter = renderers.keys(); iter
139: .hasMoreElements();) {
140: Class c = (Class) iter.nextElement();
141: if (c.isInstance(aChild)) {
142: rendererClass = c;
143: break;
144: }
145: }
146: }
147: if (rendererClass != null) {
148: try {
149: return (Renderer) rendererClass.newInstance();
150: } catch (Exception e) {
151: throw new RuntimeException(
152: "Error instantiating renderer for " + aChild, e);
153: }
154: }
155: throw new NullPointerException("Renderer for " + aChild
156: + " does not exist");
157: }
158:
159: public void iterate(AbstractSwingMLModel model, Container container) {
160: // get children and render them iteratively
161: List theChildren = model.getChildren();
162: Renderer theRenderer = null;
163: SwingMLModel childModel = null;
164: for (int i = 0; i < theChildren.size(); i++) {
165: childModel = (SwingMLModel) theChildren.get(i);
166: childModel.validate();
167: theRenderer = this .getRenderer(childModel);
168: if (theRenderer != null) {
169: Container childContainer = theRenderer.render(
170: childModel, container);
171: // register this model to this container for later retrieval
172: if (childContainer != null) {
173: childModel.setContainer(childContainer);
174: SwingMLModelToContainerRegistry
175: .register(childModel);
176: }
177:
178: if (childContainer instanceof JComponent) {
179: // notify of rendering complete (for rendering listeners -
180: // i.e. ComponentListener.componentRendered)
181: EventHandler.getInstance().handleEvent(childModel,
182: Constants.COMPONENT_RENDERED,
183: (JComponent) childContainer);
184: }
185: }
186: }
187: // register this model to this container for later retrieval
188: SwingMLModelToContainerRegistry.register(model);
189: }
190:
191: public Class removeRenderer(Class model) {
192: return (Class) this.renderers.remove(model);
193: }
194: }
|