b94843a25d2dfc13b8059107daace96a7fddb271
[public/netxms.git] / webui / webapp / Dashboard / src / org / netxms / ui / eclipse / dashboard / widgets / ComparisonChartElement.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2015 Victor Kirhenshtein
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 package org.netxms.ui.eclipse.dashboard.widgets;
20
21 import java.util.Date;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.events.DisposeEvent;
27 import org.eclipse.swt.events.DisposeListener;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.layout.FillLayout;
30 import org.eclipse.swt.widgets.Widget;
31 import org.eclipse.ui.IViewPart;
32 import org.netxms.client.NXCSession;
33 import org.netxms.client.dashboards.DashboardElement;
34 import org.netxms.client.datacollection.ChartDciConfig;
35 import org.netxms.client.datacollection.DciData;
36 import org.netxms.client.datacollection.DciDataRow;
37 import org.netxms.ui.eclipse.charts.api.DataComparisonChart;
38 import org.netxms.ui.eclipse.dashboard.Activator;
39 import org.netxms.ui.eclipse.dashboard.Messages;
40 import org.netxms.ui.eclipse.jobs.ConsoleJob;
41 import org.netxms.ui.eclipse.shared.ConsoleSharedData;
42 import org.netxms.ui.eclipse.tools.ViewRefreshController;
43
44 /**
45 * Base class for data comparison charts - like bar chart, pie chart, etc.
46 */
47 public abstract class ComparisonChartElement extends ElementWidget
48 {
49 protected DataComparisonChart chart;
50 protected NXCSession session;
51 protected int refreshInterval = 30;
52
53 private ViewRefreshController refreshController;
54 private boolean updateInProgress = false;
55
56 /**
57 * @param parent
58 * @param data
59 */
60 public ComparisonChartElement(DashboardControl parent, DashboardElement element, IViewPart viewPart)
61 {
62 super(parent, element, viewPart);
63 session = (NXCSession)ConsoleSharedData.getSession();
64
65 setLayout(new FillLayout());
66
67 addDisposeListener(new DisposeListener() {
68 @Override
69 public void widgetDisposed(DisposeEvent e)
70 {
71 if (refreshController != null)
72 refreshController.dispose();
73 }
74 });
75 }
76
77 /**
78 * Start refresh timer
79 */
80 protected void startRefreshTimer()
81 {
82 refreshController = new ViewRefreshController(viewPart, refreshInterval, new Runnable() {
83 @Override
84 public void run()
85 {
86 if (ComparisonChartElement.this.isDisposed())
87 return;
88
89 refreshData(getDciList());
90 }
91 });
92 refreshData(getDciList());
93 }
94
95 /**
96 * Refresh graph's data
97 */
98 protected void refreshData(final ChartDciConfig[] dciList)
99 {
100 if (updateInProgress)
101 return;
102
103 updateInProgress = true;
104
105 ConsoleJob job = new ConsoleJob(Messages.get().ComparisonChartElement_JobTitle, viewPart, Activator.PLUGIN_ID, Activator.PLUGIN_ID) {
106 @Override
107 protected void runInternal(IProgressMonitor monitor) throws Exception
108 {
109 final DciData[] data = new DciData[dciList.length];
110 for(int i = 0; i < dciList.length; i++)
111 {
112 if (dciList[i].type == ChartDciConfig.ITEM)
113 data[i] = session.getCollectedData(dciList[i].nodeId, dciList[i].dciId, null, null, 1);
114 else
115 data[i] = session.getCollectedTableData(dciList[i].nodeId, dciList[i].dciId, dciList[i].instance, dciList[i].column, null, null, 1);
116 }
117 runInUIThread(new Runnable() {
118 @Override
119 public void run()
120 {
121 if (!((Widget)chart).isDisposed())
122 {
123 for(int i = 0; i < data.length; i++)
124 {
125 DciDataRow lastValue = data[i].getLastValue();
126 chart.updateParameter(i, (lastValue != null) ? lastValue : new DciDataRow(new Date(), 0.0), data[i].getDataType(), false);
127 }
128 chart.refresh();
129 chart.clearErrors();
130 }
131 updateInProgress = false;
132 }
133 });
134 }
135
136 @Override
137 protected String getErrorMessage()
138 {
139 return Messages.get().ComparisonChartElement_JobError;
140 }
141
142 @Override
143 protected void jobFailureHandler()
144 {
145 updateInProgress = false;
146 super.jobFailureHandler();
147 }
148
149 @Override
150 protected IStatus createFailureStatus(final Exception e)
151 {
152 runInUIThread(new Runnable() {
153 @Override
154 public void run()
155 {
156 chart.addError(getErrorMessage() + " (" + e.getLocalizedMessage() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
157 }
158 });
159 return Status.OK_STATUS;
160 }
161 };
162 job.setUser(false);
163 job.start();
164 }
165
166 /* (non-Javadoc)
167 * @see org.eclipse.swt.widgets.Composite#computeSize(int, int, boolean)
168 */
169 @Override
170 public Point computeSize(int wHint, int hHint, boolean changed)
171 {
172 Point size = super.computeSize(wHint, hHint, changed);
173 if ((hHint == SWT.DEFAULT) && (size.y < 250))
174 size.y = 250;
175 return size;
176 }
177
178 protected abstract ChartDciConfig[] getDciList();
179 }