Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / filter / model / TmfFilterTreeNode.java
1 /*******************************************************************************
2 * Copyright (c) 2010 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Yuriy Vashchuk (yvashchuk@gmail.com) - Initial API and implementation
11 * Patrick Tasse - Refactoring
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.filter.model;
15
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19
20 import org.eclipse.linuxtools.tmf.event.TmfEvent;
21
22 /**
23 * <b><u>TmfFilterTreeNode</u></b>
24 * <p>
25 * The Filter Tree node
26 * <p>
27 */
28 public abstract class TmfFilterTreeNode implements ITmfFilterTreeNode, Cloneable {
29
30 private static final String[] VALID_CHILDREN = {
31 TmfFilterEventTypeNode.NODE_NAME,
32 TmfFilterAndNode.NODE_NAME,
33 TmfFilterOrNode.NODE_NAME,
34 TmfFilterContainsNode.NODE_NAME,
35 TmfFilterEqualsNode.NODE_NAME,
36 TmfFilterMatchesNode.NODE_NAME,
37 TmfFilterCompareNode.NODE_NAME
38 };
39
40 private ITmfFilterTreeNode parent = null;
41 private ArrayList<ITmfFilterTreeNode> children = new ArrayList<ITmfFilterTreeNode>();
42
43 public TmfFilterTreeNode(final ITmfFilterTreeNode parent) {
44 if (parent != null) {
45 parent.addChild(this);
46 }
47 }
48
49 /* (non-Javadoc)
50 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getParent()
51 */
52 @Override
53 public ITmfFilterTreeNode getParent() {
54 return parent;
55 }
56
57 /* (non-Javadoc)
58 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getNodeName()
59 */
60 @Override
61 public abstract String getNodeName();
62
63 /* (non-Javadoc)
64 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#hasChildren()
65 */
66 @Override
67 public boolean hasChildren() {
68 return (children.size() > 0);
69 }
70
71 /* (non-Javadoc)
72 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChildrenCount()
73 */
74 @Override
75 public int getChildrenCount() {
76 return children.size();
77 }
78
79 /* (non-Javadoc)
80 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChildren()
81 */
82 @Override
83 public ITmfFilterTreeNode[] getChildren() {
84 return children.toArray(new ITmfFilterTreeNode[0]);
85 }
86
87 /* (non-Javadoc)
88 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChild(int)
89 */
90 @Override
91 public ITmfFilterTreeNode getChild(final int index) throws IndexOutOfBoundsException {
92 return children.get(index);
93 }
94
95 /* (non-Javadoc)
96 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#remove()
97 */
98 @Override
99 public ITmfFilterTreeNode remove() {
100 if (getParent() != null) {
101 getParent().removeChild(this);
102 }
103 return this;
104 }
105
106 /*
107 * (non-Javadoc)
108 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#removeChild(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
109 */
110 @Override
111 public ITmfFilterTreeNode removeChild(ITmfFilterTreeNode node) {
112 children.remove(node);
113 node.setParent(null);
114 return node;
115 }
116
117 /* (non-Javadoc)
118 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#addChild(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
119 */
120 @Override
121 public int addChild(final ITmfFilterTreeNode node) {
122 node.setParent(this);
123 if(children.add(node)) {
124 return (children.size() - 1);
125 }
126 return -1;
127 }
128
129 /* (non-Javadoc)
130 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#replaceChild(int, org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
131 */
132 @Override
133 public ITmfFilterTreeNode replaceChild(final int index, final ITmfFilterTreeNode node) throws IndexOutOfBoundsException {
134 node.setParent(this);
135 return children.set(index, node);
136 }
137
138 /* (non-Javadoc)
139 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#setParent(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
140 */
141 @Override
142 public void setParent(final ITmfFilterTreeNode parent) {
143 this.parent = parent;
144 }
145
146
147 /* (non-Javadoc)
148 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#matches(org.eclipse.linuxtools.tmf.event.TmfEvent)
149 */
150 @Override
151 public abstract boolean matches(TmfEvent event);
152
153 /* (non-Javadoc)
154 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getValidChildren()
155 *
156 * By default, all node types are valid children. Override if different.
157 */
158 @Override
159 public List<String> getValidChildren() {
160 return Arrays.asList(VALID_CHILDREN);
161 }
162
163 @Override
164 public ITmfFilterTreeNode clone() {
165 try {
166 TmfFilterTreeNode clone = (TmfFilterTreeNode) super.clone();
167 clone.parent = null;
168 clone.children = new ArrayList<ITmfFilterTreeNode>(children.size());
169 for (ITmfFilterTreeNode child : getChildren()) {
170 clone.addChild(child.clone());
171 }
172 return clone;
173 } catch (CloneNotSupportedException e) {
174 return null;
175 }
176 }
177 }
This page took 0.03473 seconds and 5 git commands to generate.