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 List
<CdmMetaDataAwareDataSourceContainer
> containers
= new ArrayList
<CdmMetaDataAwareDataSourceContainer
>();
117 for(ICdmDataSource dataSource
: dataSources
){
118 containers
.add(new CdmMetaDataAwareDataSourceContainer(dataSource
, CdmDataSourceViewPart
.this));
122 // Display.getDefault().asyncExec(new Runnable() {
125 // public void run() {
126 // viewer.setInput(containers);
129 viewer
.setInput(containers
);
137 /** Constant <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code> */
138 public static String ID
= "eu.etaxonomy.taxeditor.view.datasource";
140 private TableViewer viewer
;
142 private String partNameCache
;
144 private IWorkbenchSiteProgressService service
;
146 private IContextListener contextListener
;
148 private CdmDataSourceViewerComparator comparator
;
151 * <p>Constructor for CdmDataSourceViewPart.</p>
153 public CdmDataSourceViewPart(){
158 public void createPartControl(Composite parent
) {
159 service
= (IWorkbenchSiteProgressService
) getSite().getAdapter(IWorkbenchSiteProgressService
.class);
160 contextListener
= new ContextListener();
161 CdmStore
.getContextManager().addContextListener(contextListener
);
163 // Create top composite
164 FillLayout fillLayout
= new FillLayout();
165 fillLayout
.marginWidth
= 0;
166 fillLayout
.marginHeight
= 0;
167 fillLayout
.type
= SWT
.VERTICAL
;
168 parent
.setLayout(fillLayout
);
170 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
171 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
172 getSite().setSelectionProvider(viewer
);
174 createColumns(viewer
);
176 viewer
.setContentProvider(new CdmDataSourceContentProvider());
177 viewer
.setLabelProvider(new CdmDataSourceLabelProvider());
178 comparator
= new CdmDataSourceViewerComparator();
179 viewer
.setComparator(comparator
);
182 // register context menu
183 MenuManager menuMgr
= new MenuManager();
184 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
185 getSite().registerContextMenu(menuMgr
, viewer
);
187 Control control
= viewer
.getControl();
188 Menu menu
= menuMgr
.createContextMenu(control
);
189 control
.setMenu(menu
);
191 // connect on doubleclick
192 viewer
.addDoubleClickListener(new IDoubleClickListener() {
194 public void doubleClick(DoubleClickEvent event
) {
195 if (event
.getSelection() instanceof StructuredSelection
) {
197 Object element
= ((StructuredSelection
) event
198 .getSelection()).getFirstElement();
199 if (element
instanceof CdmMetaDataAwareDataSourceContainer
) {
200 String commandId
= "eu.etaxonomy.taxeditor.store.datasource.change";
202 ICommandService commandService
= (ICommandService
)getSite().getService(ICommandService
.class);
204 Command command
= commandService
.getCommand(commandId
);
205 if(command
.isEnabled()) {
207 IHandlerService handlerService
= (IHandlerService
) StoreUtil
.getService(IHandlerService
.class);
210 handlerService
.executeCommand(commandId
, null);
211 } catch (ExecutionException e
) {
212 StoreUtil
.error(this.getClass(), e
);
213 } catch (NotDefinedException e
) {
214 StoreUtil
.error(this.getClass(), e
);
215 } catch (NotEnabledException e
) {
216 StoreUtil
.error(this.getClass(), e
);
217 } catch (NotHandledException e
) {
218 StoreUtil
.error(this.getClass(), e
);
230 // This will create the columns for the table
231 private void createColumns(TableViewer viewer
) {
232 Table table
= viewer
.getTable();
233 String
[] titles
= {"Connected", "Name", "Server", "Type", "Database", "Nomenclatural Code", "Up", "Created", "CDM Version", "Compatible", "Notes"};
234 int[] bounds
= { 24, 200, 100, 100 , 200, 120, 50, 100, 100, 50, 300};
236 for (int i
= 0; i
< titles
.length
; i
++) {
237 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
238 column
.getColumn().setText(titles
[i
]);
239 column
.getColumn().setWidth(bounds
[i
]);
240 column
.getColumn().setResizable(true);
241 column
.getColumn().setMoveable(true);
242 column
.getColumn().addSelectionListener(getSelectionAdapter(column
.getColumn(), i
));
244 table
.setSortColumn(column
.getColumn());
247 table
.setHeaderVisible(true);
248 table
.setLinesVisible(true);
249 table
.setSortDirection(SWT
.UP
);
256 public void refresh(){
258 StoreUtil
.runInUI(new DataSourceRunnable(Display
.getCurrent(), CdmDataSourceRepository
.getAll()), null);
259 } catch (InvocationTargetException e
) {
260 StoreUtil
.error(this.getClass(), e
);
261 } catch (InterruptedException e
) {
262 StoreUtil
.error(this.getClass(), e
);
268 public void dispose() {
269 CdmStore
.getContextManager().removeContextListener(contextListener
);
275 public void setFocus() {
276 viewer
.getControl().setFocus();
281 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
285 public void showBusy(boolean busy
) {
286 super.showBusy(busy
);
287 viewer
.getTable().setEnabled(!busy
);
289 partNameCache
= getPartName();
290 setPartName("Loading datasources");
292 if(partNameCache
!= null){
293 setPartName(partNameCache
);
299 * <p>Getter for the field <code>viewer</code>.</p>
301 * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
303 public StructuredViewer
getViewer() {
308 * <p>Getter for the field <code>service</code>.</p>
310 * @return the service
312 public IWorkbenchSiteProgressService
getService() {
316 private SelectionAdapter
getSelectionAdapter(final TableColumn column
,
318 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
320 public void widgetSelected(SelectionEvent e
) {
321 comparator
.setColumn(index
);
322 int dir
= viewer
.getTable().getSortDirection();
323 if (viewer
.getTable().getSortColumn() == column
) {
324 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
328 viewer
.getTable().setSortDirection(dir
);
329 viewer
.getTable().setSortColumn(column
);
333 return selectionAdapter
;