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.
9 package eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.e4
;
11 import java
.util
.ArrayList
;
12 import java
.util
.List
;
14 import javax
.annotation
.PostConstruct
;
15 import javax
.annotation
.PreDestroy
;
16 import javax
.inject
.Inject
;
18 import org
.apache
.logging
.log4j
.LogManager
;
19 import org
.apache
.logging
.log4j
.Logger
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.core
.runtime
.IStatus
;
22 import org
.eclipse
.core
.runtime
.Status
;
23 import org
.eclipse
.core
.runtime
.jobs
.Job
;
24 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
25 import org
.eclipse
.e4
.ui
.di
.Focus
;
26 import org
.eclipse
.e4
.ui
.di
.UIEventTopic
;
27 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
28 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
29 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
30 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
31 import org
.eclipse
.jface
.viewers
.StructuredViewer
;
32 import org
.eclipse
.jface
.viewers
.TableViewer
;
33 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.events
.SelectionAdapter
;
36 import org
.eclipse
.swt
.events
.SelectionEvent
;
37 import org
.eclipse
.swt
.layout
.FillLayout
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Table
;
40 import org
.eclipse
.swt
.widgets
.TableColumn
;
41 import org
.eclipse
.ui
.IMemento
;
43 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
44 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
45 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
46 import eu
.etaxonomy
.taxeditor
.local
.datasource
.common
.CdmDataSourceRepository
;
47 import eu
.etaxonomy
.taxeditor
.local
.datasource
.common
.CdmRemoteServerRepository
;
48 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmDataSourceContentProvider
;
49 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmDataSourceLabelProvider
;
50 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmDataSourceViewerComparator
;
51 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmMetaDataAwareDataSourceContainer
;
52 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmRemoteServerContentProvider
;
53 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmRemoteServerLabelProvider
;
54 import eu
.etaxonomy
.taxeditor
.local
.view
.datasource
.CdmRemoteServerViewerComparator
;
55 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
56 import eu
.etaxonomy
.taxeditor
.model
.IContextListener
;
57 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmServerInfo
;
58 import eu
.etaxonomy
.taxeditor
.remoting
.source
.CdmServerInfoConfig
;
59 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
65 public class CdmRemoteServerViewPart
{
67 // private static final Logger logger = LogManager.getLogger(CdmRemoteServerViewPart.class);
70 private UISynchronize sync
;
74 private class RemoteServerJob
extends Job
{
76 private final List
<CdmServerInfoConfig
> cdmSources
;
78 public RemoteServerJob(String title
, List
<CdmServerInfoConfig
> cdmSources
) {
80 this.cdmSources
= cdmSources
;
84 public IStatus
run(final IProgressMonitor monitor
) {
86 //logger.debug("Begin of eclipse core runtime Job to Retrieve datasources"); //$NON-NLS-1$
87 monitor
.beginTask("Retrieving datasources", cdmSources
.size() + 1); //$NON-NLS-1$
91 sync
.asyncExec(()->viewer
.setInput(this.cdmSources
));
95 for(final CdmServerInfoConfig container
: cdmSources
){
96 // if(logger.isDebugEnabled()) {
97 // logger.debug(" #" + container.hashCode() + " : next DataSourceContainer"); //$NON-NLS-1$ //$NON-NLS-2$
100 sync
.asyncExec(()-> {
101 // if(logger.isDebugEnabled()){
102 // logger.debug(" #" + container.hashCode() + " starting sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
104 viewer
.update(container
, null);
105 // if(logger.isDebugEnabled()){
106 // logger.debug(" #" + container.hashCode() + " end of sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
110 // if(logger.isDebugEnabled()) {
111 // logger.debug(" #" + container.hashCode() + " done"); //$NON-NLS-1$ //$NON-NLS-2$
119 return Status
.OK_STATUS
;
123 private TableViewer viewer
;
125 private CdmRemoteServerViewerComparator comparator
;
127 public CdmRemoteServerViewPart(){
130 private ISelectionChangedListener selectionChangedListener
;
133 private ESelectionService selService
;
136 public void create(Composite parent
, EMenuService menuService
) {
137 // Create top composite
138 FillLayout fillLayout
= new FillLayout();
139 fillLayout
.marginWidth
= 0;
140 fillLayout
.marginHeight
= 0;
141 fillLayout
.type
= SWT
.VERTICAL
;
142 parent
.setLayout(fillLayout
);
144 viewer
= new TableViewer(parent
, SWT
.H_SCROLL
145 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
147 createColumns(viewer
);
149 viewer
.setContentProvider(new CdmRemoteServerContentProvider());
150 viewer
.setLabelProvider(new CdmRemoteServerLabelProvider());
151 comparator
= new CdmRemoteServerViewerComparator();
152 viewer
.setComparator(comparator
);
154 // Propagate selection from viewer
155 selectionChangedListener
= (event
-> selService
.setSelection(event
.getSelection()));
156 viewer
.addSelectionChangedListener(selectionChangedListener
);
158 //create context menu
159 menuService
.registerContextMenu(viewer
.getControl(), "eu.etaxonomy.taxeditor.local.popupmenu.remoteServerViewPart");
165 // This will create the columns for the table
166 private void createColumns(TableViewer viewer
) {
167 Table table
= viewer
.getTable();
168 String
[] titles
= {"Name", "Server", "Prefix", "Port"};
169 int[] bounds
= { 200, 200, 100, 50 };
171 for (int i
= 0; i
< titles
.length
; i
++) {
172 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
173 column
.getColumn().setText(titles
[i
]);
174 column
.getColumn().setWidth(bounds
[i
]);
175 column
.getColumn().setResizable(true);
176 column
.getColumn().setMoveable(true);
177 column
.getColumn().addSelectionListener(getSelectionAdapter(column
.getColumn(), i
));
179 table
.setSortColumn(column
.getColumn());
182 table
.setHeaderVisible(true);
183 table
.setLinesVisible(true);
184 table
.setSortDirection(SWT
.UP
);
189 private void updateView(@UIEventTopic(WorkbenchEventConstants
.REFRESH_DATASOURCE
)boolean refresh
){
195 public void refresh(){
196 RemoteServerJob dataSourceJob
= new RemoteServerJob(Messages
.CdmDataSourceViewPart_1
, CdmRemoteServerRepository
.getAll());
197 dataSourceJob
.schedule();
201 public void setFocus() {
202 if (viewer
.getControl() != null && !viewer
.getControl().isDisposed()) {
203 viewer
.getControl().setFocus();
209 // public void showBusy(boolean busy) {
210 // super.showBusy(busy);
211 //// viewer.getTable().setEnabled(!busy);
213 // partNameCache = getPartName();
214 // setPartName(Messages.CdmDataSourceViewPart_1);
216 // if(partNameCache != null){
217 // setPartName(partNameCache);
222 public StructuredViewer
getViewer() {
226 // public IWorkbenchSiteProgressService getService() {
230 private SelectionAdapter
getSelectionAdapter(final TableColumn column
,
232 SelectionAdapter selectionAdapter
= new SelectionAdapter() {
234 public void widgetSelected(SelectionEvent e
) {
235 comparator
.setColumn(index
);
236 int dir
= viewer
.getTable().getSortDirection();
237 if (viewer
.getTable().getSortColumn() == column
) {
238 dir
= dir
== SWT
.UP ? SWT
.DOWN
: SWT
.UP
;
242 viewer
.getTable().setSortDirection(dir
);
243 viewer
.getTable().setSortColumn(column
);
247 return selectionAdapter
;