1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24 package org.wsmostudio.choreography.editors;
25
26 import java.util.LinkedList;
27 import java.util.List;
28
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.*;
35 import org.wsmo.common.exception.InvalidModelException;
36 import org.wsmo.service.rule.*;
37 import org.wsmo.wsml.ParserException;
38 import org.wsmostudio.choreography.editors.model.ChoreographyModel;
39 import org.wsmostudio.choreography.helper.Render;
40 import org.wsmostudio.ui.Utils;
41
42 public class UpdateRuleEditor extends RuleFormEditor {
43
44 private Text exprContainer;
45 private boolean dirty = false;
46
47 public UpdateRuleEditor(ChoreographyRule rule, ChoreographyModel model) {
48 super(rule, model);
49 }
50
51 public Composite initUI(Composite parent) {
52 Composite mainContainer = new Composite(parent, SWT.BORDER);
53 mainContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
54 mainContainer.setLayout(new GridLayout(1, false));
55 Composite header = new Composite(mainContainer, SWT.NONE);
56 header.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
57 header.setLayout(new GridLayout(2, false));
58
59 new Label(header, SWT.NONE).setImage(RulesManagementPanel.iconForRule(rule));
60 Label lab = new Label(header, SWT.NONE);
61 lab.setText(labelForRule());
62 lab.setForeground(
63 parent.getDisplay().getSystemColor(SWT.COLOR_BLUE));
64 lab.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
65
66 exprContainer = new Text(mainContainer,
67 SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
68 exprContainer.setText(prepareRuleContent((UpdateRule)rule));
69 exprContainer.addModifyListener(new ModifyListener() {
70 public void modifyText(ModifyEvent me) {
71 uiModel.setChanged();
72 dirty = true;
73 }
74 });
75 exprContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
76
77 return mainContainer;
78 }
79
80 private String prepareRuleContent(UpdateRule rule) {
81
82 List<CompoundFact> facts = new LinkedList<CompoundFact>();
83 facts.add(rule.getFact());
84 if (rule instanceof Update) {
85 Update updateRule = (Update)rule;
86 CompoundFact oldFact = updateRule.getOldFact();
87 if (oldFact != null
88 && false == oldFact.isEmpty()) {
89 facts.add(oldFact);
90 }
91 }
92 return Render.compoundFactToWSMLString(Utils.findTopContainer(uiModel), facts);
93 }
94
95 @Override
96 public void updateRule() throws ParserException, InvalidModelException {
97 if (dirty == false) {
98 return;
99 }
100 String newFactText = exprContainer.getText().trim();
101
102 UpdateRule theRule = (UpdateRule)getRule();
103 if (theRule.getFact() != null
104 && theRule.getFact().toString().equals(newFactText)) {
105 return;
106 }
107
108 List<CompoundFact> facts =
109 Render.wsmlStringToCompoundFact(Utils.findTopContainer(uiModel), newFactText);
110 if (facts == null || facts.size() == 0) {
111 throw new ParserException("No fact definitions found!", null);
112 }
113
114 CompoundFact newFact = facts.get(0);
115 theRule.setFact(newFact);
116 if (theRule instanceof Update
117 && facts.size() > 1) {
118 ((Update)theRule).setOldFact(facts.get(1));
119 }
120 }
121
122 public void dispose() {
123 exprContainer.dispose();
124 }
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156