2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.e4
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
15 import javax
.annotation
.PostConstruct
;
16 import javax
.annotation
.PreDestroy
;
17 import javax
.inject
.Inject
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.core
.runtime
.IStatus
;
22 import org
.eclipse
.core
.runtime
.Status
;
23 import org
.eclipse
.core
.runtime
.jobs
.Job
;
24 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
25 import org
.eclipse
.e4
.ui
.di
.Focus
;
26 import org
.eclipse
.e4
.ui
.di
.UIEventTopic
;
27 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
28 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
29 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
30 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
31 import org
.eclipse
.jface
.viewers
.StructuredViewer
;
32 import org
.eclipse
.jface
.viewers
.TableViewer
;
33 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.events
.SelectionAdapter
;
36 import org
.eclipse
.swt
.events
.SelectionEvent
;
37 import org
.eclipse
.swt
.layout
.FillLayout
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Table
;
40 import org
.eclipse
.swt
.widgets
.TableColumn
;
41 import org
.eclipse
.ui
.IMemento
;
43 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
44 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
45 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
46 import eu
.etaxonomy
.taxeditor
.local
.datasource
.common
.CdmDataSourceRepository
;
47 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmDataSourceContentProvider
;
48 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmDataSourceLabelProvider
;
49 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmDataSourceViewerComparator
;
50 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmMetaDataAwareDataSourceContainer
;
51 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
52 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
53 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
59 public class CdmDataSourceViewPartE4
{
62 private UISynchronize sync
;
64 private static final Logger logger
= Logger
.getLogger(CdmDataSourceViewPartE4
.class);
66 private class ContextListener
extends ContextListenerAdapter
{
68 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
69 monitor
.subTask("Refreshing Datasource View"); //$NON-NLS-1$
74 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
75 monitor
.subTask("Refreshing Datasource View"); //$NON-NLS-1$
80 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
81 monitor
.subTask("Refreshing Datasource View"); //$NON-NLS-1$
86 private class DataSourceJob
extends Job
{
88 private final List
<ICdmSource
> cdmSources
;
90 public DataSourceJob(String title
, List
<ICdmSource
> cdmSources
) {
92 this.cdmSources
= cdmSources
;
96 public IStatus
run(final IProgressMonitor monitor
) {
98 logger
.debug("Begin of eclipse core runtime Job to Retrieve datasources"); //$NON-NLS-1$
99 monitor
.beginTask("Retrieving datasources", cdmSources
.size() + 1); //$NON-NLS-1$
101 final List
<CdmMetaDataAwareDataSourceContainer
> containers
= new ArrayList
<CdmMetaDataAwareDataSourceContainer
>();
103 for(ICdmSource cdmSource
: cdmSources
){
104 containers
.add(new CdmMetaDataAwareDataSourceContainer(cdmSource
));
107 sync
.asyncExec(()->viewer
.setInput(containers
));
111 for(final CdmMetaDataAwareDataSourceContainer container
: containers
){
112 if(logger
.isDebugEnabled()) {
113 logger
.debug(" #" + container
.hashCode() + " : next DataSourceContainer"); //$NON-NLS-1$ //$NON-NLS-2$
115 container
.getMetaDataFromDataSource();
116 if(logger
.isDebugEnabled())
118 logger
.debug(" #" + container
.hashCode() + " : metadata retrieved, creating new runnable ..."); //$NON-NLS-1$ //$NON-NLS-2$
120 sync
.asyncExec(new Runnable() {
124 if(logger
.isDebugEnabled())
126 logger
.debug(" #" + container
.hashCode() + " starting sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
128 viewer
.update(container
, null);
129 if(logger
.isDebugEnabled())
131 logger
.debug(" #" + container
.hashCode() + " end of sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
135 if(logger
.isDebugEnabled())
137 logger
.debug(" #" + container
.hashCode() + " done"); //$NON-NLS-1$ //$NON-NLS-2$
145 return Status
.OK_STATUS
;
149 private TableViewer viewer
;
152 // private IWorkbenchSiteProgressService service;
154 private IContextListener contextListener
;
156 private CdmDataSourceViewerComparator comparator
;
158 public CdmDataSourceViewPartE4(){
161 private ISelectionChangedListener selectionChangedListener
;
164 private ESelectionService selService
;
167 public void create(Composite parent
, EMenuService menuService
) {
168 // service = getSite().getAdapter(IWorkbenchSiteProgressService.class);
169 contextListener
= new ContextListener();
170 CdmStore
.getContextManager().addContextListener(contextListener
);
172 // Create top composite
173 FillLayout fillLayout
= new FillLayout();
174 fillLayout
.marginWidth
= 0;
175 fillLayout
.marginHeight
= 0;
176 fillLayout
.type
= SWT
.VERTICAL
;
177 parent
.setLayout(fillLayout
);
179 viewer
= new TableViewer(parent
, SWT
.H_SCROLL
180 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
182 createColumns(viewer
);
184 viewer
.setContentProvider(new CdmDataSourceContentProvider());
185 viewer
.setLabelProvider(new CdmDataSourceLabelProvider());
186 comparator
= new CdmDataSourceViewerComparator();
187 viewer
.setComparator(comparator
);
189 // Propagate selection from viewer
190 selectionChangedListener
= (event
-> selService
.setSelection(event
.getSelection()));
191 viewer
.addSelectionChangedListener(selectionChangedListener
);
193 //create context menu
194 menuService
.registerContextMenu(viewer
.getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.datasourceview");
200 // This will create the columns for the table
201 private void createColumns(TableViewer viewer
) {
202 Table table
= viewer
.getTable();
203 String
[] titles
= {Messages
.CdmDataSourceViewPart_12
, Messages
.CdmDataSourceViewPart_11
, Messages
.CdmDataSourceViewPart_10
, Messages
.CdmDataSourceViewPart_9
, Messages
.CdmDataSourceViewPart_8
, Messages
.CdmDataSourceViewPart_7
, Messages
.CdmDataSourceViewPart_5
, Messages
.CdmDataSourceViewPart_4
, Messages
.CdmDataSourceViewPart_3
, Messages
.CdmDataSourceViewPart_2
};
204 int[] bounds
= { 24, 200, 100, 50 , 80, 120, 100, 100, 50, 300};
206 for (int i
= 0; i
< titles
.length
; i
++) {
207 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
208 column
.getColumn().setText(titles
[i
]);
209 column
.getColumn().setWidth(bounds
[i
]);
210 column
.getColumn().setResizable(true);
211 column
.getColumn().setMoveable(true);
212 column
.getColumn().addSelectionListener(getSelectionAdapter(column
.getColumn(), i
));
214 table
.setSortColumn(column
.getColumn());
217 table
.setHeaderVisible(true);
218 table
.setLinesVisible(true);
219 table
.setSortDirection(SWT
.UP
);
224 private void updateView(@UIEventTopic(WorkbenchEventConstants
.REFRESH_DATASOURCE
)boolean refresh
){
230 public void refresh(){
231 DataSourceJob dataSourceJob
= new DataSourceJob(Messages
.CdmDataSourceViewPart_1
, CdmDataSourceRepository
.getAll());
232 dataSourceJob
.schedule();
236 public void dispose() {
237 CdmStore
.getContextManager().removeContextListener(contextListener
);
241 public void setFocus() {
242 if (viewer
.getControl() != null && !viewer
.getControl().isDisposed()) {
243 viewer
.getControl().setFocus();
249 // public void showBusy(boolean busy) {
250 // super.showBusy(busy);
251 //// viewer.getTable().setEnabled(!busy);
253 // partNameCache = getPartName();
254 // setPartName(Messages.CdmDataSourceViewPart_1);
256 // if(partNameCache != null){
257 // setPartName(partNameCache);
262 public StructuredViewer
getViewer() {
266 // public IWorkbenchSiteProgressService getService() {
270 private SelectionAdapter
getSelectionAdapter(final TableColumn column
,
272 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
274 public void widgetSelected(SelectionEvent e
) {
275 comparator
.setColumn(index
);
276 int dir
= viewer
.getTable().getSortDirection();
277 if (viewer
.getTable().getSortColumn() == column
) {
278 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
282 viewer
.getTable().setSortDirection(dir
);
283 viewer
.getTable().setSortColumn(column
);
287 return selectionAdapter
;