fixing #5341 and colouring for cache relevant fields #4915 and #4944
[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
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.store.CdmStore;
37
38 /**
39 * <p>SelectFeatureTreeWizardPage class.</p>
40 *
41 * @author n.hoffmann
42 * @created Aug 5, 2010
43 * @version 1.0
44 */
45 public class SelectFeatureTreeWizardPage extends WizardPage implements ISelectionChangedListener, ModifyListener{
46
47 private ListViewer viewer;
48 private Text text_title;
49 private Button button_add;
50 private Button button_remove;
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 setMessage("Select a Feature Tree or create a new one.");
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 Composite composite = new Composite(parent, SWT.NULL);
69
70 composite.setLayout(new GridLayout());
71
72 Composite composite_treeContent = new Composite(composite, SWT.NULL);
73 composite_treeContent.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
74 true, true));
75 composite_treeContent.setLayout(new GridLayout(2, false));
76
77 viewer = new ListViewer(composite_treeContent);
78 viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
79
80 button_remove = new Button(composite_treeContent, SWT.PUSH);
81 button_remove.setText("Remove");
82 button_remove.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
83
84 Composite composite_treeTitle = new Composite(composite, SWT.NULL);
85 composite_treeTitle.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true,
86 false));
87 composite_treeTitle.setLayout(new GridLayout(3, false));
88
89
90
91 Label label_title = new Label(composite_treeTitle, SWT.NULL);
92 label_title.setText("New Feature Tree");
93
94 text_title = new Text(composite_treeTitle, SWT.NULL);
95 text_title.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
96
97 button_add = new Button(composite_treeTitle, SWT.PUSH);
98 button_add.setText("Add");
99
100 viewer.setContentProvider(new FeatureTreeContentProvider());
101 viewer.setLabelProvider(new FeatureTreeLabelProvider());
102
103 viewer.addSelectionChangedListener(this);
104
105 text_title.addModifyListener(this);
106 button_add.addSelectionListener(new AddButtonSelectionListener());
107 button_remove.addSelectionListener(new RemoveButtonSelectionListener());
108
109 List<FeatureTree> input = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
110
111 viewer.setInput(input);
112 modifyText(null);
113 setControl(composite);
114 }
115
116 /** {@inheritDoc} */
117 @Override
118 public void selectionChanged(SelectionChangedEvent event) {
119 IStructuredSelection selection = (IStructuredSelection) event.getSelection();
120
121 if(selection.size() == 1){
122 FeatureTree selectedFeatureTree = (FeatureTree) selection.getFirstElement();
123 ((FeatureTreeEditorWizard) getWizard()).setSelectedFeatureTree(selectedFeatureTree);
124
125 }
126
127 setPageComplete(true);
128
129 button_remove.setEnabled(selection.size() >= 1);
130 }
131
132 /** {@inheritDoc} */
133 @Override
134 public boolean canFlipToNextPage() {
135 return ((IStructuredSelection) viewer.getSelection()).size() == 1;
136 }
137
138 /** {@inheritDoc} */
139 @Override
140 public void modifyText(ModifyEvent e) {
141 button_add.setEnabled(! text_title.getText().isEmpty());
142 }
143
144 private class AddButtonSelectionListener extends SelectionAdapter {
145 @Override
146 public void widgetSelected(SelectionEvent e) {
147 FeatureTree featureTree = FeatureTree.NewInstance();
148 featureTree.setTitleCache(text_title.getText(), true);
149
150 viewer.add(featureTree);
151 if(CdmStore.getCurrentSessionManager().isRemoting()) {
152 CdmStore.getService(IFeatureTreeService.class).merge(featureTree);
153 } else {
154 CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(featureTree);
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
174 }
175 viewer.setSelection(new StructuredSelection(new Object[0]));
176 }
177 }
178 }
179 }