Project

General

Profile

Revision f00083df

IDf00083df4287f22081778102648c814708ed9588
Parent 00069e6b
Child 9d2cf91f

Added by Patrick Plitzner about 1 year ago

ref #6913 Remove data source view

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/datasource/CdmDataSourceViewPart.java
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9

  
10
package eu.etaxonomy.taxeditor.view.datasource;
11

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

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

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

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

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

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

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

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

  
77
	private class DataSourceJob extends Job{
78

  
79
		private final List<ICdmSource> cdmSources;
80

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

  
89
		@Override
90
		public IStatus run(final IProgressMonitor monitor) {
91
			try{
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$
94

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

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

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

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

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

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

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

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

  
151
	private TableViewer viewer;
152

  
153
	private String partNameCache;
154

  
155
	private IWorkbenchSiteProgressService service;
156

  
157
	private IContextListener contextListener;
158

  
159
	private CdmDataSourceViewerComparator comparator;
160

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

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

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

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

  
185
		createColumns(viewer);
186

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

  
192

  
193
		// register context menu
194
		MenuManager menuMgr = new MenuManager();
195
		menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
196
		getSite().registerContextMenu(menuMgr, viewer);
197
		menuMgr.setRemoveAllWhenShown(true);
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
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)