e8794eb3b4dce1819ba45383ee81b210a6b0d5a0
[public/netxms.git] / webui / webapp / PerfView / src / org / netxms / ui / eclipse / perfview / widgets / helpers / TableLabelProvider.java
1 /**
2 * NetXMS - open source network management system
3 * Copyright (C) 2003-2013 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.perfview.widgets.helpers;
20
21 import java.text.NumberFormat;
22 import org.eclipse.jface.resource.JFaceResources;
23 import org.eclipse.jface.viewers.ITableFontProvider;
24 import org.eclipse.jface.viewers.ITableLabelProvider;
25 import org.eclipse.jface.viewers.LabelProvider;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.graphics.Font;
28 import org.eclipse.swt.graphics.FontData;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.widgets.Display;
31 import org.netxms.client.TableColumnDefinition;
32 import org.netxms.client.TableRow;
33 import org.netxms.client.datacollection.DataCollectionItem;
34 import org.netxms.client.datacollection.DataCollectionObject;
35
36 /**
37 * Label provider for NetXMS table
38 */
39 public class TableLabelProvider extends LabelProvider implements ITableLabelProvider, ITableFontProvider
40 {
41 private TableColumnDefinition[] columns = null;
42 private TableRow row;
43 private Font keyColumnFont;
44 private boolean useMultipliers = true;
45
46 /**
47 *
48 */
49 public TableLabelProvider()
50 {
51 FontData fd = JFaceResources.getDefaultFont().getFontData()[0];
52 fd.setStyle(SWT.BOLD);
53 keyColumnFont = new Font(Display.getCurrent(), fd);
54 }
55
56 /* (non-Javadoc)
57 * @see org.eclipse.jface.viewers.BaseLabelProvider#dispose()
58 */
59 @Override
60 public void dispose()
61 {
62 keyColumnFont.dispose();
63 super.dispose();
64 }
65
66 /**
67 * @param tableColumnDefinitions the columns to set
68 */
69 public void setColumns(TableColumnDefinition[] columns)
70 {
71 this.columns = columns;
72 }
73
74 /* (non-Javadoc)
75 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
76 */
77 @Override
78 public Image getColumnImage(Object element, int columnIndex)
79 {
80 return null;
81 }
82
83 /* (non-Javadoc)
84 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
85 */
86 @Override
87 public String getColumnText(Object element, int columnIndex)
88 {
89 row = (TableRow)element;
90
91 if (columnIndex >= row.size())
92 return null;
93 if (useMultipliers)
94 return getValueForFormat(columnIndex);
95 return row.get(columnIndex).getValue();
96 }
97
98 /* (non-Javadoc)
99 * @see org.eclipse.jface.viewers.ITableFontProvider#getFont(java.lang.Object, int)
100 */
101 @Override
102 public Font getFont(Object element, int columnIndex)
103 {
104 if ((columns == null) || (columnIndex >= columns.length))
105 return null;
106 if (columns[columnIndex].isInstanceColumn())
107 return keyColumnFont;
108 return null;
109 }
110
111 /**
112 * @param useMultipliers
113 */
114 public void setUseMultipliers(boolean useMultipliers)
115 {
116 this.useMultipliers = useMultipliers;
117 }
118
119 /**
120 * @return
121 */
122 public boolean areMultipliersUsed()
123 {
124 return useMultipliers;
125 }
126
127 /**
128 * @param columnIndex
129 * @return value converted to multiplier form
130 */
131 private String getValueForFormat(int columnIndex)
132 {
133 String value;
134 String suffix = null;
135
136 try
137 {
138 switch(columns[columnIndex].getDataType())
139 {
140 case DataCollectionObject.DT_INT:
141 case DataCollectionObject.DT_UINT:
142 case DataCollectionItem.DT_INT64:
143 case DataCollectionItem.DT_UINT64:
144 if (useMultipliers)
145 {
146 long i = Long.parseLong(row.get(columnIndex).getValue());
147 if ((i >= 10000000000000L) || (i <= -10000000000000L))
148 {
149 i = i / 1000000000000L;
150 suffix = "T";
151 }
152 if ((i >= 10000000000L) || (i <= -10000000000L))
153 {
154 i = i / 1000000000L;
155 suffix = "G";
156 }
157 if ((i >= 10000000) || (i <= -10000000))
158 {
159 i = i / 1000000;
160 suffix = "M";
161 }
162 if ((i >= 10000) || (i <= -10000))
163 {
164 i = i / 1000;
165 suffix = "K";
166 }
167 value = Long.toString(i);
168 }
169 else
170 {
171 value = row.get(columnIndex).getValue();
172 suffix = " ";
173 }
174 break;
175 case DataCollectionObject.DT_FLOAT:
176 if (useMultipliers)
177 {
178 double d = Double.parseDouble(row.get(columnIndex).getValue());
179 NumberFormat nf = NumberFormat.getNumberInstance();
180 nf.setMaximumFractionDigits(2);
181 if ((d >= 10000000000000.0) || (d <= -10000000000000.0))
182 {
183 d = d / 1000000000000.0;
184 suffix = "T";
185 }
186 if ((d >= 10000000000.0) || (d <= -10000000000.0))
187 {
188 d = d / 1000000000.0;
189 suffix = "G";
190 }
191 if ((d >= 10000000) || (d <= -10000000))
192 {
193 d = d / 1000000;
194 suffix = "M";
195 }
196 if ((d >= 10000) || (d <= -10000))
197 {
198 d = d / 1000;
199 suffix = "K";
200 }
201 value = Double.toString(d);
202 }
203 else
204 {
205 value = row.get(columnIndex).getValue();
206 }
207 break;
208 default:
209 value = row.get(columnIndex).getValue();
210 break;
211 }
212 }
213 catch(NumberFormatException e)
214 {
215 value = row.get(columnIndex).getValue();
216 }
217
218 if (suffix != null)
219 return value + " " + suffix;
220 return value;
221 }
222 }