Merge branch 'release/4.6.0'
[taxeditor.git] / 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 = (IWorkbenchSiteProgressService) 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
198 Control control = viewer.getControl();
199 Menu menu = menuMgr.createContextMenu(control);
200 control.setMenu(menu);
201
202 refresh();
203 }
204
205
206 // This will create the columns for the table
207 private void createColumns(TableViewer viewer) {
208 Table table = viewer.getTable();
209 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};
210 int[] bounds = { 24, 200, 100, 50 , 80, 120, 100, 100, 50, 300};
211
212 for (int i = 0; i < titles.length; i++) {
213 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
214 column.getColumn().setText(titles[i]);
215 column.getColumn().setWidth(bounds[i]);
216 column.getColumn().setResizable(true);
217 column.getColumn().setMoveable(true);
218 column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), i));
219 if(i == 1){
220 table.setSortColumn(column.getColumn());
221 }
222 }
223 table.setHeaderVisible(true);
224 table.setLinesVisible(true);
225 table.setSortDirection(SWT.UP);
226
227 }
228
229 /**
230 * <p>refresh</p>
231 */
232 public void refresh(){
233 getService().schedule(new DataSourceJob(Messages.CdmDataSourceViewPart_1, CdmDataSourceRepository.getAll()), Job.LONG);
234 }
235
236 /** {@inheritDoc} */
237 @Override
238 public void dispose() {
239 CdmStore.getContextManager().removeContextListener(contextListener);
240 super.dispose();
241 }
242
243 /** {@inheritDoc} */
244 @Override
245 public void setFocus() {
246 viewer.getControl().setFocus();
247 }
248
249 /** {@inheritDoc} */
250 @Override
251 public void showBusy(boolean busy) {
252 super.showBusy(busy);
253 // viewer.getTable().setEnabled(!busy);
254 if(busy){
255 partNameCache = getPartName();
256 setPartName(Messages.CdmDataSourceViewPart_1);
257 }else{
258 if(partNameCache != null){
259 setPartName(partNameCache);
260 }
261 }
262 }
263
264 /**
265 * <p>Getter for the field <code>viewer</code>.</p>
266 *
267 * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
268 */
269 public StructuredViewer getViewer() {
270 return viewer;
271 }
272
273 /**
274 * <p>Getter for the field <code>service</code>.</p>
275 *
276 * @return the service
277 */
278 public IWorkbenchSiteProgressService getService() {
279 return service;
280 }
281
282 private SelectionAdapter getSelectionAdapter(final TableColumn column,
283 final int index) {
284 SelectionAdapter selectionAdapter = new SelectionAdapter() {
285 @Override
286 public void widgetSelected(SelectionEvent e) {
287 comparator.setColumn(index);
288 int dir = viewer.getTable().getSortDirection();
289 if (viewer.getTable().getSortColumn() == column) {
290 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
291 } else {
292 dir = SWT.DOWN;
293 }
294 viewer.getTable().setSortDirection(dir);
295 viewer.getTable().setSortColumn(column);
296 viewer.refresh();
297 }
298 };
299 return selectionAdapter;
300 }
301
302 }