1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.view.datasource;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
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.core.runtime.IStatus;
|
24
|
import org.eclipse.core.runtime.Status;
|
25
|
import org.eclipse.core.runtime.jobs.Job;
|
26
|
import org.eclipse.jface.action.GroupMarker;
|
27
|
import org.eclipse.jface.action.MenuManager;
|
28
|
import org.eclipse.jface.viewers.DoubleClickEvent;
|
29
|
import org.eclipse.jface.viewers.IDoubleClickListener;
|
30
|
import org.eclipse.jface.viewers.StructuredSelection;
|
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.Control;
|
40
|
import org.eclipse.swt.widgets.Display;
|
41
|
import org.eclipse.swt.widgets.Menu;
|
42
|
import org.eclipse.swt.widgets.Table;
|
43
|
import org.eclipse.swt.widgets.TableColumn;
|
44
|
import org.eclipse.ui.IMemento;
|
45
|
import org.eclipse.ui.IWorkbenchActionConstants;
|
46
|
import org.eclipse.ui.commands.ICommandService;
|
47
|
import org.eclipse.ui.handlers.IHandlerService;
|
48
|
import org.eclipse.ui.part.ViewPart;
|
49
|
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
|
50
|
import org.springframework.security.core.GrantedAuthority;
|
51
|
|
52
|
import eu.etaxonomy.cdm.config.ICdmSource;
|
53
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
54
|
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
|
55
|
import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
|
56
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
57
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
58
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
59
|
|
60
|
/**
|
61
|
* <p>CdmDataSourceViewPart class.</p>
|
62
|
*
|
63
|
* @author n.hoffmann
|
64
|
* @created 14.04.2009
|
65
|
* @version 1.0
|
66
|
*/
|
67
|
public class CdmDataSourceViewPart extends ViewPart{
|
68
|
|
69
|
private static final Logger logger = Logger.getLogger(CdmDataSourceViewPart.class);
|
70
|
|
71
|
private class ContextListener extends ContextListenerAdapter{
|
72
|
/* (non-Javadoc)
|
73
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
74
|
*/
|
75
|
@Override
|
76
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
77
|
monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
|
78
|
viewer.refresh();
|
79
|
}
|
80
|
|
81
|
/* (non-Javadoc)
|
82
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
83
|
*/
|
84
|
@Override
|
85
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
86
|
monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
|
87
|
viewer.refresh();
|
88
|
}
|
89
|
|
90
|
/* (non-Javadoc)
|
91
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
92
|
*/
|
93
|
@Override
|
94
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
95
|
monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
|
96
|
viewer.refresh();
|
97
|
}
|
98
|
}
|
99
|
|
100
|
private class DataSourceJob extends Job{
|
101
|
|
102
|
private List<ICdmSource> cdmSources;
|
103
|
|
104
|
/**
|
105
|
* @param name
|
106
|
*/
|
107
|
public DataSourceJob(String title, List<ICdmSource> cdmSources) {
|
108
|
super(title);
|
109
|
this.cdmSources = cdmSources;
|
110
|
}
|
111
|
|
112
|
/* (non-Javadoc)
|
113
|
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
|
114
|
*/
|
115
|
@Override
|
116
|
public IStatus run(final IProgressMonitor monitor) {
|
117
|
try{
|
118
|
logger.debug("Begin of eclipse core runtime Job to Retrieve datasources"); //$NON-NLS-1$
|
119
|
monitor.beginTask("Retrieving datasources", cdmSources.size() + 1); //$NON-NLS-1$
|
120
|
|
121
|
final List<CdmMetaDataAwareDataSourceContainer> containers = new ArrayList<CdmMetaDataAwareDataSourceContainer>();
|
122
|
|
123
|
for(ICdmSource cdmSource : cdmSources){
|
124
|
containers.add(new CdmMetaDataAwareDataSourceContainer(cdmSource));
|
125
|
}
|
126
|
|
127
|
Display.getDefault().asyncExec(new Runnable() {
|
128
|
|
129
|
@Override
|
130
|
public void run() {
|
131
|
viewer.setInput(containers);
|
132
|
}
|
133
|
});
|
134
|
monitor.worked(1);
|
135
|
|
136
|
for(final CdmMetaDataAwareDataSourceContainer container : containers){
|
137
|
|
138
|
|
139
|
if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " : next DataSourceContainer"); //$NON-NLS-1$ //$NON-NLS-2$
|
140
|
container.getMetaDataFromDataSource();
|
141
|
if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " : metadata retrieved, creating new runnable ..."); //$NON-NLS-1$ //$NON-NLS-2$
|
142
|
Display.getDefault().asyncExec(new Runnable() {
|
143
|
|
144
|
@Override
|
145
|
public void run() {
|
146
|
if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " starting sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
|
147
|
viewer.update(container, null);
|
148
|
if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " end of sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
|
149
|
}
|
150
|
});
|
151
|
if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " done"); //$NON-NLS-1$ //$NON-NLS-2$
|
152
|
monitor.worked(1);
|
153
|
}
|
154
|
|
155
|
}finally{
|
156
|
monitor.done();
|
157
|
}
|
158
|
return Status.OK_STATUS;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
/** Constant <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code> */
|
163
|
public static String ID = "eu.etaxonomy.taxeditor.view.datasource"; //$NON-NLS-1$
|
164
|
|
165
|
private TableViewer viewer;
|
166
|
|
167
|
private String partNameCache;
|
168
|
|
169
|
private IWorkbenchSiteProgressService service;
|
170
|
|
171
|
private IContextListener contextListener;
|
172
|
|
173
|
private CdmDataSourceViewerComparator comparator;
|
174
|
|
175
|
/**
|
176
|
* <p>Constructor for CdmDataSourceViewPart.</p>
|
177
|
*/
|
178
|
public CdmDataSourceViewPart(){
|
179
|
}
|
180
|
|
181
|
/** {@inheritDoc} */
|
182
|
@Override
|
183
|
public void createPartControl(Composite parent) {
|
184
|
service = (IWorkbenchSiteProgressService) getSite().getAdapter(IWorkbenchSiteProgressService.class);
|
185
|
contextListener = new ContextListener();
|
186
|
CdmStore.getContextManager().addContextListener(contextListener);
|
187
|
|
188
|
// Create top composite
|
189
|
FillLayout fillLayout = new FillLayout();
|
190
|
fillLayout.marginWidth = 0;
|
191
|
fillLayout.marginHeight = 0;
|
192
|
fillLayout.type = SWT.VERTICAL;
|
193
|
parent.setLayout(fillLayout);
|
194
|
|
195
|
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
|
196
|
| SWT.V_SCROLL | SWT.FULL_SELECTION);
|
197
|
getSite().setSelectionProvider(viewer);
|
198
|
|
199
|
createColumns(viewer);
|
200
|
|
201
|
viewer.setContentProvider(new CdmDataSourceContentProvider());
|
202
|
viewer.setLabelProvider(new CdmDataSourceLabelProvider());
|
203
|
comparator = new CdmDataSourceViewerComparator();
|
204
|
viewer.setComparator(comparator);
|
205
|
|
206
|
|
207
|
// register context menu
|
208
|
MenuManager menuMgr = new MenuManager();
|
209
|
menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
|
210
|
getSite().registerContextMenu(menuMgr, viewer);
|
211
|
|
212
|
Control control = viewer.getControl();
|
213
|
Menu menu = menuMgr.createContextMenu(control);
|
214
|
control.setMenu(menu);
|
215
|
|
216
|
// connect on doubleclick
|
217
|
viewer.addDoubleClickListener(new IDoubleClickListener() {
|
218
|
|
219
|
public void doubleClick(DoubleClickEvent event) {
|
220
|
if (event.getSelection() instanceof StructuredSelection) {
|
221
|
|
222
|
Object element = ((StructuredSelection) event
|
223
|
.getSelection()).getFirstElement();
|
224
|
if (element instanceof CdmMetaDataAwareDataSourceContainer) {
|
225
|
String commandId = "eu.etaxonomy.taxeditor.store.datasource.change"; //$NON-NLS-1$
|
226
|
|
227
|
ICommandService commandService = (ICommandService)getSite().getService(ICommandService.class);
|
228
|
|
229
|
Command command = commandService.getCommand(commandId);
|
230
|
if(command.isEnabled()) {
|
231
|
|
232
|
IHandlerService handlerService = (IHandlerService) StoreUtil.getService(IHandlerService.class);
|
233
|
|
234
|
try {
|
235
|
handlerService.executeCommand(commandId, null);
|
236
|
} catch (ExecutionException e) {
|
237
|
StoreUtil.error(this.getClass(), e);
|
238
|
} catch (NotDefinedException e) {
|
239
|
StoreUtil.error(this.getClass(), e);
|
240
|
} catch (NotEnabledException e) {
|
241
|
StoreUtil.error(this.getClass(), e);
|
242
|
} catch (NotHandledException e) {
|
243
|
StoreUtil.error(this.getClass(), e);
|
244
|
}
|
245
|
}
|
246
|
}
|
247
|
}
|
248
|
}
|
249
|
});
|
250
|
|
251
|
refresh();
|
252
|
}
|
253
|
|
254
|
|
255
|
// This will create the columns for the table
|
256
|
private void createColumns(TableViewer viewer) {
|
257
|
Table table = viewer.getTable();
|
258
|
String[] titles = {Messages.CdmDataSourceViewPart_12, Messages.CdmDataSourceViewPart_11, Messages.CdmDataSourceViewPart_10, Messages.CdmDataSourceViewPart_9, Messages.CdmDataSourceViewPart_8, Messages.CdmDataSourceViewPart_7, Messages.CdmDataSourceViewPart_6, Messages.CdmDataSourceViewPart_5, Messages.CdmDataSourceViewPart_4, Messages.CdmDataSourceViewPart_3, Messages.CdmDataSourceViewPart_2};
|
259
|
int[] bounds = { 24, 200, 100, 50 , 80, 120, 50, 100, 100, 50, 300};
|
260
|
|
261
|
for (int i = 0; i < titles.length; i++) {
|
262
|
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
|
263
|
column.getColumn().setText(titles[i]);
|
264
|
column.getColumn().setWidth(bounds[i]);
|
265
|
column.getColumn().setResizable(true);
|
266
|
column.getColumn().setMoveable(true);
|
267
|
column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), i));
|
268
|
if(i == 1){
|
269
|
table.setSortColumn(column.getColumn());
|
270
|
}
|
271
|
}
|
272
|
table.setHeaderVisible(true);
|
273
|
table.setLinesVisible(true);
|
274
|
table.setSortDirection(SWT.UP);
|
275
|
|
276
|
}
|
277
|
|
278
|
/**
|
279
|
* <p>refresh</p>
|
280
|
*/
|
281
|
public void refresh(){
|
282
|
getService().schedule(new DataSourceJob(Messages.CdmDataSourceViewPart_1, CdmDataSourceRepository.getAll()), Job.LONG);
|
283
|
}
|
284
|
|
285
|
/** {@inheritDoc} */
|
286
|
@Override
|
287
|
public void dispose() {
|
288
|
CdmStore.getContextManager().removeContextListener(contextListener);
|
289
|
super.dispose();
|
290
|
}
|
291
|
|
292
|
/** {@inheritDoc} */
|
293
|
@Override
|
294
|
public void setFocus() {
|
295
|
viewer.getControl().setFocus();
|
296
|
}
|
297
|
|
298
|
|
299
|
/* (non-Javadoc)
|
300
|
* @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
|
301
|
*/
|
302
|
/** {@inheritDoc} */
|
303
|
@Override
|
304
|
public void showBusy(boolean busy) {
|
305
|
super.showBusy(busy);
|
306
|
// viewer.getTable().setEnabled(!busy);
|
307
|
if(busy){
|
308
|
partNameCache = getPartName();
|
309
|
setPartName(Messages.CdmDataSourceViewPart_1);
|
310
|
}else{
|
311
|
if(partNameCache != null){
|
312
|
setPartName(partNameCache);
|
313
|
}
|
314
|
}
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* <p>Getter for the field <code>viewer</code>.</p>
|
319
|
*
|
320
|
* @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
|
321
|
*/
|
322
|
public StructuredViewer getViewer() {
|
323
|
return viewer;
|
324
|
}
|
325
|
|
326
|
/**
|
327
|
* <p>Getter for the field <code>service</code>.</p>
|
328
|
*
|
329
|
* @return the service
|
330
|
*/
|
331
|
public IWorkbenchSiteProgressService getService() {
|
332
|
return service;
|
333
|
}
|
334
|
|
335
|
private SelectionAdapter getSelectionAdapter(final TableColumn column,
|
336
|
final int index) {
|
337
|
SelectionAdapter selectionAdapter = new SelectionAdapter() {
|
338
|
@Override
|
339
|
public void widgetSelected(SelectionEvent e) {
|
340
|
comparator.setColumn(index);
|
341
|
int dir = viewer.getTable().getSortDirection();
|
342
|
if (viewer.getTable().getSortColumn() == column) {
|
343
|
dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
|
344
|
} else {
|
345
|
dir = SWT.DOWN;
|
346
|
}
|
347
|
viewer.getTable().setSortDirection(dir);
|
348
|
viewer.getTable().setSortColumn(column);
|
349
|
viewer.refresh();
|
350
|
}
|
351
|
};
|
352
|
return selectionAdapter;
|
353
|
}
|
354
|
|
355
|
}
|