Merge branch 'release/4.3.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / 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.featuretree;
12
13 import java.util.List;
14 import java.util.logging.ErrorManager;
15
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.ListViewer;
19 import org.eclipse.jface.viewers.SelectionChangedEvent;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.jface.wizard.WizardPage;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.ModifyEvent;
24 import org.eclipse.swt.events.ModifyListener;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Text;
33
34 import eu.etaxonomy.cdm.api.service.DeleteResult;
35 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
36 import eu.etaxonomy.cdm.model.description.FeatureTree;
37 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
38 import eu.etaxonomy.taxeditor.model.MessagingUtils;
39 import eu.etaxonomy.taxeditor.store.CdmStore;
40
41 /**
42 * <p>SelectFeatureTreeWizardPage class.</p>
43 *
44 * @author n.hoffmann
45 * @created Aug 5, 2010
46 * @version 1.0
47 */
48 public class SelectFeatureTreeWizardPage extends WizardPage implements ISelectionChangedListener, ModifyListener{
49
50 private ListViewer viewer;
51 private Text text_title;
52 private Button button_add;
53 private Button button_remove;
54
55 /**
56 * <p>Constructor for SelectFeatureTreeWizardPage.</p>
57 *
58 * @param pageName a {@link java.lang.String} object.
59 */
60 protected SelectFeatureTreeWizardPage(String pageName) {
61 super(pageName);
62 setMessage("Select a Feature Tree or create a new one.");
63 }
64
65 /* (non-Javadoc)
66 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
67 */
68 /** {@inheritDoc} */
69 @Override
70 public void createControl(Composite parent) {
71 Composite composite = new Composite(parent, SWT.NULL);
72
73 composite.setLayout(new GridLayout());
74
75 Composite composite_treeContent = new Composite(composite, SWT.NULL);
76 composite_treeContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
77 true, true));
78 composite_treeContent.setLayout(new GridLayout(2, false));
79
80 viewer = new ListViewer(composite_treeContent);
81 viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
82
83 button_remove = new Button(composite_treeContent, SWT.PUSH);
84 button_remove.setText("Remove");
85 button_remove.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
86
87 Composite composite_treeTitle = new Composite(composite, SWT.NULL);
88 composite_treeTitle.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
89 false));
90 composite_treeTitle.setLayout(new GridLayout(3, false));
91
92
93
94 Label label_title = new Label(composite_treeTitle, SWT.NULL);
95 label_title.setText("New Feature Tree");
96
97 text_title = new Text(composite_treeTitle, SWT.NULL);
98 text_title.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
99
100 button_add = new Button(composite_treeTitle, SWT.PUSH);
101 button_add.setText("Add");
102
103 viewer.setContentProvider(new FeatureTreeContentProvider());
104 viewer.setLabelProvider(new FeatureTreeLabelProvider());
105
106 viewer.addSelectionChangedListener(this);
107
108 text_title.addModifyListener(this);
109 button_add.addSelectionListener(new AddButtonSelectionListener());
110 button_remove.addSelectionListener(new RemoveButtonSelectionListener());
111
112 List<FeatureTree> input = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
113
114 viewer.setInput(input);
115 modifyText(null);
116 setControl(composite);
117 }
118
119 /** {@inheritDoc} */
120 @Override
121 public void selectionChanged(SelectionChangedEvent event) {
122 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
123
124 if(selection.size() == 1){
125 FeatureTree selectedFeatureTree = (FeatureTree) selection.getFirstElement();
126 ((FeatureTreeEditorWizard) getWizard()).setSelectedFeatureTree(selectedFeatureTree);
127
128 }
129
130 setPageComplete(true);
131
132 button_remove.setEnabled(selection.size() >= 1);
133 }
134
135 /** {@inheritDoc} */
136 @Override
137 public boolean canFlipToNextPage() {
138 return ((IStructuredSelection) viewer.getSelection()).size() == 1;
139 }
140
141 /** {@inheritDoc} */
142 @Override
143 public void modifyText(ModifyEvent e) {
144 button_add.setEnabled(! text_title.getText().isEmpty());
145 }
146
147 private class AddButtonSelectionListener extends SelectionAdapter {
148 @Override
149 public void widgetSelected(SelectionEvent e) {
150 FeatureTree featureTree = FeatureTree.NewInstance();
151 featureTree.setTitleCache(text_title.getText(), true);
152
153 viewer.add(featureTree);
154 CdmStore.getService(IFeatureTreeService.class).merge(featureTree, true);
155
156 text_title.setText("");
157 viewer.setSelection(new StructuredSelection(featureTree));
158
159 }
160 }
161
162 private class RemoveButtonSelectionListener extends SelectionAdapter {
163 @Override
164 public void widgetSelected(SelectionEvent e) {
165 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
166 Object source = e.getSource();
167 for(Object element : selection.toArray()){
168 viewer.remove(element);
169 ((FeatureTreeEditorWizard) getWizard()).setSelectedFeatureTree(null);
170 DeleteResult result = CdmStore.getService(IFeatureTreeService.class).delete(((FeatureTree) element).getUuid());
171
172 if (result.isError()){
173 DeleteResultMessagingUtils.messageDialogWithDetails(result, "The delete of the feature tree was not successful.", null);
174 }
175 viewer.setSelection(new StructuredSelection(new Object[0]));
176 }
177 }
178 }
179 }