3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.store
.datasource
.view
;
13 import java
.util
.List
;
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
;
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
;
48 public class CdmDataSourceView
extends ViewPart
{
50 public static String ID
= "eu.etaxonomy.taxeditor.store.datasource.datasourceView";
52 private CdmDataSourceRepository cdmDataSourceRepository
;
54 private TableViewer tableViewer
;
56 public CdmDataSourceView(){
57 cdmDataSourceRepository
= CdmDataSourceRepository
.getDefault();
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
);
69 List
<ICdmDataSource
> datasources
= cdmDataSourceRepository
.getAll();
71 tableViewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
72 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
73 createColumns(tableViewer
);
75 tableViewer
.setContentProvider(new CdmDataSourceContentProvider());
76 tableViewer
.setLabelProvider(new CdmDataSourceLabelProvider());
78 tableViewer
.setInput(datasources
);
81 // register context menu
82 MenuManager menuMgr
= new MenuManager();
83 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
84 getSite().registerContextMenu(menuMgr
, tableViewer
);
86 Control control
= tableViewer
.getControl();
87 Menu menu
= menuMgr
.createContextMenu(control
);
88 control
.setMenu(menu
);
90 // connect on doubleclick
91 tableViewer
.addDoubleClickListener(new IDoubleClickListener() {
93 public void doubleClick(DoubleClickEvent event
) {
94 if (event
.getSelection() instanceof StructuredSelection
) {
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
;
102 boolean confirmed
= MessageDialog
.openConfirm(StoreUtil
.getShell(), "Confirm Datasource Change", "Do you really want to connect to this datasource?\n\n"
103 + dataSource
.getName());
106 CdmDataSourceRepository
.getDefault().setCurrentDataSource(dataSource
);
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 };
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);
129 table
.setHeaderVisible(true);
130 table
.setLinesVisible(true);
133 public void refresh(){
134 tableViewer
.setInput(cdmDataSourceRepository
.getAll());
135 tableViewer
.refresh();
140 public void setFocus() {
141 tableViewer
.getControl().setFocus();