KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > structuremergeviewer > DiffContainer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.structuremergeviewer;
12
13 import java.util.ArrayList JavaDoc;
14
15 /**
16  * The standard implementation of a diff container element.
17  * <p>
18  * This class may be instantiated, or further subclassed.
19  * </p>
20  */

21 public abstract class DiffContainer extends DiffElement implements IDiffContainer {
22
23     private static IDiffElement[] fgEmptyArray= new IDiffElement[0];
24     private ArrayList JavaDoc fChildren;
25     
26     /**
27      * Creates a new container with the specified kind under the given parent.
28      *
29      * @param parent under which the new container is added as a child or <code>null</code>.
30      * @param kind of difference (defined in <code>Differencer</code>).
31      */

32     public DiffContainer(IDiffContainer parent, int kind) {
33         super(parent, kind);
34     }
35     
36     /**
37      * Tries to find the child with the given name.
38      * Returns <code>null</code> if no such child exists.
39      *
40      * @param name of the child to find
41      * @return the first element with a matching name
42      */

43     public IDiffElement findChild(String JavaDoc name) {
44         Object JavaDoc[] children= getChildren();
45         for (int i= 0; i < children.length; i++) {
46             IDiffElement child= (IDiffElement) children[i];
47             if (name.equals(child.getName()))
48                 return child;
49         }
50         return null;
51     }
52
53     /* (non Javadoc)
54      * see IDiffContainer.add
55      */

56     public void add(IDiffElement diff) {
57         if (fChildren == null)
58             fChildren= new ArrayList JavaDoc();
59         fChildren.add(diff);
60         diff.setParent(this);
61     }
62
63     /*
64      * Removes the given child from this container.
65      * If the container becomes empty it is removed from its container.
66      */

67     public void removeToRoot(IDiffElement child) {
68         if (fChildren != null) {
69             fChildren.remove(child);
70             child.setParent(null);
71             if (fChildren.size() == 0) {
72                 IDiffContainer p= getParent();
73                 if (p != null)
74                     p.removeToRoot(this);
75             }
76         }
77     }
78
79     /**
80      * Removes the given child (non-recursively) from this container.
81      *
82      * @param child to remove
83      */

84     public void remove(IDiffElement child) {
85         if (fChildren != null) {
86             fChildren.remove(child);
87             child.setParent(null);
88         }
89     }
90     
91     /* (non Javadoc)
92      * see IDiffContainer.hasChildren
93      */

94     public boolean hasChildren() {
95         return fChildren != null && fChildren.size() > 0;
96     }
97
98     /* (non Javadoc)
99      * see IDiffContainer.getChildren
100      */

101     public IDiffElement[] getChildren() {
102         if (fChildren != null)
103             return (IDiffElement[]) fChildren.toArray(fgEmptyArray);
104         return fgEmptyArray;
105     }
106 }
107
108
Popular Tags