Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2010, 2012 Ericsson |
be222f56 PT |
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.core.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.core.event.ITmfEvent; | |
21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
22 | ||
23 | /** | |
24 | * The base class for the Filter tree nodes | |
25 | * | |
26 | * @version 1.0 | |
27 | * @author Yuriy Vashchuk | |
28 | * @author Patrick Tasse | |
29 | */ | |
30 | public abstract class TmfFilterTreeNode implements ITmfFilterTreeNode, Cloneable { | |
31 | ||
32 | private static final String[] VALID_CHILDREN = { | |
33 | TmfFilterEventTypeNode.NODE_NAME, | |
34 | TmfFilterAndNode.NODE_NAME, | |
35 | TmfFilterOrNode.NODE_NAME, | |
36 | TmfFilterContainsNode.NODE_NAME, | |
37 | TmfFilterEqualsNode.NODE_NAME, | |
38 | TmfFilterMatchesNode.NODE_NAME, | |
39 | TmfFilterCompareNode.NODE_NAME | |
40 | }; | |
41 | ||
42 | private ITmfFilterTreeNode parent = null; | |
43 | private ArrayList<ITmfFilterTreeNode> children = new ArrayList<ITmfFilterTreeNode>(); | |
44 | ||
45 | /** | |
46 | * @param parent the parent node | |
47 | */ | |
48 | public TmfFilterTreeNode(final ITmfFilterTreeNode parent) { | |
49 | if (parent != null) { | |
50 | parent.addChild(this); | |
51 | } | |
52 | } | |
53 | ||
54 | /* (non-Javadoc) | |
55 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getParent() | |
56 | */ | |
57 | @Override | |
58 | public ITmfFilterTreeNode getParent() { | |
59 | return parent; | |
60 | } | |
61 | ||
62 | /* (non-Javadoc) | |
63 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getNodeName() | |
64 | */ | |
65 | @Override | |
66 | public abstract String getNodeName(); | |
67 | ||
68 | /* (non-Javadoc) | |
69 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#hasChildren() | |
70 | */ | |
71 | @Override | |
72 | public boolean hasChildren() { | |
73 | return (children.size() > 0); | |
74 | } | |
75 | ||
76 | /* (non-Javadoc) | |
77 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChildrenCount() | |
78 | */ | |
79 | @Override | |
80 | public int getChildrenCount() { | |
81 | return children.size(); | |
82 | } | |
83 | ||
84 | /* (non-Javadoc) | |
85 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChildren() | |
86 | */ | |
87 | @Override | |
88 | public ITmfFilterTreeNode[] getChildren() { | |
89 | return children.toArray(new ITmfFilterTreeNode[0]); | |
90 | } | |
91 | ||
92 | /* (non-Javadoc) | |
93 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChild(int) | |
94 | */ | |
95 | @Override | |
96 | public ITmfFilterTreeNode getChild(final int index) throws IndexOutOfBoundsException { | |
97 | return children.get(index); | |
98 | } | |
99 | ||
100 | /* (non-Javadoc) | |
101 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#remove() | |
102 | */ | |
103 | @Override | |
104 | public ITmfFilterTreeNode remove() { | |
105 | if (getParent() != null) { | |
106 | getParent().removeChild(this); | |
107 | } | |
108 | return this; | |
109 | } | |
110 | ||
111 | /* | |
112 | * (non-Javadoc) | |
113 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#removeChild(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode) | |
114 | */ | |
115 | @Override | |
116 | public ITmfFilterTreeNode removeChild(ITmfFilterTreeNode node) { | |
117 | children.remove(node); | |
118 | node.setParent(null); | |
119 | return node; | |
120 | } | |
121 | ||
122 | /* (non-Javadoc) | |
123 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#addChild(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode) | |
124 | */ | |
125 | @Override | |
126 | public int addChild(final ITmfFilterTreeNode node) { | |
127 | node.setParent(this); | |
128 | if(children.add(node)) { | |
129 | return (children.size() - 1); | |
130 | } | |
131 | return -1; | |
132 | } | |
133 | ||
134 | /* (non-Javadoc) | |
135 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#replaceChild(int, org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode) | |
136 | */ | |
137 | @Override | |
138 | public ITmfFilterTreeNode replaceChild(final int index, final ITmfFilterTreeNode node) throws IndexOutOfBoundsException { | |
139 | node.setParent(this); | |
140 | return children.set(index, node); | |
141 | } | |
142 | ||
143 | /* (non-Javadoc) | |
144 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#setParent(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode) | |
145 | */ | |
146 | @Override | |
147 | public void setParent(final ITmfFilterTreeNode parent) { | |
148 | this.parent = parent; | |
149 | } | |
150 | ||
151 | ||
152 | /* (non-Javadoc) | |
153 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#matches(org.eclipse.linuxtools.tmf.event.TmfEvent) | |
154 | */ | |
155 | @Override | |
156 | public abstract boolean matches(ITmfEvent event); | |
157 | ||
158 | /** | |
159 | * @param event the event | |
160 | * @param field the field id | |
161 | * @return the field value | |
162 | */ | |
163 | protected Object getFieldValue(ITmfEvent event, String field) { | |
164 | Object value = null; | |
165 | if (ITmfEvent.EVENT_FIELD_CONTENT.equals(field)) { | |
a4a6acf4 | 166 | value = event.getContent().toString(); |
be222f56 PT |
167 | } |
168 | else if (ITmfEvent.EVENT_FIELD_TYPE.equals(field)) { | |
169 | value = event.getType().getName(); | |
170 | } | |
171 | else if (ITmfEvent.EVENT_FIELD_TIMESTAMP.equals(field)) { | |
172 | value = event.getTimestamp().toString(); | |
173 | } | |
174 | else if (ITmfEvent.EVENT_FIELD_SOURCE.equals(field)) { | |
175 | value = event.getSource(); | |
176 | } | |
177 | else if (ITmfEvent.EVENT_FIELD_REFERENCE.equals(field)) { | |
178 | value = event.getReference(); | |
179 | } | |
180 | else { | |
181 | ITmfEventField eventField = event.getContent().getField(field); | |
182 | if (eventField != null) { | |
183 | value = eventField.getValue(); | |
184 | } | |
185 | } | |
186 | return value; | |
187 | } | |
188 | ||
189 | /* (non-Javadoc) | |
190 | * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getValidChildren() | |
191 | * | |
192 | * By default, all node types are valid children. Override if different. | |
193 | */ | |
194 | @Override | |
195 | public List<String> getValidChildren() { | |
196 | return Arrays.asList(VALID_CHILDREN); | |
197 | } | |
198 | ||
199 | @Override | |
200 | public ITmfFilterTreeNode clone() { | |
201 | try { | |
202 | TmfFilterTreeNode clone = (TmfFilterTreeNode) super.clone(); | |
203 | clone.parent = null; | |
204 | clone.children = new ArrayList<ITmfFilterTreeNode>(children.size()); | |
205 | for (ITmfFilterTreeNode child : getChildren()) { | |
206 | clone.addChild(child.clone()); | |
207 | } | |
208 | return clone; | |
209 | } catch (CloneNotSupportedException e) { | |
210 | return null; | |
211 | } | |
212 | } | |
213 | } |