001: package com.xoetrope.carousel.survey;
002:
003: import com.xoetrope.survey.Condition;
004: import com.xoetrope.survey.Question;
005: import java.awt.Color;
006: import java.awt.Font;
007: import java.awt.Graphics;
008: import java.awt.Graphics2D;
009: import java.awt.Polygon;
010: import java.awt.Rectangle;
011: import java.awt.RenderingHints;
012: import java.awt.event.ActionEvent;
013: import java.awt.event.ActionListener;
014: import java.awt.event.MouseEvent;
015: import java.awt.geom.Rectangle2D;
016: import java.awt.geom.RoundRectangle2D;
017: import java.util.Enumeration;
018: import javax.swing.BorderFactory;
019: import javax.swing.JLabel;
020: import javax.swing.JMenuItem;
021: import javax.swing.JPopupMenu;
022:
023: /**
024: * A view of the condition in the graphic editor.
025: *
026: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
027: * the GNU Public License (GPL), please see license.txt for more details. If
028: * you make commercial use of this software you must purchase a commercial
029: * license from Xoetrope.</p>
030: * <p> $Revision: 1.5 $</p>
031: */
032: public class XConditionView extends XSurveyComponentSmall {
033: public static Color BACKGROUND_COLOR = new Color(255, 234, 234);
034: public static Color BORDER_COLOR = Color.BLACK;
035: public static int WIDTH = 150;
036: public static int HEIGHT = 20;
037: public static int SHAPE_GAP = 10;
038: public static int TEXT_GAP = 3;
039: public static int FONT_STYLE = 0;
040: public static int FONT_SIZE = 12;
041: public static String FONT_NAME = "Dialog";
042: public static Color FONT_COLOR;
043:
044: protected int shapeGap, textGap;
045: protected Condition condition;
046:
047: public XConditionView(Condition c, XRulesEditorPane ep, XRuleView r) {
048: super (ep, r);
049: condition = c;
050: }
051:
052: protected void initVariables() {
053: super .initVariables();
054: width = WIDTH;
055: height = HEIGHT;
056: textGap = TEXT_GAP;
057: fontSize = FONT_SIZE;
058: leftGap = LEFT_GAP;
059: rightGap = RIGHT_GAP;
060: topGap = TOP_GAP;
061: bottomGap = BOTTOM_GAP;
062: shapeGap = SHAPE_GAP;
063:
064: setBackgroundColor(BACKGROUND_COLOR);
065: setBorderColor(BORDER_COLOR);
066: setFontName(FONT_NAME);
067: setFontStyle(FONT_STYLE);
068: setFontColor(FONT_COLOR);
069: }
070:
071: protected void createPopupMenu() {
072: JMenuItem menuItem;
073: popupMenu = new JPopupMenu();
074: JLabel header = new JLabel(" Condition Options");
075: header.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
076: popupMenu.add(header);
077: popupMenu.addSeparator();
078: menuItem = new JMenuItem("delete condition");
079: popupMenu.add(menuItem);
080: menuItem.addActionListener(new ActionListener() {
081: public void actionPerformed(ActionEvent e) {
082: XRuleView ruleView = (XRuleView) owner;
083: ruleView.getRule().deleteCondition(condition);
084: }
085: });
086: menuItem = new JMenuItem("show table");
087: popupMenu.add(menuItem);
088: menuItem.addActionListener(new ActionListener() {
089: public void actionPerformed(ActionEvent e) {
090: XRuleView ruleView = (XRuleView) owner;
091: editorFrame
092: .showCondition(ruleView.getRule(), condition);
093: }
094: });
095: menuItem = new JMenuItem("show edit panel");
096: popupMenu.add(menuItem);
097: menuItem.addActionListener(new ActionListener() {
098: public void actionPerformed(ActionEvent e) {
099: editorPane.showAnswersView(XConditionView.this );
100: }
101: });
102: }
103:
104: protected void updateModel(XSurveyComponentBig oldOwner,
105: XSurveyComponentBig newOwner) {
106: XRuleView oldRuleView = (XRuleView) oldOwner;
107: XRuleView newRuleView = (XRuleView) newOwner;
108: XSurvey.setProjectModified(true);
109: if (oldRuleView != null) {
110: XRule rule = oldRuleView.getRule();
111: rule.getConditions().removeAllElements();
112: Enumeration enumConditionViews = oldRuleView
113: .getXComponents().elements();
114: while (enumConditionViews.hasMoreElements()) {
115: XConditionView conditionView = (XConditionView) enumConditionViews
116: .nextElement();
117: Condition condition = conditionView.getCondition();
118: rule.getConditions().add(condition);
119: }
120: }
121:
122: if (oldRuleView != null && oldRuleView.equals(newRuleView)
123: || oldRuleView == null && newRuleView == null) {
124: editorFrame.getRulesEditorPanel().refreshTables();
125: return;
126: }
127:
128: if (newRuleView != null) {
129: XRule rule = newRuleView.getRule();
130: rule.getConditions().removeAllElements();
131: Enumeration enumConditionViews = newRuleView
132: .getXComponents().elements();
133: while (enumConditionViews.hasMoreElements()) {
134: XConditionView conditionView = (XConditionView) enumConditionViews
135: .nextElement();
136: Condition condition = conditionView.getCondition();
137: rule.getConditions().add(condition);
138: }
139: }
140: editorFrame.getRulesEditorPanel().refreshTables();
141: }
142:
143: public void showPopupMenu(MouseEvent e) {
144: int x = (int) (e.getPoint().getX());
145: int y = (int) (e.getPoint().getY());
146: popupMenu.show(this , x, y);
147: }
148:
149: public void remove() {
150: super .remove();
151: editorPane.repaint();
152: }
153:
154: protected void createShape() {
155: int w = getWidth() - 1;
156: int h = getHeight() - 1;
157: int sg = getShapeGap();
158:
159: int x0 = 1;
160: int y0 = h / 2 + 1;
161: int x1 = sg;
162: int y1 = 1;
163: int x2 = w - sg + 1;
164: int y2 = 1;
165: int x3 = w;
166: int y3 = h / 2 + 1;
167: int x4 = w - sg + 1;
168: int y4 = h;
169: int x5 = sg;
170: int y5 = h;
171:
172: int[] xPoints = { x0, x1, x2, x3, x4, x5 };
173: int[] yPoints = { y0, y1, y2, y3, y4, y5 };
174: shape = new Polygon(xPoints, yPoints, 6);
175: }
176:
177: public Condition getCondition() {
178: return condition;
179: }
180:
181: public void mouseClicked(MouseEvent e) {
182: if (e.getClickCount() == 2) {
183: XRuleView ruleView = (XRuleView) owner;
184: editorFrame.showCondition(ruleView.getRule(), condition);
185: }
186: }
187:
188: protected void paintCondition(Graphics2D g) {
189: Color backgroundColor = getBackgroundColor();
190: Color borderColor = getBorderColor();
191: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
192: RenderingHints.VALUE_ANTIALIAS_ON);
193: g.setColor(backgroundColor);
194: g.fill(shape);
195: g.setColor(borderColor);
196: g.draw(shape);
197:
198: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
199: RenderingHints.VALUE_ANTIALIAS_OFF);
200: Font font = getFont();
201: Color fontColor = getFontColor();
202: int textGap = getTextGap();
203: int shapeGap = getShapeGap();
204: int width = getWidth();
205:
206: String desc = condition.getQuestion().getText();
207: String truncDesc = getTruncatedText(desc, width - 2 * textGap
208: - 2 * shapeGap, g, font);
209: Rectangle2D rc = font.getStringBounds(truncDesc, g
210: .getFontRenderContext());
211: int x = textGap + shapeGap;
212: int y = (int) (rc.getHeight());
213: g.drawString(truncDesc, x, y);
214:
215: }
216:
217: public void paint(Graphics g) {
218: Graphics2D g2d = (Graphics2D) g;
219: paintCondition(g2d);
220: }
221:
222: protected Class getOwnerClass() {
223: return XRuleView.class;
224: }
225:
226: public int getShapeGap() {
227: return (int) (getScale() * shapeGap);
228: }
229:
230: public void setShapeGap(int sg) {
231: shapeGap = (int) ((double) sg / getScale());
232: }
233:
234: public int getTextGap() {
235: return (int) ((double) getScale() * textGap);
236: }
237:
238: public void setTextGap(int tg) {
239: textGap = (int) ((double) tg / getScale());
240: }
241:
242: }
|