Commit | Line | Data |
---|---|---|
be222f56 | 1 | /******************************************************************************* |
11252342 | 2 | * Copyright (c) 2010, 2013 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 | ||
11252342 AM |
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 | }; | |
be222f56 | 41 | |
11252342 AM |
42 | private ITmfFilterTreeNode parent = null; |
43 | private ArrayList<ITmfFilterTreeNode> children = new ArrayList<ITmfFilterTreeNode>(); | |
be222f56 PT |
44 | |
45 | /** | |
11252342 AM |
46 | * @param parent |
47 | * the parent node | |
be222f56 PT |
48 | */ |
49 | public TmfFilterTreeNode(final ITmfFilterTreeNode parent) { | |
11252342 AM |
50 | if (parent != null) { |
51 | parent.addChild(this); | |
52 | } | |
53 | } | |
54 | ||
55 | @Override | |
56 | public ITmfFilterTreeNode getParent() { | |
57 | return parent; | |
58 | } | |
59 | ||
60 | @Override | |
61 | public abstract String getNodeName(); | |
62 | ||
63 | @Override | |
64 | public boolean hasChildren() { | |
65 | return (children.size() > 0); | |
be222f56 PT |
66 | } |
67 | ||
11252342 AM |
68 | @Override |
69 | public int getChildrenCount() { | |
70 | return children.size(); | |
71 | } | |
72 | ||
73 | @Override | |
74 | public ITmfFilterTreeNode[] getChildren() { | |
75 | return children.toArray(new ITmfFilterTreeNode[0]); | |
76 | } | |
77 | ||
78 | @Override | |
79 | public ITmfFilterTreeNode getChild(final int index) throws IndexOutOfBoundsException { | |
80 | return children.get(index); | |
81 | } | |
82 | ||
83 | @Override | |
84 | public ITmfFilterTreeNode remove() { | |
85 | if (getParent() != null) { | |
86 | getParent().removeChild(this); | |
87 | } | |
88 | return this; | |
89 | } | |
90 | ||
91 | @Override | |
92 | public ITmfFilterTreeNode removeChild(ITmfFilterTreeNode node) { | |
93 | children.remove(node); | |
94 | node.setParent(null); | |
95 | return node; | |
96 | } | |
97 | ||
98 | @Override | |
99 | public int addChild(final ITmfFilterTreeNode node) { | |
100 | node.setParent(this); | |
101 | if (children.add(node)) { | |
102 | return (children.size() - 1); | |
103 | } | |
104 | return -1; | |
105 | } | |
106 | ||
107 | @Override | |
108 | public ITmfFilterTreeNode replaceChild(final int index, final ITmfFilterTreeNode node) throws IndexOutOfBoundsException { | |
109 | node.setParent(this); | |
110 | return children.set(index, node); | |
111 | } | |
112 | ||
113 | @Override | |
114 | public void setParent(final ITmfFilterTreeNode parent) { | |
115 | this.parent = parent; | |
116 | } | |
117 | ||
118 | @Override | |
119 | public abstract boolean matches(ITmfEvent event); | |
be222f56 PT |
120 | |
121 | /** | |
122 | * @param event the event | |
123 | * @param field the field id | |
124 | * @return the field value | |
125 | */ | |
126 | protected Object getFieldValue(ITmfEvent event, String field) { | |
127 | Object value = null; | |
128 | if (ITmfEvent.EVENT_FIELD_CONTENT.equals(field)) { | |
a4a6acf4 | 129 | value = event.getContent().toString(); |
be222f56 PT |
130 | } |
131 | else if (ITmfEvent.EVENT_FIELD_TYPE.equals(field)) { | |
132 | value = event.getType().getName(); | |
133 | } | |
134 | else if (ITmfEvent.EVENT_FIELD_TIMESTAMP.equals(field)) { | |
135 | value = event.getTimestamp().toString(); | |
136 | } | |
137 | else if (ITmfEvent.EVENT_FIELD_SOURCE.equals(field)) { | |
138 | value = event.getSource(); | |
139 | } | |
140 | else if (ITmfEvent.EVENT_FIELD_REFERENCE.equals(field)) { | |
141 | value = event.getReference(); | |
142 | } | |
143 | else { | |
144 | ITmfEventField eventField = event.getContent().getField(field); | |
145 | if (eventField != null) { | |
146 | value = eventField.getValue(); | |
147 | } | |
148 | } | |
149 | return value; | |
150 | } | |
151 | ||
11252342 AM |
152 | @Override |
153 | public List<String> getValidChildren() { | |
154 | return Arrays.asList(VALID_CHILDREN); | |
155 | } | |
156 | ||
157 | @Override | |
158 | public ITmfFilterTreeNode clone() { | |
159 | try { | |
160 | TmfFilterTreeNode clone = (TmfFilterTreeNode) super.clone(); | |
161 | clone.parent = null; | |
162 | clone.children = new ArrayList<ITmfFilterTreeNode>(children.size()); | |
163 | for (ITmfFilterTreeNode child : getChildren()) { | |
164 | clone.addChild(child.clone()); | |
165 | } | |
166 | return clone; | |
167 | } catch (CloneNotSupportedException e) { | |
168 | return null; | |
169 | } | |
170 | } | |
be222f56 | 171 | } |