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.repository.ui.actions;
26  
27  import java.util.*;
28  
29  import org.eclipse.core.runtime.*;
30  import org.eclipse.jface.action.*;
31  import org.eclipse.jface.resource.ImageDescriptor;
32  import org.osgi.framework.Bundle;
33  import org.wsmo.datastore.WsmoRepository;
34  import org.wsmostudio.runtime.LogManager;
35  import org.wsmostudio.runtime.extension.Initialisable;
36  
37  public class ActionsManager {
38  
39      public static final String ACTIONS_EXT_ID = "org.wsmostudio.repository.actions";
40      public static final String INSTANCE_REF = "RepoInstance";
41  
42      public static final String SELECTION_REF = "IRI";
43      public static final String SELECTION_TYPE_REF = "IRIType";
44  
45      public static final String ACTION_CONFIG_ELEMENT = "action";
46      public static final String CONF_ATTR_NAME = "name";
47      public static final String CONF_ATTR_HANDLER = "handler";
48      public static final String CONF_ATTR_R_TYPE = "repositoryType";
49      public static final String CONF_ATTR_ICON = "icon";
50      
51      public static final String ANY_REPOSITORY_TYPE = "";
52  
53      public static Comparator<IAction> comparator = new Comparator<IAction>() {
54          public int compare(IAction o1, IAction o2) {
55              return o1.getText().compareToIgnoreCase(o2.getText());
56          }
57      };
58  
59      private Map<String, IAction[]> actionsMap;
60      
61      public ActionsManager() {
62          actionsMap = new HashMap<String, IAction[]>();
63          loadActionsData();
64      }
65      
66      public void initActions(MenuManager menuMgr, 
67                              String repositoryType, 
68                              WsmoRepository repo) {
69          IAction[] actions = actionsMap.get(repositoryType);
70          Map<String, WsmoRepository> params = new HashMap<String, WsmoRepository>();
71          params.put(INSTANCE_REF, repo);
72          boolean flagPutMenuSeparator = false;
73          if (actions != null) {
74          	for (int i = 0; i < actions.length; i++) {
75          		if (actions[i] instanceof Initialisable) {
76          			try {
77          				((Initialisable)actions[i]).initialise(params);
78          			}
79          			catch(Exception ex) {
80          				LogManager.logError(ex);
81          			}
82          		}
83          		menuMgr.add(actions[i]);
84          		flagPutMenuSeparator = true;
85          	}
86          }
87  
88          // search for actions applicable for any type of repository
89          actions = actionsMap.get(ANY_REPOSITORY_TYPE);
90          if (actions != null) {
91          	for (int i = 0; i < actions.length; i++) {
92          		if (actions[i] instanceof Initialisable) {
93          			try {
94          				((Initialisable)actions[i]).initialise(params);
95          			}
96          			catch(Exception ex) {
97          				LogManager.logError(ex);
98          			}
99          		}
100         		menuMgr.add(actions[i]);
101         		flagPutMenuSeparator = true;
102         	}
103         }
104         
105         if (flagPutMenuSeparator) {
106         	menuMgr.add(new Separator());
107         }
108     }
109 
110 
111     @SuppressWarnings("unchecked")
112     private void loadActionsData() {
113         Map<String, Set<IAction>> actionsBuffer = new HashMap<String, Set<IAction>>();
114         
115         IExtensionPoint iExtPoint = Platform
116                                         .getExtensionRegistry()
117                                             .getExtensionPoint(ACTIONS_EXT_ID);
118         IExtension[] exts = iExtPoint.getExtensions();
119         for (int i = 0; i < exts.length; i++) {
120             IConfigurationElement[] confs = exts[i].getConfigurationElements(); 
121             for (int j = 0; j < confs.length; j++) {
122                 if (false == confs[j].getName().equals(ACTION_CONFIG_ELEMENT)) {
123                     continue;
124                 }
125 
126                 String name = confs[j].getAttribute(CONF_ATTR_NAME);
127                 if (name == null || name.trim().length() == 0) {
128                     continue;
129                 }
130                 String repoType = confs[j].getAttribute(CONF_ATTR_R_TYPE);
131                 if (repoType == null || repoType.trim().length() == 0) {
132                 	repoType = ANY_REPOSITORY_TYPE;
133                 }
134                 String managerClass = confs[j].getAttribute(CONF_ATTR_HANDLER);
135                 IAction actionHandler = null;
136                 try {
137                     if (managerClass != null 
138                             && managerClass.trim().length() > 0) {
139                         actionHandler = (IAction)confs[j].createExecutableExtension(
140                                     CONF_ATTR_HANDLER);
141                     }
142                 }
143                 catch(CoreException coreEx) {
144                     continue;
145                 }
146                 if (actionHandler == null) {
147                     continue;
148                 }
149                 String iconPath = confs[j].getAttribute(CONF_ATTR_ICON);
150                 if (iconPath != null) {
151                     Bundle bundle = Platform.getBundle(confs[j].getNamespaceIdentifier());
152                     actionHandler.setImageDescriptor(
153                             ImageDescriptor.createFromURL(bundle.getEntry(iconPath)));
154                 }
155 
156                 if (actionHandler instanceof Initialisable) {
157                     Map params = new HashMap();
158                     params.put(CONF_ATTR_NAME, name);
159                     try {
160                         ((Initialisable)actionHandler).initialise(params);
161                     }
162                     catch(Exception ex) {
163                         LogManager.logError(ex);
164                     }
165                 }
166                 addAction(repoType, actionHandler, actionsBuffer);
167             }
168         }
169         prepareData(actionsBuffer);
170     }
171     
172     private void addAction(String repoType, IAction action, Map<String, Set<IAction>> container) {
173         Set<IAction> actions = container.get(repoType); 
174         if (actions == null) {
175             actions = new HashSet<IAction>();
176             container.put(repoType, actions);
177         }
178         actions.add(action);
179     }
180     
181     private void prepareData(Map<String, Set<IAction>> actionsBuffer) {
182         Comparator<IAction> comparator = new Comparator<IAction>() {
183             public int compare(IAction o1, IAction o2) {
184                 return o1.getText().compareToIgnoreCase(o2.getText());
185             }
186         };
187         for(String repoType : actionsBuffer.keySet()) {
188             Set<IAction> actionsSet = actionsBuffer.get(repoType);
189             IAction[] actions = actionsSet.toArray(new IAction[actionsSet.size()]);
190             Arrays.sort(actions, comparator); 
191             actionsMap.put(repoType, actions);
192         }
193     }
194 }
195 
196 /*
197  * $Log$
198  * Revision 1.6  2006/09/12 08:48:46  alex_simov
199  * Icon property added to extension actions
200  *
201  * Revision 1.5  2006/09/08 14:16:59  alex_simov
202  * repository type specification is not required any more - underspecified
203  * extensions are treated as applicable to any repository type
204  *
205  * Revision 1.4  2006/08/02 12:32:23  alex_simov
206  * bugfix[1531709]: Context menus relied on right button mouse click instead of
207  * being registered as dedicated context menus for the corresponding UI
208  * controls
209  *
210  * Revision 1.3  2006/07/11 16:04:11  alex_simov
211  * no message
212  *
213  * Revision 1.2  2006/05/22 12:16:38  alex_simov
214  * new extension point added for custom actions on repository manager's
215  * content
216  *
217  * Revision 1.1  2006/02/23 15:57:11  alex_simov
218  * new extension point created, allowing adding custom actions for repositories
219  *  in the Repositories Explorer view
220  *
221  */
222