0001: package com.xoetrope.carousel.survey;
0002:
0003: import com.xoetrope.survey.Condition;
0004: import com.xoetrope.survey.Question;
0005: import java.awt.Color;
0006: import java.awt.Component;
0007: import java.awt.Dimension;
0008: import java.awt.Graphics;
0009: import java.awt.Graphics2D;
0010: import java.awt.Point;
0011: import java.awt.Rectangle;
0012: import java.awt.event.ActionEvent;
0013: import java.awt.event.ActionListener;
0014: import java.awt.event.KeyAdapter;
0015: import java.awt.event.KeyEvent;
0016: import java.awt.event.MouseAdapter;
0017: import java.awt.event.MouseEvent;
0018: import java.awt.event.MouseListener;
0019: import java.awt.event.MouseWheelEvent;
0020: import java.awt.event.MouseWheelListener;
0021: import java.awt.geom.Rectangle2D;
0022: import java.util.Enumeration;
0023: import java.util.Observable;
0024: import java.util.Observer;
0025: import java.util.Vector;
0026: import javax.swing.BorderFactory;
0027: import javax.swing.DefaultComboBoxModel;
0028: import javax.swing.JComboBox;
0029: import javax.swing.JLabel;
0030:
0031: import javax.swing.JLayeredPane;
0032: import javax.swing.JMenuItem;
0033: import javax.swing.JPanel;
0034: import javax.swing.JPopupMenu;
0035: import javax.swing.JScrollPane;
0036: import javax.swing.JTextField;
0037: import net.xoetrope.editor.project.XEditorProject;
0038: import net.xoetrope.xui.XProject;
0039: import net.xoetrope.xui.XProjectManager;
0040:
0041: /**
0042: * A layer pane containing all survey components views.
0043: *
0044: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
0045: * the GNU Public License (GPL), please see license.txt for more details. If
0046: * you make commercial use of this software you must purchase a commercial
0047: * license from Xoetrope.</p>
0048: * <p> $Revision: 1.5 $</p>
0049: */
0050: public class XRulesEditorPane extends JLayeredPane implements
0051: MouseListener, MouseWheelListener {
0052:
0053: private static final int GRID_GAP = 10;
0054: private static final Color GRID_COLOR = new Color(220, 220, 220);
0055: public static double SCALE = 1;
0056: public static int WIDTH = 3000;
0057: public static int HEIGHT = 2000;
0058:
0059: protected Vector groupViews;
0060: protected XSurveyEditorFrame editorFrame;
0061: protected int gridGap;
0062: protected MouseAdapter changesSaver;
0063: protected JTextField questionIdField;
0064: protected JTextField questionTextField;
0065: protected JComboBox questionTypeField;
0066: protected XQuestionView editQuestionView;
0067: protected JTextField groupIdField;
0068: protected JTextField groupNameField;
0069: protected JComboBox groupTargetField;
0070: protected XGroupView editGroupView;
0071: protected JTextField ruleIdField;
0072: protected JTextField ruleNameField;
0073: protected JComboBox ruleTargetField;
0074: protected XRuleView editRuleView;
0075: protected XAnswersView answersView;
0076: protected XRulesEditorPanel editorPanel;
0077: protected XSurvey survey;
0078: protected JPopupMenu popupMenu;
0079:
0080: protected int scale;
0081: protected int width, height;
0082: protected XNotifier scaleNotifier;
0083:
0084: public XRulesEditorPane(XRulesEditorPanel ep) {
0085: super ();
0086: XProject project = XProjectManager.getCurrentProject();
0087: survey = (XSurvey) project.getObject("Survey");
0088: editorFrame = (XSurveyEditorFrame) project
0089: .getObject("EditorFrame");
0090: groupViews = new Vector();
0091: gridGap = GRID_GAP;
0092: width = WIDTH;
0093: height = HEIGHT;
0094: editorPanel = ep;
0095: init();
0096: setSize();
0097: createQuestionEditFields();
0098: createGroupEditFields();
0099: createRuleEditFields();
0100: createAnswersEditFields();
0101: createPopupMenu();
0102: changesSaver = new ChangesSaver();
0103: createObservers();
0104:
0105: XEditorProject editorProject = (project instanceof XEditorProject ? (XEditorProject) project
0106: : null);
0107: scaleNotifier = new XNotifier(editorProject, false);
0108:
0109: addMouseWheelListener(this );
0110: addMouseListener(this );
0111: addMouseListener(changesSaver);
0112: }
0113:
0114: public XNotifier getScaleNotifier() {
0115: return scaleNotifier;
0116: }
0117:
0118: protected void init() {
0119: setLayout(null);
0120: JPanel backgroundPanel = new JPanel();
0121: backgroundPanel.setBackground(Color.WHITE);
0122: repaint();
0123: }
0124:
0125: protected void setSize() {
0126: Dimension size = new Dimension(getWidth(), getHeight());
0127: setSize(size);
0128: setPreferredSize(size);
0129: setMinimumSize(size);
0130: }
0131:
0132: protected void createPopupMenu() {
0133: JMenuItem menuItem;
0134: popupMenu = new JPopupMenu();
0135: JLabel header = new JLabel(" General Options");
0136: header.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
0137: popupMenu.add(header);
0138: popupMenu.addSeparator();
0139:
0140: menuItem = new JMenuItem("add new group");
0141: popupMenu.add(menuItem);
0142: menuItem.addActionListener(new ActionListener() {
0143: public void actionPerformed(ActionEvent e) {
0144: survey.addNewGroup();
0145: }
0146: });
0147:
0148: popupMenu.addSeparator();
0149: menuItem = new JMenuItem("expand all groups");
0150: popupMenu.add(menuItem);
0151: menuItem.addActionListener(new ActionListener() {
0152: public void actionPerformed(ActionEvent e) {
0153: expandAllGroups(true);
0154: }
0155: });
0156:
0157: menuItem = new JMenuItem("collapse all groups");
0158: popupMenu.add(menuItem);
0159: menuItem.addActionListener(new ActionListener() {
0160: public void actionPerformed(ActionEvent e) {
0161: expandAllGroups(false);
0162: }
0163: });
0164:
0165: popupMenu.addSeparator();
0166: menuItem = new JMenuItem("expand all rules");
0167: popupMenu.add(menuItem);
0168: menuItem.addActionListener(new ActionListener() {
0169: public void actionPerformed(ActionEvent e) {
0170: expandAllRules(true);
0171: }
0172: });
0173:
0174: menuItem = new JMenuItem("collapse all rules ");
0175: popupMenu.add(menuItem);
0176: menuItem.addActionListener(new ActionListener() {
0177: public void actionPerformed(ActionEvent e) {
0178: expandAllRules(false);
0179: }
0180: });
0181: }
0182:
0183: public void expandAllGroups(boolean state) {
0184: Enumeration enumGroupViews = groupViews.elements();
0185: while (enumGroupViews.hasMoreElements()) {
0186: XGroupView groupView = (XGroupView) enumGroupViews
0187: .nextElement();
0188: groupView.setExpanded(state);
0189: }
0190: }
0191:
0192: public void expandAllRules(boolean state) {
0193: Enumeration enumGroupViews = groupViews.elements();
0194: while (enumGroupViews.hasMoreElements()) {
0195: XGroupView groupView = (XGroupView) enumGroupViews
0196: .nextElement();
0197: Vector[] groupRuleLines = groupView.getGroupRuleLines();
0198: for (int i = 0; i < groupRuleLines.length; i++) {
0199: Enumeration enumGroupRuleLines = groupRuleLines[i]
0200: .elements();
0201: while (enumGroupRuleLines.hasMoreElements()) {
0202: XGroupRuleLine groupRuleLine = (XGroupRuleLine) enumGroupRuleLines
0203: .nextElement();
0204: groupRuleLine.getRuleView().setExpanded(state);
0205: }
0206: }
0207: }
0208: }
0209:
0210: public int getWidth() {
0211: return (int) (SCALE * width);
0212: }
0213:
0214: public int getHeight() {
0215: return (int) (SCALE * height);
0216: }
0217:
0218: public MouseAdapter getChangesSaver() {
0219: return changesSaver;
0220: }
0221:
0222: protected void createQuestionEditFields() {
0223: editQuestionView = null;
0224: Dimension size = new Dimension(0, 0);
0225:
0226: questionIdField = new JTextField();
0227: questionIdField.setHorizontalAlignment(JTextField.CENTER);
0228: questionIdField.setBorder(null);
0229: add(questionIdField, JLayeredPane.DRAG_LAYER);
0230: questionIdField.setVisible(false);
0231: questionIdField.addKeyListener(new KeyAdapter() {
0232: public void keyPressed(KeyEvent e) {
0233: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
0234: disposeQuestionIdField(true);
0235: } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
0236: disposeQuestionIdField(false);
0237: }
0238: }
0239: });
0240:
0241: questionTextField = new JTextField();
0242: questionTextField.setBorder(null);
0243: add(questionTextField, JLayeredPane.DRAG_LAYER);
0244: questionTextField.setVisible(false);
0245: questionTextField.addKeyListener(new KeyAdapter() {
0246: public void keyPressed(KeyEvent e) {
0247: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
0248: disposeQuestionTextField(true);
0249: } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
0250: disposeQuestionTextField(false);
0251: }
0252: }
0253: });
0254:
0255: String[] types = { "E", "M", "F" };
0256: questionTypeField = new JComboBox(types);
0257: add(questionTypeField, JLayeredPane.DRAG_LAYER);
0258: questionTypeField.setVisible(false);
0259: questionTypeField.addActionListener(new ActionListener() {
0260: public void actionPerformed(ActionEvent e) {
0261: disposeQuestionTypeField(true);
0262: }
0263: });
0264: }
0265:
0266: protected void createGroupEditFields() {
0267: Dimension size = new Dimension(0, 0);
0268: editGroupView = null;
0269: groupIdField = new JTextField();
0270: groupIdField.setHorizontalAlignment(JTextField.CENTER);
0271: groupIdField.setBorder(null);
0272: groupIdField.setSize(size);
0273: groupIdField.setPreferredSize(size);
0274: add(groupIdField, JLayeredPane.DRAG_LAYER);
0275: groupIdField.setVisible(false);
0276: groupIdField.addKeyListener(new KeyAdapter() {
0277: public void keyPressed(KeyEvent e) {
0278: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
0279: disposeGroupIdField(true);
0280: } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
0281: disposeGroupIdField(false);
0282: }
0283: }
0284: });
0285:
0286: groupNameField = new JTextField();
0287: groupNameField.setHorizontalAlignment(JTextField.LEFT);
0288: groupNameField.setBorder(null);
0289: add(groupNameField, JLayeredPane.DRAG_LAYER);
0290: groupNameField.setVisible(false);
0291: groupNameField.addKeyListener(new KeyAdapter() {
0292: public void keyPressed(KeyEvent e) {
0293: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
0294: disposeGroupNameField(true);
0295: } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
0296: disposeGroupNameField(false);
0297: }
0298: }
0299: });
0300:
0301: groupTargetField = new JComboBox(survey.getTargetGroups());
0302: groupTargetField.setSize(size);
0303: groupTargetField.setPreferredSize(size);
0304: add(groupTargetField, JLayeredPane.DRAG_LAYER);
0305: groupTargetField.setVisible(false);
0306: groupTargetField.addActionListener(new ActionListener() {
0307: public void actionPerformed(ActionEvent e) {
0308: disposeGroupTargetField(true);
0309: }
0310: });
0311: }
0312:
0313: protected void createAnswersEditFields() {
0314: answersView = new XAnswersView(this );
0315: }
0316:
0317: protected void createRuleEditFields() {
0318: Dimension size = new Dimension(0, 0);
0319: editRuleView = null;
0320: ruleIdField = new JTextField();
0321: ruleIdField.setHorizontalAlignment(JTextField.CENTER);
0322: ruleIdField.setBorder(null);
0323: ruleIdField.setSize(size);
0324: ruleIdField.setPreferredSize(size);
0325: add(ruleIdField, JLayeredPane.DRAG_LAYER);
0326: ruleIdField.setVisible(false);
0327: ruleIdField.addKeyListener(new KeyAdapter() {
0328: public void keyPressed(KeyEvent e) {
0329: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
0330: disposeRuleIdField(true);
0331: } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
0332: disposeRuleIdField(false);
0333: }
0334: }
0335: });
0336:
0337: ruleNameField = new JTextField();
0338: ruleNameField.setHorizontalAlignment(JTextField.LEFT);
0339: ruleNameField.setBorder(null);
0340: ruleNameField.setSize(size);
0341: ruleNameField.setPreferredSize(size);
0342: add(ruleNameField, JLayeredPane.DRAG_LAYER);
0343: ruleNameField.setVisible(false);
0344: ruleNameField.addKeyListener(new KeyAdapter() {
0345: public void keyPressed(KeyEvent e) {
0346: if (e.getKeyCode() == KeyEvent.VK_ENTER) {
0347: disposeRuleNameField(true);
0348: } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
0349: disposeRuleNameField(false);
0350: }
0351: }
0352: });
0353:
0354: ruleTargetField = new JComboBox(survey.getTargetGroups());
0355: ruleTargetField.setSize(size);
0356: ruleTargetField.setPreferredSize(size);
0357: add(ruleTargetField, JLayeredPane.DRAG_LAYER);
0358: ruleTargetField.setVisible(false);
0359: ruleTargetField.addActionListener(new ActionListener() {
0360: public void actionPerformed(ActionEvent e) {
0361: disposeRuleTargetField(true);
0362: }
0363: });
0364: }
0365:
0366: public void showAnswersView(XConditionView conditionView) {
0367: SCALE = 1;
0368: scaleChanged();
0369: int x = conditionView.getLocation().x;
0370: int y = conditionView.getLocation().y;
0371: int w = conditionView.getWidth() - 1;
0372: int h = conditionView.getHeight() - 1;
0373: int aw = answersView.getWidth() - 1;
0374: int px = x + (w - aw) / 2 + 1;
0375: int py = y + h;
0376: answersView.setLocation(px, py);
0377: answersView.setSizeAndShape();
0378: answersView.setConditionView(conditionView);
0379: answersView.setVisible(true);
0380: answersView.moveToFront();
0381: }
0382:
0383: public void showQuestionTextField(XQuestionView questionView) {
0384: editQuestionView = questionView;
0385: int w = editQuestionView.getNameWidth() - 2
0386: * editQuestionView.getTextGap();
0387: int h = editQuestionView.getHeight() - 8;
0388: int x = editQuestionView.getLocation().x
0389: + editQuestionView.getIdWidth()
0390: + editQuestionView.getFieldGap()
0391: + editQuestionView.getTextGap();
0392: int y = editQuestionView.getLocation().y + 4;
0393: Dimension size = new Dimension(w, h);
0394: questionTextField.setSize(size);
0395: questionTextField.setPreferredSize(size);
0396: questionTextField.setMinimumSize(size);
0397: questionTextField.setFont(editQuestionView.getFont());
0398: questionTextField.setLocation(x, y);
0399: questionTextField.setBackground(editQuestionView
0400: .getBackgroundColor());
0401: questionTextField.setText(editQuestionView.getQuestion()
0402: .getText());
0403: questionTextField.setVisible(true);
0404: questionTextField.requestFocus();
0405: }
0406:
0407: public void showGroupNameField(XGroupView groupView) {
0408: editGroupView = groupView;
0409: XHeaderField header = editGroupView.getHeaderField();
0410: int w = editGroupView.getHeaderNameWidth() - 2
0411: * editGroupView.getHeaderTextGap();
0412: int h = editGroupView.getHeaderHeight() - 8;
0413: int x = header.getLocation().x
0414: + editGroupView.getHeaderIdWidth()
0415: + editGroupView.getHeaderFieldGap()
0416: + editGroupView.getHeaderTextGap();
0417: int y = header.getLocation().y + 4;
0418: Dimension size = new Dimension(w, h);
0419: groupNameField.setSize(size);
0420: groupNameField.setPreferredSize(size);
0421: groupNameField.setMinimumSize(size);
0422: groupNameField.setFont(editGroupView.getHeaderFont());
0423: groupNameField.setLocation(x, y);
0424: groupNameField.setBackground(editGroupView
0425: .getHeaderBackground());
0426: groupNameField.setText(editGroupView.getGroup().getName());
0427: groupNameField.setVisible(true);
0428: groupNameField.requestFocus();
0429: }
0430:
0431: public void showRuleNameField(XRuleView ruleView) {
0432: editRuleView = ruleView;
0433: XHeaderField header = editRuleView.getHeaderField();
0434: int w = editRuleView.getHeaderNameWidth() - 2
0435: * editRuleView.getHeaderTextGap();
0436: int h = editRuleView.getHeaderHeight() - 8;
0437: int x = header.getLocation().x
0438: + editRuleView.getHeaderIdWidth()
0439: + editRuleView.getHeaderFieldGap()
0440: + editRuleView.getHeaderTextGap();
0441: int y = header.getLocation().y + 4;
0442: Dimension size = new Dimension(w, h);
0443: ruleNameField.setSize(size);
0444: ruleNameField.setPreferredSize(size);
0445: ruleNameField.setMinimumSize(size);
0446: ruleNameField.setFont(editRuleView.getHeaderFont());
0447: ruleNameField.setLocation(x, y);
0448: ruleNameField.setBackground(editRuleView.getHeaderBackground());
0449: ruleNameField.setText(editRuleView.getRule().getName());
0450: ruleNameField.setVisible(true);
0451: ruleNameField.requestFocus();
0452: }
0453:
0454: public void showRuleIdField(XRuleView ruleView) {
0455: editRuleView = ruleView;
0456: XHeaderField header = editRuleView.getHeaderField();
0457: int w = editRuleView.getHeaderIdWidth() - 2
0458: * editRuleView.getHeaderTextGap();
0459: int h = editRuleView.getHeaderHeight() - 10;
0460: int x = header.getLocation().x
0461: + editRuleView.getHeaderTextGap();
0462: int y = header.getLocation().y + 5;
0463: Dimension size = new Dimension(w, h);
0464: ruleIdField.setSize(size);
0465: ruleIdField.setPreferredSize(size);
0466: ruleIdField.setMinimumSize(size);
0467: ruleIdField.setFont(editRuleView.getHeaderFont());
0468: ruleIdField.setLocation(x, y);
0469: ruleIdField.setBackground(editRuleView.getHeaderBackground());
0470: ruleIdField.setText(String.valueOf(editRuleView.getRule()
0471: .getId()));
0472: ruleIdField.setVisible(true);
0473: ruleIdField.requestFocus();
0474: }
0475:
0476: public void showGroupIdField(XGroupView groupView) {
0477: editGroupView = groupView;
0478: XHeaderField header = groupView.getHeaderField();
0479: int w = editGroupView.getHeaderIdWidth() - 2
0480: * editGroupView.getHeaderTextGap();
0481: int h = editGroupView.getHeaderHeight() - 10;
0482: int x = header.getLocation().x
0483: + editGroupView.getHeaderTextGap();
0484: int y = header.getLocation().y + 5;
0485: Dimension size = new Dimension(w, h);
0486: groupIdField.setSize(size);
0487: groupIdField.setPreferredSize(size);
0488: groupIdField.setMinimumSize(size);
0489: groupIdField.setFont(editGroupView.getHeaderFont());
0490: groupIdField.setLocation(x, y);
0491: groupIdField.setBackground(editGroupView.getHeaderBackground());
0492: groupIdField.setText(String.valueOf(editGroupView.getGroup()
0493: .getId()));
0494: groupIdField.setVisible(true);
0495: groupIdField.requestFocus();
0496: }
0497:
0498: public void showQuestionIdField(XQuestionView questionView) {
0499: editQuestionView = questionView;
0500: int w = editQuestionView.getIdWidth() - 2
0501: * editQuestionView.getTextGap();
0502: int h = editQuestionView.getHeight() - 10;
0503: int x = editQuestionView.getLocation().x
0504: + editQuestionView.getTextGap();
0505: int y = editQuestionView.getLocation().y + 5;
0506: Dimension size = new Dimension(w, h);
0507: questionIdField.setSize(size);
0508: questionIdField.setPreferredSize(size);
0509: questionIdField.setMinimumSize(size);
0510: questionIdField.setFont(editQuestionView.getFont());
0511: questionIdField.setLocation(x, y);
0512: questionIdField.setBackground(editQuestionView
0513: .getBackgroundColor());
0514: questionIdField.setText(String.valueOf(questionView
0515: .getQuestion().getId()));
0516: questionIdField.setVisible(true);
0517: questionIdField.requestFocus();
0518: }
0519:
0520: public void showQuestionTypeField(XQuestionView questionView) {
0521: editQuestionView = questionView;
0522: int idx = 0;
0523: switch (editQuestionView.getQuestion().getQuestionType()) {
0524: case Question.MUTUALLY_EXCLUSIVE:
0525: idx = 0;
0526: break;
0527: case Question.MULTIPLE_CHOICE:
0528: idx = 1;
0529: break;
0530: case Question.FREE_TEXT:
0531: idx = 2;
0532: break;
0533: }
0534: int x = editQuestionView.getLocation().x + 2
0535: * editQuestionView.getFieldGap()
0536: + editQuestionView.getIdWidth()
0537: + editQuestionView.getNameWidth();
0538: int y = (int) editQuestionView.getLocation().y;
0539: int w = editQuestionView.getTypeWidth();
0540: int h = editQuestionView.getHeight();
0541: questionTypeField.setLocation(x, y);
0542: Dimension size = new Dimension(w, h);
0543: questionTypeField.setSize(size);
0544: questionTypeField.setPreferredSize(size);
0545: questionTypeField.setMinimumSize(size);
0546: questionTypeField.setVisible(true);
0547: questionTypeField.requestFocus();
0548: questionTypeField.setBorder(null);
0549: questionTypeField.setFont(editQuestionView.getFont());
0550: questionTypeField.setPopupVisible(true);
0551: questionTypeField.setVisible(false);
0552: }
0553:
0554: public void showGroupTargetField(XGroupView groupView) {
0555: editGroupView = groupView;
0556: XHeaderField header = editGroupView.getHeaderField();
0557: int w = editGroupView.getHeaderTargetWidth() * 6;
0558: int h = editGroupView.getHeaderHeight();
0559:
0560: int x = header.getLocation().x + 2
0561: * editGroupView.getHeaderFieldGap()
0562: + editGroupView.getHeaderIdWidth()
0563: + editGroupView.getHeaderNameWidth()
0564: + (editGroupView.getHeaderTargetWidth() - w) / 2;
0565: int y = header.getLocation().y;
0566: Dimension size = new Dimension(w, h);
0567: groupTargetField.setFont(editGroupView.getHeaderFont());
0568: groupTargetField.setSize(size);
0569: groupTargetField.setPreferredSize(size);
0570: groupTargetField.setMinimumSize(size);
0571: groupTargetField.setLocation(x, y);
0572: groupTargetField.setModel(new DefaultComboBoxModel(survey
0573: .getTargetGroups()));
0574: groupTargetField.setVisible(true);
0575: groupTargetField.setLocation(x, y);
0576: groupTargetField.requestFocus();
0577: groupTargetField.setBorder(null);
0578: groupTargetField.setPopupVisible(true);
0579: groupTargetField.setVisible(false);
0580: }
0581:
0582: public void showRuleTargetField(XRuleView ruleView) {
0583: editRuleView = ruleView;
0584: XHeaderField header = editRuleView.getHeaderField();
0585: int w = editRuleView.getHeaderTargetWidth() * 6;
0586: int h = editRuleView.getHeaderHeight();
0587:
0588: int x = header.getLocation().x + 2
0589: * editRuleView.getHeaderFieldGap()
0590: + editRuleView.getHeaderIdWidth()
0591: + editRuleView.getHeaderNameWidth()
0592: + (editRuleView.getHeaderTargetWidth() - w) / 2;
0593: int y = header.getLocation().y;
0594: Dimension size = new Dimension(w, h);
0595: ruleTargetField.setFont(editRuleView.getHeaderFont());
0596: ruleTargetField.setSize(size);
0597: ruleTargetField.setPreferredSize(size);
0598: ruleTargetField.setMinimumSize(size);
0599: ruleTargetField.setLocation(x, y);
0600: ruleTargetField.setModel(new DefaultComboBoxModel(survey
0601: .getTargetGroups()));
0602: ruleTargetField.setVisible(true);
0603: ruleTargetField.setLocation(x, y);
0604: ruleTargetField.requestFocus();
0605: ruleTargetField.setBorder(null);
0606: ruleTargetField.setPopupVisible(true);
0607: ruleTargetField.setVisible(false);
0608: }
0609:
0610: public void disposeEditFields(boolean save) {
0611: disposeQuestionIdField(save);
0612: disposeQuestionTextField(save);
0613: disposeGroupIdField(save);
0614: disposeGroupNameField(save);
0615: disposeRuleIdField(save);
0616: disposeRuleNameField(save);
0617: }
0618:
0619: public void hideAnswersView() {
0620: if (answersView.isVisible())
0621: answersView.setVisible(false);
0622: repaint();
0623: }
0624:
0625: public void disposeGroupIdField(boolean save) {
0626: if (!groupIdField.isVisible())
0627: return;
0628:
0629: if (editGroupView != null) {
0630: if (save) {
0631: XQuestionGroup group = editGroupView.getGroup();
0632: int newId = group.getId();
0633: try {
0634: newId = new Integer(groupIdField.getText())
0635: .intValue();
0636: } catch (NumberFormatException e) {
0637: }
0638: ;
0639: group.setId(newId);
0640: }
0641: groupIdField.setVisible(false);
0642: editGroupView = null;
0643: repaint();
0644: }
0645: }
0646:
0647: public void disposeRuleIdField(boolean save) {
0648: if (!ruleIdField.isVisible())
0649: return;
0650:
0651: if (editRuleView != null) {
0652: if (save) {
0653: XRule rule = editRuleView.getRule();
0654: int newId = rule.getId();
0655: try {
0656: newId = new Integer(ruleIdField.getText())
0657: .intValue();
0658: } catch (NumberFormatException e) {
0659: }
0660: ;
0661: rule.setId(newId);
0662: }
0663: ruleIdField.setVisible(false);
0664: editRuleView = null;
0665: repaint();
0666: }
0667: }
0668:
0669: public void disposeRuleNameField(boolean save) {
0670: if (!ruleNameField.isVisible())
0671: return;
0672:
0673: if (editRuleView != null) {
0674: if (save) {
0675: XRule rule = editRuleView.getRule();
0676: rule.setName(ruleNameField.getText());
0677: }
0678: ruleNameField.setVisible(false);
0679: editRuleView = null;
0680: repaint();
0681: }
0682: }
0683:
0684: public void disposeQuestionIdField(boolean save) {
0685: if (!questionIdField.isVisible())
0686: return;
0687:
0688: if (editQuestionView != null) {
0689: if (save) {
0690: Question question = editQuestionView.getQuestion();
0691: int newId = question.getId();
0692: try {
0693: newId = new Integer(questionIdField.getText())
0694: .intValue();
0695: } catch (NumberFormatException e) {
0696: }
0697: ;
0698:
0699: if (newId != question.getId())
0700: XSurvey.setProjectModified(true);
0701: question.setId(newId);
0702: }
0703: questionIdField.setVisible(false);
0704: editQuestionView = null;
0705: repaint();
0706: }
0707: }
0708:
0709: public void disposeGroupNameField(boolean save) {
0710: if (!groupNameField.isVisible())
0711: return;
0712:
0713: if (editGroupView != null) {
0714: if (save) {
0715: XQuestionGroup group = editGroupView.getGroup();
0716: group.setName(groupNameField.getText());
0717: }
0718: groupNameField.setVisible(false);
0719: editGroupView = null;
0720: repaint();
0721: }
0722: }
0723:
0724: public void disposeQuestionTextField(boolean save) {
0725: if (!questionTextField.isVisible())
0726: return;
0727:
0728: if (editQuestionView != null) {
0729: if (save) {
0730: Question question = editQuestionView.getQuestion();
0731: String newText = questionTextField.getText();
0732: if (!newText.equals(question.getText()))
0733: XSurvey.setProjectModified(true);
0734: question.setText(newText);
0735: }
0736: questionTextField.setVisible(false);
0737: editQuestionView = null;
0738: repaint();
0739: }
0740: }
0741:
0742: public void disposeGroupTargetField(boolean save) {
0743: if (editGroupView != null) {
0744: if (save) {
0745: XQuestionGroup targetGroup = ((XTargetGroup) groupTargetField
0746: .getSelectedItem()).getGroup();
0747: XQuestionGroup group = editGroupView.getGroup();
0748: group.changeNextGroupId(targetGroup);
0749: }
0750: groupTargetField.setVisible(false);
0751: editGroupView = null;
0752: repaint();
0753: }
0754: }
0755:
0756: public void disposeRuleTargetField(boolean save) {
0757: if (editRuleView != null) {
0758: if (save) {
0759: XQuestionGroup targetGroup = ((XTargetGroup) ruleTargetField
0760: .getSelectedItem()).getGroup();
0761: XRule rule = editRuleView.getRule();
0762: rule.changeTarget(targetGroup);
0763: }
0764: ruleTargetField.setVisible(false);
0765: editRuleView = null;
0766: repaint();
0767: }
0768: }
0769:
0770: public void disposeQuestionTypeField(boolean save) {
0771: if (editQuestionView != null) {
0772: if (save) {
0773: Question question = editQuestionView.getQuestion();
0774: int idx = questionTypeField.getSelectedIndex();
0775: boolean state = false;
0776: switch (idx) {
0777: case 0:
0778: state = Question.MUTUALLY_EXCLUSIVE != question
0779: .getQuestionType();
0780: question
0781: .setQuestionType(Question.MUTUALLY_EXCLUSIVE);
0782: break;
0783: case 1:
0784: state = Question.MULTIPLE_CHOICE != question
0785: .getQuestionType();
0786: question.setQuestionType(Question.MULTIPLE_CHOICE);
0787: break;
0788: case 2:
0789: state = Question.FREE_TEXT != question
0790: .getQuestionType();
0791: question.setQuestionType(Question.FREE_TEXT);
0792: break;
0793: }
0794: if (state)
0795: XSurvey.setProjectModified(true);
0796: }
0797: questionTypeField.setVisible(false);
0798: editQuestionView = null;
0799: }
0800: }
0801:
0802: public int getGridGap() {
0803: return gridGap;
0804: }
0805:
0806: public void setGridGap(int gg) {
0807: gridGap = gg;
0808: }
0809:
0810: public Component getIntersectedView(Rectangle rect, Class c) {
0811: Component component = null;
0812: Component[] comps = getComponentsInLayer(JLayeredPane.PALETTE_LAYER);
0813: long max = 0;
0814: for (int i = 0; i < comps.length; i++) {
0815: if (!comps[i].isVisible() || !c.isInstance(comps[i]))
0816: continue;
0817: Rectangle2D intersectRect = rect
0818: .createIntersection(comps[i].getBounds());
0819: int width = (int) Math.max(intersectRect.getWidth(), 0);
0820: int height = (int) Math.max(intersectRect.getHeight(), 0);
0821: int val = width * height;
0822: if (val > max) {
0823: component = comps[i];
0824: max = val;
0825: }
0826: }
0827: return component;
0828: }
0829:
0830: public XGroupView getIntGroupView(Point p) {
0831: XGroupView groupView = null;
0832: int px = (int) p.getX();
0833: int py = (int) p.getY();
0834: Enumeration enumGroupViews = groupViews.elements();
0835: while (enumGroupViews.hasMoreElements() && groupView == null) {
0836: XGroupView gv = (XGroupView) enumGroupViews.nextElement();
0837: int x1 = (int) gv.getLocation().getX();
0838: int x2 = x1 + (int) (gv.getSize().getWidth());
0839: int y1 = (int) gv.getLocation().getY();
0840: int y2 = y1 + (int) gv.getSize().getHeight();
0841:
0842: if (px >= x1 && px <= x2 && py >= y1 && py <= y2)
0843: groupView = gv;
0844: }
0845: return groupView;
0846: }
0847:
0848: protected void paintGrid(Graphics2D g) {
0849: int w = (int) getSize().getWidth();
0850: int h = (int) getSize().getHeight();
0851:
0852: g.setColor(Color.WHITE);
0853: g.fillRect(0, 0, w, h);
0854:
0855: g.setColor(GRID_COLOR);
0856: int x = 0;
0857: int nvl = (int) (w / GRID_GAP);
0858: while (nvl-- > 0) {
0859: g.drawLine(x, 0, x, h);
0860: x += gridGap;
0861: }
0862: int y = 0;
0863: int nhl = (int) (h / GRID_GAP);
0864: while (nhl-- > 0) {
0865: g.drawLine(0, y, w, y);
0866: y += gridGap;
0867: }
0868: }
0869:
0870: public void paint(Graphics g) {
0871: Graphics2D g2d = (Graphics2D) g;
0872: paintGrid(g2d);
0873: super .paint(g2d);
0874: }
0875:
0876: public Vector getGroupViews() {
0877: return groupViews;
0878: }
0879:
0880: protected void addGroupView(XQuestionGroup group) {
0881: XGroupView groupView = new XGroupView(this , group);
0882: add(groupView, JLayeredPane.PALETTE_LAYER);
0883: groupView.setLocation(300, 300);
0884: groupViews.add(groupView);
0885: groupView.moveToFront();
0886: }
0887:
0888: protected void createObservers() {
0889: // add new group
0890: survey.getAddGroupNotifier().addObserver(new Observer() {
0891: public void update(Observable o, Object arg) {
0892: XQuestionGroup group = (XQuestionGroup) arg;
0893: XGroupView groupView = new XGroupView(
0894: XRulesEditorPane.this , group);
0895: groupView.createSourceLine();
0896: groupView.setLocation(300, 300);
0897: groupViews.add(groupView);
0898: groupView.moveToFront();
0899: repaint();
0900: }
0901: });
0902: // deleteGroup
0903: survey.getDeleteGroupNotifier().addObserver(new Observer() {
0904: public void update(Observable o, Object arg) {
0905: XQuestionGroup group = (XQuestionGroup) arg;
0906: XGroupView groupView = getGroupView(group.getId());
0907: if (groupView != null) {
0908: groupView.remove();
0909: groupViews.remove(groupView);
0910: }
0911: repaint();
0912: }
0913: });
0914: }
0915:
0916: public void removeAllGroups() {
0917: while (groupViews.size() > 0) {
0918: XGroupView groupView = (XGroupView) groupViews.get(0);
0919: groupView.remove();
0920: }
0921: }
0922:
0923: public void refreshComponents() {
0924: Enumeration enumGroupViews = groupViews.elements();
0925: while (enumGroupViews.hasMoreElements()) {
0926: XGroupView groupView = (XGroupView) enumGroupViews
0927: .nextElement();
0928: groupView.refresh();
0929:
0930: Vector[] groupRuleLines = groupView.getGroupRuleLines();
0931: if (groupRuleLines != null)
0932: for (int i = 0; i < groupRuleLines.length; i++) {
0933: Enumeration enumLines = groupRuleLines[i]
0934: .elements();
0935: while (enumLines.hasMoreElements()) {
0936: XGroupRuleLine line = (XGroupRuleLine) enumLines
0937: .nextElement();
0938: XRuleView ruleView = line.getRuleView();
0939: ruleView.refresh();
0940: }
0941: }
0942: }
0943: }
0944:
0945: public void refreshLines() {
0946: Enumeration enumGroupViews = groupViews.elements();
0947: while (enumGroupViews.hasMoreElements()) {
0948:
0949: XGroupView groupView = (XGroupView) enumGroupViews
0950: .nextElement();
0951: Vector[] groupRuleLines = groupView.getGroupRuleLines();
0952: if (groupRuleLines != null)
0953: for (int i = 0; i < groupRuleLines.length; i++) {
0954: Enumeration enumLines = groupRuleLines[i]
0955: .elements();
0956: while (enumLines.hasMoreElements()) {
0957: XGroupRuleLine line = (XGroupRuleLine) enumLines
0958: .nextElement();
0959: line.setLocation();
0960: }
0961: }
0962:
0963: XLine line = groupView.getSourceLine();
0964: if (line != null)
0965: line.setLocation();
0966:
0967: Enumeration enumLines = groupView.getTargetLines()
0968: .elements();
0969: while (enumLines.hasMoreElements()) {
0970: line = (XLine) enumLines.nextElement();
0971: line.setLocation();
0972: }
0973: }
0974: }
0975:
0976: public XGroupView getGroupView(int groupId) {
0977: XGroupView groupView = null;
0978: Enumeration enumGroups = groupViews.elements();
0979: while (enumGroups.hasMoreElements() && groupView == null) {
0980: XGroupView gv = (XGroupView) enumGroups.nextElement();
0981: if (gv.getGroup().getId() == groupId)
0982: groupView = gv;
0983: }
0984: return groupView;
0985: }
0986:
0987: public void createView() {
0988: int x = 100, y = 100;
0989: Enumeration enumGroups = survey.getGroups().elements();
0990: while (enumGroups.hasMoreElements()) {
0991: XQuestionGroup group = (XQuestionGroup) enumGroups
0992: .nextElement();
0993: XGroupView groupView = new XGroupView(this , group);
0994: groupView.setLocation(x, y);
0995: groupView.moveToFront();
0996: groupViews.add(groupView);
0997: x += 400;
0998: }
0999:
1000: Enumeration enumGroupViews = groupViews.elements();
1001: while (enumGroupViews.hasMoreElements()) {
1002: XGroupView groupView = (XGroupView) enumGroupViews
1003: .nextElement();
1004: groupView.createSourceLine();
1005: groupView.createRulesSourceLines();
1006: }
1007: }
1008:
1009: private Observer selectionObserver = (new Observer() {
1010: public void update(Observable o, Object arg) {
1011: if (arg instanceof XGroupView) {
1012: XGroupView groupView = (XGroupView) arg;
1013: editorPanel.showGroupView(groupView);
1014: } else if (arg instanceof XRuleView) {
1015: XRuleView ruleView = (XRuleView) arg;
1016: editorPanel.showRuleView(ruleView);
1017: } else if (arg instanceof XConditionView) {
1018: XConditionView conditionView = (XConditionView) arg;
1019: editorPanel.showConditionView(conditionView);
1020: } else if (arg instanceof XQuestionView) {
1021: XQuestionView questionView = (XQuestionView) arg;
1022: editorPanel.showQuestionView(questionView);
1023: }
1024: }
1025: });
1026:
1027: public Observer getSelectionObserver() {
1028: return selectionObserver;
1029: }
1030:
1031: public void selectAllComponents(boolean state) {
1032: Enumeration enumGroupViews = groupViews.elements();
1033: while (enumGroupViews.hasMoreElements()) {
1034: XGroupView groupView = (XGroupView) enumGroupViews
1035: .nextElement();
1036: groupView.selectRules(state);
1037: groupView.selectXComponents(state);
1038: groupView.isSelected(false);
1039: groupView.repaint();
1040: }
1041: }
1042:
1043: public void selectGroup(XQuestionGroup group) {
1044: Enumeration enumGroupViews = groupViews.elements();
1045: while (enumGroupViews.hasMoreElements()) {
1046: XGroupView groupView = (XGroupView) enumGroupViews
1047: .nextElement();
1048: groupView.selectRules(false);
1049: groupView.selectXComponents(false);
1050: if (groupView.getGroup().equals(group)) {
1051: groupView.isSelected(true);
1052: } else
1053: groupView.isSelected(false);
1054: groupView.repaint();
1055: }
1056: }
1057:
1058: public void selectQuestion(Question question) {
1059: if (question == null)
1060: return;
1061: XQuestionGroup group = (XQuestionGroup) question.getGroup();
1062: Enumeration enumGroupViews = groupViews.elements();
1063: while (enumGroupViews.hasMoreElements()) {
1064: XGroupView groupView = (XGroupView) enumGroupViews
1065: .nextElement();
1066: groupView.selectRules(false);
1067: if (groupView.getGroup().equals(group)) {
1068: groupView.isSelected(true);
1069: groupView.selectQuestion(question);
1070: } else {
1071: groupView.isSelected(false);
1072: groupView.selectXComponents(false);
1073: }
1074: groupView.repaint();
1075: }
1076: }
1077:
1078: public void selectCondition(XRule rule, Condition condition) {
1079: if (rule == null || condition == null)
1080: return;
1081: XQuestionGroup group = rule.getGroup();
1082: Enumeration enumGroupViews = groupViews.elements();
1083: while (enumGroupViews.hasMoreElements()) {
1084: XGroupView groupView = (XGroupView) enumGroupViews
1085: .nextElement();
1086: if (groupView.getGroup().equals(group))
1087: groupView.selectCondition(rule, condition);
1088: }
1089: }
1090:
1091: public void selectRule(XRule rule) {
1092: if (rule == null)
1093: return;
1094: XQuestionGroup group = rule.getGroup();
1095: Enumeration enumGroupViews = groupViews.elements();
1096: while (enumGroupViews.hasMoreElements()) {
1097: XGroupView groupView = (XGroupView) enumGroupViews
1098: .nextElement();
1099: groupView.isSelected(false);
1100: groupView.selectXComponents(false);
1101: if (groupView.getGroup().equals(group)) {
1102: groupView.selectRule(rule);
1103: }
1104: groupView.repaint();
1105: }
1106: }
1107:
1108: public void mouseClicked(MouseEvent e) {
1109: }
1110:
1111: protected void showPopupMenu(Point p) {
1112: if (popupMenu != null)
1113: popupMenu.show(this , p.x, p.y);
1114: }
1115:
1116: public void mousePressed(MouseEvent e) {
1117: selectAllComponents(false);
1118: if (e.getButton() == MouseEvent.BUTTON3)
1119: showPopupMenu(e.getPoint());
1120: }
1121:
1122: public void mouseReleased(MouseEvent e) {
1123: }
1124:
1125: public void mouseEntered(MouseEvent e) {
1126: }
1127:
1128: public void mouseExited(MouseEvent e) {
1129: }
1130:
1131: public Point getSelectedPoint() {
1132: Point point = null;
1133: Enumeration enumGroupViews = groupViews.elements();
1134: while (enumGroupViews.hasMoreElements() && point == null) {
1135: XGroupView groupView = (XGroupView) enumGroupViews
1136: .nextElement();
1137: if (groupView.isSelected()) {
1138: int x = groupView.getLocation().x
1139: + groupView.getWidth() / 2;
1140: int y = groupView.getLocation().y
1141: + groupView.getHeight() / 2;
1142: point = new Point(x, y);
1143: break;
1144: }
1145: Vector[] groupRuleLines = groupView.getGroupRuleLines();
1146: for (int i = 0; i < groupRuleLines.length && point == null; i++) {
1147: Enumeration enumLines = groupRuleLines[i].elements();
1148: while (enumLines.hasMoreElements() && point == null) {
1149: XGroupRuleLine line = (XGroupRuleLine) enumLines
1150: .nextElement();
1151: XRuleView ruleView = line.getRuleView();
1152: if (ruleView.isSelected()) {
1153: int x = ruleView.getLocation().x
1154: + ruleView.getWidth() / 2;
1155: int y = ruleView.getLocation().y
1156: + ruleView.getHeight() / 2;
1157: point = new Point(x, y);
1158: break;
1159: }
1160: }
1161: }
1162: }
1163:
1164: if (point == null)
1165: point = new Point(getWidth() / 2, getHeight() / 2);
1166: return point;
1167: }
1168:
1169: protected void centerPosition() {
1170: Point point = getSelectedPoint();
1171: if (point == null)
1172: return;
1173:
1174: JScrollPane scroll = editorPanel.getRulesEditorScroll();
1175: int x = point.x - scroll.getSize().width / 2;
1176: int y = point.y - scroll.getSize().height / 2;
1177: if (x < 0)
1178: x = 0;
1179: if (y < 0)
1180: y = 0;
1181: scroll.getViewport().setViewPosition(new Point(x, y));
1182: }
1183:
1184: protected boolean checkScale(double sc) {
1185: int w = (int) (sc * width);
1186: int h = (int) (sc * height);
1187: JScrollPane scroll = editorPanel.getRulesEditorScroll();
1188: int sw = scroll.getSize().width;
1189: int sh = scroll.getSize().height;
1190: return (w > sw && h > sh);
1191: }
1192:
1193: public void mouseWheelMoved(MouseWheelEvent e) {
1194: if (e.getUnitsToScroll() > 0) {
1195: if (checkScale(1.1 * SCALE)) {
1196: SCALE = 1.1 * SCALE;
1197: scaleChanged();
1198: }
1199: }
1200:
1201: if (e.getUnitsToScroll() < 0) {
1202: if (checkScale(0.9 * SCALE)) {
1203: SCALE = 0.9 * SCALE;
1204: scaleChanged();
1205: }
1206: }
1207: }
1208:
1209: protected void scaleChanged() {
1210: setSize();
1211: disposeEditFields(true);
1212: hideAnswersView();
1213: refreshComponents();
1214: refreshLines();
1215: centerPosition();
1216: repaint();
1217: }
1218:
1219: private class ChangesSaver extends MouseAdapter {
1220: public void mousePressed(MouseEvent e) {
1221: disposeEditFields(true);
1222: hideAnswersView();
1223: }
1224: }
1225:
1226: }
|