001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mapper.basicmapper;
021:
022: import java.beans.PropertyChangeListener;
023: import java.util.List;
024: import java.util.Vector;
025:
026: import org.netbeans.modules.soa.mapper.basicmapper.util.MapperUtilities;
027: import org.netbeans.modules.soa.mapper.common.basicmapper.IBasicMapperModel;
028: import org.netbeans.modules.soa.mapper.common.basicmapper.IBasicViewModel;
029: import org.netbeans.modules.soa.mapper.common.IMapperNode;
030: import org.netbeans.modules.soa.mapper.common.IMapperViewModel;
031: import org.netbeans.modules.soa.mapper.common.basicmapper.tree.IMapperTreeNode;
032:
033: /**
034: * <p>
035: *
036: * Title: </p> MapperViewModel <p>
037: *
038: * Description: </p> MapperViewModel provides basic implementation of
039: * IMapperViewModel<p>
040: *
041: * Copyright: Copyright (c) 2002 </p> <p>
042: *
043: * Company: </p>
044: *
045: * @author Un Seng Leong
046: * @created December 19, 2002
047: * @version 1.0
048: */
049: public class BasicViewModel implements IBasicViewModel {
050:
051: /**
052: * the mapper model that contains this view model
053: */
054: private IBasicMapperModel mMapperModel;
055:
056: /**
057: * storge of links object in this model
058: */
059: private List mNodes;
060:
061: /**
062: * storge of PropertyChangeListener of this model
063: */
064: private List mPropertyListeners;
065:
066: /**
067: * Construct a view model with no node initially.
068: */
069: public BasicViewModel() {
070: mNodes = new Vector();
071: mPropertyListeners = new Vector();
072: }
073:
074: /**
075: * Return the mapper model that contains this view model
076: *
077: * @return a reference to mapper model that contains this view model.
078: */
079: public IBasicMapperModel getMapperModel() {
080: return mMapperModel;
081: }
082:
083: /**
084: * Return the number of nodes in this model.
085: *
086: * @return the number of nodes in this model.
087: */
088: public int getNodeCount() {
089: return mNodes.size();
090: }
091:
092: /**
093: * Return all the nodes in this model in a Set repersentation.
094: *
095: * @return all the nodes in this model.
096: */
097: public List getNodes() {
098: return mNodes;
099: }
100:
101: /**
102: * Set the mapper model that contains this view model.
103: *
104: * @param model the mapper model contains this view model.
105: */
106: public void setMapperModel(IBasicMapperModel model) {
107: mMapperModel = model;
108: }
109:
110: /**
111: * Add a new node to this model. And fired node added property change event.
112: *
113: * @param node the node to be added.
114: */
115: public void addNode(IMapperNode node) {
116: // there can only be 1 tree node allow to exist
117: // in the same view model.
118: if (node instanceof IMapperTreeNode && mNodes.contains(node)) {
119: return;
120: }
121: mNodes.add(node);
122: firePropertyChange(IMapperViewModel.NODE_ADDED, node, null);
123: }
124:
125: /**
126: * Adds a PropertyChangeListener to the listener list.
127: *
128: * @param listener the PropertyChangeListener to be added
129: */
130: public void addPropertyChangeListener(
131: PropertyChangeListener listener) {
132: mPropertyListeners.add(listener);
133: }
134:
135: /**
136: * Return true if the specified node is in this model, false otherwise.
137: *
138: * @param node the specified node to check
139: * @return true if the node is in this model, false otherwise.
140: */
141: public boolean containsNode(IMapperNode node) {
142: return mNodes.contains(node);
143: }
144:
145: /**
146: * Remove a node from this model. And fired node removed property change
147: * event.
148: *
149: * @param node the node to be removed.
150: */
151: public void removeNode(IMapperNode node) {
152: if (mNodes.remove(node)) {
153: firePropertyChange(IMapperViewModel.NODE_REMOVED, null,
154: node);
155: }
156: }
157:
158: /**
159: * Removes a PropertyChangeListener from the listener list.
160: *
161: * @param listener the PropertyChangeListener to be removed
162: */
163: public void removePropertyChangeListener(
164: PropertyChangeListener listener) {
165: mPropertyListeners.remove(listener);
166: }
167:
168: /**
169: * Fire a specified property change event of this model.
170: *
171: * @param propertyName the name of this property has changed
172: * @param newValue the new value of the property
173: * @param oldValue the old value of the property
174: */
175: protected void firePropertyChange(String propertyName,
176: Object newValue, Object oldValue) {
177:
178: if (mPropertyListeners.size() > 0) {
179: MapperUtilities
180: .firePropertyChanged(
181: (PropertyChangeListener[]) mPropertyListeners
182: .toArray(new PropertyChangeListener[mPropertyListeners
183: .size()]), this ,
184: propertyName, newValue, oldValue);
185: }
186: }
187:
188: /**
189: * Gets the paramString attribute of the MapperViewModel object
190: *
191: * @return The paramString value
192: */
193: public String getParamString() {
194: return super .toString() + "[node list=" + mNodes;
195: }
196: }
|