001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.romaframework.aspect.view.echo2.component;
017:
018: import java.beans.PropertyChangeEvent;
019: import java.beans.PropertyChangeListener;
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import nextapp.echo2.app.Border;
024: import nextapp.echo2.app.Color;
025: import nextapp.echo2.app.Component;
026: import nextapp.echo2.app.Insets;
027: import nextapp.echo2.app.Label;
028: import nextapp.echo2.app.Style;
029: import nextapp.echo2.app.event.ChangeListener;
030:
031: import org.romaframework.aspect.view.ViewCallback;
032: import org.romaframework.aspect.view.echo2.form.EntityForm;
033: import org.romaframework.aspect.view.echo2.form.FormUtil;
034:
035: import echopointng.TabbedPane;
036: import echopointng.tabbedpane.DefaultTabImageRenderer;
037: import echopointng.tabbedpane.DefaultTabModel;
038:
039: /**
040: * Extension of EchoPointNG TabbedPane component. Allow to configure by XML stylesheet:
041: * <ul>
042: * <li><b>TabbedPane</b> component</li>
043: * <li>Underlying <b>DefaultTabImageRenderer</b> component</li>
044: * <li>Underlying <b>DefaultTabModel</b> component</li>
045: * </ul>
046: * Example: <br/><br/> <font face="courier"> <style name="Object"
047: * type="org.romaframework.aspect.view.echo2.component.DynaTabbedPane"><br/> <properties><br/> <property
048: * name="activeTabBackgroundColor" value="#fbfbfb" /><br/> <property name="activeTabForegroundColor" value="#000000" /><br/> <property
049: * name="inactiveTabBackgroundColor" value="#e1e1e1" /><br/> <property name="inactiveTabForegroundColor"
050: * value="#000000" /><br/> <property name="tabPlacement" value="top" /><br/> <property
051: * name="insetsTab" value="8px 8px" /><br/> <property name="border"><br/> <border
052: * color="#fbfbfb" size="2px" style="outset" /><br/> </property><br/> </properties><br/></style> </font>
053: *
054: * @author Luca Garulli (luca.garulli@assetdata.it)
055: */
056: public class DynaTabbedPane extends TabbedPane implements
057: PropertyChangeListener {
058:
059: public static final String PROPERTY_ACTIVE_TAB_BACKGROUND_COLOR = "activeTabBackgroundColor";
060: public static final String PROPERTY_ACTIVE_TAB_FOREGROUND_COLOR = "activeTabForegroundColor";
061: public static final String PROPERTY_INACTIVE_TAB_BACKGROUND_COLOR = "inactiveTabBackgroundColor";
062: public static final String PROPERTY_INACTIVE_TAB_FOREGROUND_COLOR = "inactiveTabForegroundColor";
063: public static final String PROPERTY_BORDER_TAB_COLOR = "borderTabColor";
064: public static final String PROPERTY_INSETS_TAB = "insetsTab";
065: protected Color activeTabBackgroundColor = Color.LIGHTGRAY;
066: protected Color activeTabForegroundColor = Color.BLACK;
067: protected Color inactiveTabBackgroundColor = Color.WHITE;
068: protected Color inactiveTabForegroundColor = Color.BLACK;
069: protected Color borderTabColor = Color.BLACK;
070: protected Insets insetsTab = new Insets(5);
071: protected DefaultTabModel tabModel;
072: protected boolean lazyRendering;
073: protected Map<EntityForm, Boolean> renderedComponents;
074:
075: public DynaTabbedPane(boolean iLazyRendering) {
076: lazyRendering = iLazyRendering;
077:
078: tabModel = new DefaultTabModel();
079: setModel(tabModel);
080:
081: addPropertyChangeListener(this );
082:
083: initLazyMap();
084: }
085:
086: public void addTab(String iText, Component childForm) {
087: Label label = new Label(iText);
088: if (activeTabForegroundColor != null)
089: label.setForeground(activeTabForegroundColor);
090: tabModel.addTab(label, childForm);
091: }
092:
093: public void addChangeListener(ChangeListener iEventListener) {
094: tabModel.addChangeListener(iEventListener);
095: }
096:
097: @Override
098: public void setStyle(Style iStyle) {
099: // LOAD ALL OWN PROPERTIES
100: if (iStyle.isPropertySet(PROPERTY_ACTIVE_TAB_BACKGROUND_COLOR))
101: activeTabBackgroundColor = (Color) iStyle
102: .getProperty(PROPERTY_ACTIVE_TAB_BACKGROUND_COLOR);
103: if (iStyle.isPropertySet(PROPERTY_ACTIVE_TAB_FOREGROUND_COLOR))
104: activeTabForegroundColor = (Color) iStyle
105: .getProperty(PROPERTY_ACTIVE_TAB_FOREGROUND_COLOR);
106: if (iStyle
107: .isPropertySet(PROPERTY_INACTIVE_TAB_BACKGROUND_COLOR))
108: inactiveTabBackgroundColor = (Color) iStyle
109: .getProperty(PROPERTY_INACTIVE_TAB_BACKGROUND_COLOR);
110: if (iStyle
111: .isPropertySet(PROPERTY_INACTIVE_TAB_FOREGROUND_COLOR))
112: inactiveTabForegroundColor = (Color) iStyle
113: .getProperty(PROPERTY_INACTIVE_TAB_FOREGROUND_COLOR);
114: if (iStyle.isPropertySet(PROPERTY_BORDER_TAB_COLOR))
115: borderTabColor = (Color) iStyle
116: .getProperty(PROPERTY_BORDER_TAB_COLOR);
117: else {
118: if (iStyle.isPropertySet(PROPERTY_BORDER))
119: borderTabColor = ((Border) iStyle
120: .getProperty(PROPERTY_BORDER)).getColor();
121: }
122: int tabPlacement = getTabPlacement();
123: if (iStyle.isPropertySet(PROPERTY_TAB_PLACEMENT))
124: tabPlacement = (Integer) iStyle
125: .getProperty(PROPERTY_TAB_PLACEMENT);
126: // SET IMAGE RENDERED
127: DefaultTabImageRenderer imageRender = DefaultTabImageRenderer
128: .getInstance(activeTabBackgroundColor,
129: inactiveTabBackgroundColor, borderTabColor,
130: tabPlacement);
131: if (iStyle.isPropertySet(PROPERTY_INSETS_TAB))
132: insetsTab = (Insets) iStyle
133: .getProperty(PROPERTY_INSETS_TAB);
134: imageRender.setInsets(insetsTab);
135: tabModel.setTabImageRenderer(imageRender);
136: super .setStyle(iStyle);
137: }
138:
139: public Color getActiveTabBackgroundColor() {
140: return activeTabBackgroundColor;
141: }
142:
143: public void setActiveTabBackgroundColor(Color activeTabColor) {
144: this .activeTabBackgroundColor = activeTabColor;
145: }
146:
147: public Color getBorderTabColor() {
148: return borderTabColor;
149: }
150:
151: public void setBorderTabColor(Color borderTabColor) {
152: this .borderTabColor = borderTabColor;
153: }
154:
155: public Color getInactiveTabBackgroundColor() {
156: return inactiveTabBackgroundColor;
157: }
158:
159: public void setInactiveTabBackgroundColor(Color inactiveTabColor) {
160: this .inactiveTabBackgroundColor = inactiveTabColor;
161: }
162:
163: public Insets getInsetsTab() {
164: return insetsTab;
165: }
166:
167: public void setInsetsTab(Insets insetsTab) {
168: this .insetsTab = insetsTab;
169: }
170:
171: public Color getActiveTabForegroundColor() {
172: return activeTabForegroundColor;
173: }
174:
175: public void setActiveTabForegroundColor(
176: Color activeTabForegroundColor) {
177: this .activeTabForegroundColor = activeTabForegroundColor;
178: }
179:
180: public Color getInactiveTabForegroundColor() {
181: return inactiveTabForegroundColor;
182: }
183:
184: public void setInactiveTabForegroundColor(
185: Color inactiveTabForegroundColor) {
186: this .inactiveTabForegroundColor = inactiveTabForegroundColor;
187: }
188:
189: public void clear() {
190: renderedComponents.clear();
191: while (getComponentCount() > 0)
192: tabModel.removeTabAt(0);
193: }
194:
195: @Override
196: public void setEnabled(boolean iNewValue) {
197: }
198:
199: public boolean renderLazy(EntityForm iComponent) {
200: if (!lazyRendering)
201: return false;
202:
203: if (renderedComponents.containsKey(iComponent))
204: return false;
205:
206: renderedComponents.put(iComponent, Boolean.TRUE);
207: return true;
208: }
209:
210: public boolean isLazyRendering() {
211: return lazyRendering;
212: }
213:
214: public void setLazyRendering(boolean lazyRendering) {
215: this .lazyRendering = lazyRendering;
216:
217: initLazyMap();
218: }
219:
220: public void propertyChange(PropertyChangeEvent iEvent) {
221: if (iEvent.getSource() instanceof DynaTabbedPane) {
222: Object oldValue = iEvent.getOldValue();
223: Object newValue = iEvent.getNewValue();
224: if (oldValue != null && oldValue instanceof EntityForm) {
225: EntityForm form = (EntityForm) oldValue;
226:
227: Object content = form.getContent();
228: if (content instanceof ViewCallback)
229: ((ViewCallback) content).onDispose();
230:
231: } else if (newValue != null
232: && newValue instanceof EntityForm) {
233: EntityForm form = (EntityForm) newValue;
234:
235: if (renderLazy(form)) {
236: form.renderContent();
237: }
238:
239: FormUtil.invokeOnShow(form.getContent());
240: }
241: }
242: }
243:
244: private void initLazyMap() {
245: if (lazyRendering)
246: renderedComponents = new HashMap<EntityForm, Boolean>();
247: }
248: }
|