1 /*******************************************************************************
2 * Copyright (c) 2010, 2012 Ericsson
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
10 * Yuriy Vashchuk (yvashchuk@gmail.com) - Initial API and implementation
11 * Patrick Tasse - Refactoring
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.filter
.model
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Arrays
;
18 import java
.util
.List
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
24 * The base class for the Filter tree nodes
27 * @author Yuriy Vashchuk
28 * @author Patrick Tasse
30 public abstract class TmfFilterTreeNode
implements ITmfFilterTreeNode
, Cloneable
{
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
42 private ITmfFilterTreeNode parent
= null;
43 private ArrayList
<ITmfFilterTreeNode
> children
= new ArrayList
<ITmfFilterTreeNode
>();
46 * @param parent the parent node
48 public TmfFilterTreeNode(final ITmfFilterTreeNode parent
) {
50 parent
.addChild(this);
55 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getParent()
58 public ITmfFilterTreeNode
getParent() {
63 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getNodeName()
66 public abstract String
getNodeName();
69 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#hasChildren()
72 public boolean hasChildren() {
73 return (children
.size() > 0);
77 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChildrenCount()
80 public int getChildrenCount() {
81 return children
.size();
85 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChildren()
88 public ITmfFilterTreeNode
[] getChildren() {
89 return children
.toArray(new ITmfFilterTreeNode
[0]);
93 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChild(int)
96 public ITmfFilterTreeNode
getChild(final int index
) throws IndexOutOfBoundsException
{
97 return children
.get(index
);
101 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#remove()
104 public ITmfFilterTreeNode
remove() {
105 if (getParent() != null) {
106 getParent().removeChild(this);
113 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#removeChild(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
116 public ITmfFilterTreeNode
removeChild(ITmfFilterTreeNode node
) {
117 children
.remove(node
);
118 node
.setParent(null);
123 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#addChild(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
126 public int addChild(final ITmfFilterTreeNode node
) {
127 node
.setParent(this);
128 if(children
.add(node
)) {
129 return (children
.size() - 1);
135 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#replaceChild(int, org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
138 public ITmfFilterTreeNode
replaceChild(final int index
, final ITmfFilterTreeNode node
) throws IndexOutOfBoundsException
{
139 node
.setParent(this);
140 return children
.set(index
, node
);
144 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#setParent(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
147 public void setParent(final ITmfFilterTreeNode parent
) {
148 this.parent
= parent
;
153 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#matches(org.eclipse.linuxtools.tmf.event.TmfEvent)
156 public abstract boolean matches(ITmfEvent event
);
159 * @param event the event
160 * @param field the field id
161 * @return the field value
163 protected Object
getFieldValue(ITmfEvent event
, String field
) {
165 if (ITmfEvent
.EVENT_FIELD_CONTENT
.equals(field
)) {
166 value
= event
.getContent().toString();
168 else if (ITmfEvent
.EVENT_FIELD_TYPE
.equals(field
)) {
169 value
= event
.getType().getName();
171 else if (ITmfEvent
.EVENT_FIELD_TIMESTAMP
.equals(field
)) {
172 value
= event
.getTimestamp().toString();
174 else if (ITmfEvent
.EVENT_FIELD_SOURCE
.equals(field
)) {
175 value
= event
.getSource();
177 else if (ITmfEvent
.EVENT_FIELD_REFERENCE
.equals(field
)) {
178 value
= event
.getReference();
181 ITmfEventField eventField
= event
.getContent().getField(field
);
182 if (eventField
!= null) {
183 value
= eventField
.getValue();
190 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getValidChildren()
192 * By default, all node types are valid children. Override if different.
195 public List
<String
> getValidChildren() {
196 return Arrays
.asList(VALID_CHILDREN
);
200 public ITmfFilterTreeNode
clone() {
202 TmfFilterTreeNode clone
= (TmfFilterTreeNode
) super.clone();
204 clone
.children
= new ArrayList
<ITmfFilterTreeNode
>(children
.size());
205 for (ITmfFilterTreeNode child
: getChildren()) {
206 clone
.addChild(child
.clone());
209 } catch (CloneNotSupportedException e
) {