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