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