|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.form; import java.util.*; import javax.swing.event.UndoableEditEvent; import javax.swing.undo.*; import org.openide.awt.UndoRedo; import org.openide.util.Mutex; import org.openide.util.MutexException; import org.netbeans.modules.form.layoutsupport.*; import org.netbeans.modules.form.codestructure.CodeStructure; /** * Holds all data of a form. * * @author Tran Duc Trung, Tomas Pavek */ public class FormModel { // the top metacomponent of the form (null if form is based on Object) private RADComponent topRADComponent; // the class on which the form is based (which is extended in the java file) private Class formBaseClass; // other components - out of the main hierarchy under topRADComponent private ArrayList otherComponents = new ArrayList(10); // holds both topRADComponent and otherComponents private ComponentContainer modelContainer; // name of the form (name of the DataObject) private String formName; private boolean readOnly = false; private boolean formLoaded = false; private UndoRedo.Manager undoRedoManager; private boolean undoRedoRecording = false; private CompoundEdit compoundEdit; private FormEvents formEvents; // list of listeners registered on FormModel private ArrayList listeners; private EventBroker eventBroker; private MetaComponentCreator metaCreator; private CodeStructure codeStructure = new CodeStructure(false); private CodeGenerator codeGenerator; // [this reference should be removed] // ------------- // initialization FormModel() { } /** This methods sets the form base class (which is in fact the superclass * of the form class in source java file). It is used for initializing * the top meta component, and is also presented as the top component * in designer and inspector. */ public void setFormBaseClass(Class formClass) throws Exception { if (formBaseClass != null) throw new IllegalStateException("Form type already initialized."); // NOI18N RADComponent topComp; if (java.awt.Component.class.isAssignableFrom(formClass)) { topComp = FormUtils.isContainer(formClass) ? new RADVisualFormContainer() : new RADVisualComponent(); } else if (java.lang.Object.class != formClass) topComp = new RADFormContainer(); else topComp = null; if (topComp != null) { topRADComponent = topComp; topComp.initialize(this); topComp.initInstance(formClass); topComp.setInModel(true); } formBaseClass = formClass; // topRADComponent = topComp; } public Class getFormBaseClass() { return formBaseClass; } void setName(String name) { formName = name; } void setReadOnly(boolean readOnly) { this.readOnly = readOnly; } // ----------- // getters public final String getName() { return formName; } public final boolean isReadOnly() { return readOnly; } public final boolean isFormLoaded() { return formLoaded; } public final FormDesigner getFormDesigner() { return FormEditorSupport.getFormDesigner(this); } // for compatibility with previous version public final FormDataObject getFormDataObject() { return FormEditorSupport.getFormDataObject(this); } public ComponentContainer getModelContainer() { if (modelContainer == null) modelContainer = new ModelContainer(); return modelContainer; } public final RADComponent getTopRADComponent() { return topRADComponent; } public RADComponent findRADComponent(String name) { Iterator allComps = getMetaComponents().iterator(); while (allComps.hasNext()) { RADComponent comp = (RADComponent) allComps.next(); if (name.equals(comp.getName())) return comp; } return null; } /** Returns all meta components in the model. The components are collected * recursively, and placed in a List. */ public java.util.List getMetaComponents() { ArrayList list = new ArrayList(); collectMetaComponents(getModelContainer(), list); return list; //Collections.unmodifiableList(list); } /** Collects and returns all components in the main visual hierarchy. */ public RADVisualComponent[] getVisualComponents() { ArrayList list = new ArrayList(); if (topRADComponent instanceof RADVisualComponent) list.add(topRADComponent); if (topRADComponent instanceof RADVisualContainer) collectVisualMetaComponents((RADVisualContainer)topRADComponent, list); return (RADVisualComponent[]) list.toArray(new RADVisualComponent[list.size()]); } /** Returns all "other components" (not in the main visual hierarchy). * @param recursively whether also all sub-componets should be collected */ public RADComponent[] getOtherComponents(boolean recursively) { ArrayList list = new ArrayList(); for (Iterator it=otherComponents.iterator(); it.hasNext(); ) { RADComponent comp = (RADComponent) it.next(); list.add(comp); if (recursively && comp instanceof ComponentContainer) collectMetaComponents((ComponentContainer) comp, list); } return (RADComponent[]) list.toArray(new RADComponent[list.size()]); } // for compatibility with previous version public RADComponent[] getNonVisualComponents() { return (RADComponent[]) otherComponents.toArray( new RADComponent[otherComponents.size()]); } public FormEvents getFormEvents() { if (formEvents == null) formEvents = new FormEvents(this); return formEvents; } private static void collectMetaComponents(ComponentContainer cont, java.util.List list) { RADComponent[] comps = cont.getSubBeans(); for (int i = 0; i < comps.length; i++) { RADComponent comp = comps[i]; list.add(comp); if (comp instanceof ComponentContainer) collectMetaComponents((ComponentContainer) comp, list); } } private static void collectVisualMetaComponents(RADVisualContainer cont, java.util.List list) { RADVisualComponent[] comps = cont.getSubComponents(); for (int i = 0; i < comps.length; i++) { RADComponent comp = comps[i]; list.add(comp); if (comp instanceof RADVisualContainer) collectVisualMetaComponents((RADVisualContainer) comp, list); } } // ----------- // adding/deleting components, setting layout, etc /** Returns MetaComponentCreator which is responsible for creating new * components and adding them to the model. */ public MetaComponentCreator getComponentCreator() { if (metaCreator == null) metaCreator = new MetaComponentCreator(this); return metaCreator; } public void addComponent(RADComponent metacomp, ComponentContainer parentContainer) { if (parentContainer != null) { parentContainer.add(metacomp); } else { metacomp.setParentComponent(null); otherComponents.add(metacomp); } boolean newlyAdded = !metacomp.isInModel(); if (newlyAdded) setInModelRecursively(metacomp, true); fireComponentAdded(metacomp, newlyAdded); } public void addVisualComponent(RADVisualComponent metacomp, RADVisualContainer parentContainer, LayoutConstraints constraints) { LayoutSupportManager layoutSupport = parentContainer.getLayoutSupport(); RADVisualComponent[] compArray = new RADVisualComponent[] { metacomp }; LayoutConstraints[] constrArray = new LayoutConstraints[] { constraints }; // this may throw a RuntimeException if the components are not accepted layoutSupport.acceptNewComponents(compArray, constrArray, -1); parentContainer.add(metacomp); layoutSupport.addComponents(compArray, constrArray, -1); boolean newlyAdded = !metacomp.isInModel(); if (newlyAdded) setInModelRecursively(metacomp, true); fireComponentAdded(metacomp, newlyAdded); } public void setContainerLayout(RADVisualContainer metacont, LayoutSupportDelegate layoutDelegate, java.awt.LayoutManager lmInstance) throws Exception { LayoutSupportDelegate current = metacont.getLayoutSupport().getLayoutDelegate(); metacont.setLayoutSupportDelegate(layoutDelegate, lmInstance); fireContainerLayoutExchanged(metacont, current, layoutDelegate); } public void removeComponentFromContainer(RADComponent metacomp) { RADComponent parent = metacomp.getParentComponent(); ComponentContainer parentContainer = parent instanceof ComponentContainer ? (ComponentContainer) parent : getModelContainer(); int index = parentContainer.getIndexOf(metacomp); parentContainer.remove(metacomp); fireComponentRemoved(metacomp, parentContainer, index, false, null, null); } public void removeComponent(RADComponent metacomp) { if (formEvents != null) removeEventHandlersRecursively(metacomp); RADComponent parent = metacomp.getParentComponent(); ComponentContainer parentContainer = parent instanceof ComponentContainer ? (ComponentContainer) parent : getModelContainer(); int index = parentContainer.getIndexOf(metacomp); parentContainer.remove(metacomp); // turn on undo/redo recording on code structure (if allowed) Object codeStructureMark1 = null, codeStructureMark2 = null; boolean codeStructureUndoRedo = codeStructure.isUndoRedoRecording(); if (undoRedoRecording && !codeStructureUndoRedo) { codeStructure.setUndoRedoRecording(true); codeStructureMark1 = codeStructure.markForUndo(); } metacomp.removeCodeExpression(); metacomp.setInModel(false); if (metacomp instanceof ComponentContainer) releaseComponent(metacomp); // turn off undo/redo recording on code structure (if turned on) if (undoRedoRecording && !codeStructureUndoRedo) { codeStructureMark2 = codeStructure.markForUndo(); if (codeStructureMark2.equals(codeStructureMark1)) codeStructureMark2 = codeStructureMark1 = null; codeStructure.setUndoRedoRecording(false); } fireComponentRemoved(metacomp, parentContainer, index, true, codeStructureMark1, codeStructureMark2); } // removes all event handlers attached to given component and all // its subcomponents private void removeEventHandlersRecursively(RADComponent comp) { if (comp instanceof ComponentContainer) { RADComponent[] subcomps = ((ComponentContainer)comp).getSubBeans(); for (int i=0; i |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.