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