1/*******************************************************************************
2 * Copyright (c) 2000, 2009 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.test.internal.performance.results.model;
12
13import java.util.ArrayList;
14import java.util.Arrays;
15import java.util.Iterator;
16import java.util.List;
17import java.util.Vector;
18
19import org.eclipse.core.runtime.preferences.IEclipsePreferences;
20import org.eclipse.core.runtime.preferences.InstanceScope;
21import org.eclipse.test.internal.performance.results.db.AbstractResults;
22import org.eclipse.test.internal.performance.results.db.ComponentResults;
23import org.eclipse.test.internal.performance.results.db.PerformanceResults;
24import org.eclipse.test.internal.performance.results.db.ScenarioResults;
25import org.eclipse.test.internal.performance.results.utils.IPerformancesConstants;
26import org.eclipse.test.internal.performance.results.utils.Util;
27import org.eclipse.ui.views.properties.IPropertyDescriptor;
28import org.eclipse.ui.views.properties.PropertyDescriptor;
29import org.eclipse.ui.views.properties.TextPropertyDescriptor;
30
31public class ComponentResultsElement extends ResultsElement {
32
33	// Property descriptors
34	static final String P_ID_NAME = "ComponentResultsElement.name"; //$NON-NLS-1$
35	static final String P_ID_CURRENT_BUILD = "ComponentResultsElement.currentbuild"; //$NON-NLS-1$
36	static final String P_ID_BASELINE_BUILD = "ComponentResultsElement.baselinebuild"; //$NON-NLS-1$
37
38	static final String P_STR_NAME = "name"; //$NON-NLS-1$
39	static final String P_STR_CURRENT_BUILD = "current build"; //$NON-NLS-1$
40	static final String P_STR_BASELINE_BUILD = "baseline build"; //$NON-NLS-1$
41
42	private static final TextPropertyDescriptor NAME_DESCRIPTOR = new TextPropertyDescriptor(P_ID_NAME, P_STR_NAME);
43	private static final PropertyDescriptor CURRENT_BUILD_DESCRIPTOR = new PropertyDescriptor(P_ID_CURRENT_BUILD, P_STR_CURRENT_BUILD);
44	private static final PropertyDescriptor BASELINE_BUILD_DESCRIPTOR = new PropertyDescriptor(P_ID_BASELINE_BUILD, P_STR_BASELINE_BUILD);
45
46    private static Vector DESCRIPTORS;
47    static Vector initDescriptors(int status) {
48        DESCRIPTORS = new Vector();
49		// Status category
50		DESCRIPTORS.add(getInfosDescriptor(status));
51		DESCRIPTORS.add(getWarningsDescriptor(status));
52		DESCRIPTORS.add(ERROR_DESCRIPTOR);
53		ERROR_DESCRIPTOR.setCategory("Status");
54		// Results category
55		DESCRIPTORS.addElement(NAME_DESCRIPTOR);
56		NAME_DESCRIPTOR.setCategory("Results");
57		DESCRIPTORS.addElement(CURRENT_BUILD_DESCRIPTOR);
58		CURRENT_BUILD_DESCRIPTOR.setCategory("Results");
59		DESCRIPTORS.addElement(BASELINE_BUILD_DESCRIPTOR);
60		BASELINE_BUILD_DESCRIPTOR.setCategory("Results");
61		// Survey category
62		DESCRIPTORS.add(COMMENT_DESCRIPTOR);
63		COMMENT_DESCRIPTOR.setCategory("Survey");
64        return DESCRIPTORS;
65	}
66    static Vector getDescriptors() {
67    	return DESCRIPTORS;
68	}
69
70public ComponentResultsElement(String name, ResultsElement parent) {
71	super(name, parent);
72}
73
74public ComponentResultsElement(AbstractResults results, ResultsElement parent) {
75	super(results, parent);
76}
77
78/*
79 * Do not create non-fingerprint child when only fingerprint is specified.
80 *
81 * @see org.eclipse.test.internal.performance.results.model.ResultsElement#createChild(org.eclipse.test.internal.performance.results.db.AbstractResults)
82 */
83ResultsElement createChild(AbstractResults testResults) {
84//	if (onlyFingerprints()) {
85//		ScenarioResults scenarioResults = (ScenarioResults) testResults;
86//		if (!scenarioResults.hasSummary()) {
87//			return null;
88//		}
89//	}
90	return new ScenarioResultsElement(testResults, this);
91}
92
93/**
94 * Get all results numbers for a given machine of the current component.
95 *
96 * @param configName The name of the configuration to get numbers
97 * @param fingerprints Set whether only fingerprints scenario should be taken into account
98 * @return A list of lines. Each line represent a build and is a list of either strings or values.
99 */
100public List getConfigNumbers(String configName, boolean fingerprints) {
101	if (this.results == null) return null;
102	return ((ComponentResults)this.results).getConfigNumbers(configName, fingerprints, new ArrayList());
103}
104
105/* (non-Javadoc)
106 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
107 */
108public IPropertyDescriptor[] getPropertyDescriptors() {
109	Vector descriptors = getDescriptors();
110	if (descriptors == null) {
111		descriptors = initDescriptors(getStatus());
112	}
113	int size = descriptors.size();
114	IPropertyDescriptor[] descriptorsArray = new IPropertyDescriptor[size];
115	descriptorsArray[0] = getInfosDescriptor(getStatus());
116	descriptorsArray[1] = getWarningsDescriptor(getStatus());
117	for (int i=2; i<size; i++) {
118		descriptorsArray[i] = (IPropertyDescriptor) descriptors.get(i);
119	}
120	return descriptorsArray;
121}
122
123/* (non-Javadoc)
124 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
125 */
126public Object getPropertyValue(Object propKey) {
127	if (propKey.equals(P_ID_NAME)) {
128		return getName();
129	}
130	if (propKey.equals(P_ID_CURRENT_BUILD)) {
131		if (this.results == null) {
132			PerformanceResultsElement performanceResultsElement = (PerformanceResultsElement) getParent(null);
133			return performanceResultsElement.getName();
134		}
135		PerformanceResults performanceResults = (PerformanceResults) this.results.getParent();
136		return performanceResults.getName();
137	}
138	if (propKey.equals(P_ID_BASELINE_BUILD)) {
139		if (this.results == null) {
140			return "?";
141		}
142		PerformanceResults performanceResults = (PerformanceResults) this.results.getParent();
143		return performanceResults.getBaselineName();
144	}
145    return super.getPropertyValue(propKey);
146}
147
148/**
149 * Get the list of the scenarios results from the model. Put only fingerprint ones if specified.
150 *
151 * @param fingerprint Tell whether only fingerprint scenarios are expected or not.
152 * @return A list of {@link ScenarioResults}.
153 */
154public List getScenarios(boolean fingerprint) {
155	if (!fingerprint) {
156		return Arrays.asList(this.results.getChildren());
157	}
158	List scenarios = new ArrayList();
159	if (this.results != null) {
160		Iterator iterator = this.results.getResults();
161		while (iterator.hasNext()) {
162			ScenarioResults scenarioResults = (ScenarioResults) iterator.next();
163			if (scenarioResults.hasSummary()) {
164				scenarios.add(scenarioResults);
165			}
166		}
167	}
168	return scenarios;
169}
170
171/**
172 * Get the list of the scenarios names. Put only fingerprint ones if specified.
173 *
174 * @param fingerprint Tell whether only fingerprint scenarios are expected or not.
175 * @return A list of {@link String}.
176 */
177public List getScenariosLabels(boolean fingerprint) {
178	List labels = new ArrayList();
179	if (this.results != null) {
180		AbstractResults[] scenarios = this.results.getChildren();
181		int length = scenarios.length;
182		for (int i=0; i<length; i++) {
183			ScenarioResults scenarioResults = (ScenarioResults) scenarios[i];
184			if (!fingerprint || scenarioResults.hasSummary()) {
185				labels.add(scenarioResults.getLabel());
186			}
187		}
188	}
189	return labels;
190}
191
192/*
193 * (non-Javadoc)
194 * @see org.eclipse.test.internal.performance.results.model.ResultsElement#initStatus()
195 */
196void initStatus() {
197	if (this.results == null) {
198		this.status = UNREAD;
199	} else {
200		super.initStatus();
201	}
202}
203
204StringBuffer writableStatus(StringBuffer buffer, int kind, StringBuffer excluded) {
205	// Write status for scenarios having error
206	if ((getStatus() & ERROR_MASK) != 0) {
207
208		// Get children status
209		StringBuffer childrenBuffer = super.writableStatus(new StringBuffer(), kind, excluded);
210
211		// Write status on file if not excluded
212		if (childrenBuffer.length() > 0) {
213			buffer.append(getName());
214			IEclipsePreferences preferences = new InstanceScope().getNode(IPerformancesConstants.PLUGIN_ID);
215			String comment = preferences.get(getId(), null);
216			if (comment != null) {
217				if ((kind & IPerformancesConstants.STATUS_VALUES) != 0) {
218					buffer.append("												");
219				} else {
220					buffer.append("			");
221				}
222				buffer.append(comment);
223			}
224			buffer.append(Util.LINE_SEPARATOR);
225			buffer.append(childrenBuffer);
226			buffer.append(Util.LINE_SEPARATOR);
227		}
228	}
229	return buffer;
230}
231
232}
233