1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.view.datasource.e4;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
|
15
|
import javax.annotation.PostConstruct;
|
16
|
import javax.annotation.PreDestroy;
|
17
|
import javax.inject.Inject;
|
18
|
|
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;
|
42
|
|
43
|
import eu.etaxonomy.cdm.config.ICdmSource;
|
44
|
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
|
45
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
46
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
47
|
import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
|
48
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
49
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
50
|
import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceContentProvider;
|
51
|
import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceLabelProvider;
|
52
|
import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewerComparator;
|
53
|
import eu.etaxonomy.taxeditor.view.datasource.CdmMetaDataAwareDataSourceContainer;
|
54
|
|
55
|
/**
|
56
|
* @author pplitzner
|
57
|
* @date 22.08.2017
|
58
|
*/
|
59
|
public class CdmDataSourceViewPartE4 {
|
60
|
|
61
|
@Inject
|
62
|
private UISynchronize sync;
|
63
|
|
64
|
private static final Logger logger = Logger.getLogger(CdmDataSourceViewPartE4.class);
|
65
|
|
66
|
private class ContextListener extends ContextListenerAdapter{
|
67
|
@Override
|
68
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
69
|
monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
|
70
|
viewer.refresh();
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
75
|
monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
|
76
|
viewer.refresh();
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
81
|
monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
|
82
|
viewer.refresh();
|
83
|
}
|
84
|
}
|
85
|
|
86
|
private class DataSourceJob extends Job{
|
87
|
|
88
|
private final List<ICdmSource> cdmSources;
|
89
|
|
90
|
public DataSourceJob(String title, List<ICdmSource> cdmSources) {
|
91
|
super(title);
|
92
|
this.cdmSources = cdmSources;
|
93
|
}
|
94
|
|
95
|
@Override
|
96
|
public IStatus run(final IProgressMonitor monitor) {
|
97
|
try{
|
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$
|
100
|
|
101
|
final List<CdmMetaDataAwareDataSourceContainer> containers = new ArrayList<CdmMetaDataAwareDataSourceContainer>();
|
102
|
|
103
|
for(ICdmSource cdmSource : cdmSources){
|
104
|
containers.add(new CdmMetaDataAwareDataSourceContainer(cdmSource));
|
105
|
}
|
106
|
|
107
|
sync.asyncExec(()->viewer.setInput(containers));
|
108
|
|
109
|
monitor.worked(1);
|
110
|
|
111
|
for(final CdmMetaDataAwareDataSourceContainer container : containers){
|
112
|
if(logger.isDebugEnabled()) {
|
113
|
logger.debug(" #" + container.hashCode() + " : next DataSourceContainer"); //$NON-NLS-1$ //$NON-NLS-2$
|
114
|
}
|
115
|
container.getMetaDataFromDataSource();
|
116
|
if(logger.isDebugEnabled())
|
117
|
{
|
118
|
logger.debug(" #" + container.hashCode() + " : metadata retrieved, creating new runnable ..."); //$NON-NLS-1$ //$NON-NLS-2$
|
119
|
}
|
120
|
sync.asyncExec(new Runnable() {
|
121
|
|
122
|
@Override
|
123
|
public void run() {
|
124
|
if(logger.isDebugEnabled())
|
125
|
{
|
126
|
logger.debug(" #" + container.hashCode() + " starting sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
|
127
|
}
|
128
|
viewer.update(container, null);
|
129
|
if(logger.isDebugEnabled())
|
130
|
{
|
131
|
logger.debug(" #" + container.hashCode() + " end of sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
|
132
|
}
|
133
|
}
|
134
|
});
|
135
|
if(logger.isDebugEnabled())
|
136
|
{
|
137
|
logger.debug(" #" + container.hashCode() + " done"); //$NON-NLS-1$ //$NON-NLS-2$
|
138
|
}
|
139
|
monitor.worked(1);
|
140
|
}
|
141
|
|
142
|
}finally{
|
143
|
monitor.done();
|
144
|
}
|
145
|
return Status.OK_STATUS;
|
146
|
}
|
147
|
}
|
148
|
|
149
|
private TableViewer viewer;
|
150
|
|
151
|
//FIXME E4 migrate
|
152
|
// private IWorkbenchSiteProgressService service;
|
153
|
|
154
|
private IContextListener contextListener;
|
155
|
|
156
|
private CdmDataSourceViewerComparator comparator;
|
157
|
|
158
|
public CdmDataSourceViewPartE4(){
|
159
|
}
|
160
|
|
161
|
private ISelectionChangedListener selectionChangedListener;
|
162
|
|
163
|
@Inject
|
164
|
private ESelectionService selService;
|
165
|
|
166
|
@PostConstruct
|
167
|
public void create(Composite parent, EMenuService menuService) {
|
168
|
// service = getSite().getAdapter(IWorkbenchSiteProgressService.class);
|
169
|
contextListener = new ContextListener();
|
170
|
CdmStore.getContextManager().addContextListener(contextListener);
|
171
|
|
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);
|
178
|
|
179
|
viewer = new TableViewer(parent, SWT.H_SCROLL
|
180
|
| SWT.V_SCROLL | SWT.FULL_SELECTION);
|
181
|
|
182
|
createColumns(viewer);
|
183
|
|
184
|
viewer.setContentProvider(new CdmDataSourceContentProvider());
|
185
|
viewer.setLabelProvider(new CdmDataSourceLabelProvider());
|
186
|
comparator = new CdmDataSourceViewerComparator();
|
187
|
viewer.setComparator(comparator);
|
188
|
|
189
|
// Propagate selection from viewer
|
190
|
selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
|
191
|
viewer.addSelectionChangedListener(selectionChangedListener);
|
192
|
|
193
|
//create context menu
|
194
|
menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.datasourceview");
|
195
|
|
196
|
refresh();
|
197
|
}
|
198
|
|
199
|
|
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};
|
205
|
|
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));
|
213
|
if(i == 1){
|
214
|
table.setSortColumn(column.getColumn());
|
215
|
}
|
216
|
}
|
217
|
table.setHeaderVisible(true);
|
218
|
table.setLinesVisible(true);
|
219
|
table.setSortDirection(SWT.UP);
|
220
|
}
|
221
|
|
222
|
@Inject
|
223
|
@Optional
|
224
|
private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_DATASOURCE)boolean refresh){
|
225
|
if(refresh){
|
226
|
refresh();
|
227
|
}
|
228
|
}
|
229
|
|
230
|
public void refresh(){
|
231
|
DataSourceJob dataSourceJob = new DataSourceJob(Messages.CdmDataSourceViewPart_1, CdmDataSourceRepository.getAll());
|
232
|
dataSourceJob.schedule();
|
233
|
}
|
234
|
|
235
|
@PreDestroy
|
236
|
public void dispose() {
|
237
|
CdmStore.getContextManager().removeContextListener(contextListener);
|
238
|
}
|
239
|
|
240
|
@Focus
|
241
|
public void setFocus() {
|
242
|
if (viewer.getControl() != null && !viewer.getControl().isDisposed()) {
|
243
|
viewer.getControl().setFocus();
|
244
|
}
|
245
|
}
|
246
|
|
247
|
//FIXME E4 migrate
|
248
|
// @Override
|
249
|
// public void showBusy(boolean busy) {
|
250
|
// super.showBusy(busy);
|
251
|
//// viewer.getTable().setEnabled(!busy);
|
252
|
// if(busy){
|
253
|
// partNameCache = getPartName();
|
254
|
// setPartName(Messages.CdmDataSourceViewPart_1);
|
255
|
// }else{
|
256
|
// if(partNameCache != null){
|
257
|
// setPartName(partNameCache);
|
258
|
// }
|
259
|
// }
|
260
|
// }
|
261
|
|
262
|
public StructuredViewer getViewer() {
|
263
|
return viewer;
|
264
|
}
|
265
|
|
266
|
// public IWorkbenchSiteProgressService getService() {
|
267
|
// return service;
|
268
|
// }
|
269
|
|
270
|
private SelectionAdapter getSelectionAdapter(final TableColumn column,
|
271
|
final int index) {
|
272
|
SelectionAdapter selectionAdapter = new SelectionAdapter() {
|
273
|
@Override
|
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;
|
279
|
} else {
|
280
|
dir = SWT.DOWN;
|
281
|
}
|
282
|
viewer.getTable().setSortDirection(dir);
|
283
|
viewer.getTable().setSortColumn(column);
|
284
|
viewer.refresh();
|
285
|
}
|
286
|
};
|
287
|
return selectionAdapter;
|
288
|
}
|
289
|
}
|