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