7fa7f6c83a583ba0031084fabaea3d92f773c5fe
[taxeditor.git] / eu.etaxonomy.taxeditor.printpublisher / src / main / java / eu / etaxonomy / taxeditor / printpublisher / wizard / SelectFeatureTreeWizardPage.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.util.ArrayList;
14 import java.util.List;
15 import java.util.UUID;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.IStructuredContentProvider;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.jface.viewers.ListViewer;
23 import org.eclipse.jface.viewers.SelectionChangedEvent;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.jface.wizard.WizardDialog;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Button;
33 import org.eclipse.swt.widgets.Composite;
34 import org.jdom.Element;
35
36 import eu.etaxonomy.cdm.print.IXMLEntityFactory;
37 import eu.etaxonomy.cdm.print.XMLHelper;
38 import eu.etaxonomy.taxeditor.featuretree.FeatureTreeEditorWizard;
39
40 /**
41 * <p>SelectFeatureTreeWizardPage class.</p>
42 *
43 * @author n.hoffmann
44 * @created Aug 6, 2010
45 * @version 1.0
46 */
47 public class SelectFeatureTreeWizardPage extends AbstractPublishWizardPage
48 implements ISelectionChangedListener, IHasPersistableSettings{
49
50 private static final String DIALOG_SETTING_FEATURE_TREE_UUID = "dialogSettingFeatureTreeUuid";
51 private ListViewer viewer;
52
53 /**
54 * <p>Constructor for SelectFeatureTreeWizardPage.</p>
55 *
56 * @param pageName a {@link java.lang.String} object.
57 */
58 protected SelectFeatureTreeWizardPage(String pageName) {
59 super(pageName);
60 setTitle("Select Feature Tree");
61 }
62
63 /* (non-Javadoc)
64 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
65 */
66 /** {@inheritDoc} */
67 @Override
68 public void createControl(Composite parent) {
69 setPageComplete(false);
70
71 Composite composite = new Composite(parent, SWT.NULL);
72 composite.setLayout(new GridLayout());
73
74 viewer = new ListViewer(composite);
75 viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
76 viewer.setContentProvider(new FeatureTreeContentProvider());
77 viewer.setLabelProvider(new FeatureTreeLabelProvider());
78
79 viewer.addSelectionChangedListener(this);
80
81 // offer feature tree editor when local
82 if(getConfigurator().isLocal()){
83 final Button button_openFeatureTree = new Button(composite, SWT.PUSH);
84 button_openFeatureTree.setText("Open FeatureTree Editor");
85
86 button_openFeatureTree.addSelectionListener(new SelectionAdapter() {
87 @Override
88 public void widgetSelected(SelectionEvent e) {
89 FeatureTreeEditorWizard featureTreeEditorDialog = new FeatureTreeEditorWizard();
90 WizardDialog dialog = new WizardDialog(getShell(), featureTreeEditorDialog);
91
92 if(dialog.open() == IStatus.OK){
93 refresh();
94 }
95 }
96 });
97 }
98
99 // when using a local factory we can set the input right here
100 if(getConfigurator() != null && getConfigurator().isLocal()){
101 refresh();
102 }
103
104 loadSettings();
105
106 setControl(composite);
107 }
108
109 @Override
110 public void loadSettings() {
111 String featureTreeUuidString = getDialogSettingValue(DIALOG_SETTING_FEATURE_TREE_UUID);
112
113 if(featureTreeUuidString != null){
114 setSelectedFeatureTree(featureTreeUuidString);
115
116 }
117 }
118
119 /** {@inheritDoc} */
120 @Override
121 public void selectionChanged(SelectionChangedEvent event) {
122 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
123
124 setPageComplete(selection.size() == 1);
125
126 Element featureTreeElement = (Element) selection.getFirstElement();
127
128 UUID featureTreeUuid = XMLHelper.getUuid(featureTreeElement);
129
130 setFeatureTree(featureTreeUuid.toString());
131 }
132
133 private void setSelectedFeatureTree(String featureTreeUuid){
134 List<Element> input = (ArrayList<Element>) viewer.getInput();
135
136 for(Element element : input){
137 UUID uuid = XMLHelper.getUuid(element);
138 if(uuid.toString().equals(featureTreeUuid)){
139 viewer.setSelection(new StructuredSelection(element));
140 break;
141 }
142 }
143 }
144
145 public void setFeatureTree(String featureTreeUuidString){
146 putDialogSettingValue(DIALOG_SETTING_FEATURE_TREE_UUID, featureTreeUuidString);
147 UUID featureTreeUuid = UUID.fromString(featureTreeUuidString);
148 getConfigurator().setFeatureTree(featureTreeUuid);
149 setPageComplete(featureTreeUuid != null);
150 }
151
152 /** {@inheritDoc} */
153 @Override
154 public boolean canFlipToNextPage() {
155 return isPageComplete();
156 }
157
158 /**
159 * Refreshes the input of the viewer
160 */
161 public void refresh() {
162 IXMLEntityFactory factory = getConfigurator().getFactory();
163
164 List<Element> featureTrees = factory.getFeatureTrees();
165 List<Element> realFeatureTrees = new ArrayList<Element>();
166 for(Element featureTree : featureTrees){
167 if(XMLHelper.getClazz(featureTree).equals("FeatureTree")){
168 realFeatureTrees.add(featureTree);
169 }
170 }
171 viewer.setInput(realFeatureTrees);
172 }
173
174 private class FeatureTreeContentProvider implements IStructuredContentProvider {
175
176 @Override
177 public void dispose() {}
178
179 @Override
180 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
181
182 @Override
183 public Object[] getElements(Object inputElement) {
184 if(inputElement instanceof List){
185 return ((List) inputElement).toArray();
186 }
187 return new Object[0];
188 }
189
190 }
191
192 private class FeatureTreeLabelProvider extends LabelProvider {
193
194 /*
195 * (non-Javadoc)
196 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
197 */
198 @Override
199 public String getText(Object element) {
200 if(element instanceof Element){
201 Element xmlElement = (Element) element;
202 return XMLHelper.getTitleCache(xmlElement);
203 }
204 return "no title cache";
205 }
206
207 }
208 }