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;
26  
27  import java.util.List;
28  
29  import org.eclipse.core.runtime.Preferences;
30  import org.eclipse.jface.action.*;
31  import org.eclipse.jface.dialogs.MessageDialog;
32  import org.eclipse.jface.viewers.*;
33  import org.eclipse.swt.SWT;
34  import org.eclipse.swt.events.*;
35  import org.eclipse.swt.graphics.Image;
36  import org.eclipse.swt.widgets.*;
37  import org.eclipse.ui.*;
38  import org.eclipse.ui.part.ViewPart;
39  import org.wsmo.datastore.WsmoRepository;
40  import org.wsmostudio.repository.*;
41  import org.wsmostudio.runtime.LogManager;
42  import org.wsmostudio.ui.GUIHelper;
43  
44  /***
45   * This class represents the main viewer for all instanciated repositories 
46   * (extensions of point <code>org.wsmostudio.repository.Repository</code>)
47   * in the studio. It is a part of the Repository perspective.
48   * The main supported functionality is creation, opening and removal of 
49   * repository instances. Opening here means showing the content of a repository
50   * in a specialized <i>Repository Editor</i>. 
51   *
52   * @author not attributable
53   * @version $Revision: 1224 $ $Date: 2007-07-19 15:54:57 +0300 $
54   */
55  
56  public class RepositoriesExplorer extends ViewPart {
57  
58      public static final String VIEW_ID = "org.wsmostudio.repository.ui.explorer";
59      
60      private TableViewer viewer;
61      
62      public RepositoriesExplorer() {
63          super();
64      }
65      
66      public void createPartControl(Composite parent) {
67          
68          viewer = new TableViewer(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
69          createContentProvider();
70          createLabelProvider();
71          viewer.setInput(Registry.getInstance().getRepositoriesData());
72          viewer.getTable().addMouseListener(new MouseAdapter() {
73              public void mouseDoubleClick(MouseEvent e) {
74                  processDoubleClick(e);
75              }
76          });
77          createContextMenu();
78      }
79  
80      private void createContextMenu() {
81      	final MenuManager menuMgr = new MenuManager();
82      	menuMgr.setRemoveAllWhenShown(true);
83      	menuMgr.addMenuListener(new IMenuListener() {
84      		public void menuAboutToShow(IMenuManager mgr) {
85      			fillContextMenu(menuMgr);
86      		}
87      	});
88      	viewer.getTable().setMenu(menuMgr.createContextMenu(viewer.getTable()));
89      }
90  
91      private void fillContextMenu(MenuManager menuMgr) {
92  
93      	if (false == GUIHelper.containsCursor(viewer.getTable())) {
94      		return;
95      	}
96      	menuMgr.add(new Action("New Repository") {
97      		public void run() {
98      			doCreateRepository();
99      		}
100     	});
101     	final TableItem[] sel = viewer.getTable().getSelection();
102     	if (sel != null 
103     			&& sel.length > 0 
104     			&& GUIHelper.containsCursor(sel[0].getBounds(0), viewer.getTable())) {
105     		menuMgr.add(new Separator());
106     		menuMgr.add(new Action("Open Repository") {
107     			public void run() {
108     				doOpenRepository((WsmoRepository)sel[0].getData());
109     			}
110     		});
111     		menuMgr.add(new Separator());
112     		menuMgr.add(new Action("Configure Repository") {
113     			public void run() {
114     				doConfigureRepository((WsmoRepository)sel[0].getData());
115     			}
116     		});
117     		menuMgr.add(new Separator());
118     		
119             String repoType = ExtensionManager.getRepositoryType((WsmoRepository)sel[0].getData());
120             if (repoType != null) {
121                 Registry.getInstance()
122                     .getActionsManager()
123                     .initActions(menuMgr, repoType, (WsmoRepository)sel[0].getData());
124             }
125 
126             menuMgr.add(new Action("Remove Repository") {
127     			public void run() {
128     	            Registry.getInstance().removeRepository(
129     	            		(WsmoRepository)sel[0].getData());
130     	            viewer.refresh(true);
131     			}
132     		});
133     	}
134     }
135     public void setFocus() {
136         viewer.getControl().setFocus();
137     }
138     
139     public void processDoubleClick(MouseEvent e) {
140         ISelection selection = viewer.getSelection();
141         if (false == selection instanceof IStructuredSelection) {
142             return; 
143         }
144         Object target = ((IStructuredSelection)selection).getFirstElement();
145         if (target != null 
146                 && target instanceof WsmoRepository) {
147             doOpenRepository((WsmoRepository)target);
148         }
149     }
150     
151     private void createContentProvider() {
152         viewer.setContentProvider(new ITreeContentProvider() {
153             public Object[] getChildren(Object parentElement) {
154                 return null;
155             }
156             
157             public Object getParent(Object element) {
158                 return null;
159             }
160             
161             public boolean hasChildren(Object element) {
162                 return element instanceof List;
163             }
164             
165             public Object[] getElements(Object inputElement) {
166                 if (inputElement == Registry.getInstance().getRepositoriesData()) {
167                     return Registry.getInstance().getRepositoriesData().keySet().toArray();
168                 }
169                 return null;
170             }
171             
172             public void dispose() {}
173             
174             public void inputChanged(Viewer viewer,
175                     Object oldInput,
176                     Object newInput) { }
177         });
178     }
179 
180     private void createLabelProvider() {
181         viewer.setLabelProvider(new LabelProvider() {
182             public String getText(Object element) {
183                 if (element instanceof WsmoRepository) {
184                     return ((WsmoRepository)element).getDescription();
185                 }
186                 return super.getText(element);
187             }
188             
189             public Image getImage(Object element) {
190                 if (element instanceof WsmoRepository) {
191                     Preferences pluginPrefs = 
192                         RepositoryPlugin.getDefault().getPluginPreferences();
193                     String type = pluginPrefs.getString(ExtensionManager.REP_PROP_PREFIX 
194                         + ((WsmoRepository)element).getDescription());
195                     return Registry.getInstance().iconForRepository(type);
196                 }
197                 return null;
198             }
199         });
200     }
201     
202     private void doCreateRepository() {
203         NewRepositoryDialog nrd = new NewRepositoryDialog(getSite().getShell());
204         if (false == nrd.open()) {
205             return;
206         }
207         Registry.getInstance().addRepository(
208                 nrd.getRepositoryName(), 
209                 nrd.getRepositoryType());
210         viewer.refresh(true);
211     }
212     
213     private void doOpenRepository(WsmoRepository repo) {
214         
215         if (false == Registry.getInstance().configureRepository(repo, false)) {
216             return; // cancelled or error reported
217         }
218         try {
219             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
220             .openEditor(
221                     new RepositoryContent(repo), 
222                     RepositoryEditor.EDITOR_ID);
223         }
224         catch(PartInitException pie) {
225             LogManager.logError(RepositoryPlugin.getDefault().getBundle().getSymbolicName(),
226                                 "Error opening editor \n"+pie.getMessage(),
227                                 pie);
228         }
229     }
230 
231     private void doConfigureRepository(WsmoRepository repo) {
232         IWorkbenchPage page = 
233             PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
234         IEditorReference[] editorRefs = page.getEditorReferences();
235         try {
236             if (editorRefs != null) {
237                 for (int i = 0; i < editorRefs.length; i++) {
238                     if (repo.equals(editorRefs[i].getEditorInput()
239                             .getAdapter(WsmoRepository.class))) {
240                         if (false == MessageDialog.openConfirm(getSite().getShell(),
241                                 "Closing Repository",
242                                 "The selected repository is already opened."
243                                 + "\nIn order to proceed it should be closed."
244                                 + "\nDo you want to close it now?")) {
245                             return;
246                         }
247                         page.closeEditor(editorRefs[i].getEditor(false), false);
248                     }
249                 }
250             }
251         }
252         catch(PartInitException pie) { }
253         Registry.getInstance().configureRepository(repo, true);
254     }
255     
256 
257     public void dispose() {
258         super.dispose();
259 
260         viewer.getTable().dispose();
261         viewer = null;
262     }
263 }
264 
265 /*
266  * $Log$
267  * Revision 1.16  2007/07/19 12:54:57  alex_simov
268  * refactoring in WSMO UI plug-in
269  *
270  * Revision 1.15  2006/08/02 12:32:23  alex_simov
271  * bugfix[1531709]: Context menus relied on right button mouse click instead of
272  * being registered as dedicated context menus for the corresponding UI
273  * controls
274  *
275  * Revision 1.14  2006/02/23 15:56:12  alex_simov
276  * Repositories Explorer split into repository management part and UI part
277  *
278  * Revision 1.13  2006/01/19 10:06:21  alex_simov
279  * exception stack trace not logged
280  *
281  * Revision 1.12  2006/01/09 12:51:11  alex_simov
282  * Copyright message in header updated
283  *
284  * Revision 1.11  2005/12/01 14:10:17  alex_simov
285  * no message
286  *
287  * Revision 1.10  2005/11/30 14:55:01  alex_simov
288  * code refactoring
289  *
290  * Revision 1.9  2005/09/08 16:46:23  alex_simov
291  * Migrating to Java 1.5
292  *
293  * Revision 1.8  2005/07/21 11:46:32  alex_simov
294  * added javadoc: class description, footer
295  *
296  */