KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > bridge > FlyweightIndexedProperty


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.java.bridge;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.util.*;
24
25 /**
26  * PENDING: Remove FlyweightIndexedProperty class entirely, move createXXX events
27  * to the IndexedPropertyBase and have the subclasses implement getSource() method.
28  *
29  */

30 public abstract class FlyweightIndexedProperty extends IndexedPropertyBase {
31     public FlyweightIndexedProperty(String JavaDoc propertyName) {
32         super(propertyName);
33     }
34
35     protected abstract Object JavaDoc[] getValue(ElementImpl beanImpl);
36
37     protected abstract Object JavaDoc[] createValue(int size);
38
39     /** Creates a PropertyChangeEvent that informs about insertion of some items at
40      * the end of the value array. The event does not contain indices for the insertion.
41      */

42     public PropertyChangeEvent JavaDoc add(ElementImpl beanImpl, Object JavaDoc[] adding) {
43         Object JavaDoc[] oldVal = getValue(beanImpl);
44         Object JavaDoc[] newVal = createValue(oldVal.length + adding.length);
45         System.arraycopy(oldVal, 0, newVal, 0, oldVal.length);
46         System.arraycopy(adding, 0, newVal, oldVal.length, adding.length);
47         return createInsertionEvent(beanImpl.getElement(), oldVal, newVal.clone(), Arrays.asList(adding),
48             null);
49     }
50
51     /** Creates a PropertyChangeEvent that informs about removal of some items.
52      */

53     public PropertyChangeEvent JavaDoc remove(ElementImpl beanImpl, Object JavaDoc[] toRemove) {
54         Object JavaDoc[] oldVal = getValue(beanImpl);
55         Object JavaDoc[] newVal = applyRemove(oldVal, toRemove);
56         return createRemovalEvent(beanImpl.getElement(), oldVal, newVal.clone(), Arrays.asList(toRemove), null);
57     }
58     
59     protected boolean compareValuesForRemove(Object JavaDoc old, Object JavaDoc removed) {
60         return compareValues(old, removed);
61     }
62     
63     /** Filters out the items that should be removed and creates a list of new ones. Uses
64      * a dumb algorithm O(n^2) for searching for objects to be removed and uses
65      */

66     private Object JavaDoc[] applyRemove(Object JavaDoc[] oldVals, Object JavaDoc[] remove) {
67         List result = new ArrayList(oldVals.length - remove.length);
68         for (int i = 0; i < oldVals.length; i++) {
69             boolean shouldRemove = false;
70             for (int j = 0; !shouldRemove && j < remove.length; j++) {
71                 if (oldVals[i] == remove[j] || compareValuesForRemove(oldVals[i], remove[j])) {
72                     shouldRemove = true;
73                 }
74             }
75             if (!shouldRemove)
76                 result.add(oldVals[i]);
77         }
78         return result.toArray(createValue(0));
79     }
80     
81     /** Creates a generic PropertyChangeEvent.
82      */

83     public PropertyChangeEvent JavaDoc set(ElementImpl beanImpl, Object JavaDoc[] newVal) {
84         Object JavaDoc[] oldVal = getValue(beanImpl);
85         return createChangeEvent(beanImpl.getElement(), oldVal, (Object JavaDoc[])newVal.clone());
86     }
87 }
88
Popular Tags