001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.support.swing;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.GradientPaint;
018: import java.awt.Graphics;
019: import java.awt.Graphics2D;
020: import java.awt.LayoutManager;
021: import java.awt.Paint;
022:
023: import javax.swing.JPanel;
024:
025: /**
026: * Created by IntelliJ IDEA.
027: */
028:
029: public class GradientPanel extends JPanel {
030: // ------------------------------ FIELDS ------------------------------
031:
032: public final static int HORIZONTAL = 0;
033: public final static int VERTICAL = 1;
034: public final static int DIAGONAL_LEFT = 2;
035: public final static int DIAGONAL_RIGHT = 3;
036:
037: private int direction = HORIZONTAL;
038: private boolean cyclic;
039: private int maxLength;
040:
041: // --------------------------- CONSTRUCTORS ---------------------------
042:
043: public GradientPanel() {
044: this (HORIZONTAL);
045: }
046:
047: public GradientPanel(int direction) {
048: super (new BorderLayout());
049: setOpaque(false);
050: this .direction = direction;
051: }
052:
053: public GradientPanel(LayoutManager layoutManager) {
054: super (layoutManager);
055: setOpaque(false);
056: this .direction = HORIZONTAL;
057: }
058:
059: // --------------------- GETTER / SETTER METHODS ---------------------
060:
061: public int getDirection() {
062: return direction;
063: }
064:
065: public void setDirection(int direction) {
066: this .direction = direction;
067: }
068:
069: public boolean isCyclic() {
070: return cyclic;
071: }
072:
073: public void setCyclic(boolean cyclic) {
074: this .cyclic = cyclic;
075: }
076:
077: public void setMaxLength(int maxLength) {
078: this .maxLength = maxLength;
079: }
080:
081: // -------------------------- OTHER METHODS --------------------------
082:
083: public void paintComponent(Graphics g) {
084: if (isOpaque()) {
085: super .paintComponent(g);
086: return;
087: }
088:
089: int width = getWidth();
090: int height = getHeight();
091:
092: // Create the gradient paint
093: GradientPaint paint = null;
094:
095: Color sc = getForeground();
096: Color ec = getBackground();
097:
098: switch (direction) {
099: case HORIZONTAL: {
100: paint = new GradientPaint(0, height / 2, sc, width,
101: height / 2, ec, cyclic);
102: break;
103: }
104: case VERTICAL: {
105: paint = new GradientPaint(width / 2, 0, sc, width / 2,
106: maxLength > 0 ? maxLength : height, ec, cyclic);
107: break;
108: }
109: case DIAGONAL_LEFT: {
110: paint = new GradientPaint(0, 0, sc, width, height, ec,
111: cyclic);
112: break;
113: }
114: case DIAGONAL_RIGHT: {
115: paint = new GradientPaint(width, 0, sc, 0, height, ec,
116: cyclic);
117: break;
118: }
119: }
120:
121: if (paint == null) {
122: throw new RuntimeException(
123: "Invalid direction specified in GradientPanel");
124: }
125:
126: // we need to cast to Graphics2D for this operation
127: Graphics2D g2d = (Graphics2D) g;
128:
129: // save the old paint
130: Paint oldPaint = g2d.getPaint();
131:
132: // set the paint to use for this operation
133: g2d.setPaint(paint);
134:
135: // fill the background using the paint
136: g2d.fillRect(0, 0, width, height);
137:
138: // restore the original paint
139: g2d.setPaint(oldPaint);
140:
141: super.paintComponent(g);
142: }
143: }
|