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.io.IOException;
033:
034: import javax.swing.Icon;
035: import javax.swing.JTabbedPane;
036:
037: import com.jeta.forms.gui.beans.JETABean;
038: import com.jeta.forms.gui.common.FormException;
039: import com.jeta.forms.gui.common.FormUtils;
040: import com.jeta.forms.gui.components.ContainedFormFactory;
041: import com.jeta.forms.gui.form.FormComponent;
042: import com.jeta.forms.logger.FormsLogger;
043: import com.jeta.forms.store.JETAObjectInput;
044: import com.jeta.forms.store.JETAObjectOutput;
045: import com.jeta.forms.store.memento.FormMemento;
046: import com.jeta.forms.store.memento.StateRequest;
047: import com.jeta.open.registry.JETARegistry;
048:
049: /**
050: * A <code>TabProperty</code> defines a single tab in a JTabbedPane. Included
051: * in this description is the tab title, icon, and the content of the tab -
052: * which is a form. This class is used by <code>TabbedPaneProperties</code> to
053: * intialize a JTabbedPane during runtime.
054: *
055: * @author Jeff Tassin
056: */
057: public class TabProperty extends JETAProperty {
058: static final long serialVersionUID = 2375434406561274626L;
059:
060: /**
061: * The current version number of this class
062: */
063: public static final int VERSION = 1;
064:
065: /**
066: * The tab title
067: */
068: private String m_title;
069:
070: /**
071: * The icon for the tab.
072: */
073: private IconProperty m_icon_property;
074:
075: /**
076: * A cached value for the form content for this tab
077: */
078: private transient FormComponent m_form;
079:
080: /**
081: * The form state. This gets serialized to the persistent store and is used
082: * to create and initialize the cached form <code>m_form</code> value.
083: */
084: private FormMemento m_memento;
085:
086: /**
087: * The name of this property
088: */
089: public static final String PROPERTY_ID = "tab";
090:
091: /**
092: * Creates an unitialized <code>TabProperty</code> instance
093: */
094: public TabProperty() {
095: super (PROPERTY_ID);
096: }
097:
098: /**
099: * Creates a <code>TabProperty</code> instance with the given title.
100: */
101: public TabProperty(String title) {
102: super (PROPERTY_ID);
103: m_title = title;
104: }
105:
106: /**
107: * Returns the form component contained by this tab property.
108: *
109: * @return the form component contained by this tab property.
110: */
111: public FormComponent getForm() throws FormException {
112: if (m_form == null) {
113: ContainedFormFactory factory = (ContainedFormFactory) JETARegistry
114: .lookup(ContainedFormFactory.COMPONENT_ID);
115: FormUtils.safeAssert(factory != null);
116: m_form = factory.createContainedForm(JTabbedPane.class,
117: m_memento);
118: }
119: return m_form;
120: }
121:
122: /**
123: * Returns a memento object that completely defines the state of the form
124: * contained by this tab.
125: *
126: * @return the memento for the form contained by this tab.
127: */
128: public FormMemento getFormMemento() throws FormException {
129: if (m_form != null)
130: return m_form.getExternalState(StateRequest.DEEP_COPY);
131: else
132: return m_memento;
133: }
134:
135: /**
136: * Returns the title of this tab.
137: *
138: * @return the title of this tab.
139: */
140: public String getTitle() {
141: return m_title;
142: }
143:
144: /**
145: * Returns the icon property for this tab. This property defines the icon
146: * used by this tab.
147: *
148: * @return the icon property for this tab.
149: */
150: public IconProperty getIconProperty() {
151: return m_icon_property;
152: }
153:
154: /**
155: * Return the underlying icon that is specified by the IconProperty. This
156: * value can be null if no icon is specified.
157: *
158: * @return the underlying icon.
159: */
160: public Icon icon() {
161: return m_icon_property;
162: }
163:
164: /**
165: * Sets the icon property for this tab.
166: *
167: * @param iprop
168: * the icon property.
169: */
170: public void setIconProperty(IconProperty iprop) {
171: if (m_icon_property == null)
172: m_icon_property = new IconProperty();
173: m_icon_property.setValue(iprop);
174: }
175:
176: /**
177: * Sets this property to that of another TabProperty
178: *
179: * @param prop
180: * a TabProperty instance.
181: */
182: public void setValue(Object prop) {
183: if (prop instanceof TabProperty) {
184: TabProperty tp = (TabProperty) prop;
185: m_title = tp.m_title;
186: m_form = tp.m_form;
187: m_memento = tp.m_memento;
188:
189: if (m_icon_property == null)
190: m_icon_property = new IconProperty();
191: m_icon_property.setValue(tp.m_icon_property);
192: }
193: }
194:
195: /**
196: * Sets the title for this tab.
197: *
198: * @param title
199: * the tab title
200: */
201: public void setTitle(String title) {
202: m_title = title;
203: }
204:
205: /**
206: * Updates the bean with the current value of this property
207: */
208: public void updateBean(JETABean jbean) {
209: // no op
210: }
211:
212: /**
213: * JETAPersistable Implementation
214: */
215: public void read(JETAObjectInput in) throws ClassNotFoundException,
216: IOException {
217: super .read(in.getSuperClassInput());
218: int version = in.readVersion();
219: m_title = (String) in.readObject("title");
220: m_icon_property = (IconProperty) in.readObject("icon");
221: m_memento = (FormMemento) in.readObject("form");
222: m_form = null;
223: }
224:
225: /**
226: * JETAPersistable Implementation
227: */
228: public void write(JETAObjectOutput out) throws IOException {
229: super .write(out.getSuperClassOutput(JETAProperty.class));
230: out.writeVersion(VERSION);
231: out.writeObject("title", m_title);
232: out.writeObject("icon", m_icon_property);
233: /**
234: * This is a hack to obtain the state request which is set by the
235: * caller. There is no way to obtain this value other than getting it
236: * from the registry. The caller should have set this value. This is
237: * needed during preview when in design mode; otherwise we won't get the
238: * most current state for linked forms if they are opened in the
239: * designer
240: */
241: StateRequest state_req = StateRequest.SHALLOW_COPY;
242: Object obj = com.jeta.open.registry.JETARegistry
243: .lookup(StateRequest.COMPONENT_ID);
244: if (obj instanceof StateRequest)
245: state_req = (StateRequest) obj;
246:
247: try {
248: getForm();
249: if (m_form != null) {
250: out.writeObject("form", m_form
251: .getExternalState(state_req));
252: } else {
253: out.writeObject("form", null);
254: }
255: } catch (Exception e) {
256: FormsLogger.severe(e);
257: out.writeObject("form", null);
258: }
259: }
260:
261: }
|