View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2007, 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-2007</p>
22   * <p>Company: Ontotext Lab. / SIRMA </p>
23   */
24  
25  package org.semanticgov.ui.editor;
26  
27  import java.util.*;
28  import java.util.List;
29  
30  import org.eclipse.jface.viewers.*;
31  import org.eclipse.swt.SWT;
32  import org.eclipse.swt.widgets.*;
33  import org.omwg.ontology.*;
34  import org.semanticgov.ui.Constants;
35  import org.wsmo.common.IRI;
36  import org.wsmo.common.Identifier;
37  import org.wsmo.factory.WsmoFactory;
38  import org.wsmostudio.runtime.WSMORuntime;
39  import org.wsmostudio.runtime.WsmoImageRegistry;
40  import org.wsmostudio.ui.GUIHelper;
41  import org.wsmostudio.ui.Utils;
42  import org.wsmostudio.ui.views.navigator.WSMOLabelProvider;
43  
44  public class UIUtils {
45      
46      private static WsmoFactory fact = WSMORuntime.getRuntime().getWsmoFactory();
47      
48      public static IStructuredContentProvider contentProvider = 
49          new IStructuredContentProvider() {
50  
51              @SuppressWarnings("unchecked")
52  			public Object[] getElements(Object inputElement) {
53                  if (inputElement == null) {
54                      return null;
55                  }
56                  if (inputElement instanceof List) {
57                      return ((List)inputElement).toArray();
58                  }
59                  if (inputElement instanceof Set) {
60                      return ((Set)inputElement).toArray();
61                  }
62                  if (inputElement instanceof Object[]) {
63                      return (Object[])inputElement;
64                  }
65                  return null;
66              }
67              public void dispose() {
68              }
69              public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
70              }
71      };
72      
73      public static ComboViewer createInstancesCombo(Composite parent) {
74          ComboViewer result = new ComboViewer(parent, SWT.BORDER | SWT.READ_ONLY);
75          result.setLabelProvider(new WSMOLabelProvider() {
76              public String getText(Object element) {
77                  if (element instanceof IRI) {
78                      return Utils.getEntityLocalName(element.toString());
79                  }
80                  return super.getText(element);
81              }
82          });
83          result.setContentProvider(contentProvider);
84  
85          List<String> inputs = new LinkedList<String>();
86          inputs.add(" ");
87          result.setInput(inputs);
88          return result;
89      }
90      
91      @SuppressWarnings("unchecked")
92      public static void updateCombo(
93              ComboViewer viewer, Set<IRI> entries, IRI selection) {
94  
95          List<Object> oldData = (List<Object>)viewer.getInput();
96          
97          for(Iterator it = oldData.listIterator(1); it.hasNext();) {
98              Object entry = it.next();
99              if (false == entries.contains(entry)) {
100                 it.remove();
101             }
102             else {
103                 entries.remove(entry);
104             }
105         }
106         if(entries.size() > 0) {
107             for(IRI iri : entries) {
108                 oldData.add(iri);
109             }
110         }
111         Object newSelection = (selection != null && oldData.contains(selection))? selection : "";
112         viewer.setInput(oldData);
113         viewer.setSelection(new StructuredSelection(newSelection));
114     }
115 
116     public static void updateTable(Table table, Set<IRI> entries) {
117         
118         TableItem[] items = table.getItems();
119         Set<IRI> toDo = new HashSet<IRI>(entries);
120         for(int i = 0; i < items.length; i++) {
121             if (toDo.contains(items[i].getData())) {
122                 toDo.remove(items[i].getData());
123             }
124             else {
125                 items[i].dispose();
126             }
127         }
128         for(IRI iri : toDo) {
129             TableItem newItem = GUIHelper.createTableItem(table, 
130                         Utils.getEntityLocalName(iri.toString()), 
131                         WsmoImageRegistry.INSTANCE_ICON);
132             newItem.setData(iri);
133         }
134     }
135 
136     @SuppressWarnings("unchecked")
137     public static void updateAxiomsTable(Table table, Set axioms) {
138         
139         TableItem[] items = table.getItems();
140         Set toDo = new HashSet(axioms);
141         for(int i = 0; i < items.length; i++) {
142             if (toDo.contains(items[i].getData())) {
143                 toDo.remove(items[i].getData());
144             }
145             else {
146                 items[i].dispose();
147             }
148         }
149         for(Iterator it = toDo.iterator(); it.hasNext(); ) {
150             Axiom ax = (Axiom)it.next();
151             GUIHelper.createTableItem(table, ax, ax.getOntology());
152         }
153     }
154 
155     public static Set<IRI> getInstances(Identifier conceptID, Set<Ontology> importedOntos) {
156         Set<IRI> result = new HashSet<IRI>();
157         for(Ontology onto : importedOntos) {
158             result.addAll(getInstances(conceptID, onto));
159         }
160         return result;
161     }
162 
163     public static Set<IRI> getInstances(Identifier conceptID, Ontology onto) {
164         return getInstances((conceptID != null) 
165                     ? fact.getConcept(conceptID) 
166                     : null, onto);
167     }
168     public static Set<IRI> getInstances(Concept concept, Ontology onto) {
169         Set<IRI> result = new HashSet<IRI>();
170         if (onto == null) {
171             return result;
172         }
173         for(Instance inst : onto.listInstances()) {
174             if (false == inst.getIdentifier() instanceof IRI) {
175                 continue;
176             }
177             if (concept == null) {
178                 result.add((IRI)inst.getIdentifier());
179             }
180             else if (inst.listConcepts().contains(concept)) {
181                 result.add((IRI)inst.getIdentifier());
182             }
183         }
184         for (Concept subConcept : concept.listSubConcepts()) {
185             result.addAll(getInstances(subConcept, onto));
186         }
187         return result;
188     }
189 
190     public static Set<IRI> getSubConcepts(Identifier conceptID, Set<Ontology> importedOntos) {
191         Set<IRI> result = new HashSet<IRI>();
192         for(Ontology onto :  importedOntos) {
193         	result.addAll(getSubConcepts(conceptID, onto));
194         }
195     	return result;
196     }
197     
198     public static Set<IRI> getSubConcepts(Identifier conceptID, Ontology onto) {
199         Set<IRI> result = new HashSet<IRI>();
200         if (onto == null) {
201             return result;
202         }
203         for(Concept type : onto.listConcepts()) {
204             if (false == type.getIdentifier() instanceof IRI) {
205                 continue;
206             }
207             if (conceptID == null) {
208                 result.add((IRI)type.getIdentifier());
209             }
210             else {
211                 for(Concept concept : type.listSuperConcepts()) {
212                     if (conceptID.equals(concept.getIdentifier())) {
213                         result.add((IRI)type.getIdentifier());
214                         break;
215                     }
216                 }
217             }
218         }
219         return result;
220     }
221 
222     
223     public static Set<IRI> listSubdomainEntries(Identifier domainID, Set<Ontology> importedOntos) {
224 
225         if (domainID == null) {
226             return new HashSet<IRI>();
227         }
228         Instance domainInstance = fact.getInstance(domainID);
229         Set<IRI> result = new HashSet<IRI>();
230         Set<IRI> allSubdomainInstances = getInstances(Constants.PUBLIC_SERVICE_SUB_DOMAIN, importedOntos);
231         for (IRI subDomainInstID : allSubdomainInstances) {
232             Instance subDomainInst = fact.getInstance(subDomainInstID);
233             if (subDomainInst.listAttributeValues(Constants.PA_ATTR_IS_SUBDOMAIN_OF)
234                     .contains(domainInstance)) {
235                 result.add(subDomainInstID);
236             }
237         }
238         return result;
239     }
240 
241     public static boolean isSubconceptOf(Concept subConcept, Concept superConcept) {
242         if (subConcept.listSuperConcepts().contains(superConcept)) {
243             return true;
244         }
245         for(Concept concept : subConcept.listSuperConcepts()) {
246             if (isSubconceptOf(concept, superConcept)) {
247                 return true;
248             }
249         }
250         return false;
251     }
252 
253 
254 }
255 
256 /*
257  * $Log: UIUtils.java,v $
258  * Revision 1.9  2007/07/19 12:50:20  alex_simov
259  * refactoring
260  *
261  * Revision 1.8  2007/04/17 14:24:05  alex_simov
262  * migration to the latest wsmo4j (java 5)
263  *
264  * Revision 1.7  2007/02/21 14:39:02  alex_simov
265  * instance ontology specification removed from the wizard page
266  *
267  * Revision 1.5  2007/01/24 15:51:09  alex_simov
268  * no message
269  *
270  * Revision 1.4  2007/01/23 17:49:24  alex_simov
271  * no message
272  *
273  * Revision 1.3  2007/01/23 16:23:34  alex_simov
274  * no message
275  *
276  * Revision 1.2  2007/01/22 17:25:35  alex_simov
277  * no message
278  *
279  * Revision 1.1  2007/01/12 16:30:28  alex_simov
280  * PA Service editor initial version
281  *
282  */