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.ui.di.Focus;
|
25
|
import org.eclipse.e4.ui.services.EMenuService;
|
26
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
27
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
28
|
import org.eclipse.jface.viewers.StructuredViewer;
|
29
|
import org.eclipse.jface.viewers.TableViewer;
|
30
|
import org.eclipse.jface.viewers.TableViewerColumn;
|
31
|
import org.eclipse.swt.SWT;
|
32
|
import org.eclipse.swt.events.SelectionAdapter;
|
33
|
import org.eclipse.swt.events.SelectionEvent;
|
34
|
import org.eclipse.swt.layout.FillLayout;
|
35
|
import org.eclipse.swt.widgets.Composite;
|
36
|
import org.eclipse.swt.widgets.Display;
|
37
|
import org.eclipse.swt.widgets.Table;
|
38
|
import org.eclipse.swt.widgets.TableColumn;
|
39
|
import org.eclipse.ui.IMemento;
|
40
|
|
41
|
import eu.etaxonomy.cdm.config.ICdmSource;
|
42
|
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
|
43
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
44
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
45
|
import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
|
46
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
47
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
48
|
import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceContentProvider;
|
49
|
import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceLabelProvider;
|
50
|
import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewerComparator;
|
51
|
import eu.etaxonomy.taxeditor.view.datasource.CdmMetaDataAwareDataSourceContainer;
|
52
|
|
53
|
/**
|
54
|
*
|
55
|
* @author pplitzner
|
56
|
* @date 22.08.2017
|
57
|
*
|
58
|
*/
|
59
|
public class CdmDataSourceViewPartE4 {
|
60
|
|
61
|
private static final Logger logger = Logger.getLogger(CdmDataSourceViewPartE4.class);
|
62
|
|
63
|
private class ContextListener extends ContextListenerAdapter{
|
64
|
@Override
|
65
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
66
|
monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
|
67
|
viewer.refresh();
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
72
|
monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
|
73
|
viewer.refresh();
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
78
|
monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
|
79
|
viewer.refresh();
|
80
|
}
|
81
|
}
|
82
|
|
83
|
private class DataSourceJob extends Job{
|
84
|
|
85
|
private final List<ICdmSource> cdmSources;
|
86
|
|
87
|
/**
|
88
|
* @param name
|
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
|
Display.getDefault().asyncExec(new Runnable() {
|
108
|
|
109
|
@Override
|
110
|
public void run() {
|
111
|
viewer.setInput(containers);
|
112
|
}
|
113
|
});
|
114
|
monitor.worked(1);
|
115
|
|
116
|
for(final CdmMetaDataAwareDataSourceContainer container : containers){
|
117
|
if(logger.isDebugEnabled()) {
|
118
|
logger.debug(" #" + container.hashCode() + " : next DataSourceContainer"); //$NON-NLS-1$ //$NON-NLS-2$
|
119
|
}
|
120
|
container.getMetaDataFromDataSource();
|
121
|
if(logger.isDebugEnabled())
|
122
|
{
|
123
|
logger.debug(" #" + container.hashCode() + " : metadata retrieved, creating new runnable ..."); //$NON-NLS-1$ //$NON-NLS-2$
|
124
|
}
|
125
|
Display.getDefault().asyncExec(new Runnable() {
|
126
|
|
127
|
@Override
|
128
|
public void run() {
|
129
|
if(logger.isDebugEnabled())
|
130
|
{
|
131
|
logger.debug(" #" + container.hashCode() + " starting sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
|
132
|
}
|
133
|
viewer.update(container, null);
|
134
|
if(logger.isDebugEnabled())
|
135
|
{
|
136
|
logger.debug(" #" + container.hashCode() + " end of sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
|
137
|
}
|
138
|
}
|
139
|
});
|
140
|
if(logger.isDebugEnabled())
|
141
|
{
|
142
|
logger.debug(" #" + container.hashCode() + " done"); //$NON-NLS-1$ //$NON-NLS-2$
|
143
|
}
|
144
|
monitor.worked(1);
|
145
|
}
|
146
|
|
147
|
}finally{
|
148
|
monitor.done();
|
149
|
}
|
150
|
return Status.OK_STATUS;
|
151
|
}
|
152
|
}
|
153
|
|
154
|
private TableViewer viewer;
|
155
|
|
156
|
//FIXME E4 migrate
|
157
|
// private IWorkbenchSiteProgressService service;
|
158
|
|
159
|
private IContextListener contextListener;
|
160
|
|
161
|
private CdmDataSourceViewerComparator comparator;
|
162
|
|
163
|
public CdmDataSourceViewPartE4(){
|
164
|
}
|
165
|
|
166
|
|
167
|
private ISelectionChangedListener selectionChangedListener;
|
168
|
|
169
|
@Inject
|
170
|
private ESelectionService selService;
|
171
|
|
172
|
@PostConstruct
|
173
|
public void create(Composite parent, EMenuService menuService) {
|
174
|
// service = getSite().getAdapter(IWorkbenchSiteProgressService.class);
|
175
|
contextListener = new ContextListener();
|
176
|
CdmStore.getContextManager().addContextListener(contextListener);
|
177
|
|
178
|
// Create top composite
|
179
|
FillLayout fillLayout = new FillLayout();
|
180
|
fillLayout.marginWidth = 0;
|
181
|
fillLayout.marginHeight = 0;
|
182
|
fillLayout.type = SWT.VERTICAL;
|
183
|
parent.setLayout(fillLayout);
|
184
|
|
185
|
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
|
186
|
| SWT.V_SCROLL | SWT.FULL_SELECTION);
|
187
|
|
188
|
createColumns(viewer);
|
189
|
|
190
|
viewer.setContentProvider(new CdmDataSourceContentProvider());
|
191
|
viewer.setLabelProvider(new CdmDataSourceLabelProvider());
|
192
|
comparator = new CdmDataSourceViewerComparator();
|
193
|
viewer.setComparator(comparator);
|
194
|
|
195
|
|
196
|
// Propagate selection from viewer
|
197
|
selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
|
198
|
viewer.addSelectionChangedListener(selectionChangedListener);
|
199
|
|
200
|
//create context menu
|
201
|
menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.store.popupmenu.datasourceview");
|
202
|
|
203
|
refresh();
|
204
|
}
|
205
|
|
206
|
|
207
|
// This will create the columns for the table
|
208
|
private void createColumns(TableViewer viewer) {
|
209
|
Table table = viewer.getTable();
|
210
|
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};
|
211
|
int[] bounds = { 24, 200, 100, 50 , 80, 120, 100, 100, 50, 300};
|
212
|
|
213
|
for (int i = 0; i < titles.length; i++) {
|
214
|
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
|
215
|
column.getColumn().setText(titles[i]);
|
216
|
column.getColumn().setWidth(bounds[i]);
|
217
|
column.getColumn().setResizable(true);
|
218
|
column.getColumn().setMoveable(true);
|
219
|
column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), i));
|
220
|
if(i == 1){
|
221
|
table.setSortColumn(column.getColumn());
|
222
|
}
|
223
|
}
|
224
|
table.setHeaderVisible(true);
|
225
|
table.setLinesVisible(true);
|
226
|
table.setSortDirection(SWT.UP);
|
227
|
|
228
|
}
|
229
|
|
230
|
public void refresh(){
|
231
|
DataSourceJob dataSourceJob = new DataSourceJob(Messages.CdmDataSourceViewPart_1, CdmDataSourceRepository.getAll());
|
232
|
dataSourceJob.schedule();
|
233
|
}
|
234
|
|
235
|
/** {@inheritDoc} */
|
236
|
@PreDestroy
|
237
|
public void dispose() {
|
238
|
CdmStore.getContextManager().removeContextListener(contextListener);
|
239
|
}
|
240
|
|
241
|
/** {@inheritDoc} */
|
242
|
@Focus
|
243
|
public void setFocus() {
|
244
|
viewer.getControl().setFocus();
|
245
|
}
|
246
|
|
247
|
//FIXME E4 migrate
|
248
|
// /** {@inheritDoc} */
|
249
|
// @Override
|
250
|
// public void showBusy(boolean busy) {
|
251
|
// super.showBusy(busy);
|
252
|
//// viewer.getTable().setEnabled(!busy);
|
253
|
// if(busy){
|
254
|
// partNameCache = getPartName();
|
255
|
// setPartName(Messages.CdmDataSourceViewPart_1);
|
256
|
// }else{
|
257
|
// if(partNameCache != null){
|
258
|
// setPartName(partNameCache);
|
259
|
// }
|
260
|
// }
|
261
|
// }
|
262
|
|
263
|
public StructuredViewer getViewer() {
|
264
|
return viewer;
|
265
|
}
|
266
|
|
267
|
// public IWorkbenchSiteProgressService getService() {
|
268
|
// return service;
|
269
|
// }
|
270
|
|
271
|
private SelectionAdapter getSelectionAdapter(final TableColumn column,
|
272
|
final int index) {
|
273
|
SelectionAdapter selectionAdapter = new SelectionAdapter() {
|
274
|
@Override
|
275
|
public void widgetSelected(SelectionEvent e) {
|
276
|
comparator.setColumn(index);
|
277
|
int dir = viewer.getTable().getSortDirection();
|
278
|
if (viewer.getTable().getSortColumn() == column) {
|
279
|
dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
|
280
|
} else {
|
281
|
dir = SWT.DOWN;
|
282
|
}
|
283
|
viewer.getTable().setSortDirection(dir);
|
284
|
viewer.getTable().setSortColumn(column);
|
285
|
viewer.refresh();
|
286
|
}
|
287
|
};
|
288
|
return selectionAdapter;
|
289
|
}
|
290
|
}
|