KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > codestructure > CodeObjectUsage


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
20 package org.netbeans.modules.form.codestructure;
21
22 import java.util.*;
23
24 /**
25  * @author Tomas Pavek
26  */

27
28 final class CodeObjectUsage {
29
30     private UsedCodeObject usedObject;
31     private java.util.List JavaDoc usageList;
32
33     public CodeObjectUsage(UsedCodeObject usedObject) {
34         this.usedObject = usedObject;
35     }
36
37     public CodeStructureChange addUsingObject(UsingCodeObject usingObject,
38                                               int useType,
39                                               Object JavaDoc useCategory,
40                                               boolean provideUndoableChange)
41     {
42         if (useCategory == null)
43             throw new IllegalArgumentException JavaDoc();
44
45         if (usageList == null)
46             usageList = new LinkedList();
47
48         ObjectUse use = new ObjectUse(usingObject, useType, useCategory);
49         usageList.add(use); // [check if the object is not already registered??]
50

51         usingObject.usageRegistered(usedObject);
52
53         return provideUndoableChange ?
54                new UsageChange(usedObject, use, true) : null;
55     }
56
57     public CodeStructureChange removeUsingObject(UsingCodeObject usingObject,
58                                                  boolean provideUndoableChange)
59     {
60         if (usageList == null)
61             return null;
62
63         ObjectUse removed = null;
64         Iterator it = usageList.iterator();
65         while (it.hasNext()) {
66             ObjectUse use = (ObjectUse) it.next();
67             if (usingObject == use.usingObject) {
68                 removed = use;
69                 it.remove();
70             }
71         }
72
73         if (removed != null)
74             usingObject.usedObjectRemoved(usedObject);
75
76         return provideUndoableChange && removed != null ?
77                new UsageChange(usedObject, removed, false) : null;
78     }
79
80     public Iterator getUsingObjectsIterator(int useType, Object JavaDoc useCategory) {
81         Iterator it = usageList != null ? usageList.iterator() : null;
82         return new UsageIterator(it, useType, useCategory);
83     }
84
85     public boolean isEmpty() {
86         return usageList == null || usageList.size() == 0;
87     }
88
89     // -------
90

91     private static class ObjectUse {
92         private UsingCodeObject usingObject;
93         private int type;
94         private Object JavaDoc category;
95
96         ObjectUse(UsingCodeObject usingObject, int useType, Object JavaDoc useCategory) {
97             this.usingObject = usingObject;
98             this.type = useType;
99             this.category = useCategory;
100         }
101
102         boolean matches(int type, Object JavaDoc category) {
103             if (type != 0 && type != this.type)
104                 return false;
105             if (category == null)
106                 return true;
107             return category.equals(this.category);
108         }
109     }
110
111     // --------
112

113     private static class UsageChange implements CodeStructureChange {
114         private UsedCodeObject usedObject;
115         private ObjectUse use;
116         private boolean added; // true: added, false: removed
117

118         UsageChange(UsedCodeObject usedObject, ObjectUse use, boolean added) {
119             this.usedObject = usedObject;
120             this.use = use;
121             this.added = added;
122         }
123
124         public void undo() {
125             if (added)
126                 usedObject.removeUsingObject(use.usingObject);
127             else
128                 usedObject.addUsingObject(use.usingObject, use.type, use.category);
129         }
130
131         public void redo() {
132             if (added)
133                 usedObject.addUsingObject(use.usingObject, use.type, use.category);
134             else
135                 usedObject.removeUsingObject(use.usingObject);
136         }
137     }
138
139     // --------
140

141     private static class UsageIterator implements Iterator {
142         private int useType;
143         private Object JavaDoc useCategory;
144
145         private Iterator iterator;
146         private Object JavaDoc next;
147
148         public UsageIterator(Iterator iterator,
149                              int useType, Object JavaDoc useCategory)
150         {
151             this.iterator = iterator;
152             this.useType = useType;
153             this.useCategory = useCategory;
154         }
155
156         public boolean hasNext() {
157             if (iterator == null)
158                 return false;
159             if (next != null)
160                 return true;
161
162             while (iterator.hasNext()) {
163                 ObjectUse use = (ObjectUse) iterator.next();
164                 if (use.matches(useType, useCategory)) {
165                     next = use.usingObject;
166                     return true;
167                 }
168             }
169             return false;
170         }
171
172         public Object JavaDoc next() {
173             if (!hasNext())
174                 throw new NoSuchElementException();
175
176             Object JavaDoc nextObject = next;
177             next = null;
178             return nextObject;
179         }
180
181         public void remove() {
182             throw new UnsupportedOperationException JavaDoc();
183         }
184     }
185 }
186
Popular Tags