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;
26  
27  import java.util.*;
28  
29  import org.eclipse.jface.dialogs.MessageDialog;
30  import org.eclipse.jface.resource.JFaceResources;
31  import org.eclipse.swt.graphics.Image;
32  import org.eclipse.swt.widgets.Display;
33  import org.eclipse.ui.*;
34  import org.wsmo.datastore.WsmoRepository;
35  import org.wsmostudio.repository.ui.RepositoryEditor;
36  import org.wsmostudio.repository.ui.actions.ActionsManager;
37  import org.wsmostudio.repository.ui.manageractions.ExtensionActionsManager;
38  import org.wsmostudio.runtime.*;
39  import org.wsmostudio.runtime.extension.Configurator;
40  
41  
42  public class Registry {
43  
44      private static Registry _instance = new Registry();
45      
46      private ActionsManager actionsManager; // actions in Repository Explorer
47      private ExtensionActionsManager actionsContentManager; // actions in repository manager
48      
49      private Map<WsmoRepository, Configurator> repoStore;
50      private Map<String, Image> iconsMap;
51      private Set<WsmoRepository> configuredSet;
52      
53      public static Registry getInstance() {
54          return _instance;
55      }
56      
57      public Registry() {
58          iconsMap = new HashMap<String, Image>();
59          configuredSet = new HashSet<WsmoRepository>();
60          actionsManager = new ActionsManager();
61          actionsContentManager = new ExtensionActionsManager();
62          repoStore = ExtensionManager.loadRepositories();
63  
64      }
65      
66      public void addRepository(String repoName, String repoType) {
67          try {
68              WsmoRepository repInstance = ExtensionManager.createRepository(repoName, repoType);
69              Configurator conf = ExtensionManager.getConfiguratorFor(repoType);
70              this.repoStore.put(repInstance, conf);
71          }
72          catch(Exception ex) {
73              MessageDialog.openError(Display.getCurrent().getActiveShell(),
74                      "Plugin Error",
75                      ex.getMessage());
76          }
77      }
78      
79      public void removeRepository(WsmoRepository repository) {
80          RepositoryPlugin.getDefault()
81              .getPluginPreferences()
82              .setToDefault(ExtensionManager.REP_PROP_PREFIX
83                      + repository.getDescription());
84  
85          repoStore.remove(repository);
86      }
87      
88      public static void reloadRepository(WsmoRepository repo) {
89          IWorkbenchPage page = 
90              PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
91          if (page == null) {
92              return;
93          }
94          IEditorReference[] refs = page.getEditorReferences();
95          if (refs == null) {
96              return;
97          }
98          for(int i = 0; i < refs.length; i++) {
99              try {
100                 if (repo.equals(
101                         refs[i].getEditorInput().getAdapter(WsmoRepository.class))) {
102                     IEditorPart editor = refs[i].getEditor(true);
103                     if (editor instanceof RepositoryEditor) {
104                         ((RepositoryEditor)editor).reloadRepositoryContent();
105                         return;
106                     }
107                 }
108             }
109             catch(PartInitException pie) {
110                 continue;
111             }
112         }
113     }
114     
115     // true - successful ; false - cancelled
116     public boolean configureRepository(WsmoRepository repository, boolean forceConfig) {
117         if (configuredSet.contains(repository) 
118                 && false == forceConfig) {
119             return true; // the repository is already configured and no reconfiguration is needed
120         }
121         try {
122             Configurator conf = repoStore.get(repository);
123             if (conf == null) {
124                 throw new Exception("Configurator for repository '"
125                         + repository.getDescription()
126                         + "' not found!");
127             }
128             boolean status = ExtensionManager.configureRepository(repository, conf, forceConfig);
129             if (status == false) {
130                 return false; // configuration procedure cancelled
131             }
132         }
133         catch(Exception ex) {
134             MessageDialog.openError(Display.getCurrent().getActiveShell(), 
135                     "Repository Error", 
136                     "Unable to configure repository '"
137                     + repository.getDescription()
138                     + "':\n" 
139                     + ex.getMessage());
140             LogManager.logError("Repository Error", ex);
141             return false;
142         }
143         configuredSet.add(repository);
144         return true;
145     }
146 
147     public Map<WsmoRepository, Configurator> getRepositoriesData() {
148         return this.repoStore;
149     }
150     
151     public ActionsManager getActionsManager() {
152         return this.actionsManager;
153     }
154 
155     public ExtensionActionsManager getContentActionsManager() {
156         return this.actionsContentManager;
157     }
158 
159     public Image iconForRepository(String type) {
160         if (type == null) {
161             return null;
162         }
163         if (iconsMap.containsKey(type)) {
164             return iconsMap.get(type);
165         }
166         Image newIcon = ExtensionManager.loadImageForType(type);
167         if (newIcon != null) {
168             iconsMap.put(type, newIcon);
169             return newIcon;
170         }
171         newIcon = JFaceResources.getImageRegistry()
172                 .get(WsmoImageRegistry.REPOSITORY_ICON);
173         if (newIcon == null) {
174             WSMORuntime.forceEarlyInit();
175         }
176         return JFaceResources.getImageRegistry()
177                 .get(WsmoImageRegistry.REPOSITORY_ICON);
178     }
179 
180 }
181 
182 /*
183  * $Log$
184  * Revision 1.3  2006/05/22 12:16:38  alex_simov
185  * new extension point added for custom actions on repository manager's
186  * content
187  *
188  * Revision 1.2  2006/03/07 13:51:49  alex_simov
189  * Support for repository content reload added
190  *
191  * Revision 1.1  2006/02/23 15:56:00  alex_simov
192  * Repositories Explorer split into repository management part and UI part
193  *
194  */