001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */package org.netbeans.modules.vmd.midp.screen.display;
041:
042: import java.awt.BasicStroke;
043: import java.awt.Color;
044: import java.awt.Dimension;
045: import java.awt.Graphics;
046: import java.awt.Graphics2D;
047: import javax.swing.JPanel;
048:
049: /**
050: *
051: * @author Karol Harezlak
052: */
053: public class GaugeDisplayPresenterElement {
054:
055: private static final int BAR_WIDTH = 8;
056: private static final int BAR_GAP = 5;
057: private static final BasicStroke GAUGE_STROKE = new BasicStroke(1,
058: BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL, 0,
059: new float[] { 3, 2 }, 0);
060:
061: private JPanel panel;
062: private Dimension size;
063: private int value;
064: private int maxValue;
065: private boolean interactive;
066:
067: public void setPanel(JPanel panel) {
068: if (panel == null)
069: throw new IllegalArgumentException();
070: this .panel = panel;
071: }
072:
073: public Dimension getSize() {
074: return size;
075: }
076:
077: public void setSize(Dimension size) {
078: if (size == null)
079: throw new IllegalArgumentException();
080: this .size = size;
081: }
082:
083: public void setValue(int value) {
084: this .value = value;
085: }
086:
087: public void setMaxValue(int maxValue) {
088: this .maxValue = maxValue;
089: }
090:
091: public void setInteractive(boolean interactive) {
092: this .interactive = interactive;
093: }
094:
095: public void paintGauge(Graphics g) {
096: if (panel == null)
097: throw new IllegalArgumentException();
098: size = panel.getSize();
099: if (size == null) { // did not realoaded yet
100: return;
101: }
102: int gaugeWidth = size.width;
103: int gaugeHeight = size.height - 5;
104: int barsCount = gaugeWidth / (BAR_WIDTH + BAR_GAP) + 1;
105: int selectionBarCount = 0;
106: if (value > 0) {
107: selectionBarCount = barsCount * value / maxValue;
108: }
109: int px = 0;
110:
111: Graphics2D g2D = (Graphics2D) g;
112: g2D.setColor(Color.GRAY);
113: g2D.setStroke(GAUGE_STROKE);
114:
115: float barHeight = (float) gaugeHeight;
116: float heightStep = 0f;
117: if (interactive) {
118: barHeight = 1f;
119: heightStep = (float) gaugeHeight / (float) barsCount;
120: }
121:
122: for (int i = 0; i < barsCount; i++) {
123: if (i < selectionBarCount) {
124: g2D.fillRect(px, (int) (gaugeHeight - barHeight),
125: BAR_WIDTH, (int) barHeight);
126: } else {
127: g2D.drawRect(px, (int) (gaugeHeight - barHeight),
128: BAR_WIDTH, (int) barHeight);
129: }
130: px += BAR_WIDTH + BAR_GAP;
131: if (interactive && (barHeight < gaugeHeight)) {
132: barHeight += heightStep;
133: if (barHeight > gaugeHeight) {
134: barHeight = gaugeHeight;
135: }
136: }
137: }
138: }
139:
140: }
|