2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.view
.datasource
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.jface
.action
.GroupMarker
;
21 import org
.eclipse
.jface
.action
.MenuManager
;
22 import org
.eclipse
.jface
.viewers
.StructuredViewer
;
23 import org
.eclipse
.jface
.viewers
.TableViewer
;
24 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.events
.SelectionAdapter
;
27 import org
.eclipse
.swt
.events
.SelectionEvent
;
28 import org
.eclipse
.swt
.layout
.FillLayout
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Control
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.swt
.widgets
.Menu
;
33 import org
.eclipse
.swt
.widgets
.Table
;
34 import org
.eclipse
.swt
.widgets
.TableColumn
;
35 import org
.eclipse
.ui
.IMemento
;
36 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
37 import org
.eclipse
.ui
.part
.ViewPart
;
38 import org
.eclipse
.ui
.progress
.IWorkbenchSiteProgressService
;
40 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
41 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
42 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
43 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
44 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
45 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
48 * <p>CdmDataSourceViewPart class.</p>
53 public class CdmDataSourceViewPart
extends ViewPart
{
55 private static final Logger logger
= Logger
.getLogger(CdmDataSourceViewPart
.class);
57 private class ContextListener
extends ContextListenerAdapter
{
59 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
60 monitor
.subTask("Refreshing Datasource View"); //$NON-NLS-1$
65 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
66 monitor
.subTask("Refreshing Datasource View"); //$NON-NLS-1$
71 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
72 monitor
.subTask("Refreshing Datasource View"); //$NON-NLS-1$
77 private class DataSourceJob
extends Job
{
79 private final List
<ICdmSource
> cdmSources
;
84 public DataSourceJob(String title
, List
<ICdmSource
> cdmSources
) {
86 this.cdmSources
= cdmSources
;
90 public IStatus
run(final IProgressMonitor monitor
) {
92 logger
.debug("Begin of eclipse core runtime Job to Retrieve datasources"); //$NON-NLS-1$
93 monitor
.beginTask("Retrieving datasources", cdmSources
.size() + 1); //$NON-NLS-1$
95 final List
<CdmMetaDataAwareDataSourceContainer
> containers
= new ArrayList
<CdmMetaDataAwareDataSourceContainer
>();
97 for(ICdmSource cdmSource
: cdmSources
){
98 containers
.add(new CdmMetaDataAwareDataSourceContainer(cdmSource
));
101 Display
.getDefault().asyncExec(new Runnable() {
105 viewer
.setInput(containers
);
110 for(final CdmMetaDataAwareDataSourceContainer container
: containers
){
111 if(logger
.isDebugEnabled()) {
112 logger
.debug(" #" + container
.hashCode() + " : next DataSourceContainer"); //$NON-NLS-1$ //$NON-NLS-2$
114 container
.getMetaDataFromDataSource();
115 if(logger
.isDebugEnabled())
117 logger
.debug(" #" + container
.hashCode() + " : metadata retrieved, creating new runnable ..."); //$NON-NLS-1$ //$NON-NLS-2$
119 Display
.getDefault().asyncExec(new Runnable() {
123 if(logger
.isDebugEnabled())
125 logger
.debug(" #" + container
.hashCode() + " starting sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
127 viewer
.update(container
, null);
128 if(logger
.isDebugEnabled())
130 logger
.debug(" #" + container
.hashCode() + " end of sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
134 if(logger
.isDebugEnabled())
136 logger
.debug(" #" + container
.hashCode() + " done"); //$NON-NLS-1$ //$NON-NLS-2$
144 return Status
.OK_STATUS
;
148 /** Constant <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code> */
149 public static String ID
= "eu.etaxonomy.taxeditor.view.datasource"; //$NON-NLS-1$
151 private TableViewer viewer
;
153 private String partNameCache
;
155 private IWorkbenchSiteProgressService service
;
157 private IContextListener contextListener
;
159 private CdmDataSourceViewerComparator comparator
;
162 * <p>Constructor for CdmDataSourceViewPart.</p>
164 public CdmDataSourceViewPart(){
169 public void createPartControl(Composite parent
) {
170 service
= (IWorkbenchSiteProgressService
) getSite().getAdapter(IWorkbenchSiteProgressService
.class);
171 contextListener
= new ContextListener();
172 CdmStore
.getContextManager().addContextListener(contextListener
);
174 // Create top composite
175 FillLayout fillLayout
= new FillLayout();
176 fillLayout
.marginWidth
= 0;
177 fillLayout
.marginHeight
= 0;
178 fillLayout
.type
= SWT
.VERTICAL
;
179 parent
.setLayout(fillLayout
);
181 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
182 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
183 getSite().setSelectionProvider(viewer
);
185 createColumns(viewer
);
187 viewer
.setContentProvider(new CdmDataSourceContentProvider());
188 viewer
.setLabelProvider(new CdmDataSourceLabelProvider());
189 comparator
= new CdmDataSourceViewerComparator();
190 viewer
.setComparator(comparator
);
193 // register context menu
194 MenuManager menuMgr
= new MenuManager();
195 menuMgr
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
196 getSite().registerContextMenu(menuMgr
, viewer
);
198 Control control
= viewer
.getControl();
199 Menu menu
= menuMgr
.createContextMenu(control
);
200 control
.setMenu(menu
);
206 // This will create the columns for the table
207 private void createColumns(TableViewer viewer
) {
208 Table table
= viewer
.getTable();
209 String
[] titles
= {Messages
.CdmDataSourceViewPart_12
, Messages
.CdmDataSourceViewPart_11
, Messages
.CdmDataSourceViewPart_10
, Messages
.CdmDataSourceViewPart_9
, Messages
.CdmDataSourceViewPart_8
, Messages
.CdmDataSourceViewPart_7
, Messages
.CdmDataSourceViewPart_5
, Messages
.CdmDataSourceViewPart_4
, Messages
.CdmDataSourceViewPart_3
, Messages
.CdmDataSourceViewPart_2
};
210 int[] bounds
= { 24, 200, 100, 50 , 80, 120, 100, 100, 50, 300};
212 for (int i
= 0; i
< titles
.length
; i
++) {
213 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
214 column
.getColumn().setText(titles
[i
]);
215 column
.getColumn().setWidth(bounds
[i
]);
216 column
.getColumn().setResizable(true);
217 column
.getColumn().setMoveable(true);
218 column
.getColumn().addSelectionListener(getSelectionAdapter(column
.getColumn(), i
));
220 table
.setSortColumn(column
.getColumn());
223 table
.setHeaderVisible(true);
224 table
.setLinesVisible(true);
225 table
.setSortDirection(SWT
.UP
);
232 public void refresh(){
233 getService().schedule(new DataSourceJob(Messages
.CdmDataSourceViewPart_1
, CdmDataSourceRepository
.getAll()), Job
.LONG
);
238 public void dispose() {
239 CdmStore
.getContextManager().removeContextListener(contextListener
);
245 public void setFocus() {
246 viewer
.getControl().setFocus();
251 public void showBusy(boolean busy
) {
252 super.showBusy(busy
);
253 // viewer.getTable().setEnabled(!busy);
255 partNameCache
= getPartName();
256 setPartName(Messages
.CdmDataSourceViewPart_1
);
258 if(partNameCache
!= null){
259 setPartName(partNameCache
);
265 * <p>Getter for the field <code>viewer</code>.</p>
267 * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
269 public StructuredViewer
getViewer() {
274 * <p>Getter for the field <code>service</code>.</p>
276 * @return the service
278 public IWorkbenchSiteProgressService
getService() {
282 private SelectionAdapter
getSelectionAdapter(final TableColumn column
,
284 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
286 public void widgetSelected(SelectionEvent e
) {
287 comparator
.setColumn(index
);
288 int dir
= viewer
.getTable().getSortDirection();
289 if (viewer
.getTable().getSortColumn() == column
) {
290 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
294 viewer
.getTable().setSortDirection(dir
);
295 viewer
.getTable().setSortColumn(column
);
299 return selectionAdapter
;