[439] | 1 | // Module : $RCSfile: DefaultSequenceDetectionRule.java,v $ |
---|
| 2 | // Version : $Revision: 0.0 $ $Author: patrick $ $Date: 18.03.2012 $ |
---|
| 3 | // Project : TaskTreeCreator |
---|
| 4 | // Creation : 2012 by patrick |
---|
| 5 | // Copyright : Patrick Harms, 2012 |
---|
[557] | 6 | |
---|
[439] | 7 | package de.ugoe.cs.quest.tasktrees.temporalrelation; |
---|
| 8 | |
---|
[557] | 9 | import de.ugoe.cs.quest.eventcore.IEventTarget; |
---|
| 10 | import de.ugoe.cs.quest.eventcore.gui.MouseButtonDown; |
---|
| 11 | import de.ugoe.cs.quest.eventcore.gui.MouseButtonInteraction; |
---|
| 12 | import de.ugoe.cs.quest.eventcore.gui.MouseButtonUp; |
---|
| 13 | import de.ugoe.cs.quest.eventcore.gui.MouseClick; |
---|
| 14 | import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask; |
---|
| 15 | import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; |
---|
| 16 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
| 17 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 18 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
[439] | 19 | |
---|
| 20 | /** |
---|
| 21 | * TODO comment |
---|
| 22 | * |
---|
| 23 | * @version $Revision: $ $Date: 18.03.2012$ |
---|
| 24 | * @author 2012, last modified by $Author: patrick$ |
---|
| 25 | */ |
---|
[557] | 26 | public class DefaultMouseClickReductionRule implements TemporalRelationshipRule { |
---|
[439] | 27 | |
---|
[557] | 28 | /* |
---|
| 29 | * (non-Javadoc) |
---|
| 30 | * |
---|
| 31 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
| 32 | * TaskTreeBuilder, TaskTreeNodeFactory) |
---|
| 33 | */ |
---|
| 34 | @Override |
---|
| 35 | public RuleApplicationResult apply(ITaskTreeNode parent, |
---|
| 36 | ITaskTreeBuilder builder, |
---|
| 37 | ITaskTreeNodeFactory nodeFactory, |
---|
| 38 | boolean finalize) |
---|
[439] | 39 | { |
---|
[557] | 40 | if (!(parent instanceof ISequence)) { |
---|
| 41 | return null; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 45 | |
---|
| 46 | int index = 0; |
---|
| 47 | while (index < parent.getChildren().size() - 2) // -2 because we don't need to go to the end |
---|
| 48 | { |
---|
| 49 | if (mouseClickSequenceFound(parent.getChildren().get(index), |
---|
| 50 | parent.getChildren().get(index + 1), |
---|
| 51 | parent.getChildren().get(index + 2))) |
---|
| 52 | { |
---|
| 53 | builder.removeChild((ISequence) parent, index); |
---|
| 54 | builder.removeChild((ISequence) parent, index); |
---|
| 55 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 56 | return result; |
---|
| 57 | } |
---|
| 58 | else if |
---|
| 59 | ((parent.getChildren().get(index) instanceof ISequence) && |
---|
| 60 | (parent.getChildren().get(index).getChildren().size() == 2) && |
---|
| 61 | (mouseClickSequenceFound(parent.getChildren().get(index).getChildren().get(0), |
---|
| 62 | parent.getChildren().get(index).getChildren().get(1), |
---|
| 63 | parent.getChildren().get(index + 1)))) |
---|
| 64 | { |
---|
| 65 | builder.removeChild((ISequence) parent, index); |
---|
| 66 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 67 | return result; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | index++; |
---|
| 71 | } |
---|
| 72 | |
---|
[439] | 73 | return result; |
---|
| 74 | } |
---|
| 75 | |
---|
[557] | 76 | /** |
---|
[439] | 77 | * |
---|
| 78 | */ |
---|
[557] | 79 | private boolean mouseClickSequenceFound(ITaskTreeNode mouseButtonDown, |
---|
| 80 | ITaskTreeNode mouseButtonUp, |
---|
| 81 | ITaskTreeNode mouseClick) |
---|
[439] | 82 | { |
---|
[557] | 83 | // check the first in a row of three for validity |
---|
| 84 | if (!(mouseButtonDown instanceof IEventTask)) { |
---|
| 85 | return false; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | IEventTarget eventTarget = ((IEventTask) mouseButtonDown).getEventTarget(); |
---|
| 89 | |
---|
| 90 | if (!(((IEventTask) mouseButtonDown).getEventType() instanceof MouseButtonDown)) { |
---|
| 91 | return false; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | MouseButtonInteraction.Button button = |
---|
| 95 | ((MouseButtonDown) ((IEventTask) mouseButtonDown).getEventType()).getButton(); |
---|
| 96 | |
---|
| 97 | // check the second node for validity |
---|
| 98 | if (!(mouseButtonUp instanceof IEventTask)) { |
---|
| 99 | return false; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | if (!eventTarget.equals(((IEventTask) mouseButtonUp).getEventTarget())) { |
---|
| 103 | return false; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | if (!(((IEventTask) mouseButtonUp).getEventType() instanceof MouseButtonUp)) { |
---|
| 107 | return false; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | if (!button.equals(((MouseButtonUp) ((IEventTask) mouseButtonUp).getEventType()) |
---|
| 111 | .getButton())) |
---|
| 112 | { |
---|
| 113 | return false; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | // check the third node for validity |
---|
| 117 | if (!(mouseClick instanceof IEventTask)) { |
---|
| 118 | return false; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | if (!eventTarget.equals(((IEventTask) mouseClick).getEventTarget())) { |
---|
| 122 | return false; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if (!(((IEventTask) mouseClick).getEventType() instanceof MouseClick)) { |
---|
| 126 | return false; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | if (!button.equals(((MouseClick) ((IEventTask) mouseClick).getEventType()).getButton())) { |
---|
| 130 | return false; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | return true; |
---|
[439] | 134 | } |
---|
| 135 | |
---|
| 136 | } |
---|