2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.printpublisher
.wizard
;
12 import java
.lang
.reflect
.InvocationTargetException
;
13 import java
.net
.MalformedURLException
;
15 import java
.util
.List
;
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
;
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
;
52 * <p>SelectServiceWizardPage class.</p>
55 * @created Apr 6, 2010
58 public class SelectServiceWizardPage
extends AbstractPublishWizardPage
{
60 private Composite composite
;
61 private Button button_local
;
62 private Button button_remote
;
63 private Text text_serviceUrl
;
65 private TreeViewer treeViewer
;
68 * <p>Constructor for SelectServiceWizardPage.</p>
70 * @param pageName a {@link java.lang.String} object.
72 protected SelectServiceWizardPage(String pageName
) {
74 setTitle("Select a Service");
78 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
81 public void createControl(Composite parent
) {
83 composite
= new Composite(parent
, SWT
.NULL
);
84 composite
.setLayout(new GridLayout());
86 RadioSelectionListener listener
= new RadioSelectionListener();
88 Group radioGroup
= new Group(composite
, SWT
.SHADOW_ETCHED_IN
);
89 radioGroup
.setLayout(new GridLayout());
91 button_local
= new Button(radioGroup
, SWT
.RADIO
);
93 button_local
.setText("Local (By selecting this option the database you are currently " +
94 "connected to will be used to gather data.)");
96 button_local
.addSelectionListener(listener
);
100 button_remote
= new Button(radioGroup
, SWT
.RADIO
);
101 button_remote
.setText("Remote");
102 button_remote
.addSelectionListener(listener
);
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() {
109 public void modifyText(ModifyEvent e
) {
110 String text
= text_serviceUrl
.getText();
112 if(!text
.endsWith("/")){
113 SelectServiceWizardPage
.this.setErrorMessage("Webservice URL has to end with \"/\"");
114 setPageComplete(false);
121 } catch (MalformedURLException e1
) {
122 SelectServiceWizardPage
.this.setErrorMessage("Webservice URL is malformed.");
123 setPageComplete(false);
127 getConfigurator().setWebserviceUrl(url
);
129 SelectServiceWizardPage
.this.setErrorMessage(null);
134 treeViewer
= new TreeViewer(composite
);
136 treeViewer
.setContentProvider(new ContentProvider());
137 treeViewer
.setLabelProvider(new LabelProvider());
139 treeViewer
.addSelectionChangedListener(new SelectionChangedListener());
141 treeViewer
.getControl().setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
144 if(CdmStore
.isActive()){
148 button_local
.setEnabled(false);
151 setControl(composite
);
155 private class RadioSelectionListener
extends SelectionAdapter
{
157 public void widgetSelected(SelectionEvent e
) {
158 if(button_local
.getSelection()){
160 }else if(button_remote
.getSelection()){
166 private void enableRemote() {
167 button_local
.setSelection(false);
168 button_remote
.setSelection(true);
170 text_serviceUrl
.setEnabled(true);
172 setConfigurator(PublishConfigurator
.NewRemoteInstance());
173 getConfigurator().addOutputModule(getOutputModule());
176 private void enableLocal() {
177 button_remote
.setSelection(false);
178 button_local
.setSelection(true);
180 text_serviceUrl
.setEnabled(false);
182 setConfigurator(PublishConfigurator
.NewLocalInstance((ICdmRepository
) CdmStore
.getCurrentApplicationConfiguration()));
183 getConfigurator().addOutputModule(getOutputModule());
189 * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
193 public boolean canFlipToNextPage() {
194 return isPageComplete();
200 public void refresh(){
202 if(getConfigurator() != null){
204 IRunnableWithProgress runnable
= new IRunnableWithProgress(){
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();
212 Display
.getDefault().asyncExec(new Runnable(){
216 treeViewer
.setInput(classifications
);
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
);
236 * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
240 public boolean isPageComplete() {
241 boolean complete
= true;
242 if(getConfigurator().isLocal()){
244 }else if(getConfigurator().isRemote()
245 && getConfigurator().getWebserviceUrl() != null
246 && getConfigurator().getWebserviceUrl().toString().endsWith("/")){
252 List
<Element
> selectedTaxonNodes
= getConfigurator().getSelectedTaxonNodeElements();
254 complete
&= !selectedTaxonNodes
.isEmpty();
259 private class SelectionChangedListener
implements ISelectionChangedListener
{
261 public void selectionChanged(SelectionChangedEvent event
) {
262 StructuredSelection selection
= (StructuredSelection
) treeViewer
.getSelection();
264 List
<Element
> selectedElements
= selection
.toList();
265 if(selectedElements
.size() > 0){
266 getConfigurator().setSelectedTaxonNodeElements(selectedElements
);
267 setPageComplete(true);
273 private class ContentProvider
implements ITreeContentProvider
{
275 public Object
[] getChildren(Object parentElement
) {
276 if(parentElement
instanceof List
){
277 return ((List
)parentElement
).toArray();
279 else if(parentElement
instanceof Element
){
280 Element element
= (Element
) parentElement
;
282 IXMLEntityFactory factory
= getConfigurator().getFactory();
284 return factory
!= null ? factory
.getChildNodes(element
).toArray() : new Object
[]{};
288 return new Object
[]{};
291 public Object
getParent(Object element
) {
295 public boolean hasChildren(Object element
) {
296 return getChildren(element
).length
> 0;
299 public Object
[] getElements(Object inputElement
) {
300 return getChildren(inputElement
);
303 public void dispose() {
307 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
313 private class LabelProvider
extends ColumnLabelProvider
314 implements ILabelProvider
{
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
);
324 return XMLHelper
.getTitleCache(xmlElement
);
326 return "no title cache";