Project

General

Profile

Download (8.78 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.net.MalformedURLException;
15
import java.net.URL;
16
import java.util.List;
17

    
18
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.eclipse.jface.operation.IRunnableWithProgress;
20
import org.eclipse.jface.viewers.ColumnLabelProvider;
21
import org.eclipse.jface.viewers.ILabelProvider;
22
import org.eclipse.jface.viewers.ISelectionChangedListener;
23
import org.eclipse.jface.viewers.ITreeContentProvider;
24
import org.eclipse.jface.viewers.SelectionChangedEvent;
25
import org.eclipse.jface.viewers.StructuredSelection;
26
import org.eclipse.jface.viewers.TreeViewer;
27
import org.eclipse.jface.viewers.Viewer;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.events.ModifyEvent;
30
import org.eclipse.swt.events.ModifyListener;
31
import org.eclipse.swt.events.SelectionAdapter;
32
import org.eclipse.swt.events.SelectionEvent;
33
import org.eclipse.swt.layout.GridData;
34
import org.eclipse.swt.layout.GridLayout;
35
import org.eclipse.swt.widgets.Button;
36
import org.eclipse.swt.widgets.Composite;
37
import org.eclipse.swt.widgets.Group;
38
import org.eclipse.swt.widgets.Text;
39
import org.jdom.Element;
40

    
41
import eu.etaxonomy.cdm.print.IXMLEntityFactory;
42
import eu.etaxonomy.cdm.print.PublishConfigurator;
43
import eu.etaxonomy.cdm.print.XMLHelper;
44
import eu.etaxonomy.cdm.print.XMLHelper.EntityType;
45
import eu.etaxonomy.taxeditor.store.CdmStore;
46
import eu.etaxonomy.taxeditor.store.StoreUtil;
47
import eu.etaxonomy.taxeditor.store.singlesource.widget.DisplayProxy;
48

    
49
/**
50
 * <p>SelectServiceWizardPage class.</p>
51
 *
52
 * @author n.hoffmann
53
 * @created Apr 6, 2010
54
 * @version 1.0
55
 */
56
public class SelectServiceWizardPage extends AbstractPublishWizardPage {
57

    
58
	private Composite composite;
59
	private Button button_local;
60
	private Button button_remote;
61
	private Text text_serviceUrl;
62
	
63
	private TreeViewer treeViewer;
64

    
65
	/**
66
	 * <p>Constructor for SelectServiceWizardPage.</p>
67
	 *
68
	 * @param pageName a {@link java.lang.String} object.
69
	 */
70
	protected SelectServiceWizardPage(String pageName) {
71
		super(pageName);
72
		setTitle("Select a Service");
73
	}
74
	
75
	/* (non-Javadoc)
76
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
77
	 */
78
	/** {@inheritDoc} */
79
	public void createControl(Composite parent) {
80
		
81
		composite = new Composite(parent, SWT.NULL);
82
		composite.setLayout(new GridLayout());
83
		
84
		RadioSelectionListener listener = new RadioSelectionListener();
85
		
86
		Group radioGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
87
		radioGroup.setLayout(new GridLayout());
88
		
89
		button_local = new Button(radioGroup, SWT.RADIO);
90
				
91
		button_local.setText("Local (By selecting this option the database you are currently " +
92
				"connected to will be used to gather data.)");
93
		
94
		button_local.addSelectionListener(listener);
95
		
96
		
97
						
98
		button_remote = new Button(radioGroup, SWT.RADIO);
99
		button_remote.setText("Remote");
100
		button_remote.addSelectionListener(listener);
101
		
102
		text_serviceUrl = new Text(radioGroup, SWT.BORDER);
103
		text_serviceUrl.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
104
		text_serviceUrl.setText("http://");	
105
		text_serviceUrl.addModifyListener(new ModifyListener() {
106
			
107
			public void modifyText(ModifyEvent e) {
108
				String text = text_serviceUrl.getText();
109
				
110
				if(!text.endsWith("/")){
111
					SelectServiceWizardPage.this.setErrorMessage("Webservice URL has to end with \"/\"");
112
					setPageComplete(false);
113
					return;
114
				}
115
				
116
				URL url = null;
117
				try {
118
					url = new URL(text);
119
				} catch (MalformedURLException e1) {
120
					SelectServiceWizardPage.this.setErrorMessage("Webservice URL is malformed.");
121
					setPageComplete(false);
122
					return;
123
				}
124
				
125
				getConfigurator().setWebserviceUrl(url);
126
				
127
				SelectServiceWizardPage.this.setErrorMessage(null);
128
					
129
			}
130
		});
131
		
132
		treeViewer = new TreeViewer(composite);
133
		
134
		treeViewer.setContentProvider(new ContentProvider());
135
		treeViewer.setLabelProvider(new LabelProvider());
136
		
137
		treeViewer.addSelectionChangedListener(new SelectionChangedListener());
138
		
139
		treeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
140
		
141
		
142
		if(CdmStore.isActive()){
143
			enableLocal();
144
		}else{
145
			enableRemote();
146
			button_local.setEnabled(false);
147
		}
148
		
149
		setControl(composite);
150
		
151
	}
152
	
153
	private class RadioSelectionListener extends SelectionAdapter{
154
		@Override
155
		public void widgetSelected(SelectionEvent e) {
156
			if(button_local.getSelection()){
157
				enableLocal();
158
			}else if(button_remote.getSelection()){
159
				enableRemote();
160
			}
161
		}
162
	}
163
	
164
	private void enableRemote() {
165
		button_local.setSelection(false);
166
		button_remote.setSelection(true);
167
		
168
		text_serviceUrl.setEnabled(true);
169
		
170
		setConfigurator(PublishConfigurator.NewRemoteInstance());
171
		getConfigurator().addOutputModule(getOutputModule());
172
	}
173

    
174
	private void enableLocal() {
175
		button_remote.setSelection(false);
176
		button_local.setSelection(true);
177
		
178
		text_serviceUrl.setEnabled(false);
179
		
180
		setConfigurator(PublishConfigurator.NewLocalInstance(CdmStore.getCurrentApplicationConfiguration()));
181
		getConfigurator().addOutputModule(getOutputModule());
182
		refresh();
183
	}
184
	
185
	/*
186
	 * (non-Javadoc)
187
	 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
188
	 */
189
	/** {@inheritDoc} */
190
	@Override
191
	public boolean canFlipToNextPage() {	
192
		return isPageComplete();
193
	}
194
	
195
	/**
196
	 * <p>refresh</p>
197
	 */
198
	public void refresh(){
199
		
200
		if(getConfigurator() != null){
201
		
202
			IRunnableWithProgress runnable = new IRunnableWithProgress(){
203

    
204
				@Override
205
				public void run(IProgressMonitor monitor) {
206
					monitor.beginTask("Loading classifications", IProgressMonitor.UNKNOWN);
207
					IXMLEntityFactory factory = getConfigurator().getFactory();
208
					final List<Element> classifications = factory.getClassifications();
209
					
210
					DisplayProxy.getDefault().asyncExec(new Runnable(){
211

    
212
						@Override
213
						public void run() {
214
							treeViewer.setInput(classifications);
215
						}
216
						
217
					});
218
					monitor.done();
219
				}
220
				
221
			};
222
			try {
223
				getContainer().run(true, false, runnable);
224
			} catch (InvocationTargetException e) {
225
				StoreUtil.error(this.getClass(), e);
226
			} catch (InterruptedException e) {
227
				StoreUtil.error(this.getClass(), e);
228
			}
229
		}
230
	}
231
		
232
	/*
233
	 * (non-Javadoc)
234
	 * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
235
	 */
236
	/** {@inheritDoc} */
237
	@Override
238
	public boolean isPageComplete() {
239
		boolean complete = true;
240
		if(getConfigurator().isLocal()){
241
			complete &= true;
242
		}else if(getConfigurator().isRemote() 
243
				&& getConfigurator().getWebserviceUrl() != null 
244
				&& getConfigurator().getWebserviceUrl().toString().endsWith("/")){
245
			complete &= true;
246
		}else{
247
			return false;
248
		}
249
		
250
		List<Element> selectedTaxonNodes = getConfigurator().getSelectedTaxonNodeElements();
251
		
252
		complete &= !selectedTaxonNodes.isEmpty();
253
		
254
		return complete;
255
	}
256
	
257
	private class SelectionChangedListener implements ISelectionChangedListener {
258

    
259
		public void selectionChanged(SelectionChangedEvent event) {
260
			StructuredSelection selection = (StructuredSelection) treeViewer.getSelection();
261
			
262
			List<Element> selectedElements = selection.toList();
263
			if(selectedElements.size() > 0){
264
				getConfigurator().setSelectedTaxonNodeElements(selectedElements);
265
				setPageComplete(true);
266
			}
267
		}
268
		
269
	}
270
	
271
	private class ContentProvider implements ITreeContentProvider{
272

    
273
		public Object[] getChildren(Object parentElement) {
274
			if(parentElement instanceof List){
275
				return ((List)parentElement).toArray();
276
			}
277
			else if(parentElement instanceof Element){
278
				Element element = (Element) parentElement;
279
				
280
				IXMLEntityFactory factory = getConfigurator().getFactory();
281
				
282
				return factory != null ? factory.getChildNodes(element).toArray() : new Object[]{};
283
				
284
			}
285
			
286
			return new Object[]{};
287
		}
288

    
289
		public Object getParent(Object element) {
290
			return null;
291
		}
292

    
293
		public boolean hasChildren(Object element) {
294
			return getChildren(element).length > 0;
295
		}
296

    
297
		public Object[] getElements(Object inputElement) {
298
			return getChildren(inputElement);
299
		}
300

    
301
		public void dispose() {
302
			
303
		}
304

    
305
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
306
			
307
		}
308
		
309
	}
310
	
311
	private class LabelProvider  extends ColumnLabelProvider
312
		implements ILabelProvider{
313
		
314
		@Override
315
		public String getText(Object element) {
316
			if(element instanceof Element){
317
				Element xmlElement = (Element) element;
318
				EntityType entityType = XMLHelper.getEntityType(xmlElement);
319
				if(EntityType.TAXON_NODE.equals(entityType)){
320
					xmlElement = getConfigurator().getFactory().getTaxonForTaxonNode(xmlElement);
321
				}				
322
				return XMLHelper.getTitleCache(xmlElement);
323
			}
324
			return "no title cache";
325
		}
326
		
327
	}
328
	
329
}
(12-12/14)