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: package org.netbeans.modules.xslt.tmap.model.impl;
020:
021: import org.netbeans.modules.xml.xam.ComponentUpdater;
022: import org.netbeans.modules.xml.xam.ComponentUpdater.Operation;
023: import org.netbeans.modules.xslt.tmap.model.api.Invoke;
024: import org.netbeans.modules.xslt.tmap.model.api.Param;
025: import org.netbeans.modules.xslt.tmap.model.api.Service;
026: import org.netbeans.modules.xslt.tmap.model.api.TMapComponent;
027: import org.netbeans.modules.xslt.tmap.model.api.TMapVisitor;
028: import org.netbeans.modules.xslt.tmap.model.api.Transform;
029: import org.netbeans.modules.xslt.tmap.model.api.TransformMap;
030:
031: /**
032: *
033: * @author Vitaly Bychkov
034: * @version 1.0
035: */
036: public class SyncUpdateVisitor implements
037: ComponentUpdater<TMapComponent>, TMapVisitor {
038:
039: private TMapComponent myParent;
040: private int myIndex;
041: private Operation myOperation;
042:
043: public SyncUpdateVisitor() {
044: }
045:
046: public void update(TMapComponent target, TMapComponent child,
047: Operation operation) {
048: update(target, child, -1, operation);
049: }
050:
051: public void update(TMapComponent target, TMapComponent child,
052: int index, Operation operation) {
053: assert target != null;
054: assert child != null;
055: assert operation == Operation.ADD
056: || operation == Operation.REMOVE;
057:
058: myParent = target;
059: myIndex = index;
060: myOperation = operation;
061:
062: child.accept(this );
063: }
064:
065: public void visit(TransformMap transformMap) {
066: assert false : "Should never add or remove transformmap root";
067: }
068:
069: public void visit(Service service) {
070: assert getParent() instanceof TransformMap;
071: TransformMap transformMap = (TransformMap) getParent();
072: if (isAdd()) {
073: transformMap.addService(service);
074: } else if (isRemove()) {
075: transformMap.removeService(service);
076: }
077: }
078:
079: public void visit(
080: org.netbeans.modules.xslt.tmap.model.api.Operation operation) {
081: assert getParent() instanceof Service;
082: Service service = (Service) getParent();
083: if (isAdd()) {
084: service.addOperation(operation);
085: } else if (isRemove()) {
086: service.removeOperation(operation);
087: }
088: }
089:
090: public void visit(Invoke invoke) {
091: assert getParent() instanceof org.netbeans.modules.xslt.tmap.model.api.Operation;
092: org.netbeans.modules.xslt.tmap.model.api.Operation operation = (org.netbeans.modules.xslt.tmap.model.api.Operation) getParent();
093: if (isAdd()) {
094: operation.addInvoke(invoke);
095: } else if (isRemove()) {
096: operation.removeInvoke(invoke);
097: }
098: }
099:
100: public void visit(Transform transform) {
101: assert getParent() instanceof org.netbeans.modules.xslt.tmap.model.api.Operation;
102: org.netbeans.modules.xslt.tmap.model.api.Operation operation = (org.netbeans.modules.xslt.tmap.model.api.Operation) getParent();
103: if (isAdd()) {
104: operation.addTransform(transform);
105: } else if (isRemove()) {
106: operation.removeTransforms(transform);
107: }
108: }
109:
110: public void visit(Param param) {
111: assert getParent() instanceof Transform;
112: Transform transform = (Transform) getParent();
113: if (isAdd()) {
114: transform.addParam(param);
115: } else if (isRemove()) {
116: transform.removeParam(param);
117: }
118: }
119:
120: private TMapComponent getParent() {
121: return myParent;
122: }
123:
124: private int getIndex() {
125: return myIndex;
126: }
127:
128: private Operation getOperation() {
129: return myOperation;
130: }
131:
132: private boolean isAdd() {
133: return Operation.ADD == myOperation;
134: }
135:
136: private boolean isRemove() {
137: return Operation.REMOVE == myOperation;
138: }
139:
140: }
|