b55a7bc95e5fad42b233e7f2ae4695ee93d3479e
[public/netxms.git] / src / java / netxms-eclipse / Charts / src / org / netxms / ui / eclipse / charts / widgets / CurrentValueWidget.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.charts.widgets;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.GC;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.graphics.Rectangle;
27 import org.eclipse.swt.widgets.Composite;
28 import org.netxms.ui.eclipse.charts.widgets.internal.DataComparisonElement;
29 import org.netxms.ui.eclipse.console.resources.StatusDisplayInfo;
30 import org.netxms.ui.eclipse.tools.WidgetHelper;
31
32 /**
33 * Current value pseudo-chart implementation
34 */
35 public class CurrentValueWidget extends GaugeWidget
36 {
37 private Font[] valueFonts = null;
38
39 /**
40 * @param parent
41 * @param style
42 */
43 public CurrentValueWidget(Composite parent, int style)
44 {
45 super(parent, style);
46 }
47
48 /* (non-Javadoc)
49 * @see org.netxms.ui.eclipse.charts.widgets.GaugeWidget#createFonts()
50 */
51 @Override
52 protected void createFonts()
53 {
54 valueFonts = new Font[32];
55 for(int i = 0; i < valueFonts.length; i++)
56 valueFonts[i] = new Font(getDisplay(), fontName, i * 6 + 12, SWT.BOLD); //$NON-NLS-1$
57 }
58
59 /* (non-Javadoc)
60 * @see org.netxms.ui.eclipse.charts.widgets.GaugeWidget#disposeFonts()
61 */
62 @Override
63 protected void disposeFonts()
64 {
65 if (valueFonts != null)
66 {
67 for(int i = 0; i < valueFonts.length; i++)
68 valueFonts[i].dispose();
69 }
70 }
71
72 /* (non-Javadoc)
73 * @see org.netxms.ui.eclipse.charts.widgets.GaugeWidget#renderElement(org.eclipse.swt.graphics.GC, org.netxms.ui.eclipse.charts.widgets.internal.DataComparisonElement, int, int, int, int)
74 */
75 @Override
76 protected void renderElement(GC gc, DataComparisonElement dci, int x, int y, int w, int h)
77 {
78 Rectangle rect = new Rectangle(x + INNER_MARGIN_WIDTH, y + INNER_MARGIN_HEIGHT, w - INNER_MARGIN_WIDTH * 2, h - INNER_MARGIN_HEIGHT * 2);
79 gc.setAntialias(SWT.ON);
80
81 if (elementBordersVisible)
82 {
83 gc.drawRectangle(rect);
84 rect.x += INNER_MARGIN_WIDTH;
85 rect.y += INNER_MARGIN_HEIGHT;
86 rect.width -= INNER_MARGIN_WIDTH / 2;
87 rect.height -= INNER_MARGIN_HEIGHT / 2;
88 }
89
90 if (legendVisible)
91 {
92 rect.height -= gc.textExtent("MMM").y + 8; //$NON-NLS-1$
93 }
94
95 final String value = getValueAsDisplayString(dci);
96 final Font font = WidgetHelper.getBestFittingFont(gc, valueFonts, value, rect.width, rect.height); //$NON-NLS-1$
97 gc.setFont(font);
98 switch(colorMode)
99 {
100 case ZONE:
101 if ((dci.getValue() <= leftRedZone) || (dci.getValue() >= rightRedZone))
102 {
103 gc.setForeground(colors.create(RED_ZONE_COLOR));
104 }
105 else if ((dci.getValue() <= leftYellowZone) || (dci.getValue() >= rightYellowZone))
106 {
107 gc.setForeground(colors.create(YELLOW_ZONE_COLOR));
108 }
109 else
110 {
111 gc.setForeground(colors.create(GREEN_ZONE_COLOR));
112 }
113 break;
114 case CUSTOM:
115 gc.setForeground(colors.create(customColor));
116 break;
117 case THRESHOLD:
118 gc.setForeground(StatusDisplayInfo.getStatusColor(dci.getActiveThresholdSeverity()));
119 break;
120 default:
121 gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_BLACK));
122 break;
123 }
124 Point ext = gc.textExtent(value, SWT.DRAW_TRANSPARENT);
125 gc.drawText(value, rect.x + rect.width / 2 - ext.x / 2, rect.y + rect.height / 2 - ext.y / 2, SWT.TRANSPARENT);
126
127 // Draw legend, ignore legend position
128 if (legendVisible)
129 {
130 gc.setFont(null);
131 ext = gc.textExtent(dci.getName(), SWT.DRAW_TRANSPARENT);
132 gc.setForeground(getColorFromPreferences("Chart.Colors.Legend")); //$NON-NLS-1$
133 gc.drawText(dci.getName(), rect.x + ((rect.width - ext.x) / 2), rect.y + rect.height + 4, true);
134 }
135 }
136
137 /**
138 * Take snapshot of network map
139 *
140 * @return
141 */
142 public Image takeSnapshot()
143 {
144 Rectangle rect = getClientArea();
145 Image image = new Image(getDisplay(), rect.width, rect.height);
146 GC gc = new GC(image);
147 this.print(gc);
148 gc.dispose();
149 return image;
150 }
151 }