merge-update from trunk
[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.core.commands.ExecutionException;
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.IFeatureTreeService;
35 import eu.etaxonomy.cdm.api.service.exception.DataChangeNoRollbackException;
36 import eu.etaxonomy.cdm.model.description.FeatureTree;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38 import eu.etaxonomy.taxeditor.store.StoreUtil;
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 public void widgetSelected(SelectionEvent e) {
148 FeatureTree featureTree = FeatureTree.NewInstance();
149 featureTree.setTitleCache(text_title.getText(), true);
150
151 viewer.add(featureTree);
152 CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(featureTree);
153 text_title.setText("");
154 viewer.setSelection(new StructuredSelection(featureTree));
155
156 }
157 }
158
159 private class RemoveButtonSelectionListener extends SelectionAdapter {
160 public void widgetSelected(SelectionEvent e) {
161 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
162 for(Object element : selection.toArray()){
163 viewer.remove(element);
164
165 String result = CdmStore.getService(IFeatureTreeService.class).delete((FeatureTree) element);
166
167 viewer.setSelection(new StructuredSelection(new Object[0]));
168 }
169 }
170 }
171 }