aboutsummaryrefslogtreecommitdiffstats
blob: eaec07baa4b4ecbf95fb484741504900181002cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.tests.viewers;

import java.util.Vector;

public class TestElement implements Cloneable {
    TestModel fModel;

    TestElement fContainer;

    String fSomeName;

    String fId;

    Vector<TestElement> fChildren = new Vector<TestElement>();

    boolean fIsDeleted = false;

    public static final String P_SOMENAME = "org.eclipse.jface.viewertest.name";

    public static final String P_COLUMN_1 = "org.eclipse.jface.viewertest.column1";

    public TestElement(TestModel model, TestElement container) {
        fModel = model;
        fContainer = container;
        int p = 0;
        TestElement lastSibling = container.getLastChild();
        if (lastSibling != null) {
            p = lastSibling.childId() + 1;
        }
        fId = container.getID() + "-" + p;
    }

    public TestElement(TestModel model, TestElement container, int level,
            int position) {
        fModel = model;
        fContainer = container;
        if (container != null)
            fId = container.getID() + "-" + position;
        else
            fId = Integer.toString(position);
        fSomeName = "name-" + position;

        if (level < model.getNumLevels()) {
            for (int i = 0; i < model.getNumChildren(); i++) {
                fChildren.add(new TestElement(model, this, level + 1, i));
            }
        }
    }

    public TestElement addChild(int event) {
        TestElement element = new TestElement(fModel, this);
        element.fSomeName = "added";
        addChild(element, new TestModelChange(event, this, element));
        return element;
    }

    public TestElement addChild(TestElement element, TestModelChange change) {
        fChildren.add(element);
        fModel.fireModelChanged(change);
        return element;
    }

    public void addChildren(TestElement[] elements, TestModelChange change) {
        for (int i = 0; i < elements.length; i++)
            fChildren.add(elements[i]);
        fModel.fireModelChanged(change);
    }

    public TestElement[] addChildren(int event) {
        TestElement elements[] = new TestElement[] {
                new TestElement(fModel, this), new TestElement(fModel, this) };

        elements[0].fSomeName = "added1";
        elements[1].fSomeName = "added2";
        // change the id of the second element, otherwise there will be
        // two equal elements under the same parent
        elements[1].fId += "madeUnique";
        addChildren(elements, new TestModelChange(event, this, elements));
        return elements;
    }

    public TestElement basicAddChild() {
        TestElement element = new TestElement(fModel, this);
        element.fSomeName = "added";
        fChildren.add(element);
        return element;
    }

    public void basicDeleteChild(TestElement element) {
        fChildren.remove(element);
        element.fIsDeleted = true;
    }

    private int childId() {
        String id = fId.substring(fId.lastIndexOf("-") + 1);
        return Integer.parseInt(id);
    }

    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new Error(); // should not happen
        }
    }

    static public TestElement createModel(int numLevels, int numChildren) {
        return new TestElement(new TestModel(numLevels, numChildren), null, 0,
                0);
    }

    public void deleteChild(TestElement element) {
        deleteChild(element, new TestModelChange(TestModelChange.REMOVE, this,
                element));
    }

    public void deleteChild(TestElement element, TestModelChange change) {
        basicDeleteChild(element);
        fModel.fireModelChanged(change);
    }

    public void deleteChildren() {
        for (int i = fChildren.size() - 1; i >= 0; i--) {
            TestElement te = fChildren.elementAt(i);
            fChildren.remove(te);
            te.fIsDeleted = true;
        }
        fModel.fireModelChanged(new TestModelChange(
                TestModelChange.STRUCTURE_CHANGE, this));
    }

    public void deleteSomeChildren() {
        for (int i = fChildren.size() - 1; i >= 0; i -= 2) {
            TestElement te = fChildren.elementAt(i);
            fChildren.remove(te);
            te.fIsDeleted = true;
        }
        fModel.fireModelChanged(new TestModelChange(
                TestModelChange.STRUCTURE_CHANGE, this));
    }

    public boolean equals(Object arg) {
        if (!(arg instanceof TestElement))
            return false;
        TestElement element = (TestElement) arg;
        return element.fId.equals(fId);
    }

    public TestElement getChildAt(int i) {
        return fChildren.elementAt(i);
    }

    public int getChildCount() {
        return fChildren.size();
    }

    /**
     * Get the children of the receiver.
     * @return TestElement[]
     */
    public TestElement[] getChildren(){
    	TestElement[] result = new TestElement[fChildren.size()];
    	fChildren.toArray(result);
    	return result;
    }

    public TestElement getContainer() {
        return fContainer;
    }

    public TestElement getFirstChild() {
        if (fChildren.size() > 0)
            return fChildren.elementAt(0);
        return null;
    }

    public String getID() {
        return fId;
    }

    public String getLabel() {
        return fSomeName;
    }

    public TestElement getLastChild() {
        int size = fChildren.size();
        if (size > 0)
            return fChildren.elementAt(size - 1);
        return null;
    }

    public TestModel getModel() {
        return fModel;
    }

    public int hashCode() {
        return fId.hashCode();
    }

    public boolean isDeleted() {
        return fIsDeleted;
    }

    public void setLabel(String label) {
        fSomeName = label;
        fModel.fireModelChanged(new TestModelChange(
                TestModelChange.NON_STRUCTURE_CHANGE, this));
    }

    public boolean testDeleted() {
        if (fIsDeleted)
            return true;
        if (fContainer != null)
            return fContainer.testDeleted();
        return false;
    }

    public String toString() {
        return getID() + " " + getLabel();
    }
}