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
.view
.datasource
;
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
.swt
.SWT
;
33 import org
.eclipse
.swt
.events
.SelectionAdapter
;
34 import org
.eclipse
.swt
.events
.SelectionEvent
;
35 import org
.eclipse
.swt
.layout
.FillLayout
;
36 import org
.eclipse
.swt
.widgets
.Composite
;
37 import org
.eclipse
.swt
.widgets
.Control
;
38 import org
.eclipse
.swt
.widgets
.Display
;
39 import org
.eclipse
.swt
.widgets
.Menu
;
40 import org
.eclipse
.swt
.widgets
.Table
;
41 import org
.eclipse
.swt
.widgets
.TableColumn
;
42 import org
.eclipse
.ui
.IMemento
;
43 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
44 import org
.eclipse
.ui
.commands
.ICommandService
;
45 import org
.eclipse
.ui
.handlers
.IHandlerService
;
46 import org
.eclipse
.ui
.part
.ViewPart
;
47 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
49 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
50 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
51 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
52 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
54 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
57 * <p>CdmDataSourceViewPart class.</p>
63 public class CdmDataSourceViewPart
extends ViewPart
{
65 private class ContextListener
extends ContextListenerAdapter
{
67 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
70 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
71 monitor
.subTask("Refreshing Datasource View");
76 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
79 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
80 monitor
.subTask("Refreshing Datasource View");
85 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
88 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
89 monitor
.subTask("Refreshing Datasource View");
94 private class DataSourceRunnable
implements IRunnableWithProgress
{
96 private List
<ICdmDataSource
> dataSources
;
101 public DataSourceRunnable(Display display
, List
<ICdmDataSource
> dataSources
) {
102 this.dataSources
= dataSources
;
106 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
109 public void run(final IProgressMonitor monitor
) {
111 monitor
.beginTask("Retrieving datasources", dataSources
.size());
113 List
<ICdmDataSource
> dataSources
= CdmDataSourceRepository
.getAll();
115 final List
<CdmMetaDataAwareDataSourceContainer
> containers
= new ArrayList
<CdmMetaDataAwareDataSourceContainer
>();
117 for(final ICdmDataSource dataSource
: dataSources
){
118 containers
.add(new CdmMetaDataAwareDataSourceContainer(dataSource
, CdmDataSourceViewPart
.this));
122 viewer
.setInput(containers
);
130 /** Constant <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code> */
131 public static String ID
= "eu.etaxonomy.taxeditor.view.datasource";
133 private TableViewer viewer
;
135 private String partNameCache
;
137 private IWorkbenchSiteProgressService service
;
139 private IContextListener contextListener
;
141 private CdmDataSourceViewerComparator comparator
;
144 * <p>Constructor for CdmDataSourceViewPart.</p>
146 public CdmDataSourceViewPart(){
151 public void createPartControl(Composite parent
) {
152 service
= (IWorkbenchSiteProgressService
) getSite().getAdapter(IWorkbenchSiteProgressService
.class);
153 contextListener
= new ContextListener();
154 CdmStore
.getContextManager().addContextListener(contextListener
);
156 // Create top composite
157 FillLayout fillLayout
= new FillLayout();
158 fillLayout
.marginWidth
= 0;
159 fillLayout
.marginHeight
= 0;
160 fillLayout
.type
= SWT
.VERTICAL
;
161 parent
.setLayout(fillLayout
);
163 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
164 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
165 getSite().setSelectionProvider(viewer
);
167 createColumns(viewer
);
169 viewer
.setContentProvider(new CdmDataSourceContentProvider());
170 viewer
.setLabelProvider(new CdmDataSourceLabelProvider());
171 comparator
= new CdmDataSourceViewerComparator();
172 viewer
.setComparator(comparator
);
175 // register context menu
176 MenuManager menuMgr
= new MenuManager();
177 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
178 getSite().registerContextMenu(menuMgr
, viewer
);
180 Control control
= viewer
.getControl();
181 Menu menu
= menuMgr
.createContextMenu(control
);
182 control
.setMenu(menu
);
184 // connect on doubleclick
185 viewer
.addDoubleClickListener(new IDoubleClickListener() {
187 public void doubleClick(DoubleClickEvent event
) {
188 if (event
.getSelection() instanceof StructuredSelection
) {
190 Object element
= ((StructuredSelection
) event
191 .getSelection()).getFirstElement();
192 if (element
instanceof CdmMetaDataAwareDataSourceContainer
) {
193 String commandId
= "eu.etaxonomy.taxeditor.store.datasource.change";
195 ICommandService commandService
= (ICommandService
)getSite().getService(ICommandService
.class);
197 Command command
= commandService
.getCommand(commandId
);
198 if(command
.isEnabled()) {
200 IHandlerService handlerService
= (IHandlerService
) StoreUtil
.getService(IHandlerService
.class);
203 handlerService
.executeCommand(commandId
, null);
204 } catch (ExecutionException e
) {
205 StoreUtil
.error(this.getClass(), e
);
206 } catch (NotDefinedException e
) {
207 StoreUtil
.error(this.getClass(), e
);
208 } catch (NotEnabledException e
) {
209 StoreUtil
.error(this.getClass(), e
);
210 } catch (NotHandledException e
) {
211 StoreUtil
.error(this.getClass(), e
);
223 // This will create the columns for the table
224 private void createColumns(TableViewer viewer
) {
225 Table table
= viewer
.getTable();
226 String
[] titles
= {"Connected", "Name", "Server", "Type", "Database", "Nomenclatural Code", "Up", "Created", "CDM Version", "Compatible", "Notes"};
227 int[] bounds
= { 24, 200, 100, 100 , 200, 120, 50, 100, 100, 50, 300};
229 for (int i
= 0; i
< titles
.length
; i
++) {
230 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
231 column
.getColumn().setText(titles
[i
]);
232 column
.getColumn().setWidth(bounds
[i
]);
233 column
.getColumn().setResizable(true);
234 column
.getColumn().setMoveable(true);
235 column
.getColumn().addSelectionListener(getSelectionAdapter(column
.getColumn(), i
));
237 table
.setSortColumn(column
.getColumn());
240 table
.setHeaderVisible(true);
241 table
.setLinesVisible(true);
242 table
.setSortDirection(SWT
.UP
);
249 public void refresh(){
251 StoreUtil
.runInUI(new DataSourceRunnable(Display
.getCurrent(), CdmDataSourceRepository
.getAll()), null);
252 } catch (InvocationTargetException e
) {
253 StoreUtil
.error(this.getClass(), e
);
254 } catch (InterruptedException e
) {
255 StoreUtil
.error(this.getClass(), e
);
261 public void dispose() {
262 CdmStore
.getContextManager().removeContextListener(contextListener
);
268 public void setFocus() {
269 viewer
.getControl().setFocus();
274 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
278 public void showBusy(boolean busy
) {
279 super.showBusy(busy
);
280 viewer
.getTable().setEnabled(!busy
);
282 partNameCache
= getPartName();
283 setPartName("Loading datasources");
285 if(partNameCache
!= null){
286 setPartName(partNameCache
);
292 * <p>Getter for the field <code>viewer</code>.</p>
294 * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
296 public StructuredViewer
getViewer() {
301 * <p>Getter for the field <code>service</code>.</p>
303 * @return the service
305 public IWorkbenchSiteProgressService
getService() {
309 private SelectionAdapter
getSelectionAdapter(final TableColumn column
,
311 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
313 public void widgetSelected(SelectionEvent e
) {
314 comparator
.setColumn(index
);
315 int dir
= viewer
.getTable().getSortDirection();
316 if (viewer
.getTable().getSortColumn() == column
) {
317 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
321 viewer
.getTable().setSortDirection(dir
);
322 viewer
.getTable().setSortColumn(column
);
326 return selectionAdapter
;