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