Project

General

Profile

Download (9.2 KB) Statistics
| Branch: | Tag: | Revision:
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.view.datasource;
12

    
13
import java.util.ArrayList;
14
import java.util.List;
15

    
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;
40

    
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;
47

    
48
/**
49
 * <p>CdmDataSourceViewPart class.</p>
50
 *
51
 * @author n.hoffmann
52
 * @created 14.04.2009
53
 */
54
public class CdmDataSourceViewPart extends ViewPart{
55

    
56
    private static final Logger logger = Logger.getLogger(CdmDataSourceViewPart.class);
57

    
58
	private class ContextListener extends ContextListenerAdapter{
59
		@Override
60
		public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
61
			monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
62
			viewer.refresh();
63
		}
64

    
65
		@Override
66
		public void contextStop(IMemento memento, IProgressMonitor monitor) {
67
			monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
68
			viewer.refresh();
69
		}
70

    
71
		@Override
72
		public void contextStart(IMemento memento, IProgressMonitor monitor) {
73
			monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
74
			viewer.refresh();
75
		}
76
	}
77

    
78
	private class DataSourceJob extends Job{
79

    
80
		private final List<ICdmSource> cdmSources;
81

    
82
		/**
83
		 * @param name
84
		 */
85
		public DataSourceJob(String title, List<ICdmSource> cdmSources) {
86
			super(title);
87
			this.cdmSources = cdmSources;
88
		}
89

    
90
		@Override
91
		public IStatus run(final IProgressMonitor monitor) {
92
			try{
93
				logger.debug("Begin of eclipse core runtime Job to Retrieve datasources"); //$NON-NLS-1$
94
				monitor.beginTask("Retrieving datasources", cdmSources.size() + 1);			 //$NON-NLS-1$
95

    
96
				final List<CdmMetaDataAwareDataSourceContainer> containers = new ArrayList<CdmMetaDataAwareDataSourceContainer>();
97

    
98
				for(ICdmSource cdmSource : cdmSources){
99
					containers.add(new CdmMetaDataAwareDataSourceContainer(cdmSource));
100
				}
101

    
102
				Display.getDefault().asyncExec(new Runnable() {
103

    
104
					@Override
105
					public void run() {
106
						viewer.setInput(containers);
107
					}
108
				});
109
				monitor.worked(1);
110

    
111
				for(final CdmMetaDataAwareDataSourceContainer container : containers){
112
					if(logger.isDebugEnabled()) {
113
                        logger.debug("  #" + container.hashCode() + " : next DataSourceContainer");						 //$NON-NLS-1$ //$NON-NLS-2$
114
                    }
115
					container.getMetaDataFromDataSource();
116
					if(logger.isDebugEnabled())
117
                     {
118
                        logger.debug("  #" + container.hashCode() + " : metadata retrieved, creating new runnable ...");	 //$NON-NLS-1$ //$NON-NLS-2$
119
                    }
120
					Display.getDefault().asyncExec(new Runnable() {
121

    
122
						@Override
123
						public void run() {
124
							if(logger.isDebugEnabled())
125
                             {
126
                                logger.debug("  #" + container.hashCode() + " starting sub thread to update ...");	 //$NON-NLS-1$ //$NON-NLS-2$
127
                            }
128
							viewer.update(container, null);
129
							if(logger.isDebugEnabled())
130
                             {
131
                                logger.debug("  #" + container.hashCode() + " end of sub thread to update ...");	 //$NON-NLS-1$ //$NON-NLS-2$
132
                            }
133
						}
134
					});
135
					if(logger.isDebugEnabled())
136
                     {
137
                        logger.debug("  #" + container.hashCode() + " done");	 //$NON-NLS-1$ //$NON-NLS-2$
138
                    }
139
					monitor.worked(1);
140
				}
141

    
142
			}finally{
143
				monitor.done();
144
			}
145
			return Status.OK_STATUS;
146
		}
147
	}
148

    
149
	/** Constant <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code> */
150
	public static String ID = "eu.etaxonomy.taxeditor.view.datasource"; //$NON-NLS-1$
151

    
152
	private TableViewer viewer;
153

    
154
	private String partNameCache;
155

    
156
	private IWorkbenchSiteProgressService service;
157

    
158
	private IContextListener contextListener;
159

    
160
	private CdmDataSourceViewerComparator comparator;
161

    
162
	/**
163
	 * <p>Constructor for CdmDataSourceViewPart.</p>
164
	 */
165
	public CdmDataSourceViewPart(){
166
	}
167

    
168
	/** {@inheritDoc} */
169
	@Override
170
	public void createPartControl(Composite parent) {
171
		service = (IWorkbenchSiteProgressService) getSite().getAdapter(IWorkbenchSiteProgressService.class);
172
		contextListener = new ContextListener();
173
		CdmStore.getContextManager().addContextListener(contextListener);
174

    
175
		// Create top composite
176
		FillLayout fillLayout = new FillLayout();
177
		fillLayout.marginWidth = 0;
178
		fillLayout.marginHeight = 0;
179
		fillLayout.type = SWT.VERTICAL;
180
		parent.setLayout(fillLayout);
181

    
182
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
183
												| SWT.V_SCROLL | SWT.FULL_SELECTION);
184
		getSite().setSelectionProvider(viewer);
185

    
186
		createColumns(viewer);
187

    
188
		viewer.setContentProvider(new CdmDataSourceContentProvider());
189
		viewer.setLabelProvider(new CdmDataSourceLabelProvider());
190
		comparator = new CdmDataSourceViewerComparator();
191
		viewer.setComparator(comparator);
192

    
193

    
194
		// register context menu
195
		MenuManager menuMgr = new MenuManager();
196
		menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
197
		getSite().registerContextMenu(menuMgr, viewer);
198

    
199
		Control control = viewer.getControl();
200
		Menu menu = menuMgr.createContextMenu(control);
201
		control.setMenu(menu);
202

    
203
		refresh();
204
	}
205

    
206

    
207
	// This will create the columns for the table
208
	private void createColumns(TableViewer viewer) {
209
		Table table = viewer.getTable();
210
		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};
211
		int[] bounds = { 24, 200, 100, 50 , 80, 120, 100, 100, 50, 300};
212

    
213
		for (int i = 0; i < titles.length; i++) {
214
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
215
			column.getColumn().setText(titles[i]);
216
			column.getColumn().setWidth(bounds[i]);
217
			column.getColumn().setResizable(true);
218
			column.getColumn().setMoveable(true);
219
			column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), i));
220
			if(i == 1){
221
				table.setSortColumn(column.getColumn());
222
			}
223
		}
224
		table.setHeaderVisible(true);
225
		table.setLinesVisible(true);
226
		table.setSortDirection(SWT.UP);
227

    
228
	}
229

    
230
	/**
231
	 * <p>refresh</p>
232
	 */
233
	public void refresh(){
234
		getService().schedule(new DataSourceJob(Messages.CdmDataSourceViewPart_1, CdmDataSourceRepository.getAll()), Job.LONG);
235
	}
236

    
237
	/** {@inheritDoc} */
238
	@Override
239
	public void dispose() {
240
		CdmStore.getContextManager().removeContextListener(contextListener);
241
		super.dispose();
242
	}
243

    
244
	/** {@inheritDoc} */
245
	@Override
246
	public void setFocus() {
247
		viewer.getControl().setFocus();
248
	}
249

    
250
	/** {@inheritDoc} */
251
	@Override
252
	public void showBusy(boolean busy) {
253
		super.showBusy(busy);
254
//		viewer.getTable().setEnabled(!busy);
255
		if(busy){
256
			partNameCache = getPartName();
257
			setPartName(Messages.CdmDataSourceViewPart_1);
258
		}else{
259
			if(partNameCache != null){
260
				setPartName(partNameCache);
261
			}
262
		}
263
	}
264

    
265
	/**
266
	 * <p>Getter for the field <code>viewer</code>.</p>
267
	 *
268
	 * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
269
	 */
270
	public StructuredViewer getViewer() {
271
		return viewer;
272
	}
273

    
274
	/**
275
	 * <p>Getter for the field <code>service</code>.</p>
276
	 *
277
	 * @return the service
278
	 */
279
	public IWorkbenchSiteProgressService getService() {
280
		return service;
281
	}
282

    
283
	private SelectionAdapter getSelectionAdapter(final TableColumn column,
284
			final int index) {
285
		SelectionAdapter selectionAdapter = new SelectionAdapter() {
286
			@Override
287
			public void widgetSelected(SelectionEvent e) {
288
				comparator.setColumn(index);
289
				int dir = viewer.getTable().getSortDirection();
290
				if (viewer.getTable().getSortColumn() == column) {
291
					dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
292
				} else {
293
					dir = SWT.DOWN;
294
				}
295
				viewer.getTable().setSortDirection(dir);
296
				viewer.getTable().setSortColumn(column);
297
				viewer.refresh();
298
			}
299
		};
300
		return selectionAdapter;
301
	}
302

    
303
}
(3-3/5)