Project

General

Profile

Download (5.33 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.printpublisher.wizard;
12

    
13
import java.lang.reflect.InvocationTargetException;
14
import java.util.List;
15

    
16
import org.eclipse.core.runtime.IProgressMonitor;
17
import org.eclipse.jface.operation.IRunnableWithProgress;
18
import org.eclipse.jface.viewers.ColumnLabelProvider;
19
import org.eclipse.jface.viewers.ILabelProvider;
20
import org.eclipse.jface.viewers.ISelectionChangedListener;
21
import org.eclipse.jface.viewers.ITreeContentProvider;
22
import org.eclipse.jface.viewers.SelectionChangedEvent;
23
import org.eclipse.jface.viewers.StructuredSelection;
24
import org.eclipse.jface.viewers.TreeViewer;
25
import org.eclipse.jface.viewers.Viewer;
26
import org.eclipse.swt.SWT;
27
import org.eclipse.swt.layout.GridData;
28
import org.eclipse.swt.layout.GridLayout;
29
import org.eclipse.swt.widgets.Button;
30
import org.eclipse.swt.widgets.Composite;
31
import org.eclipse.swt.widgets.Display;
32
import org.jdom.Element;
33

    
34
import eu.etaxonomy.cdm.print.IXMLEntityFactory;
35
import eu.etaxonomy.cdm.print.XMLHelper;
36
import eu.etaxonomy.cdm.print.XMLHelper.EntityType;
37
import eu.etaxonomy.taxeditor.store.StoreUtil;
38

    
39
/**
40
 * <p>SelectTaxaWizardPage class.</p>
41
 *
42
 * @author n.hoffmann
43
 * @created Apr 6, 2010
44
 * @version 1.0
45
 */
46
public class SelectTaxaWizardPage extends AbstractPublishWizardPage {
47

    
48
	private Composite composite;
49

    
50
	private TreeViewer treeViewer;
51
	
52
	private Button button_refresh;
53

    
54
	/**
55
	 * <p>Constructor for SelectTaxaWizardPage.</p>
56
	 *
57
	 * @param pageName a {@link java.lang.String} object.
58
	 */
59
	public SelectTaxaWizardPage(String pageName) {
60
		super(pageName);
61
		setTitle("Select Taxa to export.");
62
		
63
		
64
	}
65

    
66
	/* (non-Javadoc)
67
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
68
	 */
69
	/** {@inheritDoc} */
70
	public void createControl(Composite parent) {		
71
		composite = new Composite(parent, SWT.NULL);
72
		composite.setLayout(new GridLayout());
73
		
74
		treeViewer = new TreeViewer(composite);
75
		
76
		treeViewer.setContentProvider(new ContentProvider());
77
		treeViewer.setLabelProvider(new LabelProvider());
78
		
79
		treeViewer.addSelectionChangedListener(new SelectionChangedListener());
80
		
81
		treeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
82
				
83
		setControl(composite);
84
	}
85
		
86
	/**
87
	 * <p>refresh</p>
88
	 */
89
	public void refresh(){
90
		
91
		if(getConfigurator() != null){
92
		
93
			IRunnableWithProgress runnable = new IRunnableWithProgress(){
94

    
95
				@Override
96
				public void run(IProgressMonitor monitor) {
97
					IXMLEntityFactory factory = getConfigurator().getFactory();
98
					final List<Element> classifications = factory.getClassifications();
99
					
100
					Display.getDefault().asyncExec(new Runnable(){
101

    
102
						@Override
103
						public void run() {
104
							treeViewer.setInput(classifications);
105
						}
106
						
107
					});
108
				}
109
				
110
			};
111
			try {
112
				getContainer().run(true, false, runnable);
113
			} catch (InvocationTargetException e) {
114
				StoreUtil.error(this.getClass(), e);
115
			} catch (InterruptedException e) {
116
				StoreUtil.error(this.getClass(), e);
117
			}
118
		}
119
	}
120
	
121
	private class SelectionChangedListener implements ISelectionChangedListener {
122

    
123
		public void selectionChanged(SelectionChangedEvent event) {
124
			StructuredSelection selection = (StructuredSelection) treeViewer.getSelection();
125
			
126
			List<Element> selectedElements = selection.toList();
127
			if(selectedElements.size() > 0){
128
				getConfigurator().setSelectedTaxonNodeElements(selectedElements);
129
				setPageComplete(true);
130
			}
131
		}
132
		
133
	}
134
	
135
	private class ContentProvider implements ITreeContentProvider{
136

    
137
		public Object[] getChildren(Object parentElement) {
138
			if(parentElement instanceof List){
139
				return ((List)parentElement).toArray();
140
			}
141
			else if(parentElement instanceof Element){
142
				Element element = (Element) parentElement;
143
				
144
				IXMLEntityFactory factory = getConfigurator().getFactory();
145
				
146
				return factory != null ? factory.getChildNodes(element).toArray() : new Object[]{};
147
				
148
			}
149
			
150
			return new Object[]{};
151
		}
152

    
153
		public Object getParent(Object element) {
154
			return null;
155
		}
156

    
157
		public boolean hasChildren(Object element) {
158
			return getChildren(element).length > 0;
159
		}
160

    
161
		public Object[] getElements(Object inputElement) {
162
			return getChildren(inputElement);
163
		}
164

    
165
		public void dispose() {
166
			
167
		}
168

    
169
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
170
			
171
		}
172
		
173
	}
174
	
175
	private class LabelProvider  extends ColumnLabelProvider
176
		implements ILabelProvider{
177
		
178
		@Override
179
		public String getText(Object element) {
180
			if(element instanceof Element){
181
				Element xmlElement = (Element) element;
182
				EntityType entityType = XMLHelper.getEntityType(xmlElement);
183
				if(EntityType.TAXON_NODE.equals(entityType)){
184
					xmlElement = getConfigurator().getFactory().getTaxonForTaxonNode(xmlElement);
185
				}				
186
				return XMLHelper.getTitleCache(xmlElement);
187
			}
188
			return "no title cache";
189
		}
190
		
191
	}
192
	
193
	/** {@inheritDoc} */
194
	@Override
195
	public boolean canFlipToNextPage() {
196
		return isPageComplete();
197
	}
198
	
199
	/** {@inheritDoc} */
200
	@Override
201
	public boolean isPageComplete() {
202
		return getConfigurator().getSelectedTaxonNodeElements() != null && getConfigurator().getSelectedTaxonNodeElements().size() > 0;
203
	}
204
}
(14-14/14)