View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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; // no update is needed
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; // no change
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  * $Log$
129  * Revision 1.10  2006/11/22 14:11:07  alex_simov
130  * Code refactoring: the plug-in is synchronized with the latest wsmo4j changes
131  *
132  * Revision 1.9  2006/03/27 09:52:43  alex_simov
133  * CompoundFact <-> String conversion corrected
134  *
135  * Revision 1.8  2006/02/16 13:41:27  alex_simov
136  * bugfix [1426200]: namespace definitions were not used in choreography rule
137  *  editors
138  *
139  * Revision 1.7  2006/02/14 15:09:38  alex_simov
140  * chor API minor changes adoption
141  *
142  * Revision 1.6  2006/01/09 12:51:11  alex_simov
143  * Copyright message in header updated
144  *
145  * Revision 1.5  2005/12/21 15:25:35  alex_simov
146  * choreography managment implementation completed
147  *
148  * Revision 1.4  2005/12/19 15:35:54  alex_simov
149  * update
150  *
151  * Revision 1.3  2005/11/24 13:48:16  alex_simov
152  * logical expression field contains now wrapped text
153  * (instead of horizontal scroll bar)
154  * [bug issue 1365441]
155  *
156  */