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
.view
.datasource
;
13 import java
.util
.ArrayList
;
14 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.core
.runtime
.jobs
.Job
;
21 import org
.eclipse
.jface
.action
.GroupMarker
;
22 import org
.eclipse
.jface
.action
.MenuManager
;
23 import org
.eclipse
.jface
.viewers
.StructuredViewer
;
24 import org
.eclipse
.jface
.viewers
.TableViewer
;
25 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.events
.SelectionAdapter
;
28 import org
.eclipse
.swt
.events
.SelectionEvent
;
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
.Display
;
33 import org
.eclipse
.swt
.widgets
.Menu
;
34 import org
.eclipse
.swt
.widgets
.Table
;
35 import org
.eclipse
.swt
.widgets
.TableColumn
;
36 import org
.eclipse
.ui
.IMemento
;
37 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
38 import org
.eclipse
.ui
.part
.ViewPart
;
39 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
41 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
42 import eu
.etaxonomy
.taxeditor
.Messages
;
43 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
44 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
45 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
46 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
49 * <p>CdmDataSourceViewPart class.</p>
55 public class CdmDataSourceViewPart
extends ViewPart
{
57 private static final Logger logger
= Logger
.getLogger(CdmDataSourceViewPart
.class);
59 private class ContextListener
extends ContextListenerAdapter
{
61 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
62 monitor
.subTask("Refreshing Datasource View"); //$NON-NLS-1$
67 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
68 monitor
.subTask("Refreshing Datasource View"); //$NON-NLS-1$
73 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
74 monitor
.subTask("Refreshing Datasource View"); //$NON-NLS-1$
79 private class DataSourceJob
extends Job
{
81 private final List
<ICdmSource
> cdmSources
;
86 public DataSourceJob(String title
, List
<ICdmSource
> cdmSources
) {
88 this.cdmSources
= cdmSources
;
92 public IStatus
run(final IProgressMonitor monitor
) {
94 logger
.debug("Begin of eclipse core runtime Job to Retrieve datasources"); //$NON-NLS-1$
95 monitor
.beginTask("Retrieving datasources", cdmSources
.size() + 1); //$NON-NLS-1$
97 final List
<CdmMetaDataAwareDataSourceContainer
> containers
= new ArrayList
<CdmMetaDataAwareDataSourceContainer
>();
99 for(ICdmSource cdmSource
: cdmSources
){
100 containers
.add(new CdmMetaDataAwareDataSourceContainer(cdmSource
));
103 Display
.getDefault().asyncExec(new Runnable() {
107 viewer
.setInput(containers
);
112 for(final CdmMetaDataAwareDataSourceContainer container
: containers
){
113 if(logger
.isDebugEnabled()) {
114 logger
.debug(" #" + container
.hashCode() + " : next DataSourceContainer"); //$NON-NLS-1$ //$NON-NLS-2$
116 container
.getMetaDataFromDataSource();
117 if(logger
.isDebugEnabled())
119 logger
.debug(" #" + container
.hashCode() + " : metadata retrieved, creating new runnable ..."); //$NON-NLS-1$ //$NON-NLS-2$
121 Display
.getDefault().asyncExec(new Runnable() {
125 if(logger
.isDebugEnabled())
127 logger
.debug(" #" + container
.hashCode() + " starting sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
129 viewer
.update(container
, null);
130 if(logger
.isDebugEnabled())
132 logger
.debug(" #" + container
.hashCode() + " end of sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
136 if(logger
.isDebugEnabled())
138 logger
.debug(" #" + container
.hashCode() + " done"); //$NON-NLS-1$ //$NON-NLS-2$
146 return Status
.OK_STATUS
;
150 /** Constant <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code> */
151 public static String ID
= "eu.etaxonomy.taxeditor.view.datasource"; //$NON-NLS-1$
153 private TableViewer viewer
;
155 private String partNameCache
;
157 private IWorkbenchSiteProgressService service
;
159 private IContextListener contextListener
;
161 private CdmDataSourceViewerComparator comparator
;
164 * <p>Constructor for CdmDataSourceViewPart.</p>
166 public CdmDataSourceViewPart(){
171 public void createPartControl(Composite parent
) {
172 service
= (IWorkbenchSiteProgressService
) getSite().getAdapter(IWorkbenchSiteProgressService
.class);
173 contextListener
= new ContextListener();
174 CdmStore
.getContextManager().addContextListener(contextListener
);
176 // Create top composite
177 FillLayout fillLayout
= new FillLayout();
178 fillLayout
.marginWidth
= 0;
179 fillLayout
.marginHeight
= 0;
180 fillLayout
.type
= SWT
.VERTICAL
;
181 parent
.setLayout(fillLayout
);
183 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
184 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
185 getSite().setSelectionProvider(viewer
);
187 createColumns(viewer
);
189 viewer
.setContentProvider(new CdmDataSourceContentProvider());
190 viewer
.setLabelProvider(new CdmDataSourceLabelProvider());
191 comparator
= new CdmDataSourceViewerComparator();
192 viewer
.setComparator(comparator
);
195 // register context menu
196 MenuManager menuMgr
= new MenuManager();
197 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
198 getSite().registerContextMenu(menuMgr
, viewer
);
200 Control control
= viewer
.getControl();
201 Menu menu
= menuMgr
.createContextMenu(control
);
202 control
.setMenu(menu
);
208 // This will create the columns for the table
209 private void createColumns(TableViewer viewer
) {
210 Table table
= viewer
.getTable();
211 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
};
212 int[] bounds
= { 24, 200, 100, 50 , 80, 120, 50, 100, 100, 50, 300};
214 for (int i
= 0; i
< titles
.length
; i
++) {
215 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
216 column
.getColumn().setText(titles
[i
]);
217 column
.getColumn().setWidth(bounds
[i
]);
218 column
.getColumn().setResizable(true);
219 column
.getColumn().setMoveable(true);
220 column
.getColumn().addSelectionListener(getSelectionAdapter(column
.getColumn(), i
));
222 table
.setSortColumn(column
.getColumn());
225 table
.setHeaderVisible(true);
226 table
.setLinesVisible(true);
227 table
.setSortDirection(SWT
.UP
);
234 public void refresh(){
235 getService().schedule(new DataSourceJob(Messages
.CdmDataSourceViewPart_1
, CdmDataSourceRepository
.getAll()), Job
.LONG
);
240 public void dispose() {
241 CdmStore
.getContextManager().removeContextListener(contextListener
);
247 public void setFocus() {
248 viewer
.getControl().setFocus();
253 public void showBusy(boolean busy
) {
254 super.showBusy(busy
);
255 // viewer.getTable().setEnabled(!busy);
257 partNameCache
= getPartName();
258 setPartName(Messages
.CdmDataSourceViewPart_1
);
260 if(partNameCache
!= null){
261 setPartName(partNameCache
);
267 * <p>Getter for the field <code>viewer</code>.</p>
269 * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
271 public StructuredViewer
getViewer() {
276 * <p>Getter for the field <code>service</code>.</p>
278 * @return the service
280 public IWorkbenchSiteProgressService
getService() {
284 private SelectionAdapter
getSelectionAdapter(final TableColumn column
,
286 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
288 public void widgetSelected(SelectionEvent e
) {
289 comparator
.setColumn(index
);
290 int dir
= viewer
.getTable().getSortDirection();
291 if (viewer
.getTable().getSortColumn() == column
) {
292 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
296 viewer
.getTable().setSortDirection(dir
);
297 viewer
.getTable().setSortColumn(column
);
301 return selectionAdapter
;