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.gui.beans.factories;
031:
032: import java.awt.Component;
033:
034: import javax.swing.DefaultListModel;
035: import javax.swing.JList;
036: import javax.swing.ListModel;
037:
038: import com.jeta.forms.gui.beans.BeanProperties;
039: import com.jeta.forms.gui.beans.JETABean;
040: import com.jeta.forms.gui.common.FormException;
041: import com.jeta.forms.store.properties.ItemsProperty;
042:
043: /**
044: * Factory for instantiating a JList bean.
045: *
046: * @author Jeff Tassin
047: */
048: public class ListBeanFactory extends JComponentBeanFactory {
049: /**
050: * The default list model.
051: */
052: private ListModel m_default_model;
053:
054: /**
055: * ctor
056: */
057: public ListBeanFactory() {
058: super (JList.class);
059: setScrollable(true);
060: }
061:
062: /**
063: * Override to set custom properties for your factory
064: */
065: public void defineProperties(BeanProperties props) {
066: super .defineProperties(props);
067: props.register(new ItemsProperty());
068: props.removeProperty("selectedIndex");
069: }
070:
071: /**
072: * Gets the deault list model
073: */
074: private ListModel getDefaultListModel() {
075: if (m_default_model == null)
076: m_default_model = new JList().getModel();
077:
078: return m_default_model;
079: }
080:
081: public JETABean createBean(String compName,
082: boolean instantiateBean, boolean setDefaults)
083: throws FormException {
084: JETABean jbean = super .createBean(compName, instantiateBean,
085: setDefaults);
086: Component comp = jbean.getDelegate();
087: if (comp instanceof JList) {
088: JList list = (JList) comp;
089: try {
090: /**
091: * Since we allow users to register specialized Java Beans that
092: * may extend JList, we don't want to override the ListModel
093: * unless is is not being set by the extended class. If it is
094: * not being set, we set the list's model to a DefaultListModel
095: * so the ItemsProperty can add/remove elements from the list as
096: * needed.
097: */
098: ListModel model = list.getModel();
099: if (model == null) {
100: list.setModel(new DefaultListModel());
101: } else {
102: ListModel defmodel = getDefaultListModel();
103: if (defmodel != null
104: && defmodel.getClass() == model.getClass()) {
105: list.setModel(new DefaultListModel());
106: }
107: }
108: } catch (Exception e) {
109: e.printStackTrace();
110: }
111: }
112: return jbean;
113: }
114: }
|