001: package com.xoetrope.carousel.survey;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005: import java.awt.Graphics;
006: import java.awt.Graphics2D;
007: import java.awt.RenderingHints;
008: import java.awt.event.MouseEvent;
009: import java.awt.event.MouseListener;
010: import javax.swing.JComponent;
011: import javax.swing.JLayeredPane;
012:
013: /**
014: * A small rectangle used by question group. when pressed rules belonging
015: * to certain question group are shown in graphic editor.
016: *
017: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
018: * the GNU Public License (GPL), please see license.txt for more details. If
019: * you make commercial use of this software you must purchase a commercial
020: * license from Xoetrope.</p>
021: * <p> $Revision: 1.5 $</p>
022: */
023: public class XExpander extends JComponent implements MouseListener {
024: protected XExpandable owner;
025: protected XRulesEditorPane editorPane;
026: protected boolean open;
027: protected int width;
028: protected int height;
029: protected int lineLength;
030: protected Color background, border;
031:
032: protected static double getScale() {
033: return XRulesEditorPane.SCALE;
034: }
035:
036: public XExpander(XExpandable o, int w, int h, int ll, Color c,
037: Color b, XRulesEditorPane ep) {
038: super ();
039: width = w;
040: height = h;
041: lineLength = ll;
042: owner = o;
043: open = true;
044: background = c;
045: border = b;
046: editorPane = ep;
047: addMouseListener(this );
048: editorPane.add(this , JLayeredPane.PALETTE_LAYER);
049: setSize();
050: }
051:
052: public void mouseClicked(MouseEvent e) {
053: }
054:
055: public void mousePressed(MouseEvent e) {
056: owner.moveToFront();
057: open ^= true;
058: revalidate();
059: repaint();
060: owner.expand(open);
061: }
062:
063: public void mouseReleased(MouseEvent e) {
064: }
065:
066: public void mouseEntered(MouseEvent e) {
067: }
068:
069: public void mouseExited(MouseEvent e) {
070: }
071:
072: public void setSize() {
073: int w = getWidth();
074: int h = getHeight();
075: Dimension dim = new Dimension(w, h);
076: setSize(dim);
077: setMinimumSize(dim);
078: setPreferredSize(dim);
079: }
080:
081: protected void paintExpander(Graphics2D g) {
082: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
083: RenderingHints.VALUE_ANTIALIAS_ON);
084: int x = 0;
085: int y = 0;
086: int w = getWidth() - 1;
087: int h = getHeight() - 1;
088: int ll = getLineLength();
089:
090: g.setColor(background);
091: g.fillRect(x, y, w, h);
092: g.setColor(border);
093: g.drawRect(x, y, w, h);
094:
095: int lg = (w - ll) / 2;
096: g.drawLine(lg, h / 2 + 1, w - lg, h / 2 + 1);
097: if (!open) {
098: g.drawLine(w / 2, lg, w / 2, h - lg + 1);
099: }
100: }
101:
102: public void paint(Graphics g) {
103: Graphics2D g2d = (Graphics2D) g;
104: paintExpander(g2d);
105: }
106:
107: public void setWidth(int w) {
108: width = (int) ((double) w / getScale());
109: }
110:
111: public int getWidth() {
112: return (int) (getScale() * width);
113: }
114:
115: public void setHeight(int h) {
116: height = (int) ((double) h / getScale());
117: }
118:
119: public int getHeight() {
120: return (int) (getScale() * height);
121: }
122:
123: public void setLineLength(int ll) {
124: lineLength = (int) ((double) ll / getScale());
125: }
126:
127: public int getLineLength() {
128: return (int) (getScale() * lineLength);
129: }
130:
131: }
|