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  
25  package org.wsmostudio.ui.views.navigator;
26  
27  import java.util.*;
28  
29  import org.eclipse.core.runtime.*;
30  import org.eclipse.jface.preference.IPreferenceStore;
31  import org.eclipse.jface.resource.ImageDescriptor;
32  import org.eclipse.swt.graphics.Image;
33  import org.osgi.framework.Bundle;
34  import org.wsmostudio.runtime.LogManager;
35  import org.wsmostudio.ui.WsmoUIPlugin;
36  
37  /***
38   * A manager class responsible for the extensions of WSMO Navigator's 
39   * <code>org.wsmostudio.ui.navigator_content</code> extension point. 
40   * It handles both filters and new entities.
41   * 
42   * @author not attributable
43   * @version $Revision: 1.5 $ $Date: 2006/01/09 12:51:14 $
44   */
45  
46  public class ContentExtensionManager {
47  
48      public static class FilterConfigWrapper {
49          public String name;
50          String className;
51          Filter manager;
52  
53          FilterConfigWrapper(String name, String className, Filter manager) {
54              this.name = name;
55              this.className = className;
56              this.manager = manager;
57          }
58          
59          // needed for <code>org.wsmostudio.preferences.NavigatorPage</code>
60          public String toString() {
61              return " " + name + " (" + className + ") ";
62          }
63      }
64      
65      public static final String CONTENT_EXT_ID = "org.wsmostudio.ui.navigator_content";
66  
67      public static final String ENTRY_CONFIG_ELEMENT = "entry";
68      public static final String FILTER_CONFIG_ELEMENT = "filter";
69  
70      public static final String CONF_ATTR_ICON = "icon";
71      public static final String CONF_ATTR_NAME = "name";
72      public static final String CONF_ATTR_CLASS = "class";
73      public static final String CONF_ATTR_MANAGER = "manager";
74      public static final String ACTIVE_FILTERS_PROPERTY_PREFIX = "$filter$";
75  
76      private Map<String, List<Filter>> activeFilters;
77      private Map<String, TreeProvider> treeProviders;
78      private Map<String, Image> icons;
79      
80      public ContentExtensionManager() {
81          activeFilters = new HashMap<String, List<Filter>>();
82          updateFilters();
83          initTreeProviders();
84      }
85      
86      public void updateFilters() {
87          activeFilters.clear();
88          List<FilterConfigWrapper> confData = loadFilterConfigData();
89          IPreferenceStore prefsStore = WsmoUIPlugin.getDefault().getPreferenceStore();
90          for(FilterConfigWrapper confItem : confData) {
91              if (false == prefsStore.getBoolean(ACTIVE_FILTERS_PROPERTY_PREFIX 
92                                                 + confItem.name)) {
93                  List<Filter> filterSet = activeFilters.get(confItem.className);
94                  if (filterSet == null) {
95                      filterSet = new LinkedList<Filter>();
96                      activeFilters.put(confItem.className, filterSet);
97                  }
98                  filterSet.add(confItem.manager);
99              }
100         }
101     }
102     
103     public static List<FilterConfigWrapper> loadFilterConfigData() {
104         IExtensionPoint iExtPoint = Platform
105                                         .getExtensionRegistry()
106                                         .getExtensionPoint(CONTENT_EXT_ID);
107         IExtension[] exts = iExtPoint.getExtensions();
108         List<FilterConfigWrapper> result = new LinkedList<FilterConfigWrapper>();
109 
110         for (int i = 0; i < exts.length; i++) {
111             IConfigurationElement[] confs = exts[i].getConfigurationElements(); 
112             for (int j = 0; j < confs.length; j++) {
113                 if (confs[j].getName().equals(FILTER_CONFIG_ELEMENT)) {
114                     String name = confs[j].getAttribute(CONF_ATTR_NAME);
115                     if (name == null || name.trim().length() == 0) {
116                         continue;
117                     }
118                     String className = confs[j].getAttribute(CONF_ATTR_CLASS);
119                     if (className == null || className.trim().length() == 0) {
120                         continue;
121                     }
122                     Filter filterManager = null;
123                     try {
124                         String managerClass = confs[j].getAttribute(CONF_ATTR_MANAGER);
125                         if (managerClass != null && managerClass.trim().length() > 0) {
126                             filterManager = (Filter)confs[j].createExecutableExtension(
127                                     CONF_ATTR_MANAGER);
128                         }
129                     }
130                     catch(CoreException coreEx) {
131                         continue;
132                     }
133                     result.add(new FilterConfigWrapper(name, className, filterManager));
134                 }
135             }
136         }
137         return result;
138     }
139     
140     public boolean isFiltered(Object entity) {
141         if (activeFilters.size() == 0) {
142             return false; // no filters at all
143         }
144         List<Filter> filters = activeFilters.get(entity.getClass().getName());
145         if (filters == null) {
146             Class[] ifaces = entity.getClass().getInterfaces();
147             for(int i = 0; i < ifaces.length; i++) {
148                 filters = activeFilters.get(ifaces[i].getName());
149                 if (filters != null) {
150                     break;
151                 }
152             }
153         }
154         if (filters == null) {
155             return false;// no filters
156         }
157         for(Filter fil : filters) {
158             if (fil == null || fil.isFiltered(entity)) {
159                 return true;
160             }
161         }
162         return false;
163     }
164     
165     public Image getImageFor(Object entity) {
166         if (icons.size() == 0) {
167             return null; // no custom icons at all
168         }
169         String key = getRelatedKeyForTable(entity, icons, true);
170         if (key != null) {
171             return icons.get(key);
172         }
173         return null;
174     }
175 
176     public String getLabelFor(Object entity) {
177         if (treeProviders.size() == 0) {
178             return null; // no tree providers
179         }
180         
181         String key = getRelatedKeyForTable(entity, treeProviders, true);
182         if (key == null) {
183             return null;
184         }
185         TreeProvider provider = treeProviders.get(key);
186         if (provider == null) {
187             return null;
188         }
189         return provider.labelFor(entity);
190     }
191     
192     public Object[] getChildrenFor(Object parentElement,
193                                    NavigatorContentProvider defaultProvider) {
194         if (treeProviders.size() == 0) { 
195             return defaultProvider.getDefaultChildren(parentElement);
196         }
197         
198         String key = getRelatedKeyForTable(parentElement, treeProviders, true);
199         if (key == null) {
200             return defaultProvider.getDefaultChildren(parentElement);
201         }
202         TreeProvider provider = treeProviders.get(key);
203         if (provider != null) {
204             return provider.getChildren(parentElement);
205         }
206         else {
207             return  null; // i.e. no content
208         }
209     }
210 
211     public boolean hasChildrenFor(Object parentElement,
212                                   NavigatorContentProvider defaultProvider) {
213         if (treeProviders.size() == 0) {
214             return defaultProvider.hasDefaultChildren(parentElement);
215         }
216         
217         String key = getRelatedKeyForTable(parentElement, treeProviders);
218         if (key == null) {
219             return defaultProvider.hasDefaultChildren(parentElement);
220         }
221         TreeProvider provider = treeProviders.get(key);
222         if (provider != null) {
223             return provider.hasChildren(parentElement);
224         }
225         return  false; // i.e. no content
226     }
227     
228     public static String getRelatedKeyForTable(Object target, Map table) {
229         return getRelatedKeyForTable(target, table, false);
230     }
231 
232     public static String getRelatedKeyForTable(Object target, Map table, boolean completeSearch) {
233         return getRelatedKeyForTable(target.getClass(), table, completeSearch);
234     }
235     
236     public static String getRelatedKeyForTable(Class targetClass, Map table, boolean completeSearch) {
237         if (table.containsKey(targetClass.getName())) {
238             return targetClass.getName();
239         }
240         Class[] ifaces = targetClass.getInterfaces();
241         for(int i = 0; i < ifaces.length; i++) {
242             if (table.containsKey(ifaces[i].getName())) {
243                 return ifaces[i].getName();
244             }
245         }
246         if (completeSearch == true) {
247             String resultKey = null;
248             if (targetClass.getSuperclass() != null
249                     && false == targetClass.getSuperclass().equals(Object.class)) {
250                 resultKey = getRelatedKeyForTable(targetClass.getSuperclass(), table, true);
251                 if (resultKey != null) {
252                     return resultKey;
253                 }
254             }
255             for(int i = 0; i < ifaces.length; i++) {
256                 resultKey = getRelatedKeyForTable(ifaces[i], table, true);
257                 if (resultKey != null) {
258                     return resultKey;
259                 }
260             }
261         }
262         return null;
263     }
264 
265     private void initTreeProviders() {
266         treeProviders = new HashMap<String, TreeProvider>();
267         icons = new HashMap<String, Image>();
268         IExtensionPoint iExtPoint = Platform
269                                         .getExtensionRegistry()
270                                             .getExtensionPoint(CONTENT_EXT_ID);
271         IExtension[] exts = iExtPoint.getExtensions();
272         for (int i = 0; i < exts.length; i++) {
273             IConfigurationElement[] confs = exts[i].getConfigurationElements(); 
274             for (int j = 0; j < confs.length; j++) {
275                 if (confs[j].getName().equals(ENTRY_CONFIG_ELEMENT)) {
276 
277                     String className = confs[j].getAttribute(CONF_ATTR_CLASS);
278                     if (className == null || className.trim().length() == 0) {
279                         continue;
280                     }
281                     String iconPath = confs[j].getAttribute(CONF_ATTR_ICON);
282                     if (iconPath != null 
283                             && iconPath.trim().length() > 0) {
284                         Bundle bundle = Platform.getBundle(exts[i].getNamespace());
285                         ImageDescriptor desc = ImageDescriptor.createFromURL(
286                                 bundle.getEntry(iconPath));
287                         Image icon = desc.createImage();
288                         if (icon != null) {
289                             icons.put(className, icon);
290                         }
291                         
292                     }
293                     TreeProvider treeManager = null;
294                     try {
295                         String managerClass = confs[j].getAttribute(CONF_ATTR_MANAGER);
296                         if (managerClass != null && managerClass.trim().length() > 0) {
297                             treeManager = (TreeProvider)confs[j].createExecutableExtension(
298                                     CONF_ATTR_MANAGER);
299                         }
300                     }
301                     catch(CoreException coreEx) {
302                         LogManager.logError(coreEx);
303                         continue;
304                     }
305                     treeProviders.put(className, treeManager); // null values are ok
306                 }
307             }
308         }
309     }
310 }
311 
312 /*
313  * $Log: ContentExtensionManager.java,v $
314  * Revision 1.5  2006/01/09 12:51:14  alex_simov
315  * Copyright message in header updated
316  *
317  * Revision 1.4  2005/11/16 16:29:41  alex_simov
318  * TreeProviders work with Object instead of Entity
319  *
320  * Revision 1.3  2005/11/09 16:15:21  alex_simov
321  * update
322  *
323  * Revision 1.2  2005/11/02 08:48:16  alex_simov
324  * org.wsmostudio.ui.navigator_content ext-point enriched with actions for objects
325  *
326  * Revision 1.1  2005/10/14 13:25:18  alex_simov
327  * WSMO Navigator's extension point creation
328  *
329  * 
330  */