p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor.prototype2 / src / eu / etaxonomy / taxeditor / prototype2 / view / MoveDialogView.java
1 package eu.etaxonomy.taxeditor.prototype2.view;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.eclipse.jface.dialogs.Dialog;
7 import org.eclipse.jface.dialogs.IDialogConstants;
8 import org.eclipse.jface.viewers.DoubleClickEvent;
9 import org.eclipse.jface.viewers.IDoubleClickListener;
10 import org.eclipse.jface.viewers.StructuredSelection;
11 import org.eclipse.jface.viewers.TableViewer;
12 import org.eclipse.jface.viewers.TreeViewer;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.FocusEvent;
15 import org.eclipse.swt.events.FocusListener;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.layout.FillLayout;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Group;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.swt.widgets.Text;
29
30 import com.swtdesigner.ResourceManager;
31 import com.swtdesigner.SWTResourceManager;
32
33 import eu.etaxonomy.cdm.model.taxon.Taxon;
34 import eu.etaxonomy.taxeditor.prototype2.Activator;
35
36 public class MoveDialogView extends Dialog {
37
38 private Text searchText;
39 private TaxonomicTreeViewer treeViewer;
40 private Label selectedTaxonNameLabel;
41 private Taxon taxon;
42
43 private List<Button> buttons = new ArrayList<Button>();
44
45 public int open(Taxon taxon) {
46 this.taxon = taxon;
47 return super.open();
48 }
49 /**
50 * Create the dialog
51 * @param parentShell
52 */
53 public MoveDialogView(Shell parentShell) {
54 super(parentShell);
55 }
56
57 /**
58 * Create contents of the dialog
59 * @param parent
60 */
61 @Override
62 protected Control createDialogArea(Composite parent) {
63 parent.setRedraw(true);
64 Composite container = (Composite) super.createDialogArea(parent);
65 final GridLayout gridLayout = new GridLayout();
66 gridLayout.numColumns = 2;
67 container.setLayout(gridLayout);
68
69 final Label youHaveChosenLabel = new Label(container, SWT.WRAP);
70 final GridData gd_youHaveChosenLabel = new GridData(SWT.LEFT, SWT.FILL, true, false, 2, 1);
71 // gd_youHaveChosenLabel.heightHint = 30;
72 youHaveChosenLabel.setLayoutData(gd_youHaveChosenLabel);
73 // youHaveChosenLabel.setText("You have chosen to turn \"" + taxon.getName().getNameCache() +
74 // "\" into a synonym. Please choose a new accepted name from one of the following options.");
75 youHaveChosenLabel.setText("Choose a taxonomic parent for \"" + taxon.getName().getTitleCache() +
76 "\".");
77
78 final Group group_1 = new Group(container, SWT.NONE);
79 group_1.setLayout(new GridLayout());
80 final GridData gd_group_1 = new GridData(SWT.FILL, SWT.FILL, true, true);
81 gd_group_1.heightHint = 83;
82 group_1.setLayoutData(gd_group_1);
83
84 final Button chooseAcceptedNameButton = new Button(group_1, SWT.RADIO);
85 buttons.add(chooseAcceptedNameButton);
86 chooseAcceptedNameButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
87 chooseAcceptedNameButton.setText("Choose from taxonomic tree");
88
89 treeViewer = new TaxonomicTreeViewer(group_1);
90 treeViewer.getTree().addFocusListener(new SelectButtonFocusListener(chooseAcceptedNameButton));
91 treeViewer.addDoubleClickListener(new SelectTaxonDoubleClickListener());
92
93 final Composite composite = new Composite(container, SWT.NONE);
94 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
95 composite.setLayout(new FillLayout(SWT.VERTICAL | SWT.HORIZONTAL));
96
97 final Group group = new Group(composite, SWT.NONE);
98 group.setLayout(new GridLayout());
99
100 final Button chooseAcceptedTaxonButton = new Button(group, SWT.RADIO);
101 buttons.add(chooseAcceptedTaxonButton);
102 chooseAcceptedTaxonButton.setText("Choose from recently viewed taxa");
103
104 TableViewer recentNamesViewer = new RecentNamesTableViewer(group);
105 recentNamesViewer.addDoubleClickListener(new SelectTaxonDoubleClickListener());
106 recentNamesViewer.getTable().addFocusListener(new SelectButtonFocusListener(chooseAcceptedTaxonButton));
107
108 final Group group_2 = new Group(composite, SWT.NONE);
109 final GridLayout gridLayout_1 = new GridLayout();
110 gridLayout_1.numColumns = 2;
111 group_2.setLayout(gridLayout_1);
112
113 final Button searchForNameButton = new Button(group_2, SWT.RADIO);
114 buttons.add(searchForNameButton);
115 searchForNameButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
116 searchForNameButton.setText("Search for name of accepted taxon");
117 new Label(group_2, SWT.NONE);
118
119 searchText = new Text(group_2, SWT.BORDER);
120 searchText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
121 searchText.setForeground(SWTResourceManager.getColor(192, 192, 192));
122 searchText.setText("Use \"*\" for wildcard searching");
123 searchText.addFocusListener(new SelectButtonFocusListener(searchForNameButton) {
124
125 public void focusGained(FocusEvent e) {
126 selectButton(button);
127 searchText.setForeground(SWTResourceManager.getColor(0,0,0));
128 searchText.setText("");
129 }
130
131 public void focusLost(FocusEvent e) {
132 if (searchText.getText() == "") {
133 searchText.setForeground(SWTResourceManager.getColor(192, 192, 192));
134 searchText.setText("Use \"*\" for wildcard searching");
135 }
136 }
137
138 });
139
140 final Button searchButton = new Button(group_2, SWT.NONE);
141 searchButton.setLayoutData(new GridData());
142 searchButton.setText("Search");
143
144 final Composite composite_1 = new Composite(container, SWT.NONE);
145 composite_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 2, 1));
146 composite_1.setLayout(new FillLayout());
147
148 new Label(composite_1, SWT.NONE).setText("Selected taxon: ");
149
150 selectedTaxonNameLabel = new Label(composite_1, SWT.RIGHT);
151 selectedTaxonNameLabel.setFont(SWTResourceManager.getFont("", 9, SWT.BOLD));
152 selectedTaxonNameLabel.setText("");
153 new Label(container, SWT.NONE);
154
155
156 //
157 initButtons();
158
159 return container;
160 }
161
162 /**
163 * Create contents of the button bar
164 * @param parent
165 */
166 @Override
167 protected void createButtonsForButtonBar(Composite parent) {
168 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
169 true);
170 createButton(parent, IDialogConstants.CANCEL_ID,
171 IDialogConstants.CANCEL_LABEL, false);
172 }
173
174 private void initButtons() {
175 for ( Button button : buttons) {
176 button.addSelectionListener(new SelectionAdapter() {
177 public void widgetSelected(final SelectionEvent e) {
178 selectButton((Button) e.getSource());
179 }
180 });
181 }
182 }
183
184 /**
185 * Since buttons are not in the same layout group, the others are not automatically
186 * deselected when one is selected. This method takes care of that.
187 * @param button
188 */
189 private void selectButton(Button selectedButton) {
190 for ( Button button : buttons) {
191 if (button == selectedButton)
192 button.setSelection(true);
193 else
194 button.setSelection(false);
195 }
196 }
197
198 /**
199 * Return the initial size of the dialog
200 */
201 @Override
202 protected Point getInitialSize() {
203 return new Point(700, 500);
204 }
205 protected void configureShell(Shell newShell) {
206 super.configureShell(newShell);
207 newShell.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/edit_16x16.ico"));
208 newShell.setText("Choose parent taxon for \"" + taxon.getName().getTitleCache() +
209 "\"");
210 }
211
212 class SelectButtonFocusListener implements FocusListener {
213
214 Button button; // the button that should be selected
215
216 SelectButtonFocusListener(Button button) {
217 this.button = button;
218 }
219
220 public void focusGained(FocusEvent e) {
221 selectButton(button);
222 }
223
224 public void focusLost(FocusEvent e) {
225 }
226
227 }
228
229 class SelectTaxonDoubleClickListener implements IDoubleClickListener {
230
231 public void doubleClick(DoubleClickEvent event) {
232 // TaxonName taxonname = null;
233 Taxon taxon = null;
234 try {
235 taxon = (Taxon) ((StructuredSelection)event.getSelection()).getFirstElement();
236 }catch (Exception e){
237 e.printStackTrace();
238 taxon = null;
239 }
240 selectedTaxonNameLabel.setText(taxon.getName().getTitleCache());
241 }
242 }
243 }