From 79e08fd082aa47e2f6dcb8ae50a8108e46a41597 Mon Sep 17 00:00:00 2001 From: Bernd Hufmann Date: Wed, 12 Oct 2011 08:48:12 -0400 Subject: [PATCH] Added TMF statistics feature (Bug 360572) --- .../linuxtools/tmf/tests/AllTmfCoreTests.java | 2 +- .../linuxtools/tmf/tests/util/AllTests.java | 18 + .../tmf/tests/util/TmfFixedArrayTest.java | 266 +++++++++ .../tmf/ui/tests/AllTmfUITests.java | 36 +- .../tmf/ui/tests/statistics/AllTests.java | 33 + .../TmfBaseColumnDataProviderTest.java | 164 +++++ .../statistics/TmfBaseColumnDataTest.java | 168 ++++++ .../statistics/TmfBaseStatisticsDataTest.java | 294 +++++++++ .../statistics/TmfStatisticsTreeNodeTest.java | 365 +++++++++++ .../TmfStatisticsTreeRootFactoryTest.java | 116 ++++ .../TmfTreeContentProviderTest.java | 170 ++++++ .../META-INF/MANIFEST.MF | 2 + .../plugin.properties | 3 +- org.eclipse.linuxtools.tmf.ui/plugin.xml | 9 + .../views/statistics/ITmfExtraEventInfo.java | 21 + .../tmf/ui/views/statistics/Messages.java | 31 + .../views/statistics/TmfStatisticsView.java | 565 ++++++++++++++++++ .../ui/views/statistics/messages.properties | 5 + .../model/AbsTmfStatisticsTree.java | 204 +++++++ .../model/ITmfColumnDataProvider.java | 29 + .../model/ITmfStatisticsColumnData.java | 67 +++ .../ui/views/statistics/model/Messages.java | 28 + .../statistics/model/TmfBaseColumnData.java | 127 ++++ .../model/TmfBaseColumnDataProvider.java | 138 +++++ .../model/TmfBaseStatisticsTree.java | 209 +++++++ .../views/statistics/model/TmfStatistics.java | 24 + .../model/TmfStatisticsTreeNode.java | 122 ++++ .../model/TmfStatisticsTreeRootFactory.java | 103 ++++ .../model/TmfTreeContentProvider.java | 81 +++ .../statistics/model/messages.properties | 2 + .../META-INF/MANIFEST.MF | 3 +- .../linuxtools/tmf/util/TmfFixedArray.java | 250 ++++++++ 32 files changed, 3644 insertions(+), 11 deletions(-) create mode 100644 org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/util/AllTests.java create mode 100755 org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/util/TmfFixedArrayTest.java create mode 100644 org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/AllTests.java create mode 100644 org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataProviderTest.java create mode 100755 org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataTest.java create mode 100755 org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseStatisticsDataTest.java create mode 100755 org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeNodeTest.java create mode 100755 org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeRootFactoryTest.java create mode 100755 org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfTreeContentProviderTest.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/ITmfExtraEventInfo.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/Messages.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/TmfStatisticsView.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/messages.properties create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/AbsTmfStatisticsTree.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/ITmfColumnDataProvider.java create mode 100644 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/ITmfStatisticsColumnData.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/Messages.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseColumnData.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseColumnDataProvider.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseStatisticsTree.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatistics.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatisticsTreeNode.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatisticsTreeRootFactory.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfTreeContentProvider.java create mode 100755 org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/messages.properties create mode 100755 org.eclipse.linuxtools.tmf/src/org/eclipse/linuxtools/tmf/util/TmfFixedArray.java diff --git a/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/AllTmfCoreTests.java b/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/AllTmfCoreTests.java index 340e2f7d7f..c4ffe5569c 100644 --- a/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/AllTmfCoreTests.java +++ b/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/AllTmfCoreTests.java @@ -32,8 +32,8 @@ public class AllTmfCoreTests { suite.addTest(org.eclipse.linuxtools.tmf.tests.trace.AllTests.suite()); suite.addTest(org.eclipse.linuxtools.tmf.tests.experiment.AllTests.suite()); suite.addTest(org.eclipse.linuxtools.tmf.tests.uml2sd.AllTests.suite()); + suite.addTest(org.eclipse.linuxtools.tmf.tests.util.AllTests.suite()); //$JUnit-END$ return suite; } - } diff --git a/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/util/AllTests.java b/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/util/AllTests.java new file mode 100644 index 0000000000..537bdc5b88 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/util/AllTests.java @@ -0,0 +1,18 @@ +package org.eclipse.linuxtools.tmf.tests.util; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.linuxtools.tmf.TmfCorePlugin; + +@SuppressWarnings("nls") +public class AllTests { + + public static Test suite() { + TestSuite suite = new TestSuite("Test suite for " + TmfCorePlugin.PLUGIN_ID + ".util"); //$NON-NLS-1$); + //$JUnit-BEGIN$ + suite.addTestSuite(TmfFixedArrayTest.class); + //$JUnit-END$ + return suite; + } +} \ No newline at end of file diff --git a/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/util/TmfFixedArrayTest.java b/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/util/TmfFixedArrayTest.java new file mode 100755 index 0000000000..076a6c2c68 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.tests/src/org/eclipse/linuxtools/tmf/tests/util/TmfFixedArrayTest.java @@ -0,0 +1,266 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial design and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.tests.util; + +import java.util.Arrays; + +import junit.framework.TestCase; + +import org.eclipse.linuxtools.tmf.util.TmfFixedArray; + +public class TmfFixedArrayTest extends TestCase { + + // ------------------------------------------------------------------------ + // Field(s) + // ------------------------------------------------------------------------ + TmfFixedArray fFixedArray1 = null; + TmfFixedArray fFixedArray2 = null; + String fString1, fString2, fString3, fString4, fString5; + + // ------------------------------------------------------------------------ + // Housekeeping + // ------------------------------------------------------------------------ + + public TmfFixedArrayTest() { + fString1 = "First String"; + fString2 = "Second String"; + fString3 = "Third String"; + fString4 = "Fourth String"; + fString5 = "Fifth String"; + fFixedArray1 = new TmfFixedArray(fString1, fString2, fString3); + fFixedArray2 = new TmfFixedArray(); // Empty array at the beginning + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + // ------------------------------------------------------------------------ + // Get + // ------------------------------------------------------------------------ + + public void testGet() throws Exception { + assertTrue("get", fString1.equals(fFixedArray1.get(0))); + assertTrue("get", fString2.equals(fFixedArray1.get(1))); + assertTrue("get", fString3.equals(fFixedArray1.get(2))); + + try { + fFixedArray2.get(0); + fail(); + } + catch (ArrayIndexOutOfBoundsException e) { + // Success + } + } + + public void testGetArray() throws Exception { + String[] stringArray = fFixedArray1.getArray(); + assertNotNull("getArray", stringArray); + assertTrue("getArray", fString1.equals(stringArray[0])); + assertTrue("getArray", fString2.equals(stringArray[1])); + assertTrue("getArray", fString3.equals(stringArray[2])); + } + + // ------------------------------------------------------------------------ + // Equals + // ------------------------------------------------------------------------ + + public void testEqualsReflexivity() throws Exception { + assertTrue("equals", fFixedArray1.equals(fFixedArray1)); + assertTrue("equals", fFixedArray2.equals(fFixedArray2)); + + assertTrue("equals", !fFixedArray1.equals(fFixedArray2)); + assertTrue("equals", !fFixedArray2.equals(fFixedArray1)); + } + + @SuppressWarnings("unchecked") + public void testEqualsSymmetry() throws Exception { + TmfFixedArray fixedArray1 = (TmfFixedArray) fFixedArray1.clone(); + TmfFixedArray fixedArray2 = (TmfFixedArray) fFixedArray2.clone(); + + assertTrue("equals", fixedArray1.equals(fFixedArray1)); + assertTrue("equals", fFixedArray1.equals(fixedArray1)); + + assertTrue("equals", fixedArray2.equals(fFixedArray2)); + assertTrue("equals", fFixedArray2.equals(fixedArray2)); + } + + @SuppressWarnings("unchecked") + public void testEqualsTransivity() throws Exception { + TmfFixedArray fixedArray1 = (TmfFixedArray) fFixedArray1.clone(); + TmfFixedArray fixedArray2 = (TmfFixedArray) fFixedArray1.clone(); + TmfFixedArray fixedArray3 = (TmfFixedArray) fFixedArray1.clone(); + + assertTrue("equals", fixedArray1.equals(fixedArray2)); + assertTrue("equals", fixedArray2.equals(fixedArray3)); + assertTrue("equals", fixedArray1.equals(fixedArray3)); + } + + public void testEqualsNull() throws Exception { + assertTrue("equals", !fFixedArray1.equals(null)); + assertTrue("equals", !fFixedArray2.equals(null)); + } + + // ------------------------------------------------------------------------ + // Append + // ------------------------------------------------------------------------ + + public void testAppend() { + TmfFixedArray fixedArray = new TmfFixedArray(); + + fixedArray = fixedArray.append(fString1, fString2, fString3); + assertEquals("append", 3, fixedArray.size()); + assertTrue("append", fString1.equals(fixedArray.get(0))); + assertTrue("append", fString2.equals(fixedArray.get(1))); + assertTrue("append", fString3.equals(fixedArray.get(2))); + + fixedArray = fixedArray.append(fString4); + assertEquals("append", 4, fixedArray.size()); + assertTrue("append", fString4.equals(fixedArray.get(3))); + } + + + @SuppressWarnings("unchecked") + public void testAppendFixedArray() throws Exception { + TmfFixedArray fixedArrayToAppend1 = new TmfFixedArray(fString4); + TmfFixedArray fixedArrayToAppend2 = new TmfFixedArray(fString5); + TmfFixedArray fixedArray = new TmfFixedArray(); + + fixedArray = fixedArray.append(fFixedArray1, fixedArrayToAppend1); + assertEquals("append", 4, fixedArray.size()); + assertTrue("append", fString1.equals(fixedArray.get(0))); + assertTrue("append", fString2.equals(fixedArray.get(1))); + assertTrue("append", fString3.equals(fixedArray.get(2))); + assertTrue("append", fString4.equals(fixedArray.get(3))); + + fixedArray = fixedArray.append(fixedArrayToAppend2); + assertEquals("append", 5, fixedArray.size()); + assertTrue("append", fString5.equals(fixedArray.get(4))); + } + + // ------------------------------------------------------------------------ + // hashCode + // ------------------------------------------------------------------------ + + @SuppressWarnings("unchecked") + public void testHashCode() throws Exception { + TmfFixedArray fixedArray1 = (TmfFixedArray) fFixedArray1.clone(); + TmfFixedArray fixedArray2 = (TmfFixedArray) fFixedArray2.clone(); + + assertTrue("hashCode", fixedArray1.hashCode() == fFixedArray1.hashCode()); + assertTrue("hashCode", fFixedArray2.hashCode() == fixedArray2.hashCode()); + + assertTrue("hashCode", fFixedArray1.hashCode() != fixedArray2.hashCode()); + assertTrue("hashCode", fFixedArray2.hashCode() != fixedArray1.hashCode()); + } + + // ------------------------------------------------------------------------ + // toArray + // ------------------------------------------------------------------------ + + public void testToArray() { + String[] expected1 = {fString1, fString2, fString3}; + assertTrue("toArray", Arrays.equals(expected1, fFixedArray1.toArray())); + + String[] expected2 = {}; + assertTrue("toArray", Arrays.equals(expected2, fFixedArray2.toArray())); + } + + public void testToArrayArg() throws Exception { + String[] stringArray = new String[3]; + fFixedArray1.toArray(stringArray); + assertTrue("toArrayArg", stringArray[0].equals(fFixedArray1.get(0))); + assertTrue("toArrayArg", stringArray[1].equals(fFixedArray1.get(1))); + assertTrue("toArrayArg", stringArray[2].equals(fFixedArray1.get(2))); + + String[] stringBigArray = new String[10]; + fFixedArray1.toArray(stringBigArray); + assertNull("toArrayArg", stringBigArray[3]); + + TmfFixedArray fFixedArrayObject = new TmfFixedArray(fString1); + stringArray = fFixedArrayObject.toArray(new String[0]); + assertTrue("toArrayArg", stringArray[0].equals(fString1)); + } + + // ------------------------------------------------------------------------ + // Size + // ------------------------------------------------------------------------ + + public void testSize() { + assertEquals("toArray", 3, fFixedArray1.size()); + + assertEquals("toArray", 0, fFixedArray2.size()); + } + + // ------------------------------------------------------------------------ + // SubArray + // ------------------------------------------------------------------------ + + public void testSubArray() throws Exception { + TmfFixedArray subArray = fFixedArray1.subArray(1); + + assertEquals("SubArray", 2, subArray.size()); + assertTrue("SubArray", fString2.equals(subArray.get(0))); + assertTrue("SubArray", fString3.equals(subArray.get(1))); + } + + public void testSubArray2() { + TmfFixedArray subArray = fFixedArray1.subArray(1, 2); + + assertEquals("SubArray", 2, subArray.size()); + assertTrue("SubArray", fString2.equals(subArray.get(0))); + assertTrue("SubArray", fString3.equals(subArray.get(1))); + } + + // ------------------------------------------------------------------------ + // Set + // ------------------------------------------------------------------------ + + public void testSet() throws Exception { + String[] newString = {"new FirstString", "new SecondString", "new ThirdString"}; + + fFixedArray1.set(0, newString[0]); + assertTrue("getArray", newString[0].equals(newString[0])); + + fFixedArray1.set(1, newString[1]); + assertTrue("getArray", newString[1].equals(newString[1])); + + fFixedArray1.set(2, newString[2]); + assertTrue("getArray", newString[2].equals(newString[2])); + + try { + fFixedArray2.set(0, "newString"); + fail(); + } catch (Exception e) { + // Success + } + } + + // ------------------------------------------------------------------------ + // toString + // ------------------------------------------------------------------------ + + public void testToString() { + String expected1 = Arrays.asList(fString1, fString2, fString3).toString(); + assertEquals("toString", expected1, fFixedArray1.toString()); + + String expected2 = Arrays.asList().toString(); + assertEquals("toString", expected2, fFixedArray2.toString()); + } +} diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/AllTmfUITests.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/AllTmfUITests.java index 392fe0bfe9..4badb73315 100644 --- a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/AllTmfUITests.java +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/AllTmfUITests.java @@ -1,17 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Bernd Hufmann - Initial API and implementation + * Bernd Hufmann - Add UML2SD tests + * Mathieu Denis (mathieu.denis@polymtl.ca) - Add Statistics test + *******************************************************************************/ + package org.eclipse.linuxtools.tmf.ui.tests; import junit.framework.Test; import junit.framework.TestSuite; -public class AllTmfUITests { - public static Test suite() { - TestSuite suite = new TestSuite(AllTmfUITests.class.getName()); - //$JUnit-BEGIN$ +/** + * AllTmfUITests + *

+ * Master test suite for TMF UI Core. + */ +public class AllTmfUITests { + + public static Test suite() { + TestSuite suite = new TestSuite(AllTmfUITests.class.getName()); + //$JUnit-BEGIN$ + suite.addTest(org.eclipse.linuxtools.tmf.ui.tests.statistics.AllTests.suite()); suite.addTest(org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.handlers.widgets.AllTests.suite()); suite.addTest(org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.impl.AllTests.suite()); suite.addTest(org.eclipse.linuxtools.tmf.ui.tests.views.uml2sd.load.AllTests.suite()); - //$JUnit-END$ - return suite; - } - + //$JUnit-END$ + return suite; + } } diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/AllTests.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/AllTests.java new file mode 100644 index 0000000000..d55cae3c8e --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/AllTests.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial API and Implementation + * Bernd Hufmann - Fixed suite name + *******************************************************************************/ +package org.eclipse.linuxtools.tmf.ui.tests.statistics; + +import junit.framework.Test; +import junit.framework.TestSuite; + +@SuppressWarnings("nls") +public class AllTests { + + public static Test suite() { + TestSuite suite = new TestSuite(AllTests.class.getName()); //$NON-NLS-1$); + //$JUnit-BEGIN$ + suite.addTestSuite(TmfBaseColumnDataProviderTest.class); + suite.addTestSuite(TmfBaseColumnDataTest.class); + suite.addTestSuite(TmfBaseStatisticsDataTest.class); + suite.addTestSuite(TmfStatisticsTreeNodeTest.class); + suite.addTestSuite(TmfStatisticsTreeRootFactoryTest.class); + suite.addTestSuite(TmfTreeContentProviderTest.class); + //$JUnit-END$ + return suite; + } +} \ No newline at end of file diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataProviderTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataProviderTest.java new file mode 100644 index 0000000000..c6bcbf316f --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataProviderTest.java @@ -0,0 +1,164 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial API and Implementation + * Bernd Hufmann - Fixed header and warnings + *******************************************************************************/ +package org.eclipse.linuxtools.tmf.ui.tests.statistics; + +import java.util.Vector; + +import junit.framework.TestCase; + +import org.eclipse.jface.viewers.ColumnLabelProvider; +import org.eclipse.jface.viewers.ViewerComparator; +import org.eclipse.linuxtools.tmf.event.TmfEvent; +import org.eclipse.linuxtools.tmf.event.TmfEventContent; +import org.eclipse.linuxtools.tmf.event.TmfEventReference; +import org.eclipse.linuxtools.tmf.event.TmfEventSource; +import org.eclipse.linuxtools.tmf.event.TmfEventType; +import org.eclipse.linuxtools.tmf.event.TmfTimestamp; +import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.Messages; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseColumnData; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseColumnData.ITmfColumnPercentageProvider; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseColumnDataProvider; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode; +import org.eclipse.linuxtools.tmf.util.TmfFixedArray; +@SuppressWarnings("nls") +public class TmfBaseColumnDataProviderTest extends TestCase { + + // ------------------------------------------------------------------------ + // Fields + // ------------------------------------------------------------------------ + private final static String LEVEL_COLUMN = org.eclipse.linuxtools.tmf.ui.views.statistics.Messages.TmfStatisticsView_LevelColumn; + private final static String EVENTS_COUNT_COLUMN = org.eclipse.linuxtools.tmf.ui.views.statistics.Messages.TmfStatisticsView_NbEventsColumn; + + private TmfBaseColumnDataProvider provider; + + private String fTestName; + + private final String fTypeId1 = "Some type1"; + private final String fTypeId2 = "Some type2"; + + private final String fLabel0 = "label1"; + private final String fLabel1 = "label2"; + private final String fLabel2 = "label3"; + private final String[] fLabels = new String[] { fLabel0, fLabel1, fLabel2 }; + + private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5); + private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5); + private final TmfTimestamp fTimestamp3 = new TmfTimestamp(12355, (byte) 2, 5); + + private final TmfEventSource fSource = new TmfEventSource("Source"); + + private final TmfEventType fType1 = new TmfEventType(fTypeId1, fLabels); + private final TmfEventType fType2 = new TmfEventType(fTypeId1, fLabels); + private final TmfEventType fType3 = new TmfEventType(fTypeId2, fLabels); + + private final TmfEventReference fReference = new TmfEventReference("Some reference"); + + private final TmfEvent fEvent1; + private final TmfEvent fEvent2; + private final TmfEvent fEvent3; + + private final TmfEventContent fContent1; + private final TmfEventContent fContent2; + private final TmfEventContent fContent3; + + private final TmfBaseStatisticsTree fStatsData; + + private final ITmfExtraEventInfo fExtraInfo; + + // ------------------------------------------------------------------------ + // Housekeeping + // ------------------------------------------------------------------------ + + public TmfBaseColumnDataProviderTest(final String name) { + super(name); + + fTestName = name; + + fEvent1 = new TmfEvent(fTimestamp1, fSource, fType1, fReference); + fContent1 = new TmfEventContent(fEvent1, "Some content"); + fEvent1.setContent(fContent1); + + fEvent2 = new TmfEvent(fTimestamp1, fTimestamp2, fSource, fType2, fReference); + fContent2 = new TmfEventContent(fEvent2, "Some other content"); + fEvent2.setContent(fContent2); + + fEvent3 = new TmfEvent(fTimestamp2, fTimestamp3, fSource, fType3, fReference); + fContent3 = new TmfEventContent(fEvent3, "Some other different content"); + fEvent3.setContent(fContent3); + + fStatsData = new TmfBaseStatisticsTree(); + fExtraInfo = new ITmfExtraEventInfo() { + @Override + public String getTraceName() { + return name; + } + }; + fStatsData.getOrCreate(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes)); + fStatsData.registerEvent(fEvent1, fExtraInfo); + fStatsData.registerEvent(fEvent2, fExtraInfo); + fStatsData.registerEvent(fEvent3, fExtraInfo); + + provider = new TmfBaseColumnDataProvider(); + } + + // ------------------------------------------------------------------------ + // Get Column Data + // ------------------------------------------------------------------------ + + public void testGetColumnData() { + Vector columnsData = provider.getColumnData(); + assertNotNull("getColumnData", columnsData); + assertEquals("getColumnData", 2, columnsData.size()); + + TmfStatisticsTreeNode parentNode = fStatsData.get(new TmfFixedArray(fTestName)); + TmfStatisticsTreeNode treeNode1 = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + TmfStatisticsTreeNode treeNode2 = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent3.getType().toString())); + ViewerComparator vComp = null; + for (TmfBaseColumnData columnData : columnsData) { + assertNotNull("getColumnData", columnData); + assertNotNull("getColumnData", columnData.getHeader()); + assertNotNull("getColumnData", columnData.getTooltip()); + + // Testing labelProvider + ColumnLabelProvider labelProvider = columnData.getLabelProvider(); + if (columnData.getHeader().compareTo(LEVEL_COLUMN) == 0) { + assertEquals("getColumnData", 0, labelProvider.getText(treeNode1).compareTo(treeNode1.getKey())); + } else if (columnData.getHeader().compareTo(EVENTS_COUNT_COLUMN) == 0) { + assertEquals("getColumnData", 0, labelProvider.getText(treeNode1).compareTo(Long.toString(2))); + } + + // Testing comparator + vComp = columnData.getComparator(); + if (columnData.getHeader().compareTo(LEVEL_COLUMN) == 0) { + assertTrue("getColumnData", vComp.compare(null, treeNode1, treeNode2) < 0); + assertTrue("getColumnData", vComp.compare(null, treeNode2, treeNode1) > 0); + assertTrue("getColumnData", vComp.compare(null, treeNode1, treeNode1) == 0); + } else if (columnData.getHeader().compareTo(EVENTS_COUNT_COLUMN) == 0) { + assertTrue("getColumnData", vComp.compare(null, treeNode1, treeNode2) > 0); + assertTrue("getColumnData", vComp.compare(null, treeNode2, treeNode1) < 0); + assertTrue("getColumnData", vComp.compare(null, treeNode1, treeNode1) == 0); + } + + // Testing percentage provider + ITmfColumnPercentageProvider percentProvider = columnData.getPercentageProvider(); + if (columnData.getHeader().compareTo(LEVEL_COLUMN) == 0) { + assertNull("getColumnData", percentProvider); + } else if (columnData.getHeader().compareTo(EVENTS_COUNT_COLUMN) == 0) { + double percentage = (double) treeNode1.getValue().nbEvents / parentNode.getValue().nbEvents; + assertEquals("getColumnData", (double) percentage, (double) percentProvider.getPercentage(treeNode1)); + } + } + } +} \ No newline at end of file diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataTest.java new file mode 100755 index 0000000000..7c55bd5fb0 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseColumnDataTest.java @@ -0,0 +1,168 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial design and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.tests.statistics; + +import junit.framework.TestCase; + +import org.eclipse.jface.viewers.ColumnLabelProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerComparator; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseColumnData; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseColumnData.ITmfColumnPercentageProvider; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode; +import org.eclipse.linuxtools.tmf.util.TmfFixedArray; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; + +@SuppressWarnings("nls") +public class TmfBaseColumnDataTest extends TestCase { + + // ------------------------------------------------------------------------ + // Fields + // ------------------------------------------------------------------------ + + private int fAlignment; + private int fWidth; + private String fHeader; + private String fToolTip; + private ColumnLabelProvider fLabelProvider; + private ViewerComparator fComparator; + private ITmfColumnPercentageProvider fPercentageProvider; + private TmfStatisticsTreeNode fTreeNode; + private String fTraceName; + private TmfBaseColumnData fBaseColumnData; + + // ------------------------------------------------------------------------ + // Housekeeping + // ------------------------------------------------------------------------ + + private void init() { + fHeader = "test Column1"; + fWidth = 300; + fAlignment = SWT.LEFT; + fToolTip = "Tooltip " + fHeader; + fLabelProvider = new ColumnLabelProvider() { + @Override + public String getText(Object element) { + return ((TmfStatisticsTreeNode) element).getKey(); + } + + @Override + public Image getImage(Object element) { + return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); + } + }; + fComparator = new ViewerComparator() { + @Override + public int compare(Viewer viewer, Object e1, Object e2) { + TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1; + TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2; + + return n1.getKey().compareTo(n2.getKey()); + } + }; + fPercentageProvider = new ITmfColumnPercentageProvider() { + @Override + public double getPercentage(TmfStatisticsTreeNode node) { + TmfStatisticsTreeNode parent = node; + do { + parent = parent.getParent(); + } while (parent != null && parent.getValue().nbEvents == 0); + + if (parent == null) { + return 0; + } else { + return (double) node.getValue().nbEvents / parent.getValue().nbEvents; + } + } + }; + + TmfBaseStatisticsTree baseData = new TmfBaseStatisticsTree(); + fTraceName = "trace1"; + fTreeNode = new TmfStatisticsTreeNode(new TmfFixedArray(fTraceName), baseData); + + fBaseColumnData = new TmfBaseColumnData(fHeader, fWidth, fAlignment, fToolTip, fLabelProvider, fComparator, fPercentageProvider); + } + + @Override + public void setUp() throws Exception { + super.setUp(); + init(); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + } + + // ------------------------------------------------------------------------ + // getHeader + // ------------------------------------------------------------------------ + + public void testGetHeader() { + assertEquals("getHeader", 0, fBaseColumnData.getHeader().compareTo(fHeader)); + } + + // ------------------------------------------------------------------------ + // getWidth + // ------------------------------------------------------------------------ + + public void testGetWidth() { + assertEquals("getWidth", fWidth, fBaseColumnData.getWidth()); + } + + // ------------------------------------------------------------------------ + // getAlignment + // ------------------------------------------------------------------------ + + public void testGetAlignment() { + assertEquals("getAlignment", fAlignment, fBaseColumnData.getAlignment()); + } + + // ------------------------------------------------------------------------ + // getToolTip + // ------------------------------------------------------------------------ + + public void testGetTooltip() { + assertEquals("getTooltip", fToolTip, fBaseColumnData.getTooltip()); + } + + // ------------------------------------------------------------------------ + // getLabelProvider + // ------------------------------------------------------------------------ + + public void testGetLabelProvider() { + assertEquals("getLabelProvider", 0, fBaseColumnData.getLabelProvider().getText(fTreeNode).compareTo(fLabelProvider.getText(fTreeNode))); + assertTrue("getLabelProvider", fBaseColumnData.getLabelProvider().getImage(fTreeNode).equals(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT))); + assertTrue("getLabelProvider", fBaseColumnData.getLabelProvider().equals(fLabelProvider)); + } + + // ------------------------------------------------------------------------ + // getComparator + // ------------------------------------------------------------------------ + + public void testGetComparator() { + assertTrue("getComparator", fBaseColumnData.getComparator().equals(fComparator)); + } + + // ------------------------------------------------------------------------ + // getPercentageProvider + // ------------------------------------------------------------------------ + + public void testGetPercentageProvider() { + assertTrue("getPercentageProvider", fBaseColumnData.getPercentageProvider().equals(fPercentageProvider)); + } +} diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseStatisticsDataTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseStatisticsDataTest.java new file mode 100755 index 0000000000..cfe1ae3c08 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfBaseStatisticsDataTest.java @@ -0,0 +1,294 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial design and implementation + * Bernd Hufmann - Fixed warnings + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.tests.statistics; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.eclipse.linuxtools.tmf.event.TmfEvent; +import org.eclipse.linuxtools.tmf.event.TmfEventContent; +import org.eclipse.linuxtools.tmf.event.TmfEventReference; +import org.eclipse.linuxtools.tmf.event.TmfEventSource; +import org.eclipse.linuxtools.tmf.event.TmfEventType; +import org.eclipse.linuxtools.tmf.event.TmfTimestamp; +import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.Messages; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode; +import org.eclipse.linuxtools.tmf.util.TmfFixedArray; + +@SuppressWarnings("nls") +public class TmfBaseStatisticsDataTest extends TestCase { + + // ------------------------------------------------------------------------ + // Fields + // ------------------------------------------------------------------------ + private String fTestName = null; + + private final String fTypeId1 = "Some type1"; + private final String fTypeId2 = "Some type2"; + + private final String fLabel0 = "label1"; + private final String fLabel1 = "label2"; + private final String fLabel2 = "label3"; + private final String[] fLabels = new String[] { fLabel0, fLabel1, fLabel2 }; + + private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5); + private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5); + private final TmfTimestamp fTimestamp3 = new TmfTimestamp(12355, (byte) 2, 5); + + private final TmfEventSource fSource = new TmfEventSource("Source"); + + private final TmfEventType fType1 = new TmfEventType(fTypeId1, fLabels); + private final TmfEventType fType2 = new TmfEventType(fTypeId1, fLabels); + private final TmfEventType fType3 = new TmfEventType(fTypeId2, fLabels); + + private final TmfEventReference fReference = new TmfEventReference("Some reference"); + + private final TmfEvent fEvent1; + private final TmfEvent fEvent2; + private final TmfEvent fEvent3; + + private final TmfEventContent fContent1; + private final TmfEventContent fContent2; + private final TmfEventContent fContent3; + + private final TmfBaseStatisticsTree fStatsData; + + private final ITmfExtraEventInfo fExtraInfo; + + // ------------------------------------------------------------------------ + // Housekeeping + // ------------------------------------------------------------------------ + + /** + * @param name of the test + */ + public TmfBaseStatisticsDataTest(final String name) { + super(name); + + fTestName = name; + + fEvent1 = new TmfEvent(fTimestamp1, fSource, fType1, fReference); + fContent1 = new TmfEventContent(fEvent1, "Some content"); + fEvent1.setContent(fContent1); + + fEvent2 = new TmfEvent(fTimestamp1, fTimestamp2, fSource, fType2, fReference); + fContent2 = new TmfEventContent(fEvent2, "Some other content"); + fEvent2.setContent(fContent2); + + fEvent3 = new TmfEvent(fTimestamp2, fTimestamp3, fSource, fType3, fReference); + fContent3 = new TmfEventContent(fEvent3, "Some other different content"); + fEvent3.setContent(fContent3); + + fStatsData = new TmfBaseStatisticsTree(); + fExtraInfo = new ITmfExtraEventInfo() { + @Override + public String getTraceName() { + return name; + } + }; + fStatsData.registerEvent(fEvent1, fExtraInfo); + fStatsData.registerEvent(fEvent2, fExtraInfo); + fStatsData.registerEvent(fEvent3, fExtraInfo); + } + + // ------------------------------------------------------------------------ + // GetChildren + // ------------------------------------------------------------------------ + + public void testGetChildren() { + // Getting children of the ROOT + Collection childrenTreeNode = fStatsData.getChildren(AbsTmfStatisticsTree.ROOT); + assertEquals("getChildren", 1, childrenTreeNode.size()); + TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); + assertEquals("getChildren", fTestName, treeNode.getKey()); + + // Getting children of the trace + childrenTreeNode = fStatsData.getChildren(new TmfFixedArray(fTestName)); + assertEquals("getChildren", 1, childrenTreeNode.size()); + treeNode = childrenTreeNode.iterator().next(); + assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); + + Vector keyExpected = new Vector(); + keyExpected.add(fEvent1.getType().toString()); + keyExpected.add(fEvent3.getType().toString()); + // Getting children of a category + childrenTreeNode = fStatsData.getChildren(treeNode.getPath()); + assertEquals("getChildren", 2, childrenTreeNode.size()); + + Iterator iterChild = childrenTreeNode.iterator(); + TmfStatisticsTreeNode temp; + while (iterChild.hasNext()) { + temp = iterChild.next(); + if (keyExpected.contains(temp.getKey())) { + keyExpected.removeElement(temp.getKey()); + } + else { + fail(); + } + } + + // Get children of a specific event type + childrenTreeNode = fStatsData.getChildren(childrenTreeNode.iterator().next().getPath()); + assertEquals("getChildren", 0, childrenTreeNode.size()); + } + + // ------------------------------------------------------------------------ + // GetAllChildren + // ------------------------------------------------------------------------ + + public void testGetAllChildren() { + // Getting children of the ROOT + Collection childrenTreeNode = fStatsData.getAllChildren(AbsTmfStatisticsTree.ROOT); + assertEquals("getChildren", 1, childrenTreeNode.size()); + TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); + assertEquals("getChildren", fTestName, treeNode.getKey()); + + // Getting children of the trace + childrenTreeNode = fStatsData.getAllChildren(new TmfFixedArray(fTestName)); + assertEquals("getChildren", 1, childrenTreeNode.size()); + treeNode = childrenTreeNode.iterator().next(); + assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); + + Vector keyExpected = new Vector(); + keyExpected.add(fEvent1.getType().toString()); + keyExpected.add(fEvent3.getType().toString()); + // It should return the eventType even though the number of events equals 0 + fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())).reset(); + // Getting children of a category + childrenTreeNode = fStatsData.get(treeNode.getPath()).getAllChildren(); + assertEquals("getChildren", 2, childrenTreeNode.size()); + + Iterator iterChild = childrenTreeNode.iterator(); + TmfStatisticsTreeNode temp; + while (iterChild.hasNext()) { + temp = iterChild.next(); + if (keyExpected.contains(temp.getKey())) { + keyExpected.removeElement(temp.getKey()); + } + else { + fail(); + } + } + + // Get children of a specific event type + childrenTreeNode = fStatsData.getAllChildren(childrenTreeNode.iterator().next().getPath()); + assertEquals("getChildren", 0, childrenTreeNode.size()); + } + + // ------------------------------------------------------------------------ + // RegisterEvent + // ------------------------------------------------------------------------ + + public void testRegisterEvent() { + TmfStatisticsTreeNode trace = fStatsData.get(new TmfFixedArray(fTestName)); + assertEquals("registerEvent", 3, trace.getValue().nbEvents); + + Collection childrenTreeNode = fStatsData.getChildren(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes)); + for (TmfStatisticsTreeNode child : childrenTreeNode) { + if (child.getKey().compareTo(fEvent1.getType().toString()) == 0) { + assertEquals("registerEvent", 2, child.getValue().nbEvents); + } + else if (child.getKey().compareTo(fEvent3.getType().toString()) == 0) { + assertEquals("registerEvent", 1, child.getValue().nbEvents); + } + } + } + + // ------------------------------------------------------------------------ + // Get a node + // ------------------------------------------------------------------------ + + public void testGet() { + TmfStatisticsTreeNode traceRoot = fStatsData.get(new TmfFixedArray(fTestName)); + assertNotNull("get", traceRoot); + assertEquals("get", 0, traceRoot.getPath().toString().compareTo("[" + fTestName + "]")); + assertEquals("get", 3, traceRoot.getValue().nbEvents); + assertEquals("get", 1, traceRoot.getNbChildren()); + } + + // ------------------------------------------------------------------------ + // GetOrCreate + // ------------------------------------------------------------------------ + + public void testGetOrCreate() { + TmfFixedArray newEventType = new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, "Fancy Type"); + TmfStatisticsTreeNode newEventTypeNode; + + // newEventType is not in the tree + newEventTypeNode = fStatsData.get(newEventType); + assertNull("getOrCreate", newEventTypeNode); + + newEventTypeNode = fStatsData.getOrCreate(newEventType); + assertNotNull("getOrCreate", newEventTypeNode); + assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType)); + + // newEventType is in the tree + newEventTypeNode.reset(); + newEventTypeNode = fStatsData.get(newEventType); + assertNotNull("getOrCreate", newEventTypeNode); + + newEventTypeNode = fStatsData.getOrCreate(newEventType); + assertNotNull("getOrCreate", newEventTypeNode); + assertTrue("getOrCreate", newEventTypeNode.getPath().equals(newEventType)); + } + + // ------------------------------------------------------------------------ + // GetParent + // ------------------------------------------------------------------------ + + public void testGetParent() { + TmfStatisticsTreeNode parentNode = fStatsData.getParent(AbsTmfStatisticsTree.ROOT); + assertNull("getParent", parentNode); + + parentNode = fStatsData.getParent(new TmfFixedArray("TreeRootNode that should not exist")); + assertNotNull("getParent", parentNode); + assertEquals("getParent", 0, parentNode.getKey().compareTo(fStatsData.get(AbsTmfStatisticsTree.ROOT).getKey().toString())); + + parentNode = fStatsData.getParent(new TmfFixedArray("TreeNode", Messages.TmfStatisticsData_EventTypes, "TreeNode that should not exist")); + assertNull("getParent", parentNode); + parentNode = fStatsData.getParent(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + assertNull("getParent", parentNode); + + parentNode = fStatsData.getParent(new TmfFixedArray(fTestName)); + assertNotNull("getParent", parentNode); + assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(AbsTmfStatisticsTree.ROOT.toString())); + + parentNode = fStatsData.getParent(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes)); + assertNotNull("getParent", parentNode); + assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(fStatsData.get(new TmfFixedArray(fTestName)).getPath().toString())); + } + + // ------------------------------------------------------------------------ + // Reset + // ------------------------------------------------------------------------ + + public void testReset() { + fStatsData.reset(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes)); + + assertEquals("reset", 0, fStatsData.getChildren(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes)).size()); + assertNull("reset", fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fType1.getTypeId()))); + assertNull("reset", fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fType3.getTypeId()))); + + fStatsData.reset(new TmfFixedArray(fTestName)); + + // A rootz should always have at least one child that is eventType + assertEquals("reset", 1, fStatsData.getChildren(new TmfFixedArray(fTestName)).size()); + } +} diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeNodeTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeNodeTest.java new file mode 100755 index 0000000000..73dbbfc048 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeNodeTest.java @@ -0,0 +1,365 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial design and implementation + * Bernd Hufmann - Fixed warnings + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.tests.statistics; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.eclipse.linuxtools.tmf.event.TmfEvent; +import org.eclipse.linuxtools.tmf.event.TmfEventContent; +import org.eclipse.linuxtools.tmf.event.TmfEventReference; +import org.eclipse.linuxtools.tmf.event.TmfEventSource; +import org.eclipse.linuxtools.tmf.event.TmfEventType; +import org.eclipse.linuxtools.tmf.event.TmfTimestamp; +import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.Messages; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode; +import org.eclipse.linuxtools.tmf.util.TmfFixedArray; + +@SuppressWarnings("nls") +public class TmfStatisticsTreeNodeTest extends TestCase { + + // ------------------------------------------------------------------------ + // Fields + // ------------------------------------------------------------------------ + private String fTestName = null; + + + private final String fTypeId1 = "Some type1"; + private final String fTypeId2 = "Some type2"; + + private final String fLabel0 = "label1"; + private final String fLabel1 = "label2"; + private final String fLabel2 = "label3"; + private final String[] fLabels = new String[] { fLabel0, fLabel1, fLabel2 }; + + private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5); + private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5); + private final TmfTimestamp fTimestamp3 = new TmfTimestamp(12355, (byte) 2, 5); + + private final TmfEventSource fSource = new TmfEventSource("Source"); + + private final TmfEventType fType1 = new TmfEventType(fTypeId1, fLabels); + private final TmfEventType fType2 = new TmfEventType(fTypeId1, fLabels); + private final TmfEventType fType3 = new TmfEventType(fTypeId2, fLabels); + + private final TmfEventReference fReference = new TmfEventReference("Some reference"); + + private final TmfEvent fEvent1; + private final TmfEvent fEvent2; + private final TmfEvent fEvent3; + + private final TmfEventContent fContent1; + private final TmfEventContent fContent2; + private final TmfEventContent fContent3; + + private final TmfBaseStatisticsTree fStatsData; + + private final ITmfExtraEventInfo fExtraInfo; + + // ------------------------------------------------------------------------ + // Housekeeping + // ------------------------------------------------------------------------ + + /** + * @param name of the test + */ + public TmfStatisticsTreeNodeTest(final String name) { + super(name); + + fTestName = name; + + fEvent1 = new TmfEvent(fTimestamp1, fSource, fType1, fReference); + fContent1 = new TmfEventContent(fEvent1, "Some content"); + fEvent1.setContent(fContent1); + + fEvent2 = new TmfEvent(fTimestamp1, fTimestamp2, fSource, fType2, fReference); + fContent2 = new TmfEventContent(fEvent2, "Some other content"); + fEvent2.setContent(fContent2); + + fEvent3 = new TmfEvent(fTimestamp2, fTimestamp3, fSource, fType3, fReference); + fContent3 = new TmfEventContent(fEvent3, "Some other different content"); + fEvent3.setContent(fContent3); + + fStatsData = new TmfBaseStatisticsTree(); + fExtraInfo = new ITmfExtraEventInfo() { + @Override + public String getTraceName() { + return name; + } + }; + fStatsData.registerEvent(fEvent1, fExtraInfo); + fStatsData.registerEvent(fEvent2, fExtraInfo); + fStatsData.registerEvent(fEvent3, fExtraInfo); + } + + // ------------------------------------------------------------------------ + // ContainsChild + // ------------------------------------------------------------------------ + + public void testContainsChild() { + TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); + TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray(fTestName)); + // Creates a category from the key already created + TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); + + assertTrue("containsChild", rootNode.containsChild(fTestName)); + assertFalse("containsChild", rootNode.containsChild(catNode.getKey())); + assertFalse("containsChild", rootNode.containsChild(null)); + + assertTrue("containsChild", traceNode.containsChild(catNode.getKey())); + assertFalse("containsChild", traceNode.containsChild(fEvent1.getType().toString())); + assertFalse("containsChild", traceNode.containsChild(null)); + + assertTrue("containsChild", catNode.containsChild(fEvent1.getType().toString())); + assertTrue("containsChild", catNode.containsChild(fEvent3.getType().toString())); + assertFalse("containsChild", catNode.containsChild(null)); + } + + // ------------------------------------------------------------------------ + // GetChildren + // ------------------------------------------------------------------------ + + public void testGetChildren() { + // Getting children of the ROOT + Collection childrenTreeNode = fStatsData.get(AbsTmfStatisticsTree.ROOT).getChildren(); + assertEquals("getChildren", 1, childrenTreeNode.size()); + TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); + assertEquals("getChildren", fTestName, treeNode.getKey()); + + // Getting children of the trace + childrenTreeNode = fStatsData.get(new TmfFixedArray(fTestName)).getChildren(); + assertEquals("getChildren", 1, childrenTreeNode.size()); + treeNode = childrenTreeNode.iterator().next(); + assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); + + Vector keyExpected = new Vector(); + keyExpected.add(fEvent1.getType().toString()); + keyExpected.add(fEvent3.getType().toString()); + // Getting children of a category + childrenTreeNode = treeNode.getChildren(); + assertEquals("getChildren", 2, childrenTreeNode.size()); + + Iterator iterChild = childrenTreeNode.iterator(); + TmfStatisticsTreeNode temp; + while (iterChild.hasNext()) { + temp = iterChild.next(); + if (keyExpected.contains(temp.getKey())) { + keyExpected.removeElement(temp.getKey()); + } + else { + fail(); + } + } + + // Get children of a specific event type + childrenTreeNode = fStatsData.get(childrenTreeNode.iterator().next().getPath()).getChildren(); + assertEquals("getChildren", 0, childrenTreeNode.size()); + } + + // ------------------------------------------------------------------------ + // GetAllChildren + // ------------------------------------------------------------------------ + + public void testGetAllChildren() { + // Getting children of the ROOT + Collection childrenTreeNode = fStatsData.get(AbsTmfStatisticsTree.ROOT).getAllChildren(); + assertEquals("getChildren", 1, childrenTreeNode.size()); + TmfStatisticsTreeNode treeNode = childrenTreeNode.iterator().next(); + assertEquals("getChildren", fTestName, treeNode.getKey()); + + // Getting children of the trace + childrenTreeNode = fStatsData.get(new TmfFixedArray(fTestName)).getAllChildren(); + assertEquals("getChildren", 1, childrenTreeNode.size()); + treeNode = childrenTreeNode.iterator().next(); + assertEquals("getChildren", Messages.TmfStatisticsData_EventTypes, treeNode.getKey()); + + Vector keyExpected = new Vector(); + keyExpected.add(fEvent1.getType().toString()); + keyExpected.add(fEvent3.getType().toString()); + // It should return the eventType even though the number of events equals 0 + fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())).reset(); + // Getting children of a category + childrenTreeNode = treeNode.getAllChildren(); + assertEquals("getChildren", 2, childrenTreeNode.size()); + + Iterator iterChild = childrenTreeNode.iterator(); + TmfStatisticsTreeNode temp; + while (iterChild.hasNext()) { + temp = iterChild.next(); + if (keyExpected.contains(temp.getKey())) { + keyExpected.removeElement(temp.getKey()); + } + else { + fail(); + } + } + + // Get children of a specific event type + childrenTreeNode = fStatsData.get(childrenTreeNode.iterator().next().getPath()).getAllChildren(); + assertEquals("getChildren", 0, childrenTreeNode.size()); + } + + // ------------------------------------------------------------------------ + // GetNbChildren + // ------------------------------------------------------------------------ + + public void testGetNbChildren() { + TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); + TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray(fTestName)); + TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); + TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + + assertEquals("getNbChildren", 1, rootNode.getNbChildren()); + assertEquals("getNbChildren", 1, traceNode.getNbChildren()); + assertEquals("getNbChildren", 2, catNode.getNbChildren()); + assertEquals("getNbChildren", 0, elementNode.getNbChildren()); + } + + // ------------------------------------------------------------------------ + // HasChildren + // ------------------------------------------------------------------------ + + public void testHasChildren() { + TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); + TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray(fTestName)); + TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); + TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + + assertTrue ("hasChildren", rootNode.hasChildren()); + assertTrue ("hasChildren", traceNode.hasChildren()); + assertTrue ("hasChildren", catNode.hasChildren()); + assertFalse("hasChildren", elementNode.hasChildren()); + } + + // ------------------------------------------------------------------------ + // GetParent + // ------------------------------------------------------------------------ + + public void testGetParent() { + TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); + TmfStatisticsTreeNode parentNode = rootNode.getParent(); + assertNull("getParent", parentNode); + + TmfStatisticsTreeNode newTraceNode = new TmfStatisticsTreeNode(new TmfFixedArray("newly created trace node"), fStatsData); + parentNode = newTraceNode.getParent(); + assertNotNull("getParent", parentNode); + assertEquals("getParent", 0, parentNode.getKey().compareTo(fStatsData.get(AbsTmfStatisticsTree.ROOT).getKey().toString())); + + TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray(fTestName)); + parentNode = traceNode.getParent(); + assertNotNull("getParent", parentNode); + assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(AbsTmfStatisticsTree.ROOT.toString())); + + TmfStatisticsTreeNode newNode = new TmfStatisticsTreeNode(new TmfFixedArray("TreeNode", Messages.TmfStatisticsData_EventTypes, "TreeNode that should not exist"), fStatsData); + parentNode = newNode.getParent(); + assertNull("getParent", parentNode); + + TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + parentNode = elementNode.getParent(); + assertNull("getParent", parentNode); + + TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); + parentNode = catNode.getParent(); + assertNotNull("getParent", parentNode); + assertEquals("getParent", 0, parentNode.getPath().toString().compareTo(fStatsData.get(new TmfFixedArray(fTestName)).getPath().toString())); + + parentNode = elementNode.getParent(); + assertNotNull("getParent", parentNode); + assertTrue("getParent", parentNode.getPath().equals(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes))); + } + + // ------------------------------------------------------------------------ + // GetKey + // ------------------------------------------------------------------------ + + public void testGetKey() { + TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); + TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray(fTestName)); + TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); + TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + + assertEquals("getKey", 0, rootNode.getKey().compareTo(AbsTmfStatisticsTree.ROOT.get(0))); + assertEquals("getKey", 0, traceNode.getKey().compareTo(fTestName)); + assertEquals("getKey", 0, catNode.getKey().compareTo(Messages.TmfStatisticsData_EventTypes)); + assertEquals("getKey", 0, elementNode.getKey().compareTo(fEvent1.getType().toString())); + } + + // ------------------------------------------------------------------------ + // GetPath + // ------------------------------------------------------------------------ + + public void testGetPath() { + TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); + TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray(fTestName)); + TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); + TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + + assertTrue("getPath", rootNode.getPath().equals(AbsTmfStatisticsTree.ROOT)); + assertTrue("getPath", traceNode.getPath().equals(new TmfFixedArray(fTestName))); + assertTrue("getPath", catNode.getPath().equals(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes))); + assertTrue("getPath", elementNode.getPath().equals(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()))); + } + + // ------------------------------------------------------------------------ + // GetValue + // ------------------------------------------------------------------------ + + public void testGetValue() { + TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); + TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray(fTestName)); + TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); + TmfStatisticsTreeNode elementNode1 = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + TmfStatisticsTreeNode elementNode2 = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent3.getType().toString())); + + assertEquals("getValue", 0, rootNode.getValue().nbEvents); + assertEquals("getValue", 3, traceNode.getValue().nbEvents); + assertEquals("getValue", 0, catNode.getValue().nbEvents); + assertEquals("getValue", 2, elementNode1.getValue().nbEvents); + assertEquals("getValue", 1, elementNode2.getValue().nbEvents); + } + + // ------------------------------------------------------------------------ + // Reset + // ------------------------------------------------------------------------ + + public void testReset() { + TmfStatisticsTreeNode rootNode = fStatsData.get(AbsTmfStatisticsTree.ROOT); + TmfStatisticsTreeNode traceNode = fStatsData.get(new TmfFixedArray(fTestName)); + TmfStatisticsTreeNode catNode = traceNode.getChildren().iterator().next(); + TmfStatisticsTreeNode elementNode = fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + + elementNode.reset(); + assertEquals("reset", 0, elementNode.getValue().nbEvents); + + catNode.reset(); + assertEquals("reset", 0, catNode.getValue().nbEvents); + assertEquals("reset", 0, catNode.getNbChildren()); + assertNull("reset", fStatsData.get(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()))); + + traceNode.reset(); + assertEquals("reset", 0, traceNode.getValue().nbEvents); + // A trace always have at least one child that is eventType + assertEquals("reset", 1, traceNode.getNbChildren()); + + rootNode.reset(); + assertEquals("reset", 0, rootNode.getValue().nbEvents); + assertEquals("reset", 1, rootNode.getNbChildren()); + } +} diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeRootFactoryTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeRootFactoryTest.java new file mode 100755 index 0000000000..7282bcc9a7 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfStatisticsTreeRootFactoryTest.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial design and implementation + * Bernd Hufmann - Fixed warnings + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.tests.statistics; + +import junit.framework.TestCase; + +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeRootFactory; + +@SuppressWarnings("nls") +public class TmfStatisticsTreeRootFactoryTest extends TestCase { + + // ------------------------------------------------------------------------ + // Fields + // ------------------------------------------------------------------------ + + AbsTmfStatisticsTree fStatisticsData1; + AbsTmfStatisticsTree fStatisticsData2; + AbsTmfStatisticsTree fStatisticsData3; + String fDataKey1 = "key1"; + String fDataKey2 = "key2"; + String fDataKey3 = "key3"; + + // ------------------------------------------------------------------------ + // Housekeeping + // ------------------------------------------------------------------------ + + @Override + protected void setUp() throws Exception { + super.setUp(); + addStatsTreeRoot(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void addStatsTreeRoot() { + fStatisticsData1 = new TmfBaseStatisticsTree(); + fStatisticsData2 = new TmfBaseStatisticsTree(); + fStatisticsData3 = new TmfBaseStatisticsTree(); + TmfStatisticsTreeRootFactory.addStatsTreeRoot(fDataKey1, fStatisticsData1); + TmfStatisticsTreeRootFactory.addStatsTreeRoot(fDataKey2, fStatisticsData2); + TmfStatisticsTreeRootFactory.addStatsTreeRoot(fDataKey2, fStatisticsData3); + } + + // ------------------------------------------------------------------------ + // get + // ------------------------------------------------------------------------ + + public void testGetStatTreeRoot() { + TmfStatisticsTreeNode value1 = TmfStatisticsTreeRootFactory.getStatTreeRoot(fDataKey1); + TmfStatisticsTreeNode value2 = TmfStatisticsTreeRootFactory.getStatTreeRoot(fDataKey2); + TmfStatisticsTreeNode value3 = TmfStatisticsTreeRootFactory.getStatTreeRoot(fDataKey1); + assertNotSame("getStatTreeRoot", value1, value2); + assertNotSame("getStatTreeRoot", value2, value3); + assertSame("getStatTreeRoot", value1, value3); + assertNull("getStatTreeRoot", TmfStatisticsTreeRootFactory.getStatTreeRoot(null)); + } + + public void testGetStatTree() { + AbsTmfStatisticsTree value1 = TmfStatisticsTreeRootFactory.getStatTree(fDataKey1); + AbsTmfStatisticsTree value2 = TmfStatisticsTreeRootFactory.getStatTree(fDataKey2); + AbsTmfStatisticsTree value3 = TmfStatisticsTreeRootFactory.getStatTree(fDataKey1); + assertNotSame("getStatTree", value1, value2); + assertNotSame("getStatTree", value2, value3); + assertSame("getStatTree", value1, value3); + assertNull("getStatTreeRoot", TmfStatisticsTreeRootFactory.getStatTree(null)); + } + + // ------------------------------------------------------------------------ + // contains + // ------------------------------------------------------------------------ + + public void testContainsTreeRoot() { + assertTrue("containsTreeRoot", TmfStatisticsTreeRootFactory.containsTreeRoot(fDataKey1)); + assertTrue("containsTreeRoot", TmfStatisticsTreeRootFactory.containsTreeRoot(fDataKey2)); + assertFalse("containsTreeRoot", TmfStatisticsTreeRootFactory.containsTreeRoot(null)); + } + + // ------------------------------------------------------------------------ + // remove + // ------------------------------------------------------------------------ + + public void testRemoveStatTreeRoot() { + TmfStatisticsTreeRootFactory.removeStatTreeRoot(fDataKey1); + assertNull("removeStatTreeRoot", TmfStatisticsTreeRootFactory.getStatTree(fDataKey1)); + + try { + TmfStatisticsTreeRootFactory.removeStatTreeRoot(null); + // Success + } catch (Exception e) { + fail("removeStatTreeRoot"); + } + } + + public void testRemoveAll() { + TmfStatisticsTreeRootFactory.removeAll(); + assertNull("removeAll", TmfStatisticsTreeRootFactory.getStatTreeRoot(fDataKey2)); + assertNull("removeAll", TmfStatisticsTreeRootFactory.getStatTreeRoot(fDataKey3)); + } +} diff --git a/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfTreeContentProviderTest.java b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfTreeContentProviderTest.java new file mode 100755 index 0000000000..7457f2ba32 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui.tests/src/org/eclipse/linuxtools/tmf/ui/tests/statistics/TmfTreeContentProviderTest.java @@ -0,0 +1,170 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial design and implementation + * Bernd Hufmann - Fixed warnings + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.tests.statistics; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.eclipse.linuxtools.tmf.event.TmfEvent; +import org.eclipse.linuxtools.tmf.event.TmfEventContent; +import org.eclipse.linuxtools.tmf.event.TmfEventReference; +import org.eclipse.linuxtools.tmf.event.TmfEventSource; +import org.eclipse.linuxtools.tmf.event.TmfEventType; +import org.eclipse.linuxtools.tmf.event.TmfTimestamp; +import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.Messages; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfTreeContentProvider; +import org.eclipse.linuxtools.tmf.util.TmfFixedArray; + +@SuppressWarnings("nls") +public class TmfTreeContentProviderTest extends TestCase { + + // ------------------------------------------------------------------------ + // Fields + // ------------------------------------------------------------------------ + + private String fTestName = null; + + private final String fTypeId1 = "Some type1"; + private final String fTypeId2 = "Some type2"; + + private final String fLabel0 = "label1"; + private final String fLabel1 = "label2"; + private final String[] fLabels = new String[] { fLabel0, fLabel1 }; + + private final TmfTimestamp fTimestamp1 = new TmfTimestamp(12345, (byte) 2, 5); + private final TmfTimestamp fTimestamp2 = new TmfTimestamp(12350, (byte) 2, 5); + + private final TmfEventSource fSource = new TmfEventSource("Source"); + + private final TmfEventType fType1 = new TmfEventType(fTypeId1, fLabels); + private final TmfEventType fType2 = new TmfEventType(fTypeId2, fLabels); + + private final TmfEventReference fReference = new TmfEventReference("Some reference"); + + private final TmfEvent fEvent1; + private final TmfEvent fEvent2; + + private final TmfEventContent fContent1; + private final TmfEventContent fContent2; + + private final TmfBaseStatisticsTree fStatsData; + + private final ITmfExtraEventInfo fExtraInfo; + + private final TmfTreeContentProvider treeProvider; + + // ------------------------------------------------------------------------ + // Housekeeping + // ------------------------------------------------------------------------ + + /** + * @param name + * of the test + */ + public TmfTreeContentProviderTest(final String name) { + super(name); + + fTestName = name; + + fEvent1 = new TmfEvent(fTimestamp1, fSource, fType1, fReference); + fContent1 = new TmfEventContent(fEvent1, "Some content"); + fEvent1.setContent(fContent1); + + fEvent2 = new TmfEvent(fTimestamp1, fTimestamp2, fSource, fType2, fReference); + fContent2 = new TmfEventContent(fEvent2, "Some other content"); + fEvent2.setContent(fContent2); + + fStatsData = new TmfBaseStatisticsTree(); + fExtraInfo = new ITmfExtraEventInfo() { + @Override + public String getTraceName() { + return name; + } + }; + fStatsData.registerEvent(fEvent1, fExtraInfo); + fStatsData.registerEvent(fEvent2, fExtraInfo); + + treeProvider = new TmfTreeContentProvider(); + } + + // ------------------------------------------------------------------------ + // GetChildren + // ------------------------------------------------------------------------ + + public void testGetChildren() { + Object[] objectArray = treeProvider.getChildren(fStatsData.getOrCreate(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes))); + TmfStatisticsTreeNode[] childrenNode = Arrays.asList(objectArray).toArray(new TmfStatisticsTreeNode[0]); + + Collection> childrenExpected = new Vector>(); + childrenExpected.add(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString())); + childrenExpected.add(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent2.getType().toString())); + + assertEquals("getChildren", childrenExpected.size(), childrenNode.length); + // assertTrue("getChildren", childrenPath.equals(childrenExpected)); + for (TmfStatisticsTreeNode childNode : childrenNode) { + if (childrenExpected.contains(childNode.getPath())) { + childrenExpected.remove(childNode.getPath()); + } else { + fail(); + } + } + } + + // ------------------------------------------------------------------------ + // GetParent + // ------------------------------------------------------------------------ + + public void testGetParent() { + TmfStatisticsTreeNode parent = (TmfStatisticsTreeNode) treeProvider.getParent(fStatsData.get(new TmfFixedArray(fTestName))); + + assertNotNull("getParent", parent); + assertTrue("getParent", parent.getPath().equals(AbsTmfStatisticsTree.ROOT)); + } + + // ------------------------------------------------------------------------ + // HasChildren + // ------------------------------------------------------------------------ + + public void testHasChildren() { + Boolean hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(AbsTmfStatisticsTree.ROOT)); + assertTrue("hasChildren", hasChildren); + + hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(new TmfFixedArray(fTestName))); + assertTrue("hasChildren", hasChildren); + + hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes))); + assertTrue("hasChildren", hasChildren); + + hasChildren = treeProvider.hasChildren(fStatsData.getOrCreate(new TmfFixedArray(fTestName, Messages.TmfStatisticsData_EventTypes, fEvent1.getType().toString()))); + assertFalse("hasChildren", hasChildren); + } + + // ------------------------------------------------------------------------ + // GetElements + // ------------------------------------------------------------------------ + + public void testGetElements() { + Object[] objectElements = treeProvider.getElements(fStatsData.get(AbsTmfStatisticsTree.ROOT)); + TmfStatisticsTreeNode[] nodeElements = Arrays.asList(objectElements).toArray(new TmfStatisticsTreeNode[0]); + assertEquals("getElements", 1, nodeElements.length); + assertTrue("getElements", nodeElements[0].getPath().equals(new TmfFixedArray(fTestName))); + } +} diff --git a/org.eclipse.linuxtools.tmf.ui/META-INF/MANIFEST.MF b/org.eclipse.linuxtools.tmf.ui/META-INF/MANIFEST.MF index cd440c46af..dff517021d 100644 --- a/org.eclipse.linuxtools.tmf.ui/META-INF/MANIFEST.MF +++ b/org.eclipse.linuxtools.tmf.ui/META-INF/MANIFEST.MF @@ -26,6 +26,8 @@ Export-Package: org.eclipse.linuxtools.tmf.ui, org.eclipse.linuxtools.tmf.ui.views.colors, org.eclipse.linuxtools.tmf.ui.views.filter, org.eclipse.linuxtools.tmf.ui.views.project, + org.eclipse.linuxtools.tmf.ui.views.statistics, + org.eclipse.linuxtools.tmf.ui.views.statistics.model, org.eclipse.linuxtools.tmf.ui.views.timechart, org.eclipse.linuxtools.tmf.ui.views.uml2sd, org.eclipse.linuxtools.tmf.ui.views.uml2sd.core, diff --git a/org.eclipse.linuxtools.tmf.ui/plugin.properties b/org.eclipse.linuxtools.tmf.ui/plugin.properties index 63efb0b69e..0eae8fa4af 100644 --- a/org.eclipse.linuxtools.tmf.ui/plugin.properties +++ b/org.eclipse.linuxtools.tmf.ui/plugin.properties @@ -9,6 +9,7 @@ events.view.name = Events filters.view.name = Filters colors.view.name = Colors timechart.view.name = Time Chart +statistics.view.name = Tmf Statistics events.editor.name = Events uml2sd.view.name = Sequence Diagram @@ -92,4 +93,4 @@ commands.uml2sd.showend = Show node end commands.uml2sd.showend.description = Show the node end commands.uml2sd.showstart = Show node start commands.uml2sd.showstart.description = Show the node start - \ No newline at end of file + diff --git a/org.eclipse.linuxtools.tmf.ui/plugin.xml b/org.eclipse.linuxtools.tmf.ui/plugin.xml index 8c23187a18..8d9ce8b2de 100644 --- a/org.eclipse.linuxtools.tmf.ui/plugin.xml +++ b/org.eclipse.linuxtools.tmf.ui/plugin.xml @@ -62,6 +62,15 @@ name="%uml2sd.view.name" restorable="true"> + + diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/ITmfExtraEventInfo.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/ITmfExtraEventInfo.java new file mode 100755 index 0000000000..68c30e0dc1 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/ITmfExtraEventInfo.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial design and implementation + * Bernd Hufmann - Changed interface and class name + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics; + +/** + * This class provides an extension for updating data model and to pass along more information beside events + */ +public interface ITmfExtraEventInfo { + public String getTraceName(); +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/Messages.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/Messages.java new file mode 100755 index 0000000000..7708fd63bc --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/Messages.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial API and Implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics; + +import org.eclipse.osgi.util.NLS; + +public class Messages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.linuxtools.tmf.ui.views.statistics.messages"; //$NON-NLS-1$ + public static String TmfStatisticsView_LevelColumn; + public static String TmfStatisticsView_LevelColumnTip; + public static String TmfStatisticsView_NbEventsColumn; + public static String TmfStatisticsView_NbEventsTip; + public static String TmfStatisticsView_UnknownTraceName; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + } +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/TmfStatisticsView.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/TmfStatisticsView.java new file mode 100755 index 0000000000..cb9cd8fda8 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/TmfStatisticsView.java @@ -0,0 +1,565 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Generalized version based on LTTng + * Bernd Hufmann - Updated to use trace reference in TmfEvent and streaming + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics; + +import java.util.Vector; + +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.jface.viewers.TreeViewerColumn; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerComparator; +import org.eclipse.linuxtools.tmf.event.TmfEvent; +import org.eclipse.linuxtools.tmf.event.TmfTimeRange; +import org.eclipse.linuxtools.tmf.experiment.TmfExperiment; +import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; +import org.eclipse.linuxtools.tmf.request.ITmfDataRequest.ExecutionType; +import org.eclipse.linuxtools.tmf.request.ITmfEventRequest; +import org.eclipse.linuxtools.tmf.request.TmfDataRequest; +import org.eclipse.linuxtools.tmf.request.TmfEventRequest; +import org.eclipse.linuxtools.tmf.signal.TmfExperimentDisposedSignal; +import org.eclipse.linuxtools.tmf.signal.TmfExperimentRangeUpdatedSignal; +import org.eclipse.linuxtools.tmf.signal.TmfExperimentSelectedSignal; +import org.eclipse.linuxtools.tmf.signal.TmfSignalHandler; +import org.eclipse.linuxtools.tmf.trace.ITmfTrace; +import org.eclipse.linuxtools.tmf.ui.views.TmfView; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.AbsTmfStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.ITmfColumnDataProvider; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseColumnData; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseColumnDataProvider; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseStatisticsTree; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeNode; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsTreeRootFactory; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfTreeContentProvider; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Cursor; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; + +/** + * TmfStatisticsView + *

+ * The generic Statistics View displays statistics for any kind of traces. + * + * It is implemented according to the MVC pattern. - The model is a TmfStatisticsTreeNode built by the State Manager. - The view is built with a + * TreeViewer. - The controller that keeps model and view synchronized is an observer of the model. + *

+ */ +public class TmfStatisticsView extends TmfView { + /** + * The ID correspond to the package in which this class is embedded + */ + public static final String ID = "org.eclipse.linuxtools.tmf.ui.views.statistics"; //$NON-NLS-1$ + + // view name + private static final String TMF_STATISTICS_VIEW = "StatisticsView"; //$NON-NLS-1$ + + // Refresh frequency + protected static final Long STATS_INPUT_CHANGED_REFRESH = 5000L; + + // Default PAGE_SIZE + protected static final int PAGE_SIZE = 50000; // For background request + + // The actual tree to display + protected TreeViewer fTreeViewer; + // Stores the request to the experiment + protected ITmfEventRequest fRequest = null; + + // Update synchronization parameters (used for streaming) + protected boolean fStatisticsUpdateBusy = false; + protected boolean fStatisticsUpdatePending = false; + protected TmfTimeRange fStatisticsUpdateRange = null; + protected final Object fStatisticsUpdateSyncObj = new Object(); + + // Object to store the cursor while waiting for the experiment to load + private Cursor fWaitCursor = null; + + // Stores the number of instance + private static int fCountInstance = 0; + + // Number of this instance. Used as an instance ID + private int fInstanceNb; + + /** + * Constructor of a statistics view. + * + * @param viewName + * The name to give to the view. + */ + public TmfStatisticsView(String viewName) { + super(viewName); + fCountInstance++; + fInstanceNb = fCountInstance; + } + + /** + * Default constructor. + */ + public TmfStatisticsView() { + this(TMF_STATISTICS_VIEW); + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) + */ + @Override + public void createPartControl(Composite parent) { + final Vector columnDataList = getColumnDataProvider().getColumnData(); + parent.setLayout(new FillLayout()); + + fTreeViewer = new TreeViewer(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); + fTreeViewer.setContentProvider(new TmfTreeContentProvider()); + fTreeViewer.getTree().setHeaderVisible(true); + fTreeViewer.setUseHashlookup(true); + + for (final TmfBaseColumnData columnData : columnDataList) { + final TreeViewerColumn treeColumn = new TreeViewerColumn(fTreeViewer, columnData.getAlignment()); + treeColumn.getColumn().setText(columnData.getHeader()); + treeColumn.getColumn().setWidth(columnData.getWidth()); + treeColumn.getColumn().setToolTipText(columnData.getTooltip()); + + if (columnData.getComparator() != null) { + treeColumn.getColumn().addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + if (fTreeViewer.getTree().getSortDirection() == SWT.UP || fTreeViewer.getTree().getSortColumn() != treeColumn.getColumn()) { + fTreeViewer.setComparator(columnData.getComparator()); + fTreeViewer.getTree().setSortDirection(SWT.DOWN); + } else { + fTreeViewer.setComparator(new ViewerComparator() { + @Override + public int compare(Viewer viewer, Object e1, Object e2) { + return -1 * columnData.getComparator().compare(viewer, e1, e2); + } + }); + fTreeViewer.getTree().setSortDirection(SWT.UP); + } + fTreeViewer.getTree().setSortColumn(treeColumn.getColumn()); + } + }); + } + treeColumn.setLabelProvider(columnData.getLabelProvider()); + } + + // Handler that will draw the bar charts. + fTreeViewer.getTree().addListener(SWT.EraseItem, new Listener() { + @Override + public void handleEvent(Event event) { + if (columnDataList.get(event.index).getPercentageProvider() != null) { + TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) event.item.getData(); + + double percentage = columnDataList.get(event.index).getPercentageProvider().getPercentage(node); + if (percentage == 0) { + return; + } + + if ((event.detail & SWT.SELECTED) > 0) { + Color oldForeground = event.gc.getForeground(); + event.gc.setForeground(event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION)); + event.gc.fillRectangle(event.x, event.y, event.width, event.height); + event.gc.setForeground(oldForeground); + event.detail &= ~SWT.SELECTED; + } + + int barWidth = (int) ((fTreeViewer.getTree().getColumn(1).getWidth() - 8) * percentage); + int oldAlpha = event.gc.getAlpha(); + Color oldForeground = event.gc.getForeground(); + Color oldBackground = event.gc.getBackground(); + event.gc.setAlpha(64); + event.gc.setForeground(event.item.getDisplay().getSystemColor(SWT.COLOR_BLUE)); + event.gc.setBackground(event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND)); + event.gc.fillGradientRectangle(event.x, event.y, barWidth, event.height, true); + event.gc.drawRectangle(event.x, event.y, barWidth, event.height); + event.gc.setForeground(oldForeground); + event.gc.setBackground(oldBackground); + event.gc.setAlpha(oldAlpha); + event.detail &= ~SWT.BACKGROUND; + } + } + }); + + fTreeViewer.setComparator(columnDataList.get(0).getComparator()); + fTreeViewer.getTree().setSortColumn(fTreeViewer.getTree().getColumn(0)); + fTreeViewer.getTree().setSortDirection(SWT.DOWN); + + // Read current data if any available + TmfExperiment experiment = TmfExperiment.getCurrentExperiment(); + if (experiment != null) { + // Insert the statistics data into the tree + @SuppressWarnings({ "rawtypes", "unchecked" }) + TmfExperimentSelectedSignal signal = new TmfExperimentSelectedSignal(this, experiment); + experimentSelected(signal); + } + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.TmfView#dispose() + */ + @Override + public void dispose() { + super.dispose(); + if (fWaitCursor != null) { + fWaitCursor.dispose(); + } + + // clean the model + TmfStatisticsTreeRootFactory.removeAll(); + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.part.WorkbenchPart#setFocus() + */ + @Override + public void setFocus() { + fTreeViewer.getTree().setFocus(); + } + + /** + * Refresh the view. + */ + public void modelInputChanged(boolean complete) { + // Ignore update if disposed + if (fTreeViewer.getTree().isDisposed()) + return; + + fTreeViewer.getTree().getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + if (!fTreeViewer.getTree().isDisposed()) + fTreeViewer.refresh(); + } + }); + + if (complete) { + sendPendingUpdate(); + } + } + + /** + * Called when an experiment request has failed or has been canceled Remove the data retrieved from the experiment from the statistics tree. + * + * @param name + * the experiment name + */ + public void modelIncomplete(String name) { + Object input = fTreeViewer.getInput(); + if (input != null && input instanceof TmfStatisticsTreeNode) { + // The data from this experiment is invalid and shall be removed to + // refresh upon next selection + TmfStatisticsTreeRootFactory.removeStatTreeRoot(getTreeID(name)); + + // Reset synchronization information + resetUpdateSynchronization(); + modelInputChanged(false); + } + waitCursor(false); + } + + /** + * If the user choose another experiment, the current must be disposed. + * + * @param signal + */ + @TmfSignalHandler + public void experimentDisposed(TmfExperimentDisposedSignal signal) { + cancelOngoingRequest(); + } + + /** + * Handler called when an experiment is selected. Checks if the experiment has changed and requests the selected experiment if it has not yet been + * cached. + * + * @param signal + * contains the information about the selection. + */ + @TmfSignalHandler + public void experimentSelected(TmfExperimentSelectedSignal signal) { + if (signal != null) { + TmfExperiment experiment = signal.getExperiment(); + String experimentName = experiment.getName(); + + if (TmfStatisticsTreeRootFactory.containsTreeRoot(getTreeID(experimentName))) { + // The experiment root is already present + TmfStatisticsTreeNode experimentTreeNode = TmfStatisticsTreeRootFactory.getStatTreeRoot(getTreeID(experimentName)); + + ITmfTrace[] traces = experiment.getTraces(); + + // check if there is partial data loaded in the experiment + int numTraces = experiment.getTraces().length; + int numNodeTraces = experimentTreeNode.getNbChildren(); + + if (numTraces == numNodeTraces) { + boolean same = true; + // Detect if the experiment contains the same traces as when + // previously selected + for (int i = 0; i < numTraces; i++) { + String traceName = traces[i].getName(); + if (!experimentTreeNode.containsChild(traceName)) { + same = false; + break; + } + } + + if (same) { + // no need to reload data, all traces are already loaded + fTreeViewer.setInput(experimentTreeNode); + + resetUpdateSynchronization(); + + return; + } + experimentTreeNode.reset(); + } + } else { + TmfStatisticsTreeRootFactory.addStatsTreeRoot(getTreeID(experimentName), getStatisticData()); + } + + resetUpdateSynchronization(); + + TmfStatisticsTreeNode treeModelRoot = TmfStatisticsTreeRootFactory.getStatTreeRoot(getTreeID(experiment.getName())); + + // if the model has contents, clear to start over + if (treeModelRoot.hasChildren()) { + treeModelRoot.reset(); + } + + // set input to a clean data model + fTreeViewer.setInput(treeModelRoot); + + // if the data is not available or has changed, reload it + requestData(experiment, experiment.getTimeRange()); + } + } + + /** + * @param signal + */ + @SuppressWarnings("unchecked") + @TmfSignalHandler + public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal) { + TmfExperiment experiment = (TmfExperiment) signal.getExperiment(); + // validate + if (! experiment.equals(TmfExperiment.getCurrentExperiment())) { + return; + } + + requestData(experiment, signal.getRange()); + } + + + /** + * Return the size of the request when performing background request. + * + * @return the block size for background request. + */ + protected int getIndexPageSize() { + return PAGE_SIZE; + } + + /** + * + * @return the quantity of data to retrieve before a refresh of the view is performed. + */ + protected long getInputChangedRefresh() { + return STATS_INPUT_CHANGED_REFRESH; + } + + /** + * This method can be overridden to implement another way to represent the statistics data and to retrieve the information for display. + * + * @return a TmfStatisticsData object. + */ + protected AbsTmfStatisticsTree getStatisticData() { + return new TmfBaseStatisticsTree(); + } + + /** + * This method can be overridden to change the representation of the data in the columns. + * + * @return an object implementing ITmfBaseColumnDataProvider. + */ + protected ITmfColumnDataProvider getColumnDataProvider() { + return new TmfBaseColumnDataProvider(); + } + + /** + * Construct the ID based on the experiment name + * @param experimentName the name of the trace name to show in the view + * @return a view ID + */ + protected String getTreeID(String experimentName) { + return experimentName + fInstanceNb; + } + + /** + * When the experiment is loading the cursor will be different so the user know the processing is not finished yet. + * + * @param waitInd + * indicates if we need to show the waiting cursor, or the default one + */ + protected void waitCursor(final boolean waitInd) { + if ((fTreeViewer == null) || (fTreeViewer.getTree().isDisposed())) { + return; + } + + Display display = fTreeViewer.getControl().getDisplay(); + if (fWaitCursor == null) { + fWaitCursor = new Cursor(display, SWT.CURSOR_WAIT); + } + + // Perform the updates on the UI thread + display.asyncExec(new Runnable() { + @Override + public void run() { + if ((fTreeViewer != null) && (!fTreeViewer.getTree().isDisposed())) { + Cursor cursor = null; /* indicates default */ + if (waitInd) { + cursor = fWaitCursor; + } + fTreeViewer.getControl().setCursor(cursor); + } + } + }); + } + + /** + * Perform the request for an experiment and populates the statistics tree with event. + * @param experiment experiment for which we need the statistics data. + * @param time range to request + */ + @SuppressWarnings("unchecked") + protected void requestData(final TmfExperiment experiment, TmfTimeRange timeRange) { + if (experiment != null) { + + // Check if update is already ongoing + if (checkUpdateBusy(timeRange)) { + return; + } + + int index = 0; + for (TmfStatisticsTreeNode node : ((TmfStatisticsTreeNode) fTreeViewer.getInput()).getChildren()) { + index += (int) node.getValue().nbEvents; + } + + // Preparation of the event request + fRequest = new TmfEventRequest(TmfEvent.class, timeRange, index, TmfDataRequest.ALL_DATA, getIndexPageSize(), ExecutionType.BACKGROUND) { + + @Override + public void handleData(TmfEvent data) { + super.handleData(data); + if (data != null) { + AbsTmfStatisticsTree statisticsData = TmfStatisticsTreeRootFactory.getStatTree(getTreeID(experiment.getName())); + + final String traceName = data.getParentTrace().getName(); + ITmfExtraEventInfo extraInfo = new ITmfExtraEventInfo() { + @Override + public String getTraceName() { + if (traceName == null) { + return Messages.TmfStatisticsView_UnknownTraceName; + } + return traceName; + } + }; + statisticsData.registerEvent(data, extraInfo); + statisticsData.increase(data, extraInfo, 1); + // Refresh View + if ((getNbRead() % getInputChangedRefresh()) == 0) { + modelInputChanged(false); + } + } + } + + @Override + public void handleSuccess() { + super.handleSuccess(); + modelInputChanged(true); + waitCursor(false); + } + + @Override + public void handleFailure() { + super.handleFailure(); + modelIncomplete(experiment.getName()); + } + + @Override + public void handleCancel() { + super.handleCancel(); + modelIncomplete(experiment.getName()); + } + }; + ((TmfExperiment) experiment).sendRequest((ITmfDataRequest) fRequest); + waitCursor(true); + } + } + + /** + * Cancels the current ongoing request + */ + protected void cancelOngoingRequest() { + if (fRequest != null && !fRequest.isCompleted()) { + fRequest.cancel(); + } + } + + /** + * Reset update synchronization information + */ + protected void resetUpdateSynchronization() { + synchronized (fStatisticsUpdateSyncObj) { + fStatisticsUpdateBusy = false; + fStatisticsUpdatePending = false; + } + } + + /** + * Checks if statistic update is ongoing. If it is ongoing the new time range is stored as pending + * + * @param timeRange - new time range + * @return true if statistic update is ongoing else false + */ + protected boolean checkUpdateBusy(TmfTimeRange timeRange) { + synchronized (fStatisticsUpdateSyncObj) { + if (fStatisticsUpdateBusy) { + fStatisticsUpdatePending = true; + fStatisticsUpdateRange = timeRange; + return true; + } + fStatisticsUpdateBusy = true; + return false; + } + } + + /** + * Sends pending request (if any) + */ + protected void sendPendingUpdate() { + synchronized (fStatisticsUpdateSyncObj) { + fStatisticsUpdateBusy = false; + if (fStatisticsUpdatePending) { + fStatisticsUpdatePending = false; + requestData(TmfExperiment.getCurrentExperiment(), fStatisticsUpdateRange); + } + } + } + +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/messages.properties b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/messages.properties new file mode 100755 index 0000000000..29e0b710a7 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/messages.properties @@ -0,0 +1,5 @@ +TmfStatisticsView_LevelColumn=Level +TmfStatisticsView_LevelColumnTip=Level at which statistics apply. +TmfStatisticsView_NbEventsColumn=Number of Events +TmfStatisticsView_NbEventsTip=Total amount of events that are tied to given resource. +TmfStatisticsView_UnknownTraceName=Unknown_Trace diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/AbsTmfStatisticsTree.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/AbsTmfStatisticsTree.java new file mode 100755 index 0000000000..fb440da536 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/AbsTmfStatisticsTree.java @@ -0,0 +1,204 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Implementation and Initial API + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.eclipse.linuxtools.tmf.event.TmfEvent; +import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo; +import org.eclipse.linuxtools.tmf.util.TmfFixedArray; + +/** + *

Base class for the statistics storage.

+ *

+ * It allow to implement a tree structure while avoiding the need to run through + * the tree each time you need to add a node at a given place. + *

+ */ +public abstract class AbsTmfStatisticsTree { + + /** + *

String builder used to merge string with more efficacy.

+ */ + protected static StringBuilder fBuilder = new StringBuilder(); + /** + *

Identification of the root.

+ */ + public static final TmfFixedArray ROOT = new TmfFixedArray("root"); //$NON-NLS-1$ + + /** + *

Function to merge many string with more efficacy.

+ * + * @param strings + * Strings to merge. + * @return A new string containing all the strings. + */ + protected synchronized static String mergeString(String... strings) { + fBuilder.setLength(0); + for (String s : strings) + fBuilder.append(s); + return fBuilder.toString(); + } + + /** + *

Define what child a node can have.

+ *

+ * The management and usage of this map is done by subclass. + *

+ *

+ * HashSet are always faster than TreeSet. + *

+ */ + private Map> fKeys; + /** + *

The nodes in the tree. + */ + private HashMap, TmfStatisticsTreeNode> fNodes; + + /** + *

Constructor.

+ */ + public AbsTmfStatisticsTree() { + fNodes = new HashMap, TmfStatisticsTreeNode>(); + fKeys = new HashMap>(); + } + + /** + *

Get a node.

+ * + * @param path + * Path to the node. + * @return The node or null. + */ + public TmfStatisticsTreeNode get(final TmfFixedArray path) { + return fNodes.get(path); + } + + /** + *

Get the children of a node.

+ * + * @param path + * Path to the node. + * @return Collection containing the children. + */ + public abstract Collection getChildren(final TmfFixedArray path); + + /** + *

Get every children of a node, even if it doesn't have any registered events, as opposed to getChildren

+ * + * @param path + * Path to the node. + * @return Collection containing all the children. + */ + public abstract Collection getAllChildren(final TmfFixedArray path); + + /** + *

Get the map of existing elements of path classified by parent.

+ * + * @return The map. + */ + protected Map> getKeys() { + return fKeys; + } + + /** + *

Get or create a node.

+ * + * @param path + * Path to the node. + * @return The node. + */ + public TmfStatisticsTreeNode getOrCreate(final TmfFixedArray path) { + TmfStatisticsTreeNode current = fNodes.get(path); + if (current == null) { + registerName(path); + current = new TmfStatisticsTreeNode(path, this); + fNodes.put(path, current); + } + return current; + } + + /** + *

Get the parent of a node.

+ * + * @param path + * Path to the node. + * @return Parent node or null. + */ + public TmfStatisticsTreeNode getParent(final TmfFixedArray path) { + if (path.size() == 1) { + if (path.equals(ROOT)) + return null; + else + return get(ROOT); + } + return get(path.subArray(0, path.size() - 1)); + } + + /** + *

Increase any kind of counter.

+ *

+ * This method must be implemented by subclass. + *

+ * @param event + * Current event. + * @param extraInfo + * Extra information to pass along with the event. + * @param values + * Values desired. + */ + public abstract void increase(TmfEvent event, ITmfExtraEventInfo extraInfo, int values); + + /** + *

Register an event.

+ *

+ * This method must be implemented by subclass. + *

+ * @param event + * Current event. + * @param extraInfo + * Extra information to pass along with the event. + */ + public abstract void registerEvent(TmfEvent event, ITmfExtraEventInfo extraInfo); + + /** + *

Register that a new node was created.

+ *

+ * Must make sure the {@link #getChildren(TmfFixedArray)} on the parent node + * will return the newly created node. + *

+ * + * @param path + * Path of the new node. + */ + protected abstract void registerName(final TmfFixedArray path); + + /** + *

Reset a node.

+ *

+ * Work recursively. + *

+ * + * @param path + * Path to the node. + */ + public void reset(final TmfFixedArray path) { + for (TmfStatisticsTreeNode node : getAllChildren(path)) { + reset(node.getPath()); + fNodes.remove(node.getPath()); + } + } +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/ITmfColumnDataProvider.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/ITmfColumnDataProvider.java new file mode 100755 index 0000000000..80066d1bba --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/ITmfColumnDataProvider.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial API and Implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import java.util.Vector; + +/** + * ITmfColumnDataProvider + *

+ * Basic methods that must be implemented in a column data provider. + *

+ */ +public interface ITmfColumnDataProvider { + /** + * Return a list of the column created for the view + * @return columns list + */ + public Vector getColumnData(); +} \ No newline at end of file diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/ITmfStatisticsColumnData.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/ITmfStatisticsColumnData.java new file mode 100644 index 0000000000..22b3f0917c --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/ITmfStatisticsColumnData.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial API and Implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import org.eclipse.jface.viewers.ColumnLabelProvider; +import org.eclipse.jface.viewers.ViewerComparator; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseColumnData.ITmfColumnPercentageProvider; + +/** + * Provide the basic interface to create a statistics column + */ +public interface ITmfStatisticsColumnData { + + /** + * Return the column name. + * @return the name of the column. + */ + public String getHeader(); + + /** + * Return the width of the column at the creation. + * @return the width of the column. + */ + public int getWidth(); + + /** + * Return the alignment of the column. + * @see org.eclipse.swt.SWT + * @return an integer representing the alignment inside the column. + */ + public int getAlignment(); + + /** + * Provide the text to show in the tooltip when the cursor comes over the column header. + * @return text to show in the tooltip + */ + public String getTooltip(); + + /** + * Return the labelProvider which provides the information to put in column cells. + * @return a ColumnLabelProvider. + */ + public ColumnLabelProvider getLabelProvider(); + + /** + * Return a ViewerComparator used to sort viewer's contents. + * @return the comparator. + */ + public ViewerComparator getComparator(); + + /** + * Return the provider of the percentage. + * Used to draw bar charts in columns. + * @return the percentageProvider. + */ + public ITmfColumnPercentageProvider getPercentageProvider(); +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/Messages.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/Messages.java new file mode 100755 index 0000000000..43d1713b05 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/Messages.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial API and Implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import org.eclipse.osgi.util.NLS; + +public class Messages extends NLS { + private static final String BUNDLE_NAME = "org.eclipse.linuxtools.tmf.ui.views.statistics.model.messages"; //$NON-NLS-1$ + public static String TmfStatisticsData_CPUs; + public static String TmfStatisticsData_EventTypes; + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, Messages.class); + } + + private Messages() { + } +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseColumnData.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseColumnData.java new file mode 100755 index 0000000000..afcf46a8c1 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseColumnData.java @@ -0,0 +1,127 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial Implementation + * Bernd Hufmann - Added Annotations + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import org.eclipse.jface.viewers.ColumnLabelProvider; +import org.eclipse.jface.viewers.ViewerComparator; + +/** + * Contains all the information necessary to build a column of the table. + */ +public class TmfBaseColumnData implements ITmfStatisticsColumnData { + + // Name of the column. + private final String fHeader; + // Width of the column. + private final int fWidth; + // Alignment of the column. + private final int fAlignment; + // Tooltip of the column. + private final String fTooltip; + // Adapts a StatisticsTreeNode into the content of it's corresponding + // cell for that column. + private final ColumnLabelProvider fLabelProvider; + // Used to sort elements of this column. Can be null. + private final ViewerComparator fComparator; + // Used to draw bar charts in this column. Can be null. + private final ITmfColumnPercentageProvider fPercentageProvider; + + // Used to draw bar charts in columns. + public interface ITmfColumnPercentageProvider { + public double getPercentage(TmfStatisticsTreeNode node); + } + + /** + * Constructor with parameters + * @param h header of the column. The name will be shown at the top of the column. + * @param w width of the column. + * @param a alignment of the text + * @param t text to shown as a tooltip when the cursor comes over the header + * @param l provide all the column element + * @param c used to compare element between them to be able to classify the content of the columns + * @param p provide the percentage of a specific element + */ + public TmfBaseColumnData(String h, int w, int a, String t, ColumnLabelProvider l, ViewerComparator c, ITmfColumnPercentageProvider p) { + fHeader = h; + fWidth = w; + fAlignment = a; + fTooltip = t; + fLabelProvider = l; + fComparator = c; + fPercentageProvider = p; + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.ITmfStatisticsColumnData#getHeader() + */ + @Override + public String getHeader() { + return fHeader; + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.ITmfStatisticsColumnData#getWidth() + */ + @Override + public int getWidth() { + return fWidth; + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.ITmfStatisticsColumnData#getAlignment() + */ + @Override + public int getAlignment() { + return fAlignment; + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.ITmfStatisticsColumnData#getTooltip() + */ + @Override + public String getTooltip() { + return fTooltip; + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.ITmfStatisticsColumnData#getLabelProvider() + */ + @Override + public ColumnLabelProvider getLabelProvider() { + return fLabelProvider; + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.ITmfStatisticsColumnData#getComparator() + */ + @Override + public ViewerComparator getComparator() { + return fComparator; + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.ITmfStatisticsColumnData#getPercentageProvider() + */ + @Override + public ITmfColumnPercentageProvider getPercentageProvider() { + return fPercentageProvider; + } +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseColumnDataProvider.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseColumnDataProvider.java new file mode 100755 index 0000000000..0f690aa009 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseColumnDataProvider.java @@ -0,0 +1,138 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Implementation and Initial API + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.Vector; + +import org.eclipse.jface.viewers.ColumnLabelProvider; +import org.eclipse.jface.viewers.Viewer; +import org.eclipse.jface.viewers.ViewerComparator; +import org.eclipse.linuxtools.tmf.ui.views.statistics.Messages; +import org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfBaseColumnData.ITmfColumnPercentageProvider; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; + +/** + * Create a basic list of columns with providers + */ +public class TmfBaseColumnDataProvider implements ITmfColumnDataProvider { + + /** + * Contains the list of the columns + */ + private static Vector fColumnData = null; + + /** + * Level column names + */ + private final static String LEVEL_COLUMN = Messages.TmfStatisticsView_LevelColumn; + /** + * Number of events column names + */ + private final static String EVENTS_COUNT_COLUMN = Messages.TmfStatisticsView_NbEventsColumn; + + /** + * Level column tooltips + */ + private final static String LEVEL_COLUMN_TIP = Messages.TmfStatisticsView_LevelColumnTip; + /** + * Number of events column tooltips + */ + private final static String EVENTS_COUNT_COLUMN_TIP = Messages.TmfStatisticsView_NbEventsTip; + + /** + * Level for which statistics should not be displayed. + */ + private static Set fFolderLevels = new HashSet(Arrays.asList(new String[] { "Event Types" })); //$NON-NLS-1$ + + /** + * Create basic columns to represent the statistics data + */ + public TmfBaseColumnDataProvider() { + // List that will be used to create the table. + fColumnData = new Vector(); + fColumnData.add(new TmfBaseColumnData(LEVEL_COLUMN, 200, SWT.LEFT, LEVEL_COLUMN_TIP, new ColumnLabelProvider() { + @Override + public String getText(Object element) { + return ((TmfStatisticsTreeNode) element).getKey(); + } + + @Override + public Image getImage(Object element) { + TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element; + if (fFolderLevels.contains(node.getKey())) { + return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER); + } else { + return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT); + } + } + }, new ViewerComparator() { + @Override + public int compare(Viewer viewer, Object e1, Object e2) { + TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1; + TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2; + + return n1.getKey().compareTo(n2.getKey()); + } + }, null)); + + fColumnData.add(new TmfBaseColumnData(EVENTS_COUNT_COLUMN, 125, SWT.LEFT, EVENTS_COUNT_COLUMN_TIP, new ColumnLabelProvider() { + @Override + public String getText(Object element) { + TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) element; + if (!fFolderLevels.contains(node.getKey())) { + return Long.toString(node.getValue().nbEvents); + } else { + return ""; //$NON-NLS-1$ + } + } + }, new ViewerComparator() { + @Override + public int compare(Viewer viewer, Object e1, Object e2) { + TmfStatisticsTreeNode n1 = (TmfStatisticsTreeNode) e1; + TmfStatisticsTreeNode n2 = (TmfStatisticsTreeNode) e2; + + return (int) (n1.getValue().nbEvents - n2.getValue().nbEvents); + } + }, new ITmfColumnPercentageProvider() { + + @Override + public double getPercentage(TmfStatisticsTreeNode node) { + TmfStatisticsTreeNode parent = node; + do { + parent = parent.getParent(); + } while (parent != null && parent.getValue().nbEvents == 0); + + if (parent == null) { + return 0; + } else { + return (double) node.getValue().nbEvents / parent.getValue().nbEvents; + } + } + })); + } + + /** + * Provide the columns to represent statistics data + */ + @Override + public Vector getColumnData() { + return fColumnData; + } + +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseStatisticsTree.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseStatisticsTree.java new file mode 100755 index 0000000000..156c0622eb --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfBaseStatisticsTree.java @@ -0,0 +1,209 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial API and Implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Set; + +import org.eclipse.linuxtools.tmf.event.TmfEvent; +import org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfExtraEventInfo; +import org.eclipse.linuxtools.tmf.util.TmfFixedArray; + +/** + *

Store information about base statistics data

+ *

This class provides a way to represent statistics data that is compatible to every kind of traces

+ */ +public class TmfBaseStatisticsTree extends AbsTmfStatisticsTree { + + /** + *

Header for the event types categories.

+ */ + private static final String HEADER_EVENT_TYPES = Messages.TmfStatisticsData_EventTypes; + + /** + *

Indicate that it's a value.

+ *

+ * Used when checking the possible child node for a node. + *

+ * * + *

+ * It differentiate a category of a value by being appended to a value. + *

+ */ + private static final String NODE = "z"; //$NON-NLS-1$ + private static final String ROOT_NODE_KEY = mergeString(ROOT.get(0), NODE); + + public TmfBaseStatisticsTree() { + super(); + Map> keys = getKeys(); + + // //////////// Adding category sets + // common + keys.put(HEADER_EVENT_TYPES, new HashSet()); + + // /////////// Adding value sets + // Under a trace + Set temp = new HashSet(8); + temp.add(HEADER_EVENT_TYPES); + keys.put(ROOT_NODE_KEY, temp); + // Under an event type + temp = new HashSet(16); + keys.put(mergeString(HEADER_EVENT_TYPES, NODE), temp); + + // //////////// CREATE root + keys.put(ROOT.get(0), new HashSet(2)); // 1 trace at the time + getOrCreate(ROOT); + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsData#getChildren(org.eclipse.linuxtools.tmf.util.TmfFixedArray) + */ + @Override + public Collection getChildren(TmfFixedArray path) { + LinkedList result = new LinkedList(); + + if (path.size() % 2 == 0) { // if we are at a Category + TmfStatisticsTreeNode current = null; + for (String value : getKeys().get(path.get(path.size() - 1))) { + current = get(path.append(value)); + if (current != null && current.getValue().nbEvents != 0) + result.add(current); + } + } else if (path.size() == 1) { // Special case. + if (path.equals(ROOT)) // Asking for the root. + for (String value : getKeys().get(ROOT.get(0))) + result.add(getOrCreate(new TmfFixedArray(value))); + else + // Get value under the root + for (String value : getKeys().get(ROOT_NODE_KEY)) + result.add(getOrCreate(path.append(value))); + } else {// If we are at a value + for (String value : getKeys().get(mergeString(path.get(path.size() - 2), NODE))) + // Search the parent name + NODE + result.add(getOrCreate(path.append(value))); + } + + return result; + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsData#getAllChildren(org.eclipse.linuxtools.tmf.util.TmfFixedArray) + */ + @Override + public Collection getAllChildren(TmfFixedArray path) { + LinkedList result = new LinkedList(); + + if (path.size() % 2 == 0) { // if we are at a Category + TmfStatisticsTreeNode current = null; + for (String value : getKeys().get(path.get(path.size() - 1))) { + current = get(path.append(value)); + if (current != null) + result.add(current); + } + } else if (path.size() == 1) { // Special case. + if (path.equals(ROOT)) // Asking for the root. + for (String value : getKeys().get(ROOT.get(0))) + result.add(getOrCreate(new TmfFixedArray(value))); + else + // Get value under the root + for (String value : getKeys().get(ROOT_NODE_KEY)) + result.add(getOrCreate(path.append(value))); + } else {// If we are at a value + for (String value : getKeys().get(mergeString(path.get(path.size() - 2), NODE))) + // Search the parent name + NODE + result.add(getOrCreate(path.append(value))); + } + return result; + } + + /** + *

Get the event types paths.

+ * + * @param event + * Event to get the path for. + * @param extraInfo + * Extra information to pass along with the event + * @return Array of FixedArray representing the paths. + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + private TmfFixedArray[] getTypePaths(TmfEvent event, ITmfExtraEventInfo extraInfo) { + String trace = extraInfo.getTraceName(); + // String type = event.getType().getTypeId(); // Add too much + // informations + String type = event.getType().toString(); + + TmfFixedArray[] paths = { new TmfFixedArray(trace, HEADER_EVENT_TYPES, type) }; + + return paths; + } + + /** + *

Get the standard paths for an event.

+ * + * @param event + * Event to get the path for. + * @param extraInfo + * Extra information to pass along with the event + * @return Array of FixedArray representing the paths. + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + private TmfFixedArray[] getNormalPaths(TmfEvent event, ITmfExtraEventInfo extraInfo) { + String trace = extraInfo.getTraceName(); + + TmfFixedArray[] paths = { new TmfFixedArray(trace) }; + return paths; + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsData#increase(org.eclipse.linuxtools.tmf.event.TmfEvent, org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfEventInfo, int) + */ + @Override + public void increase(TmfEvent event, ITmfExtraEventInfo extraInfo, int values) { + // Do nothing + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsData#registerEvent(org.eclipse.linuxtools.tmf.event.TmfEvent, org.eclipse.linuxtools.tmf.ui.views.statistics.ITmfEventInfo) + */ + @Override + public void registerEvent(TmfEvent event, ITmfExtraEventInfo extraInfo) { + TmfFixedArray[] paths = getNormalPaths(event, extraInfo); + for (TmfFixedArray path : paths) + ++(getOrCreate(path).getValue().nbEvents); + + paths = getTypePaths(event, extraInfo); + for (TmfFixedArray path : paths) + ++(getOrCreate(path).getValue().nbEvents); + } + + /* + * (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfStatisticsData#registerName + * (org.eclipse.linuxtools.tmf.ui.views.statistics.model.TmfFixedArray) + */ + @Override + protected void registerName(TmfFixedArray path) { + if (path.size() == 1) { + if (!path.equals(ROOT)) + getKeys().get(ROOT.get(0)).add(path.get(0)); + } else if (path.size() % 2 != 0) + getKeys().get(path.get(path.size() - 2)).add(path.get(path.size() - 1)); + } +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatistics.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatistics.java new file mode 100755 index 0000000000..8d6ec5c10b --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatistics.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Intial API and Implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +/** + *

Primitive container for Statistics data

+ *

Contains information about statistics that can be retrieved with any kind of traces

+ */ +public class TmfStatistics { + /** + *

Number of event

+ */ + public long nbEvents = 0; +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatisticsTreeNode.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatisticsTreeNode.java new file mode 100755 index 0000000000..f3719ab747 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatisticsTreeNode.java @@ -0,0 +1,122 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Yann N. Dauphin (dhaemon@gmail.com) - Implementation for stats + * Francois Godin (copelnug@gmail.com) - Re-design for new stats structure + * Mathieu Denis (mathieu.denis@polymtl.ca) - Re-design for new stats structure (2) + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import java.util.Collection; + +import org.eclipse.linuxtools.tmf.util.TmfFixedArray; + +/** + *

A tree where nodes can be accessed efficiently using paths.

+ * + *

It works like file systems. Each node is identified by a key. A path is an array ({@link TmfFixedArray}) of String. The elements of the array represent the path from the root to this node.

+ */ +public class TmfStatisticsTreeNode { + /** + *

Value of the node.

+ */ + private TmfStatistics fValue; + /** + *

Path of the node.

+ */ + private TmfFixedArray fPath; + /** + *

Corresponding StatisticsData.

+ */ + private AbsTmfStatisticsTree fNodes; + /** + *

Constructor.

+ * @param path Path to the node. + * @param nodes Corresponding StatisticsData. + */ + public TmfStatisticsTreeNode(final TmfFixedArray path, AbsTmfStatisticsTree nodes) { + fPath = path; + fNodes = nodes; + fValue = new TmfStatistics(); + } + /** + *

Test if a node contain the specified child.

+ * @param key Name of the child. + * @return true: if child with given key is present, false: if no child exists with given key name + */ + public boolean containsChild(String key) { + if(AbsTmfStatisticsTree.ROOT.equals(fPath)) + return fNodes.get(new TmfFixedArray(key)) != null; + return (fNodes.get(fPath.append(key)) != null); + } + /** + *

Get the children of this node.

+ * @return Direct children of this node. + */ + public Collection getChildren() { + return fNodes.getChildren(fPath); + } + /** + *

Get the children of this node.

+ * @return Direct children of this node. + */ + public Collection getAllChildren() { + return fNodes.getAllChildren(fPath); + } + /** + *

Get the key for this node.

+ * @return Key associated with this node. + */ + public String getKey() { + return fPath.get(fPath.size() - 1); + } + /** + *

Get the number of children this node have.

+ * @return Number of direct children of this node. + */ + public int getNbChildren() { + return fNodes.getChildren(fPath).size(); + } + /** + *

Return the parent node.

+ * @return Parent node. + */ + public TmfStatisticsTreeNode getParent() { + return fNodes.getParent(fPath); + } + /** + *

Get the path of the node.

+ * @return The path of the node. + */ + public TmfFixedArray getPath() { + return fPath; + } + /** + *

Get the value of this node.

+ * @return Value associated with this node. + */ + public TmfStatistics getValue() { + return fValue; + } + /** + *

Indicate if the node have children.

+ * @return True if the node has children. + */ + public boolean hasChildren() { + return !fNodes.getChildren(fPath).isEmpty(); + } + /** + *

Start from creation time i.e. keep key and parent but new statistics and no children.

+ */ + public void reset() { + fValue = new TmfStatistics(); + fNodes.reset(fPath); + } +} \ No newline at end of file diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatisticsTreeRootFactory.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatisticsTreeRootFactory.java new file mode 100755 index 0000000000..86794382ec --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfStatisticsTreeRootFactory.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial API + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import java.util.HashMap; +import java.util.Map; + +public class TmfStatisticsTreeRootFactory { + + // ----------------------------------------------------------------------- + // Data + // ----------------------------------------------------------------------- + /** + * Contains the experiment name as the key and the traces data + */ + private static final Map fTreeInstances = new HashMap(); + + // ----------------------------------------------------------------------- + // Methods + // ----------------------------------------------------------------------- + + /** + * Provide a statisticsTree instance per trace + * + * @return + */ + public static TmfStatisticsTreeNode getStatTreeRoot(String traceUniqueId) { + + AbsTmfStatisticsTree tree = getStatTree(traceUniqueId); + if (tree == null) { + return null; + } + return tree.getOrCreate(AbsTmfStatisticsTree.ROOT); + } + + /** + * + * @param traceUniqueId + * @return + */ + public static AbsTmfStatisticsTree getStatTree(String traceUniqueId) { + if (traceUniqueId == null) + return null; + + AbsTmfStatisticsTree tree = fTreeInstances.get(traceUniqueId); + return tree; + } + + /** + * Add the new trace statistics data in the tree. Can be used later on if the same traces is selected back. + * + * @param experimentName + * the name of the trace which will be used as a key to store the data. Must be different for each traces, otherwise the traces might + * be overwritten which would trigger a reload of the same trace. + * @param data + * the information about the trace + */ + public static void addStatsTreeRoot(String traceUniqueId, AbsTmfStatisticsTree statsData) { + if (traceUniqueId == null || statsData == null) + return; + + fTreeInstances.put(traceUniqueId, statsData); + // if called for the first time, create the root node + statsData.getOrCreate(AbsTmfStatisticsTree.ROOT); + } + + /** + * + * @param traceUniqueId + * @return + */ + public static boolean containsTreeRoot(String traceUniqueId) { + return fTreeInstances.containsKey(traceUniqueId); + } + + /** + * Remove previously registered statistics tree. + * + * @param traceUniqueId + */ + public static void removeStatTreeRoot(String traceUniqueId) { + if (traceUniqueId != null && fTreeInstances.containsKey(traceUniqueId)) { + fTreeInstances.remove(traceUniqueId); + } + } + + /** + * Remove all tree and root instances + */ + public static void removeAll() { + fTreeInstances.clear(); + } +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfTreeContentProvider.java b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfTreeContentProvider.java new file mode 100755 index 0000000000..39d36d0591 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/TmfTreeContentProvider.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Mathieu Denis (mathieu.denis@polymtl.ca) - Initial API + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.ui.views.statistics.model; + +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.Viewer; + +/** + * Adapter TreeViewers can use to interact with StatisticsTreeNode objects. + * + * @see org.eclipse.jface.viewers.ITreeContentProvider + */ +public class TmfTreeContentProvider implements ITreeContentProvider { + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) + */ + @Override + public Object[] getChildren(Object parentElement) { + return ((TmfStatisticsTreeNode) parentElement).getChildren().toArray(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) + */ + @Override + public Object getParent(Object element) { + return ((TmfStatisticsTreeNode) element).getParent(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) + */ + @Override + public boolean hasChildren(Object element) { + return ((TmfStatisticsTreeNode) element).hasChildren(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) + */ + @Override + public Object[] getElements(Object inputElement) { + return getChildren(inputElement); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.IContentProvider#dispose() + */ + @Override + public void dispose() { + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) + */ + @Override + public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { + } +} diff --git a/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/messages.properties b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/messages.properties new file mode 100755 index 0000000000..442d278a76 --- /dev/null +++ b/org.eclipse.linuxtools.tmf.ui/src/org/eclipse/linuxtools/tmf/ui/views/statistics/model/messages.properties @@ -0,0 +1,2 @@ +TmfStatisticsData_CPUs=CPUs +TmfStatisticsData_EventTypes=Event Types \ No newline at end of file diff --git a/org.eclipse.linuxtools.tmf/META-INF/MANIFEST.MF b/org.eclipse.linuxtools.tmf/META-INF/MANIFEST.MF index 90751fcf2a..eee744a326 100644 --- a/org.eclipse.linuxtools.tmf/META-INF/MANIFEST.MF +++ b/org.eclipse.linuxtools.tmf/META-INF/MANIFEST.MF @@ -21,5 +21,6 @@ Export-Package: org.eclipse.linuxtools.tmf, org.eclipse.linuxtools.tmf.request, org.eclipse.linuxtools.tmf.signal, org.eclipse.linuxtools.tmf.trace, - org.eclipse.linuxtools.tmf.uml2sd + org.eclipse.linuxtools.tmf.uml2sd, + org.eclipse.linuxtools.tmf.util Bundle-Localization: plugin diff --git a/org.eclipse.linuxtools.tmf/src/org/eclipse/linuxtools/tmf/util/TmfFixedArray.java b/org.eclipse.linuxtools.tmf/src/org/eclipse/linuxtools/tmf/util/TmfFixedArray.java new file mode 100755 index 0000000000..af26d3e35b --- /dev/null +++ b/org.eclipse.linuxtools.tmf/src/org/eclipse/linuxtools/tmf/util/TmfFixedArray.java @@ -0,0 +1,250 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Francois Godin (copelnug@gmail.com) - Initial design and implementation + * Mathieu Denis (mathieu.denis@polymtl.ca) - Correction and refactoring + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.util; + +import java.lang.reflect.Array; +import java.util.AbstractList; +import java.util.Arrays; +import java.util.List; +import java.util.RandomAccess; + +/** + *

Allow to create a List object that contain an already existing array.

+ *

Works like {@link java.util.Arrays#asList} but offers more functions : + *

    + *
  • {@link #hashCode()}
  • + *
  • {@link #equals(Object)}
  • + *

+ *

Those functions allow to use the FixedArray as the key of a {@link java.util.HashMap}.

+ * + * @param Type of the array content. + */ +public final class TmfFixedArray extends AbstractList implements RandomAccess { + /** + * Replace {@link java.util.Arrays#copyOf(Object[], int)} that do not exist in java 5. + * @param Content of the array. + * @param array Original array to copy from. + * @param newLength Length of the copy to be returned. + * @return A new array consisting of the elements specified. + */ + @SuppressWarnings("unchecked") + private static E[] copyOf(final E[] array, int newLength) { + E[] result = (E[])Array.newInstance(array.getClass().getComponentType(), newLength); // Is it useful to use newInstance? + System.arraycopy(array, 0, result, 0, Math.min(array.length, newLength)); + return result; + } + /** + * Replace {@link java.util.Arrays#copyOf(Object[], int, Class)} that do not exist in java 5. + * @param Content of the array. + * @param array Original array to copy from. + * @param newLength Length of the copy to be returned. + * @param newType Type of the array to be returned. + * @return A new array consisting of the elements specified. + */ + @SuppressWarnings("unchecked") + private static E[] copyOf(final U[] array, int newLength, Class newType) { + E[] result = (E[])Array.newInstance(newType.getComponentType(), newLength); + System.arraycopy(array, 0, result, 0, Math.min(array.length, newLength)); + return result; + } + /** + * Replace {@link java.util.Arrays#copyOfRange(Object[], int, int)} that do not exist in java 5. + * @param Content of the array. + * @param array Original array to copy from. + * @param start Starting position of the range, inclusive. + * @param end Ending position of the range, exclusive. + * @return A new array consisting of the elements specified. The length of the new array is equal to end-start + */ + @SuppressWarnings("unchecked") + private static E[] copyOfRange(final E[] array, int start, int end) { + E[] result = (E[])Array.newInstance(array.getClass().getComponentType(), end - start); + System.arraycopy(array, start, result, 0, end - start); + return result; + } + /** + * The array. + */ + private final T[] fArray; + /** + * Constructor. + * @param array Array to use. WILL NOT BE COPIED. + */ + public TmfFixedArray(final T... array) { + fArray = array; + } + /** + * Append a FixedArray to this FixedArray. + * @param value The FixedArray to append. + * @return A new FixedArray with the elements of the two FixedArray. + */ + public TmfFixedArray append(final TmfFixedArray value) { + TmfFixedArray result = new TmfFixedArray(copyOf(fArray, fArray.length + value.size())); + System.arraycopy(value.fArray, 0, result.fArray, fArray.length, value.fArray.length); + return result; + } + /** + * Append in order many FixedArray to this FixedArray. + * @param values The FixedArrays to append. + * @return A new FixedArray with the element of all the FixedArray. + */ + public TmfFixedArray append(final TmfFixedArray... values) { + int newLength = 0; + for(TmfFixedArray value : values) + newLength += value.size(); + TmfFixedArray result = new TmfFixedArray(copyOf(fArray, fArray.length + newLength)); + newLength = fArray.length; + for(TmfFixedArray value : values) + { + System.arraycopy(value.fArray, 0, result.fArray, newLength, value.fArray.length); + newLength += value.fArray.length; + } + return result; + } + /** + * Append an element to the array. + * @param value Element to append. + * @return A new FixedArray with the element appended. + */ + public TmfFixedArray append(final T value) { + TmfFixedArray result = new TmfFixedArray(copyOf(fArray, fArray.length + 1)); + result.set(fArray.length, value); + return result; + } + /** + * Append an array of element to the array. + * @param values Elements array to append. + * @return A new FixedArray with the elements appended. + */ + public TmfFixedArray append(final T... values) { + TmfFixedArray result = new TmfFixedArray(copyOf(fArray, fArray.length + values.length)); + for(int i = 0; i < values.length; ++i) + result.set(fArray.length + i, values[i]); + return result; + } + /* + * (non-Javadoc) + * @see java.lang.Object#clone() + */ + @Override + public Object clone() + { + return new TmfFixedArray(copyOf(fArray, fArray.length)); + } + /* + * (non-Javadoc) + * @see java.util.AbstractList#equals(java.lang.Object) + */ + @Override + public boolean equals(Object o) { + if(o instanceof TmfFixedArray) + return Arrays.equals(fArray, ((TmfFixedArray)o).fArray); + if(!(o instanceof List)) + return false; + for(int i = 0; i < fArray.length; ++i) + if(!fArray[i].equals((List)o)) + return false; + return true; + } + /* + * (non-Javadoc) + * @see java.util.AbstractList#get(int) + */ + @Override + public T get(int index) { + return fArray[index]; + } + /** + * Get the array reference. + * @return The array reference. + * @see #toArray FixedArray.toArray() to get a copy of the array. + */ + public T[] getArray() { + return fArray; + } + /* + * (non-Javadoc) + * @see java.util.AbstractList#hashCode() + */ + @Override + public int hashCode() { + return Arrays.hashCode(fArray); + } + /* + * (non-Javadoc) + * @see java.util.AbstractList#set(int, java.lang.Object) + */ + @Override + public T set(int index, T element) { + T temp = fArray[index]; + fArray[index] = element; + return temp; + } + /* + * (non-Javadoc) + * @see java.util.AbstractCollection#size() + */ + @Override + public int size() { + return fArray.length; + } + /** + * Get a array covering only a part of the array. + * @param start Starting position of the new array. + * @return A new array covering the elements specified. + */ + public TmfFixedArray subArray(int start) { + return new TmfFixedArray(copyOfRange(fArray, start, fArray.length)); + } + /** + * Get a array covering only a part of the array. + * @param start Starting position of the new array. + * @param length Number of element to include in the new array. + * @return A new array covering the elements specified. + */ + public TmfFixedArray subArray(int start, int length) { + return new TmfFixedArray(copyOfRange(fArray, start, length + start)); + } + /* + * (non-Javadoc) + * @see java.util.AbstractCollection#toArray() + */ + @Override + public T[] toArray() + { + return copyOf(fArray, fArray.length); + } + /* + * (non-Javadoc) + * @see java.util.AbstractCollection#toArray(T[]) + */ + @Override + @SuppressWarnings("unchecked") + public E[] toArray(E[] array) + { + if(array.length < fArray.length) + return copyOf(fArray, fArray.length,(Class)array.getClass()); + System.arraycopy(fArray, 0, array, 0, fArray.length); + if(array.length > fArray.length) + array[fArray.length] = null; + return array; + } + /* + * (non-Javadoc) + * @see java.util.AbstractCollection#toString() + */ + @Override + public String toString() { + return Arrays.toString(fArray); + } +} -- 2.34.1