3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.datasource
.view
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
17 import org
.eclipse
.core
.commands
.Command
;
18 import org
.eclipse
.core
.commands
.ExecutionException
;
19 import org
.eclipse
.core
.commands
.NotEnabledException
;
20 import org
.eclipse
.core
.commands
.NotHandledException
;
21 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.jface
.action
.GroupMarker
;
24 import org
.eclipse
.jface
.action
.MenuManager
;
25 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
26 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
27 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
28 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.StructuredViewer
;
30 import org
.eclipse
.jface
.viewers
.TableViewer
;
31 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
32 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.layout
.FillLayout
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.Menu
;
39 import org
.eclipse
.swt
.widgets
.Table
;
40 import org
.eclipse
.ui
.IMemento
;
41 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
42 import org
.eclipse
.ui
.commands
.ICommandService
;
43 import org
.eclipse
.ui
.handlers
.IHandlerService
;
44 import org
.eclipse
.ui
.part
.ViewPart
;
45 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
47 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
48 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
49 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
50 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
51 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
52 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
55 * <p>CdmDataSourceViewPart class.</p>
61 public class CdmDataSourceViewPart
extends ViewPart
{
63 private class ContextListener
extends ContextListenerAdapter
{
65 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
68 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
73 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
76 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
81 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
84 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
89 private class DataSourceRunnable
implements IRunnableWithProgress
{
91 private List
<ICdmDataSource
> dataSources
;
96 public DataSourceRunnable(Display display
, List
<ICdmDataSource
> dataSources
) {
97 this.dataSources
= dataSources
;
101 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
104 public void run(final IProgressMonitor monitor
) {
106 monitor
.beginTask("Retrieving datasources", dataSources
.size());
108 List
<ICdmDataSource
> dataSources
= CdmDataSourceRepository
.getAll();
110 final List
<CdmMetaDataAwareDataSourceContainer
> containers
= new ArrayList
<CdmMetaDataAwareDataSourceContainer
>();
112 for(final ICdmDataSource dataSource
: dataSources
){
113 containers
.add(new CdmMetaDataAwareDataSourceContainer(dataSource
, CdmDataSourceViewPart
.this));
117 viewer
.setInput(containers
);
125 /** Constant <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code> */
126 public static String ID
= "eu.etaxonomy.taxeditor.store.datasource.datasourceView";
128 private TableViewer viewer
;
130 private String partNameCache
;
132 private IWorkbenchSiteProgressService service
;
134 private IContextListener contextListener
;
137 * <p>Constructor for CdmDataSourceViewPart.</p>
139 public CdmDataSourceViewPart(){
144 public void createPartControl(Composite parent
) {
145 service
= (IWorkbenchSiteProgressService
) getSite().getAdapter(IWorkbenchSiteProgressService
.class);
146 contextListener
= new ContextListener();
147 CdmStore
.getContextManager().addContextListener(contextListener
);
149 // Create top composite
150 FillLayout fillLayout
= new FillLayout();
151 fillLayout
.marginWidth
= 0;
152 fillLayout
.marginHeight
= 0;
153 fillLayout
.type
= SWT
.VERTICAL
;
154 parent
.setLayout(fillLayout
);
156 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
157 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
158 getSite().setSelectionProvider(viewer
);
160 createColumns(viewer
);
162 viewer
.setContentProvider(new CdmDataSourceContentProvider());
163 viewer
.setLabelProvider(new CdmDataSourceLabelProvider());
164 viewer
.setComparator(new ViewerComparator());
167 // register context menu
168 MenuManager menuMgr
= new MenuManager();
169 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
170 getSite().registerContextMenu(menuMgr
, viewer
);
172 Control control
= viewer
.getControl();
173 Menu menu
= menuMgr
.createContextMenu(control
);
174 control
.setMenu(menu
);
176 // connect on doubleclick
177 viewer
.addDoubleClickListener(new IDoubleClickListener() {
179 public void doubleClick(DoubleClickEvent event
) {
180 if (event
.getSelection() instanceof StructuredSelection
) {
182 Object element
= ((StructuredSelection
) event
183 .getSelection()).getFirstElement();
184 if (element
instanceof CdmMetaDataAwareDataSourceContainer
) {
185 String commandId
= "eu.etaxonomy.taxeditor.store.datasource.change";
187 ICommandService commandService
= (ICommandService
)getSite().getService(ICommandService
.class);
189 Command command
= commandService
.getCommand(commandId
);
190 if(command
.isEnabled()) {
192 IHandlerService handlerService
= (IHandlerService
) StoreUtil
.getService(IHandlerService
.class);
195 handlerService
.executeCommand(commandId
, null);
196 } catch (ExecutionException e
) {
197 StoreUtil
.error(this.getClass(), e
);
198 } catch (NotDefinedException e
) {
199 StoreUtil
.error(this.getClass(), e
);
200 } catch (NotEnabledException e
) {
201 StoreUtil
.error(this.getClass(), e
);
202 } catch (NotHandledException e
) {
203 StoreUtil
.error(this.getClass(), e
);
215 // This will create the columns for the table
216 private void createColumns(TableViewer viewer
) {
217 Table table
= viewer
.getTable();
218 String
[] titles
= {"Connected", "Name", "Server", "Type", "Database", "Nomenclatural Code", "Up", "Created", "CDM Version", "Compatible", "Notes"};
219 int[] bounds
= { 24, 200, 100, 100 , 200, 120, 50, 100, 100, 50, 300};
221 for (int i
= 0; i
< titles
.length
; i
++) {
222 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
223 column
.getColumn().setText(titles
[i
]);
224 column
.getColumn().setWidth(bounds
[i
]);
225 column
.getColumn().setResizable(true);
226 column
.getColumn().setMoveable(true);
228 table
.setHeaderVisible(true);
229 table
.setLinesVisible(true);
235 public void refresh(){
237 StoreUtil
.runInUI(new DataSourceRunnable(Display
.getCurrent(), CdmDataSourceRepository
.getAll()), null);
238 } catch (InvocationTargetException e
) {
239 StoreUtil
.error(this.getClass(), e
);
240 } catch (InterruptedException e
) {
241 StoreUtil
.error(this.getClass(), e
);
247 public void dispose() {
248 CdmStore
.getContextManager().removeContextListener(contextListener
);
254 public void setFocus() {
255 viewer
.getControl().setFocus();
260 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
264 public void showBusy(boolean busy
) {
265 super.showBusy(busy
);
266 viewer
.getTable().setEnabled(!busy
);
268 partNameCache
= getPartName();
269 setPartName("Loading datasources");
271 if(partNameCache
!= null){
272 setPartName(partNameCache
);
278 * <p>Getter for the field <code>viewer</code>.</p>
280 * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
282 public StructuredViewer
getViewer() {
287 * <p>Getter for the field <code>service</code>.</p>
289 * @return the service
291 public IWorkbenchSiteProgressService
getService() {