001: package com.xoetrope.carousel.survey;
002:
003: import java.awt.BasicStroke;
004: import java.awt.Color;
005: import java.awt.Font;
006: import java.awt.Graphics;
007: import java.awt.Graphics2D;
008: import java.awt.Point;
009: import java.awt.RenderingHints;
010: import java.awt.event.MouseEvent;
011: import java.awt.event.MouseListener;
012: import java.awt.event.MouseMotionListener;
013: import java.awt.geom.Rectangle2D;
014: import java.util.Enumeration;
015: import java.util.Observable;
016: import java.util.Observer;
017: import java.util.Vector;
018: import javax.swing.JComponent;
019: import javax.swing.JLayeredPane;
020:
021: /**
022: * A view of the line connecting question group view and rule view.
023: *
024: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
025: * the GNU Public License (GPL), please see license.txt for more details. If
026: * you make commercial use of this software you must purchase a commercial
027: * license from Xoetrope.</p>
028: * <p> $Revision: 1.5 $</p>
029: */
030: public class XGroupRuleLine extends JComponent {
031:
032: private static final Color LINE_COLOR = Color.BLACK;
033:
034: protected XGroupView groupView;
035: protected XRuleView ruleView;
036: protected int orientation;
037: protected int expanderNr;
038: protected XRulesEditorPane editorPane;
039:
040: public XGroupRuleLine(XGroupView g, XRuleView r, XRulesEditorPane ep) {
041: super ();
042: setSize(10, 10); //???
043: groupView = g;
044: ruleView = r;
045: setFocusable(false);
046: expanderNr = -1;
047: editorPane = ep;
048: editorPane.add(this , JLayeredPane.PALETTE_LAYER);
049: setLocation();
050: }
051:
052: public XGroupView getGroupView() {
053: return groupView;
054: }
055:
056: public XRuleView getRuleView() {
057: return ruleView;
058: }
059:
060: public void moveBy(int dx, int dy) {
061: int x = (int) getLocation().getX() + dx;
062: int y = (int) getLocation().getY() + dy;
063: setLocation(x, y);
064: }
065:
066: public void setLocation() {
067: //int x = 0, y = 0, w = 0, h = 0;
068: int x = 10, y = 10, w = 10, h = 10;
069: orientation = 0;
070: int newExpanderNr = -1;
071:
072: int gx1 = (int) groupView.getLocation().getX();
073: int gy1 = (int) groupView.getLocation().getY();
074: int gx2 = gx1 + (int) groupView.getSize().getWidth();
075: int gy2 = gy1 + (int) groupView.getSize().getHeight();
076: int cgx = (gx1 + gx2) / 2;
077: int cgy = (gy1 + gy2) / 2;
078:
079: int rx1 = (int) ruleView.getLocation().getX();
080: int ry1 = (int) ruleView.getLocation().getY();
081: int rx2 = rx1 + (int) ruleView.getSize().getWidth();
082: int ry2 = ry1 + (int) ruleView.getSize().getHeight();
083: int crx = (rx1 + rx2) / 2;
084: int cry = (ry1 + ry2) / 2;
085:
086: if (ry1 > gy2 || gy1 > ry2) {
087: w = Math.abs(crx - cgx);
088: h = Math.min(Math.abs(ry1 - gy2), Math.abs(ry2 - gy1));
089: x = Math.min(cgx, crx);
090: y = Math.min(gy2, ry2);
091: if (cgy < cry) {
092: orientation = (cgx < crx ? 1 : 2);
093: newExpanderNr = 2;
094: } else {
095: orientation = (cgx < crx ? 2 : 1);
096: newExpanderNr = 0;
097: }
098: } else if (rx1 > gx2 || gx1 > rx2) {
099: w = Math.min(Math.abs(rx1 - gx2), Math.abs(rx2 - gx1));
100: h = Math.abs(cgy - cry);
101: x = Math.min(gx2, rx2);
102: y = Math.min(cgy, cry);
103: if (cgx < crx) {
104: orientation = (cgy < cry ? 3 : 4);
105: newExpanderNr = 1;
106: } else {
107: orientation = (cgy < cry ? 4 : 3);
108: newExpanderNr = 3;
109: }
110: }
111:
112: setSize(w + 1, h + 1);
113: setLocation(x, y);
114:
115: if (newExpanderNr >= 0 && newExpanderNr != expanderNr) {
116: if (expanderNr >= 0)
117: groupView.removeGroupRuleLine(expanderNr, this );
118: groupView.addGroupRuleLine(newExpanderNr, this );
119: expanderNr = newExpanderNr;
120: }
121: }
122:
123: protected void paintLine(Graphics2D g) {
124: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
125: RenderingHints.VALUE_ANTIALIAS_ON);
126: g.setColor(LINE_COLOR);
127:
128: int w = getSize().width - 1;
129: int h = getSize().height - 1;
130:
131: switch (orientation) {
132: case 1:
133: g.drawLine(0, 0, 0, h / 2);
134: g.drawLine(0, h / 2, w, h / 2);
135: g.drawLine(w, h / 2, w, h);
136: break;
137: case 2:
138: g.drawLine(0, h, 0, h / 2);
139: g.drawLine(0, h / 2, w, h / 2);
140: g.drawLine(w, h / 2, w, 0);
141: break;
142: case 3:
143: g.drawLine(0, 0, w / 2, 0);
144: g.drawLine(w / 2, 0, w / 2, h);
145: g.drawLine(w / 2, h, w, h);
146: break;
147: case 4:
148: g.drawLine(0, h, w / 2, h);
149: g.drawLine(w / 2, h, w / 2, 0);
150: g.drawLine(w / 2, 0, w, 0);
151: break;
152: }
153:
154: }
155:
156: public void paint(Graphics gr) {
157: Graphics2D g = (Graphics2D) gr;
158: paintLine(g);
159: }
160:
161: }
|