KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sample > registry > model > impl > SyncUpdateVisitor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.sample.registry.model.impl;
20
21 import org.netbeans.modules.xml.xam.ComponentUpdater;
22 import org.netbeans.modules.xml.xam.ComponentUpdater.Operation;
23 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
24 import org.sample.registry.model.Entries;
25 import org.sample.registry.model.KnownTypes;
26 import org.sample.registry.model.Registry;
27 import org.sample.registry.model.RegistryComponent;
28 import org.sample.registry.model.RegistryVisitor;
29 import org.sample.registry.model.Service;
30 import org.sample.registry.model.ServiceProvider;
31 import org.sample.registry.model.ServiceType;
32
33 public class SyncUpdateVisitor extends RegistryVisitor.Default implements ComponentUpdater<RegistryComponent> {
34     private RegistryComponent target;
35     private Operation operation;
36     private int index;
37     
38     public SyncUpdateVisitor() {
39     }
40
41     public void update(RegistryComponent target, RegistryComponent child, Operation operation) {
42         update(target, child, -1 , operation);
43     }
44
45     public void update(RegistryComponent target, RegistryComponent child, int index, Operation operation) {
46         assert target != null;
47         assert child != null;
48         this.target = target;
49         this.index = index;
50         this.operation = operation;
51         child.accept(this);
52     }
53
54     private void insert(String JavaDoc propertyName, RegistryComponent component) {
55         ((RegistryComponentImpl)target).insertAtIndex(propertyName, component, index);
56     }
57     
58     private void remove(String JavaDoc propertyName, RegistryComponent component) {
59         ((RegistryComponentImpl)target).removeChild(propertyName, component);
60     }
61     
62     public void visit(ServiceProvider component) {
63         if (target instanceof Service) {
64             if (operation == Operation.ADD) {
65                 insert(Service.SERVICE_PROVIDER_PROPERTY, component);
66             } else {
67                 remove(Service.SERVICE_PROVIDER_PROPERTY, component);
68             }
69         }
70     }
71
72     public void visit(Entries component) {
73         if (target instanceof Registry) {
74             if (operation == Operation.ADD) {
75                 insert(Registry.ENTRIES_PROPERTY, component);
76             } else {
77                 remove(Registry.ENTRIES_PROPERTY, component);
78             }
79         }
80     }
81
82     public void visit(ServiceType component) {
83         if (target instanceof KnownTypes) {
84             if (operation == Operation.ADD) {
85                 insert(KnownTypes.TYPE_PROPERTY, component);
86             } else {
87                 remove(KnownTypes.TYPE_PROPERTY, component);
88             }
89         }
90     }
91
92     public void visit(Service component) {
93         if (target instanceof Entries) {
94             if (operation == Operation.ADD) {
95                 insert(Entries.SERVICE_PROPERTY, component);
96             } else {
97                 remove(Entries.SERVICE_PROPERTY, component);
98             }
99         }
100     }
101
102     public void visit(KnownTypes component) {
103         if (target instanceof Registry) {
104             if (operation == Operation.ADD) {
105                 insert(Registry.KNOWN_TYPES_PROPERTY, component);
106             } else {
107                 remove(Registry.KNOWN_TYPES_PROPERTY, component);
108             }
109         }
110     }
111 }
112
Popular Tags