001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.components;
020:
021: import java.awt.Component;
022: import java.awt.Container;
023:
024: import javax.swing.JTabbedPane;
025:
026: import com.jeta.forms.gui.common.FormException;
027: import com.jeta.forms.gui.common.FormUtils;
028: import com.jeta.forms.gui.components.ComponentSource;
029: import com.jeta.forms.gui.components.ContainedFormFactory;
030: import com.jeta.forms.gui.form.FormComponent;
031: import com.jeta.forms.gui.form.FormContainerComponent;
032: import com.jeta.forms.gui.form.GridView;
033: import com.jeta.forms.store.memento.FormMemento;
034: import com.jeta.open.registry.JETARegistry;
035: import com.jgoodies.forms.layout.CellConstraints;
036: import com.jgoodies.forms.layout.ColumnSpec;
037: import com.jgoodies.forms.layout.RowSpec;
038:
039: public class DefaultContainedFormFactory implements
040: ContainedFormFactory {
041: /**
042: * ctor
043: */
044: public DefaultContainedFormFactory() {
045:
046: }
047:
048: /**
049: * Creates a form that is meant to be contained in a Swing container. This
050: * is form forms that can be edited in-place in the designer.
051: */
052: public FormComponent createContainedForm(Class swingClass,
053: FormMemento fm) throws FormException {
054: FormUtils.safeAssert(swingClass == JTabbedPane.class);
055:
056: ComponentSource compsrc = (ComponentSource) JETARegistry
057: .lookup(ComponentSource.COMPONENT_ID);
058: FormUtils.safeAssert(compsrc != null);
059:
060: EmbeddedFormComponentFactory embedded_fac = new EmbeddedFormComponentFactory(
061: compsrc);
062: FormComponent form = null;
063:
064: if (fm == null) {
065: form = embedded_fac.create(compsrc, "", null, 3, 3, true);
066: GridView.fillCells(form.getChildView(), compsrc);
067: } else {
068: form = FormComponent.create();
069: form.setState(fm);
070: }
071: return form;
072:
073: }
074:
075: /**
076: * This method creates a top-level parent form that is used to contain a
077: * form that we can edit. This is used in two cases. In the first, we use a
078: * top level form in the FormEditor. In the second, we use the top-level
079: * form in contained forms (e.g. a form that is contained in a JTabbedPane
080: * tab ).
081: *
082: * @param parent
083: * the object that will contain the top-level parent
084: * @param compsrc
085: * the component source
086: * @param form
087: * the form that will be contained by the top-level parent
088: */
089: public FormComponent createTopParent(Container container,
090: ComponentSource compsrc, FormComponent form)
091: throws FormException {
092: EmbeddedFormComponentFactory factory = new EmbeddedFormComponentFactory(
093: compsrc);
094: FormComponent parent = (FormComponent) factory.create(compsrc,
095: "formeditor.top.parent", null, 1, 1, true);
096: parent.setTopLevelForm(true);
097: parent.setControlButtonsVisible(false);
098: form.setControlButtonsVisible(true);
099: GridView view = parent.getChildView();
100: view.setGridVisible(false);
101: view.setRowSpec(1, new RowSpec("fill:pref:grow"));
102: view.setColumnSpec(1, new ColumnSpec("fill:pref:grow"));
103: CellConstraints cc = new CellConstraints();
104: view.addComponent(form, cc.xy(1, 1));
105: parent.setBorder(javax.swing.BorderFactory.createEmptyBorder(8,
106: 8, 8, 8));
107:
108: /**
109: * Now get the FormContainerComponent instance and add it as a listener
110: * to the newly created form. This is needed so that GridComponent
111: * events such as cell_selected and cell_changed are properly propagated
112: * up the form hierarchy.
113: */
114: FormContainerComponent fcc = getFormContainerComponent(container);
115: if (fcc != null) {
116: assert (fcc.getBeanDelegate() instanceof javax.swing.JTabbedPane);
117: view.addListener(fcc);
118: }
119:
120: if ((container instanceof javax.swing.JTabbedPane)
121: && FormUtils.isDesignMode()) {
122: if (fcc == null) {
123: System.err
124: .println("DefaultContainedFormFactory encountered invalid container: "
125: + container);
126: }
127: FormUtils.safeAssert(fcc != null);
128: }
129:
130: return parent;
131: }
132:
133: /**
134: * @return the FormContainerComponent that is an ancestor of the given
135: * parent.
136: */
137: private FormContainerComponent getFormContainerComponent(
138: Component parent) {
139: while (parent != null && !(parent instanceof java.awt.Window)) {
140: if (parent instanceof FormContainerComponent)
141: return (FormContainerComponent) parent;
142:
143: parent = parent.getParent();
144: }
145: return null;
146: }
147:
148: }
|