001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.store.properties;
031:
032: import java.awt.Component;
033: import java.io.IOException;
034: import java.util.Collection;
035: import java.util.Iterator;
036: import java.util.LinkedList;
037:
038: import javax.swing.JTabbedPane;
039:
040: import com.jeta.forms.gui.beans.JETABean;
041: import com.jeta.forms.gui.common.FormException;
042: import com.jeta.forms.gui.common.FormUtils;
043: import com.jeta.forms.gui.components.ComponentSource;
044: import com.jeta.forms.gui.components.ContainedFormFactory;
045: import com.jeta.forms.gui.form.FormComponent;
046: import com.jeta.forms.logger.FormsLogger;
047: import com.jeta.forms.store.JETAObjectInput;
048: import com.jeta.forms.store.JETAObjectOutput;
049: import com.jeta.open.i18n.I18N;
050: import com.jeta.open.registry.JETARegistry;
051: import com.jeta.open.support.EmptyCollection;
052:
053: /**
054: * A <code>TabbedPaneProperties</code> object is responsible for adding tabs
055: * to a JTabbedPane at runtime. Since a JTabbedPane does not have a <i>tabs</i>
056: * property, this custom property was created to provide a convienient way to
057: * work with JTabbedPanes in the designer.
058: *
059: * @author Jeff Tassin
060: */
061: public class TabbedPaneProperties extends JETAProperty {
062: static final long serialVersionUID = 3096566296606257673L;
063:
064: /**
065: * The current version number of this class
066: */
067: public static final int VERSION = 1;
068:
069: /**
070: * The Collection of tabs (TabProperty objects) in this property.
071: */
072: private LinkedList m_items;
073:
074: /**
075: * The name of this property
076: */
077: public static final String PROPERTY_ID = "tabs";
078:
079: /**
080: * Creates an unitialized <code>TabbedPaneProperties</code> instance.
081: */
082: public TabbedPaneProperties() {
083: super (PROPERTY_ID);
084: }
085:
086: /**
087: * Adds a tab property to this tabbed pane.
088: *
089: * @param prop
090: * the tab property to add.
091: */
092: public void addTab(TabProperty prop) {
093: if (m_items == null)
094: m_items = new LinkedList();
095:
096: m_items.add(prop);
097: }
098:
099: /**
100: * Returns a collection of TabProperty objects that make up the tabs in the
101: * tab pane.
102: *
103: * @return the tabs (TabProperty objects) in this property
104: */
105: public Collection getTabs() {
106: if (m_items != null)
107: return m_items;
108: else
109: return EmptyCollection.getInstance();
110: }
111:
112: /**
113: * Sets this property to that of another TabbedPaneProperties object.
114: *
115: * @param prop
116: * a TabbedPaneProperties object.
117: */
118: public void setValue(Object prop) {
119: if (prop instanceof TabbedPaneProperties) {
120: TabbedPaneProperties tpp = (TabbedPaneProperties) prop;
121: m_items = new LinkedList();
122: if (tpp.m_items != null) {
123: Iterator iter = tpp.m_items.iterator();
124: while (iter.hasNext()) {
125: TabProperty tp = (TabProperty) iter.next();
126: TabProperty new_tp = new TabProperty();
127: new_tp.setValue(tp);
128: m_items.add(new_tp);
129: }
130: }
131: } else {
132: assert (false);
133: }
134: }
135:
136: /**
137: * Updates the bean with the current value of this property. In this case
138: * the tabs specified in this property are added to the the associated
139: * JTabbedPane bean. Any pre-existing tabs in the bean are removed.
140: */
141: public void updateBean(JETABean jbean) {
142: Component comp = null;
143: if (jbean != null)
144: comp = jbean.getDelegate();
145:
146: if (comp instanceof JTabbedPane) {
147: JTabbedPane tpane = (JTabbedPane) comp;
148: int selected_tab = tpane.getSelectedIndex();
149: tpane.removeAll();
150:
151: if (m_items != null) {
152:
153: Iterator iter = m_items.iterator();
154: while (iter.hasNext()) {
155: TabProperty tp = (TabProperty) iter.next();
156: if (FormUtils.isDesignMode()) {
157: try {
158: /**
159: * if we are in design mode, forward the call to the
160: * correct factory so any mouse and keyboard
161: * handlers can be added to the form
162: */
163: ContainedFormFactory factory = (ContainedFormFactory) JETARegistry
164: .lookup(ContainedFormFactory.COMPONENT_ID);
165: ComponentSource compsrc = (ComponentSource) JETARegistry
166: .lookup(ComponentSource.COMPONENT_ID);
167: FormUtils.safeAssert(compsrc != null);
168: FormUtils.safeAssert(factory != null);
169: FormComponent topparent = factory
170: .createTopParent(tpane, compsrc, tp
171: .getForm());
172: tpane.addTab(tp.getTitle(), tp.icon(),
173: topparent);
174: } catch (FormException e) {
175: FormsLogger.severe(e);
176: }
177: } else {
178: try {
179: FormComponent fc = FormComponent.create();
180: fc.setState(tp.getFormMemento());
181: fc.setTopLevelForm(true);
182: tpane.addTab(tp.getTitle(), tp.icon(), fc);
183: } catch (FormException e) {
184: FormsLogger.severe(e);
185: }
186: }
187: }
188:
189: if ((selected_tab >= 0)
190: && (selected_tab < tpane.getTabCount())) {
191: tpane.setSelectedIndex(selected_tab);
192: }
193: }
194:
195: /**
196: * if we are in design mode, add a default tab if no tabs are
197: * currently defined. The default tab will automatically be removed
198: * if a user adds a tab in the designer
199: */
200: if (FormUtils.isDesignMode()) {
201: if (tpane.getTabCount() == 0) {
202: tpane.addTab(I18N.getLocalizedMessage("Tab Pane"),
203: new javax.swing.JLabel(""));
204: }
205: }
206:
207: }
208: }
209:
210: /**
211: * JETAPersistable Implementation
212: */
213: public void read(JETAObjectInput in) throws ClassNotFoundException,
214: IOException {
215: super .read(in.getSuperClassInput());
216: int version = in.readVersion();
217: m_items = (LinkedList) in.readObject("items");
218: }
219:
220: /**
221: * JETAPersistable Implementation
222: */
223: public void write(JETAObjectOutput out) throws IOException {
224: super .write(out.getSuperClassOutput(JETAProperty.class));
225: out.writeVersion(VERSION);
226: out.writeObject("items", m_items);
227: }
228:
229: }
|