Moved all logging and dialog functionality to the new class MessagingUtils.
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / datasource / CdmDataSourceViewPart.java
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.commands.Command;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.commands.NotEnabledException;
20 import org.eclipse.core.commands.NotHandledException;
21 import org.eclipse.core.commands.common.NotDefinedException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.core.runtime.jobs.Job;
26 import org.eclipse.jface.action.GroupMarker;
27 import org.eclipse.jface.action.MenuManager;
28 import org.eclipse.jface.viewers.DoubleClickEvent;
29 import org.eclipse.jface.viewers.IDoubleClickListener;
30 import org.eclipse.jface.viewers.StructuredSelection;
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.Control;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.swt.widgets.Menu;
42 import org.eclipse.swt.widgets.Table;
43 import org.eclipse.swt.widgets.TableColumn;
44 import org.eclipse.ui.IMemento;
45 import org.eclipse.ui.IWorkbenchActionConstants;
46 import org.eclipse.ui.commands.ICommandService;
47 import org.eclipse.ui.handlers.IHandlerService;
48 import org.eclipse.ui.part.ViewPart;
49 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
50 import org.springframework.security.core.GrantedAuthority;
51
52 import eu.etaxonomy.cdm.config.ICdmSource;
53 import eu.etaxonomy.cdm.database.ICdmDataSource;
54 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
55 import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
56 import eu.etaxonomy.taxeditor.model.IContextListener;
57 import eu.etaxonomy.taxeditor.model.MessagingUtils;
58 import eu.etaxonomy.taxeditor.store.CdmStore;
59 import eu.etaxonomy.taxeditor.store.StoreUtil;
60
61 /**
62 * <p>CdmDataSourceViewPart class.</p>
63 *
64 * @author n.hoffmann
65 * @created 14.04.2009
66 * @version 1.0
67 */
68 public class CdmDataSourceViewPart extends ViewPart{
69
70 private static final Logger logger = Logger.getLogger(CdmDataSourceViewPart.class);
71
72 private class ContextListener extends ContextListenerAdapter{
73 /* (non-Javadoc)
74 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
75 */
76 @Override
77 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
78 monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
79 viewer.refresh();
80 }
81
82 /* (non-Javadoc)
83 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
84 */
85 @Override
86 public void contextStop(IMemento memento, IProgressMonitor monitor) {
87 monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
88 viewer.refresh();
89 }
90
91 /* (non-Javadoc)
92 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
93 */
94 @Override
95 public void contextStart(IMemento memento, IProgressMonitor monitor) {
96 monitor.subTask("Refreshing Datasource View"); //$NON-NLS-1$
97 viewer.refresh();
98 }
99 }
100
101 private class DataSourceJob extends Job{
102
103 private List<ICdmSource> cdmSources;
104
105 /**
106 * @param name
107 */
108 public DataSourceJob(String title, List<ICdmSource> cdmSources) {
109 super(title);
110 this.cdmSources = cdmSources;
111 }
112
113 /* (non-Javadoc)
114 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
115 */
116 @Override
117 public IStatus run(final IProgressMonitor monitor) {
118 try{
119 logger.debug("Begin of eclipse core runtime Job to Retrieve datasources"); //$NON-NLS-1$
120 monitor.beginTask("Retrieving datasources", cdmSources.size() + 1); //$NON-NLS-1$
121
122 final List<CdmMetaDataAwareDataSourceContainer> containers = new ArrayList<CdmMetaDataAwareDataSourceContainer>();
123
124 for(ICdmSource cdmSource : cdmSources){
125 containers.add(new CdmMetaDataAwareDataSourceContainer(cdmSource));
126 }
127
128 Display.getDefault().asyncExec(new Runnable() {
129
130 @Override
131 public void run() {
132 viewer.setInput(containers);
133 }
134 });
135 monitor.worked(1);
136
137 for(final CdmMetaDataAwareDataSourceContainer container : containers){
138
139
140 if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " : next DataSourceContainer"); //$NON-NLS-1$ //$NON-NLS-2$
141 container.getMetaDataFromDataSource();
142 if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " : metadata retrieved, creating new runnable ..."); //$NON-NLS-1$ //$NON-NLS-2$
143 Display.getDefault().asyncExec(new Runnable() {
144
145 @Override
146 public void run() {
147 if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " starting sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
148 viewer.update(container, null);
149 if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " end of sub thread to update ..."); //$NON-NLS-1$ //$NON-NLS-2$
150 }
151 });
152 if(logger.isDebugEnabled()) logger.debug(" #" + container.hashCode() + " done"); //$NON-NLS-1$ //$NON-NLS-2$
153 monitor.worked(1);
154 }
155
156 }finally{
157 monitor.done();
158 }
159 return Status.OK_STATUS;
160 }
161 }
162
163 /** Constant <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code> */
164 public static String ID = "eu.etaxonomy.taxeditor.view.datasource"; //$NON-NLS-1$
165
166 private TableViewer viewer;
167
168 private String partNameCache;
169
170 private IWorkbenchSiteProgressService service;
171
172 private IContextListener contextListener;
173
174 private CdmDataSourceViewerComparator comparator;
175
176 /**
177 * <p>Constructor for CdmDataSourceViewPart.</p>
178 */
179 public CdmDataSourceViewPart(){
180 }
181
182 /** {@inheritDoc} */
183 @Override
184 public void createPartControl(Composite parent) {
185 service = (IWorkbenchSiteProgressService) getSite().getAdapter(IWorkbenchSiteProgressService.class);
186 contextListener = new ContextListener();
187 CdmStore.getContextManager().addContextListener(contextListener);
188
189 // Create top composite
190 FillLayout fillLayout = new FillLayout();
191 fillLayout.marginWidth = 0;
192 fillLayout.marginHeight = 0;
193 fillLayout.type = SWT.VERTICAL;
194 parent.setLayout(fillLayout);
195
196 viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
197 | SWT.V_SCROLL | SWT.FULL_SELECTION);
198 getSite().setSelectionProvider(viewer);
199
200 createColumns(viewer);
201
202 viewer.setContentProvider(new CdmDataSourceContentProvider());
203 viewer.setLabelProvider(new CdmDataSourceLabelProvider());
204 comparator = new CdmDataSourceViewerComparator();
205 viewer.setComparator(comparator);
206
207
208 // register context menu
209 MenuManager menuMgr = new MenuManager();
210 menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
211 getSite().registerContextMenu(menuMgr, viewer);
212
213 Control control = viewer.getControl();
214 Menu menu = menuMgr.createContextMenu(control);
215 control.setMenu(menu);
216
217 // connect on doubleclick
218 viewer.addDoubleClickListener(new IDoubleClickListener() {
219
220 public void doubleClick(DoubleClickEvent event) {
221 if (event.getSelection() instanceof StructuredSelection) {
222
223 Object element = ((StructuredSelection) event
224 .getSelection()).getFirstElement();
225 if (element instanceof CdmMetaDataAwareDataSourceContainer) {
226 String commandId = "eu.etaxonomy.taxeditor.store.datasource.change"; //$NON-NLS-1$
227
228 ICommandService commandService = (ICommandService)getSite().getService(ICommandService.class);
229
230 Command command = commandService.getCommand(commandId);
231 if(command.isEnabled()) {
232
233 IHandlerService handlerService = (IHandlerService) StoreUtil.getService(IHandlerService.class);
234
235 try {
236 handlerService.executeCommand(commandId, null);
237 } catch (ExecutionException e) {
238 MessagingUtils.error(this.getClass(), e);
239 } catch (NotDefinedException e) {
240 MessagingUtils.error(this.getClass(), e);
241 } catch (NotEnabledException e) {
242 MessagingUtils.error(this.getClass(), e);
243 } catch (NotHandledException e) {
244 MessagingUtils.error(this.getClass(), e);
245 }
246 }
247 }
248 }
249 }
250 });
251
252 refresh();
253 }
254
255
256 // This will create the columns for the table
257 private void createColumns(TableViewer viewer) {
258 Table table = viewer.getTable();
259 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};
260 int[] bounds = { 24, 200, 100, 50 , 80, 120, 50, 100, 100, 50, 300};
261
262 for (int i = 0; i < titles.length; i++) {
263 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
264 column.getColumn().setText(titles[i]);
265 column.getColumn().setWidth(bounds[i]);
266 column.getColumn().setResizable(true);
267 column.getColumn().setMoveable(true);
268 column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), i));
269 if(i == 1){
270 table.setSortColumn(column.getColumn());
271 }
272 }
273 table.setHeaderVisible(true);
274 table.setLinesVisible(true);
275 table.setSortDirection(SWT.UP);
276
277 }
278
279 /**
280 * <p>refresh</p>
281 */
282 public void refresh(){
283 getService().schedule(new DataSourceJob(Messages.CdmDataSourceViewPart_1, CdmDataSourceRepository.getAll()), Job.LONG);
284 }
285
286 /** {@inheritDoc} */
287 @Override
288 public void dispose() {
289 CdmStore.getContextManager().removeContextListener(contextListener);
290 super.dispose();
291 }
292
293 /** {@inheritDoc} */
294 @Override
295 public void setFocus() {
296 viewer.getControl().setFocus();
297 }
298
299
300 /* (non-Javadoc)
301 * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
302 */
303 /** {@inheritDoc} */
304 @Override
305 public void showBusy(boolean busy) {
306 super.showBusy(busy);
307 // viewer.getTable().setEnabled(!busy);
308 if(busy){
309 partNameCache = getPartName();
310 setPartName(Messages.CdmDataSourceViewPart_1);
311 }else{
312 if(partNameCache != null){
313 setPartName(partNameCache);
314 }
315 }
316 }
317
318 /**
319 * <p>Getter for the field <code>viewer</code>.</p>
320 *
321 * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
322 */
323 public StructuredViewer getViewer() {
324 return viewer;
325 }
326
327 /**
328 * <p>Getter for the field <code>service</code>.</p>
329 *
330 * @return the service
331 */
332 public IWorkbenchSiteProgressService getService() {
333 return service;
334 }
335
336 private SelectionAdapter getSelectionAdapter(final TableColumn column,
337 final int index) {
338 SelectionAdapter selectionAdapter = new SelectionAdapter() {
339 @Override
340 public void widgetSelected(SelectionEvent e) {
341 comparator.setColumn(index);
342 int dir = viewer.getTable().getSortDirection();
343 if (viewer.getTable().getSortColumn() == column) {
344 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
345 } else {
346 dir = SWT.DOWN;
347 }
348 viewer.getTable().setSortDirection(dir);
349 viewer.getTable().setSortColumn(column);
350 viewer.refresh();
351 }
352 };
353 return selectionAdapter;
354 }
355
356 }