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.BorderLayout;
033: import java.awt.Component;
034: import java.awt.Container;
035: import java.awt.Dimension;
036: import java.io.IOException;
037:
038: import javax.swing.JScrollPane;
039:
040: import com.jeta.forms.gui.beans.JETABean;
041: import com.jeta.forms.gui.common.FormUtils;
042: import com.jeta.forms.logger.FormsLogger;
043: import com.jeta.forms.store.JETAObjectInput;
044: import com.jeta.forms.store.JETAObjectOutput;
045:
046: /**
047: * A custom property for scrollable Swing components such as lists, tables, and
048: * text areas. We provide a scroll bars property for these components instead of
049: * requiring the user to create a separate JScrollPane in the designer. This
050: * property will automatically create a JScrollPane and add the Java bean during
051: * runtime.
052: *
053: * @author Jeff Tassin
054: */
055: public class ScrollBarsProperty extends JETAProperty {
056: static final long serialVersionUID = 9130862301613978006L;
057:
058: /**
059: * version number for this class
060: */
061: public static final int VERSION = 3;
062:
063: /**
064: * The vertical scroll bar policy.
065: *
066: * @see #setVerticalScrollBarPolicy
067: */
068: private int m_vert_policy = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
069:
070: /**
071: * The horizontal scroll bar policy.
072: *
073: * @see #setHorizontalScrollBarPolicy
074: */
075: private int m_horz_policy = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
076:
077: /**
078: * The name for the scroll pane.
079: */
080: private String m_scroll_name;
081:
082: /**
083: * The border for the scroll pane. If null, use the default scroll pane
084: * border.
085: */
086: private CompoundBorderProperty m_scroll_border = null;
087:
088: public static final String PROPERTY_ID = "scollBars";
089:
090: /**
091: * Creates a <code>ScrollBarsProperty</code> instances with the default
092: * horizontal and vertical scroll bar policies (AS_NEEDED).
093: */
094: public ScrollBarsProperty() {
095: super (PROPERTY_ID);
096: m_vert_policy = JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
097: m_horz_policy = JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
098: }
099:
100: /**
101: * Creates a <code>ScrollBarsProperty</code> instances with the specified
102: * horizontal and vertical scroll bar policies.
103: *
104: * @param vertpolicy
105: * the vertical scroll bar policy.
106: * @param horzpolicy
107: * the horizontal scroll bar policy.
108: */
109: public ScrollBarsProperty(int vertpolicy, int horzpolicy) {
110: super (PROPERTY_ID);
111: m_vert_policy = vertpolicy;
112: m_horz_policy = horzpolicy;
113: }
114:
115: /**
116: * Returns the property that defines the border to apply to the scroll pane.
117: *
118: * @return the scroll pane border attributes.
119: */
120: public CompoundBorderProperty getBorderProperty() {
121: return m_scroll_border;
122: }
123:
124: /**
125: * Returns the vertical scroll bar policy value.
126: *
127: * @return the <code>verticalScrollBarPolicy</code> property
128: * @see #setVerticalScrollBarPolicy
129: */
130: public int getVerticalScrollBarPolicy() {
131: return m_vert_policy;
132: }
133:
134: /**
135: * Returns the horizontal scroll bar policy value.
136: *
137: * @return the <code>horizontalScrollBarPolicy</code> property
138: * @see #setHorizontalScrollBarPolicy
139: */
140: public int getHorizontalScrollBarPolicy() {
141: return m_horz_policy;
142: }
143:
144: /**
145: * Returns the name of the JScrollPane component.
146: *
147: * @return the name of the JScrollPane.
148: */
149: public String getScrollName() {
150: return m_scroll_name;
151: }
152:
153: /**
154: * Returns true if both the vertical and horizontal policies are NOT equal
155: * to NEVER. If a property is scrollable then a JScrollPane will be created.
156: * If this property is not scrollabe, a JScrollPane instance will not be
157: * created.
158: *
159: * @return true if both the vertical and horizontal policies are NOT equal
160: * to NEVER
161: */
162: public boolean isScrollable() {
163: boolean result = !(getVerticalScrollBarPolicy() == JScrollPane.VERTICAL_SCROLLBAR_NEVER && getHorizontalScrollBarPolicy() == JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
164: return result;
165: }
166:
167: /**
168: * Sets the horizontal scrollbar policy. The options are:<code>JScrollPane</code>
169: * <code>HORIZONTAL_SCROLLBAR_AS_NEEDED</code>
170: * <code>HORIZONTAL_SCROLLBAR_NEVER</code>
171: * <code>HORIZONTAL_SCROLLBAR_ALWAYS</code>
172: *
173: * @param policy
174: * the horizontal scroll bar policy
175: */
176: public void setHorizontalScrollBarPolicy(int policy) {
177: m_horz_policy = policy;
178: }
179:
180: /**
181: * Sets the vertical scrollbar policy. The options are:<code>JScrollPane</code>
182: * <code>VERTICAL_SCROLLBAR_AS_NEEDED</code>
183: * <code>VERTICAL_SCROLLBAR_NEVER</code>
184: * <code>VERTICAL_SCROLLBAR_ALWAYS</code>
185: *
186: * @param policy
187: * the vertical scroll bar policy
188: */
189: public void setVerticalScrollBarPolicy(int policy) {
190: m_vert_policy = policy;
191: }
192:
193: /**
194: * Sets the name for the JScrollPane component.
195: */
196: public void setScrollName(String scrollName) {
197: m_scroll_name = scrollName;
198: }
199:
200: /**
201: * Sets the property that defines the border to apply to the scroll pane.
202: *
203: * @return the scroll pane border attributes.
204: */
205: public void setBorderProperty(CompoundBorderProperty prop) {
206: if (prop == null) {
207: m_scroll_border = null;
208: } else {
209: if (m_scroll_border == null)
210: m_scroll_border = new CompoundBorderProperty();
211:
212: m_scroll_border.setValue(prop);
213: }
214: }
215:
216: /**
217: * Sets this property to that of another ScrollBarsProperty.
218: */
219: public void setValue(Object prop) {
220: if (prop instanceof ScrollBarsProperty) {
221: ScrollBarsProperty sb = (ScrollBarsProperty) prop;
222: m_vert_policy = sb.m_vert_policy;
223: m_horz_policy = sb.m_horz_policy;
224: m_scroll_name = sb.m_scroll_name;
225:
226: if (m_scroll_border == null)
227: m_scroll_border = new CompoundBorderProperty();
228:
229: if (sb.m_scroll_border == null)
230: m_scroll_border.addBorder(new DefaultBorderProperty());
231: else
232: m_scroll_border.setValue(sb.m_scroll_border);
233: }
234: }
235:
236: /**
237: * JETAProperty implementation. Removes the Java bean from the form and adds
238: * it to a JScrollPane. The JScrollPane is then added to the form in place
239: * of the Java bean.
240: */
241: public void updateBean(JETABean bean) {
242: Component comp = null;
243: if (bean != null)
244: comp = bean.getDelegate();
245:
246: bean.removeAll();
247: if (isScrollable()) {
248: JScrollPane scroll = null;
249:
250: /** check if component's parent is a previously created scrollbar */
251: Container c = comp.getParent();
252: if (c instanceof javax.swing.JViewport) {
253: scroll = (JScrollPane) c.getParent();
254: } else {
255: final boolean bdesign = FormUtils.isDesignMode();
256: final Dimension pref_sz = new Dimension(80, 80);
257: scroll = new JScrollPane(comp);
258: /**
259: * we need to set the preferred size otherwise the scroll pane
260: * could become too large in some cases. The reason is that the
261: * scrollpanes preferred size is related to the internal size of
262: * the scrollable area and not the child component
263: */
264: scroll.setPreferredSize(pref_sz);
265: }
266:
267: scroll.setHorizontalScrollBarPolicy(m_horz_policy);
268: scroll.setVerticalScrollBarPolicy(m_vert_policy);
269:
270: if (comp.isOpaque())
271: scroll.setOpaque(true);
272:
273: if (m_scroll_name != null)
274: scroll.setName(m_scroll_name);
275:
276: if (m_scroll_border != null) {
277: try {
278: scroll.setBorder(m_scroll_border
279: .createBorder(scroll));
280: } catch (Exception e) {
281: FormsLogger.severe(e);
282: }
283: }
284: bean.add(scroll, BorderLayout.CENTER);
285: } else {
286: bean.add(comp, BorderLayout.CENTER);
287: }
288: }
289:
290: /**
291: * JETAPersistable Implementation
292: */
293: public void read(JETAObjectInput in) throws ClassNotFoundException,
294: IOException {
295: super .read(in.getSuperClassInput());
296:
297: int version = in.readVersion();
298: m_vert_policy = in.readInt("verticalpolicy");
299: m_horz_policy = in.readInt("horizontalpolicy");
300: if (version >= 2) {
301: m_scroll_name = (String) in.readObject("scrollname");
302: }
303: if (version >= 3) {
304: m_scroll_border = (CompoundBorderProperty) in
305: .readObject("border");
306: }
307: }
308:
309: /**
310: * JETAPersistable Implementation
311: */
312: public void write(JETAObjectOutput out) throws IOException {
313: super .write(out.getSuperClassOutput(JETAProperty.class));
314: out.writeVersion(VERSION);
315: out.writeInt("verticalpolicy", m_vert_policy);
316: out.writeInt("horizontalpolicy", m_horz_policy);
317: out.writeObject("scrollname", m_scroll_name);
318: out.writeObject("border", m_scroll_border);
319: }
320:
321: }
|