View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2011, 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-2008</p>
22   * <p>Company: Ontotext Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.bpmo.ui.editor;
26  
27  
28  import java.util.*;
29  
30  import org.eclipse.core.runtime.*;
31  import org.eclipse.gef.ui.actions.ActionRegistry;
32  import org.eclipse.jface.resource.ImageDescriptor;
33  import org.eclipse.ui.actions.RetargetAction;
34  import org.osgi.framework.Bundle;
35  import org.wsmostudio.bpmo.ui.actions.*;
36  import org.wsmostudio.runtime.LogManager;
37  
38  public class BpmoActionRegistry {
39  
40  	public static final String BPMO_ACTION_EXT_ID = "org.wsmostudio.bpmo.ui.action";
41  
42      private static String ACTION_CONFIG_ELEMENT = "action";
43      private static String CONF_ATTR_ID = "id";
44      private static String CONF_ATTR_LABEL = "label";
45      private static String CONF_ATTR_ICON = "icon";
46      private static String CONF_ATTR_DISABLED_ICON = "dIcon";
47      private static String CONF_ATTR_TOOLTIP = "tooltip";
48      private static String CONF_ATTR_CLASS = "factoryClass";
49      private static String CONF_ATTR_RETARGET_CLASS = "retargetActionClass";
50      
51      private List<RetargetAction> retargetActions = new LinkedList<RetargetAction>();
52      private List<ActionExtensionDescriptor> extensionActions = 
53          new LinkedList<ActionExtensionDescriptor>();
54      
55      private static BpmoActionRegistry _registry;
56  	
57      private BpmoActionRegistry() {
58  		readPluginsInfo();
59  	}
60      
61      public static BpmoActionRegistry getRegistry() {
62          if (_registry == null) {
63              _registry = new BpmoActionRegistry();
64          }
65          return _registry;
66      }
67      
68  	
69  	public List<RetargetAction> listRetargetActions() {
70  	    return Collections.unmodifiableList(retargetActions);
71  	}
72  	
73  	@SuppressWarnings("unchecked")
74      public ActionRegistry createEditorRegistry(BpmoEditor editor) {
75  
76  	    ActionRegistry resultRegistry = new ActionRegistry();
77  	    for (ActionExtensionDescriptor actionDescr : this.extensionActions) {
78  	        AbstractDiagramAction action = actionDescr.createAction(editor);
79  	        resultRegistry.registerAction(action);
80              editor.getSelectionActions().add(action.getId());
81  	    }
82  	    return resultRegistry;
83  	}
84  	
85  	@SuppressWarnings("unchecked")
86  	private void readPluginsInfo() {
87  		IExtensionPoint iExtPoint = Platform
88  		                                .getExtensionRegistry()
89  		                                .getExtensionPoint(
90  		                                		BPMO_ACTION_EXT_ID);
91  		IExtension[] exts = iExtPoint.getExtensions();
92  		for (int i = 0; i < exts.length; i++) {
93  			IConfigurationElement[] confs = exts[i].getConfigurationElements(); 
94  			for (int j = 0; j < confs.length; j++) {
95  				if (false == confs[j].getName().equals(ACTION_CONFIG_ELEMENT)) {
96  					continue;
97  				}
98  
99  				String actionLabel = confs[j].getAttribute(CONF_ATTR_LABEL);
100 				if (actionLabel == null || actionLabel.trim().length() == 0) {
101 					continue;
102 				}
103 				String actionID = confs[j].getAttribute(CONF_ATTR_ID);
104 				if (actionID == null || actionID.trim().length() == 0) {
105 					continue;
106 				}
107 				String className = confs[j].getAttribute(CONF_ATTR_CLASS);
108 				if (className == null || className.trim().length() == 0) {
109 					continue;
110 				}
111 				String iconPath = confs[j].getAttribute(CONF_ATTR_ICON);
112 				String dIconPath = confs[j].getAttribute(CONF_ATTR_DISABLED_ICON);
113 				String tooltip = confs[j].getAttribute(CONF_ATTR_TOOLTIP);
114 				
115 				String retargetClassName = confs[j].getAttribute(CONF_ATTR_RETARGET_CLASS);
116 				
117 				BpmoActionProvider factory = null;
118 				try {
119 					factory = (BpmoActionProvider)
120     				    confs[j].createExecutableExtension(CONF_ATTR_CLASS);
121 				}
122 				catch(CoreException coreEx) {
123 					continue;
124 				}
125 				if (factory == null) {
126 					continue;
127 				}
128 				
129 				ActionExtensionDescriptor actionDescriptor = 
130 				    new ActionExtensionDescriptor(actionID, factory);
131 				actionDescriptor.setLabel(actionLabel);
132                 if (tooltip != null 
133                         && tooltip.length() > 0) {
134                     actionDescriptor.setTooltipText(tooltip);
135                 }
136 				if (iconPath != null) {
137                     Bundle bundle = Platform.getBundle(confs[j].getNamespaceIdentifier());
138                     actionDescriptor.setIconDescriptor(
139                             ImageDescriptor.createFromURL(bundle.getEntry(iconPath)));
140 				}
141 				if (dIconPath != null) {
142                     Bundle bundle = Platform.getBundle(confs[j].getNamespaceIdentifier());
143                     actionDescriptor.setDisabledIconDescriptor(
144                             ImageDescriptor.createFromURL(bundle.getEntry(dIconPath)));
145 				}
146 				
147 				this.extensionActions.add(actionDescriptor);
148 				if (retargetClassName != null) {
149 					try {
150 						RetargetAction retargetActionHandler = (RetargetAction)
151 						    confs[j].createExecutableExtension(CONF_ATTR_RETARGET_CLASS);
152 						if (retargetActionHandler != null) {
153 							retargetActionHandler.setId(actionID);
154 							retargetActionHandler.setText(actionLabel);
155 							retargetActionHandler.setToolTipText(
156 							        actionDescriptor.getTooltipText());
157 							retargetActionHandler.setImageDescriptor(
158 							        actionDescriptor.getIconDescriptor());
159 							retargetActionHandler.setDisabledImageDescriptor(
160 							        actionDescriptor.getDisabledIconDescriptor());
161 							
162 							this.retargetActions.add(retargetActionHandler);
163 						}
164 					}
165 					catch(CoreException coreEx) {
166 						LogManager.logError(coreEx);
167 					}
168 				}
169 			}
170 		}
171 	}
172 }