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