reverting cdm application / controller refactoring and simply extending the CdmApplic...
[taxeditor.git] / eu.etaxonomy.taxeditor.printpublisher / src / main / java / eu / etaxonomy / taxeditor / printpublisher / wizard / SelectServiceWizardPage.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.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.Display;
38 import org.eclipse.swt.widgets.Group;
39 import org.eclipse.swt.widgets.Text;
40 import org.jdom.Element;
41
42 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
43 import eu.etaxonomy.cdm.print.IXMLEntityFactory;
44 import eu.etaxonomy.cdm.print.PublishConfigurator;
45 import eu.etaxonomy.cdm.print.XMLHelper;
46 import eu.etaxonomy.cdm.print.XMLHelper.EntityType;
47 import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
48 import eu.etaxonomy.taxeditor.printpublisher.PrintUtil;
49 import eu.etaxonomy.taxeditor.store.CdmStore;
50 import eu.etaxonomy.taxeditor.store.StoreUtil;
51
52 /**
53 * <p>SelectServiceWizardPage class.</p>
54 *
55 * @author n.hoffmann
56 * @created Apr 6, 2010
57 * @version 1.0
58 */
59 public class SelectServiceWizardPage extends AbstractPublishWizardPage {
60
61 private Composite composite;
62 private Button button_local;
63 private Button button_remote;
64 private Text text_serviceUrl;
65
66 private TreeViewer treeViewer;
67
68 /**
69 * <p>Constructor for SelectServiceWizardPage.</p>
70 *
71 * @param pageName a {@link java.lang.String} object.
72 */
73 protected SelectServiceWizardPage(String pageName) {
74 super(pageName);
75 setTitle("Select a Service");
76 }
77
78 /* (non-Javadoc)
79 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
80 */
81 /** {@inheritDoc} */
82 public void createControl(Composite parent) {
83
84 composite = new Composite(parent, SWT.NULL);
85 composite.setLayout(new GridLayout());
86
87 RadioSelectionListener listener = new RadioSelectionListener();
88
89 Group radioGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
90 radioGroup.setLayout(new GridLayout());
91
92 button_local = new Button(radioGroup, SWT.RADIO);
93
94 button_local.setText("Local (By selecting this option the database you are currently " +
95 "connected to will be used to gather data.)");
96
97 button_local.addSelectionListener(listener);
98
99
100
101 button_remote = new Button(radioGroup, SWT.RADIO);
102 button_remote.setText("Remote");
103 button_remote.addSelectionListener(listener);
104
105 text_serviceUrl = new Text(radioGroup, SWT.BORDER);
106 text_serviceUrl.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
107 text_serviceUrl.setText("http://");
108 text_serviceUrl.addModifyListener(new ModifyListener() {
109
110 public void modifyText(ModifyEvent e) {
111 String text = text_serviceUrl.getText();
112
113 if(!text.endsWith("/")){
114 SelectServiceWizardPage.this.setErrorMessage("Webservice URL has to end with \"/\"");
115 setPageComplete(false);
116 return;
117 }
118
119 URL url = null;
120 try {
121 url = new URL(text);
122 } catch (MalformedURLException e1) {
123 SelectServiceWizardPage.this.setErrorMessage("Webservice URL is malformed.");
124 setPageComplete(false);
125 return;
126 }
127
128 getConfigurator().setWebserviceUrl(url);
129
130 SelectServiceWizardPage.this.setErrorMessage(null);
131
132 }
133 });
134
135 treeViewer = new TreeViewer(composite);
136
137 treeViewer.setContentProvider(new ContentProvider());
138 treeViewer.setLabelProvider(new LabelProvider());
139
140 treeViewer.addSelectionChangedListener(new SelectionChangedListener());
141
142 treeViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
143
144
145 if(CdmStore.isActive()){
146 enableLocal();
147 }else{
148 enableRemote();
149 button_local.setEnabled(false);
150 }
151
152 setControl(composite);
153
154 }
155
156 private class RadioSelectionListener extends SelectionAdapter{
157 @Override
158 public void widgetSelected(SelectionEvent e) {
159 if(button_local.getSelection()){
160 enableLocal();
161 }else if(button_remote.getSelection()){
162 enableRemote();
163 }
164 }
165 }
166
167 private void enableRemote() {
168 button_local.setSelection(false);
169 button_remote.setSelection(true);
170
171 text_serviceUrl.setEnabled(true);
172
173 setConfigurator(PublishConfigurator.NewRemoteInstance());
174 getConfigurator().addOutputModule(getOutputModule());
175 }
176
177 private void enableLocal() {
178 button_remote.setSelection(false);
179 button_local.setSelection(true);
180
181 text_serviceUrl.setEnabled(false);
182
183 setConfigurator(PublishConfigurator.NewLocalInstance((ICdmApplicationConfiguration) CdmStore.getCurrentApplicationConfiguration()));
184 getConfigurator().addOutputModule(getOutputModule());
185 refresh();
186 }
187
188 /*
189 * (non-Javadoc)
190 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
191 */
192 /** {@inheritDoc} */
193 @Override
194 public boolean canFlipToNextPage() {
195 return isPageComplete();
196 }
197
198 /**
199 * <p>refresh</p>
200 */
201 public void refresh(){
202
203 if(getConfigurator() != null){
204
205 IRunnableWithProgress runnable = new IRunnableWithProgress(){
206
207 @Override
208 public void run(IProgressMonitor monitor) {
209 monitor.beginTask("Loading classifications", IProgressMonitor.UNKNOWN);
210 IXMLEntityFactory factory = getConfigurator().getFactory();
211 final List<Element> classifications = factory.getClassifications();
212
213 Display.getDefault().asyncExec(new Runnable(){
214
215 @Override
216 public void run() {
217 treeViewer.setInput(classifications);
218 }
219
220 });
221 monitor.done();
222 }
223
224 };
225 try {
226 getContainer().run(true, false, runnable);
227 } catch (InvocationTargetException e) {
228 StoreUtil.error(this.getClass(), e);
229 } catch (InterruptedException e) {
230 StoreUtil.error(this.getClass(), e);
231 }
232 }
233 }
234
235 /*
236 * (non-Javadoc)
237 * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
238 */
239 /** {@inheritDoc} */
240 @Override
241 public boolean isPageComplete() {
242 boolean complete = true;
243 if(getConfigurator().isLocal()){
244 complete &= true;
245 }else if(getConfigurator().isRemote()
246 && getConfigurator().getWebserviceUrl() != null
247 && getConfigurator().getWebserviceUrl().toString().endsWith("/")){
248 complete &= true;
249 }else{
250 return false;
251 }
252
253 List<Element> selectedTaxonNodes = getConfigurator().getSelectedTaxonNodeElements();
254
255 complete &= !selectedTaxonNodes.isEmpty();
256
257 return complete;
258 }
259
260 private class SelectionChangedListener implements ISelectionChangedListener {
261
262 public void selectionChanged(SelectionChangedEvent event) {
263 StructuredSelection selection = (StructuredSelection) treeViewer.getSelection();
264
265 List<Element> selectedElements = selection.toList();
266 if(selectedElements.size() > 0){
267 getConfigurator().setSelectedTaxonNodeElements(selectedElements);
268 setPageComplete(true);
269 }
270 }
271
272 }
273
274 private class ContentProvider implements ITreeContentProvider{
275
276 public Object[] getChildren(Object parentElement) {
277 if(parentElement instanceof List){
278 return ((List)parentElement).toArray();
279 }
280 else if(parentElement instanceof Element){
281 Element element = (Element) parentElement;
282
283 IXMLEntityFactory factory = getConfigurator().getFactory();
284
285 return factory != null ? factory.getChildNodes(element).toArray() : new Object[]{};
286
287 }
288
289 return new Object[]{};
290 }
291
292 public Object getParent(Object element) {
293 return null;
294 }
295
296 public boolean hasChildren(Object element) {
297 return getChildren(element).length > 0;
298 }
299
300 public Object[] getElements(Object inputElement) {
301 return getChildren(inputElement);
302 }
303
304 public void dispose() {
305
306 }
307
308 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
309
310 }
311
312 }
313
314 private class LabelProvider extends ColumnLabelProvider
315 implements ILabelProvider{
316
317 @Override
318 public String getText(Object element) {
319 if(element instanceof Element){
320 Element xmlElement = (Element) element;
321 EntityType entityType = XMLHelper.getEntityType(xmlElement);
322 if(EntityType.TAXON_NODE.equals(entityType)){
323 xmlElement = getConfigurator().getFactory().getTaxonForTaxonNode(xmlElement);
324 }
325 return XMLHelper.getTitleCache(xmlElement);
326 }
327 return "no title cache";
328 }
329
330 }
331
332 }