Project

General

Profile

Download (9.26 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
 * @version 1.0
54
 */
55
public class CdmDataSourceViewPart extends ViewPart{
56

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

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

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

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

    
79
	private class DataSourceJob extends Job{
80

    
81
		private final List<ICdmSource> cdmSources;
82

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

    
91
		@Override
92
		public IStatus run(final IProgressMonitor monitor) {
93
			try{
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$
96

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

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

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

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

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

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

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

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

    
153
	private TableViewer viewer;
154

    
155
	private String partNameCache;
156

    
157
	private IWorkbenchSiteProgressService service;
158

    
159
	private IContextListener contextListener;
160

    
161
	private CdmDataSourceViewerComparator comparator;
162

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

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

    
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);
182

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

    
187
		createColumns(viewer);
188

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

    
194

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

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

    
204
		refresh();
205
	}
206

    
207

    
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};
213

    
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));
221
			if(i == 1){
222
				table.setSortColumn(column.getColumn());
223
			}
224
		}
225
		table.setHeaderVisible(true);
226
		table.setLinesVisible(true);
227
		table.setSortDirection(SWT.UP);
228

    
229
	}
230

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

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

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

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

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

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

    
284
	private SelectionAdapter getSelectionAdapter(final TableColumn column,
285
			final int index) {
286
		SelectionAdapter selectionAdapter = new SelectionAdapter() {
287
			@Override
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;
293
				} else {
294
					dir = SWT.DOWN;
295
				}
296
				viewer.getTable().setSortDirection(dir);
297
				viewer.getTable().setSortColumn(column);
298
				viewer.refresh();
299
			}
300
		};
301
		return selectionAdapter;
302
	}
303

    
304
}
(3-3/5)