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.store.datasource.view;
|
12
|
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.eclipse.core.commands.ExecutionEvent;
|
16
|
import org.eclipse.core.commands.ExecutionException;
|
17
|
import org.eclipse.core.commands.NotEnabledException;
|
18
|
import org.eclipse.core.commands.NotHandledException;
|
19
|
import org.eclipse.core.commands.common.NotDefinedException;
|
20
|
import org.eclipse.jface.action.GroupMarker;
|
21
|
import org.eclipse.jface.action.MenuManager;
|
22
|
import org.eclipse.jface.dialogs.MessageDialog;
|
23
|
import org.eclipse.jface.viewers.DoubleClickEvent;
|
24
|
import org.eclipse.jface.viewers.IDoubleClickListener;
|
25
|
import org.eclipse.jface.viewers.StructuredSelection;
|
26
|
import org.eclipse.jface.viewers.TableViewer;
|
27
|
import org.eclipse.jface.viewers.TableViewerColumn;
|
28
|
import org.eclipse.swt.SWT;
|
29
|
import org.eclipse.swt.layout.FillLayout;
|
30
|
import org.eclipse.swt.widgets.Composite;
|
31
|
import org.eclipse.swt.widgets.Control;
|
32
|
import org.eclipse.swt.widgets.Menu;
|
33
|
import org.eclipse.swt.widgets.Table;
|
34
|
import org.eclipse.ui.IWorkbenchActionConstants;
|
35
|
import org.eclipse.ui.handlers.IHandlerService;
|
36
|
import org.eclipse.ui.part.ViewPart;
|
37
|
|
38
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
39
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
40
|
import eu.etaxonomy.taxeditor.store.datasource.CdmDataSourceRepository;
|
41
|
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
|
42
|
|
43
|
/**
|
44
|
* @author n.hoffmann
|
45
|
* @created 14.04.2009
|
46
|
* @version 1.0
|
47
|
*/
|
48
|
public class CdmDataSourceView extends ViewPart{
|
49
|
|
50
|
public static String ID = "eu.etaxonomy.taxeditor.store.datasource.datasourceView";
|
51
|
|
52
|
private CdmDataSourceRepository cdmDataSourceRepository;
|
53
|
|
54
|
private TableViewer tableViewer;
|
55
|
|
56
|
public CdmDataSourceView(){
|
57
|
cdmDataSourceRepository = CdmDataSourceRepository.getDefault();
|
58
|
}
|
59
|
|
60
|
@Override
|
61
|
public void createPartControl(Composite parent) {
|
62
|
// Create top composite
|
63
|
FillLayout fillLayout = new FillLayout();
|
64
|
fillLayout.marginWidth = 0;
|
65
|
fillLayout.marginHeight = 0;
|
66
|
fillLayout.type = SWT.VERTICAL;
|
67
|
parent.setLayout(fillLayout);
|
68
|
|
69
|
List<ICdmDataSource> datasources = cdmDataSourceRepository.getAll();
|
70
|
|
71
|
tableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
|
72
|
| SWT.V_SCROLL | SWT.FULL_SELECTION);
|
73
|
createColumns(tableViewer);
|
74
|
|
75
|
tableViewer.setContentProvider(new CdmDataSourceContentProvider());
|
76
|
tableViewer.setLabelProvider(new CdmDataSourceLabelProvider());
|
77
|
|
78
|
tableViewer.setInput(datasources);
|
79
|
|
80
|
|
81
|
// register context menu
|
82
|
MenuManager menuMgr = new MenuManager();
|
83
|
menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
|
84
|
getSite().registerContextMenu(menuMgr, tableViewer);
|
85
|
|
86
|
Control control = tableViewer.getControl();
|
87
|
Menu menu = menuMgr.createContextMenu(control);
|
88
|
control.setMenu(menu);
|
89
|
|
90
|
// connect on doubleclick
|
91
|
tableViewer.addDoubleClickListener(new IDoubleClickListener() {
|
92
|
|
93
|
public void doubleClick(DoubleClickEvent event) {
|
94
|
if (event.getSelection() instanceof StructuredSelection) {
|
95
|
|
96
|
Object element = ((StructuredSelection) event
|
97
|
.getSelection()).getFirstElement();
|
98
|
if (element instanceof ICdmDataSource) {
|
99
|
// FIXME this is duplicated code from ChangeConnectionHandler. Replace this with a call to HandlerService.
|
100
|
ICdmDataSource dataSource = (ICdmDataSource) element;
|
101
|
|
102
|
boolean confirmed = MessageDialog.openConfirm(StoreUtil.getShell(), "Confirm Datasource Change", "Do you really want to connect to this datasource?\n\n"
|
103
|
+ dataSource.getName());
|
104
|
|
105
|
if(confirmed){
|
106
|
CdmDataSourceRepository.getDefault().setCurrentDataSource(dataSource);
|
107
|
}
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
});
|
112
|
|
113
|
}
|
114
|
|
115
|
|
116
|
// This will create the columns for the table
|
117
|
private void createColumns(TableViewer viewer) {
|
118
|
Table table = viewer.getTable();
|
119
|
String[] titles = {"Connected", "Name", "Server", "Type"};
|
120
|
int[] bounds = { 20, 100, 100, 100 };
|
121
|
|
122
|
for (int i = 0; i < titles.length; i++) {
|
123
|
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
|
124
|
column.getColumn().setText(titles[i]);
|
125
|
column.getColumn().setWidth(bounds[i]);
|
126
|
column.getColumn().setResizable(true);
|
127
|
column.getColumn().setMoveable(true);
|
128
|
}
|
129
|
table.setHeaderVisible(true);
|
130
|
table.setLinesVisible(true);
|
131
|
}
|
132
|
|
133
|
public void refresh(){
|
134
|
tableViewer.setInput(cdmDataSourceRepository.getAll());
|
135
|
tableViewer.refresh();
|
136
|
}
|
137
|
|
138
|
|
139
|
@Override
|
140
|
public void setFocus() {
|
141
|
tableViewer.getControl().setFocus();
|
142
|
}
|
143
|
}
|