1 /*******************************************************************************
2 * Copyright (c) 2010 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 * <b><u>TmfFilterTreeNode</u></b>
26 * The Filter Tree node
29 public abstract class TmfFilterTreeNode
implements ITmfFilterTreeNode
, Cloneable
{
31 private static final String
[] VALID_CHILDREN
= {
32 TmfFilterEventTypeNode
.NODE_NAME
,
33 TmfFilterAndNode
.NODE_NAME
,
34 TmfFilterOrNode
.NODE_NAME
,
35 TmfFilterContainsNode
.NODE_NAME
,
36 TmfFilterEqualsNode
.NODE_NAME
,
37 TmfFilterMatchesNode
.NODE_NAME
,
38 TmfFilterCompareNode
.NODE_NAME
41 private ITmfFilterTreeNode parent
= null;
42 private ArrayList
<ITmfFilterTreeNode
> children
= new ArrayList
<ITmfFilterTreeNode
>();
44 public TmfFilterTreeNode(final ITmfFilterTreeNode parent
) {
46 parent
.addChild(this);
51 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getParent()
54 public ITmfFilterTreeNode
getParent() {
59 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getNodeName()
62 public abstract String
getNodeName();
65 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#hasChildren()
68 public boolean hasChildren() {
69 return (children
.size() > 0);
73 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChildrenCount()
76 public int getChildrenCount() {
77 return children
.size();
81 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChildren()
84 public ITmfFilterTreeNode
[] getChildren() {
85 return children
.toArray(new ITmfFilterTreeNode
[0]);
89 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getChild(int)
92 public ITmfFilterTreeNode
getChild(final int index
) throws IndexOutOfBoundsException
{
93 return children
.get(index
);
97 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#remove()
100 public ITmfFilterTreeNode
remove() {
101 if (getParent() != null) {
102 getParent().removeChild(this);
109 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#removeChild(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
112 public ITmfFilterTreeNode
removeChild(ITmfFilterTreeNode node
) {
113 children
.remove(node
);
114 node
.setParent(null);
119 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#addChild(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
122 public int addChild(final ITmfFilterTreeNode node
) {
123 node
.setParent(this);
124 if(children
.add(node
)) {
125 return (children
.size() - 1);
131 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#replaceChild(int, org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
134 public ITmfFilterTreeNode
replaceChild(final int index
, final ITmfFilterTreeNode node
) throws IndexOutOfBoundsException
{
135 node
.setParent(this);
136 return children
.set(index
, node
);
140 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#setParent(org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode)
143 public void setParent(final ITmfFilterTreeNode parent
) {
144 this.parent
= parent
;
149 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#matches(org.eclipse.linuxtools.tmf.event.TmfEvent)
152 public abstract boolean matches(ITmfEvent event
);
155 * @param event the event
156 * @param field the field id
157 * @return the field value
159 protected Object
getFieldValue(ITmfEvent event
, String field
) {
161 if (ITmfEvent
.EVENT_FIELD_CONTENT
.equals(field
)) {
162 ITmfEventField content
= event
.getContent();
163 value
= (content
.getValue() != null) ? content
.getValue().toString() : content
.toString();
165 else if (ITmfEvent
.EVENT_FIELD_TYPE
.equals(field
)) {
166 value
= event
.getType().getName();
168 else if (ITmfEvent
.EVENT_FIELD_TIMESTAMP
.equals(field
)) {
169 value
= event
.getTimestamp().toString();
171 else if (ITmfEvent
.EVENT_FIELD_SOURCE
.equals(field
)) {
172 value
= event
.getSource();
174 else if (ITmfEvent
.EVENT_FIELD_REFERENCE
.equals(field
)) {
175 value
= event
.getReference();
178 value
= event
.getContent().getField(field
).getValue();
184 * @see org.eclipse.linuxtools.tmf.filter.model.ITmfFilterTreeNode#getValidChildren()
186 * By default, all node types are valid children. Override if different.
189 public List
<String
> getValidChildren() {
190 return Arrays
.asList(VALID_CHILDREN
);
194 public ITmfFilterTreeNode
clone() {
196 TmfFilterTreeNode clone
= (TmfFilterTreeNode
) super.clone();
198 clone
.children
= new ArrayList
<ITmfFilterTreeNode
>(children
.size());
199 for (ITmfFilterTreeNode child
: getChildren()) {
200 clone
.addChild(child
.clone());
203 } catch (CloneNotSupportedException e
) {