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.ui.openurl;
|
11
|
|
12
|
import java.net.URI;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.eclipse.jface.operation.IRunnableWithProgress;
|
16
|
import org.eclipse.jface.wizard.WizardPage;
|
17
|
import org.eclipse.swt.SWT;
|
18
|
import org.eclipse.swt.events.SelectionEvent;
|
19
|
import org.eclipse.swt.events.SelectionListener;
|
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.Label;
|
25
|
import org.eclipse.ui.forms.widgets.ScrolledForm;
|
26
|
|
27
|
import eu.etaxonomy.cdm.ext.openurl.OpenUrlReference;
|
28
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
29
|
import eu.etaxonomy.taxeditor.ui.element.ImageElement;
|
30
|
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
|
31
|
import eu.etaxonomy.taxeditor.ui.element.RootElement;
|
32
|
|
33
|
/**
|
34
|
* @author n.hoffmann
|
35
|
* @created Jan 31, 2011
|
36
|
*/
|
37
|
public class OpenUrlReferencePage extends WizardPage implements SelectionListener {
|
38
|
|
39
|
public static final String NAME = "OpenUrlReferencePage";
|
40
|
|
41
|
private static final int NEXT = 1;
|
42
|
private static final int PREVIOUS = -1;
|
43
|
|
44
|
private ImageElement imageElement;
|
45
|
private RootElement rootElement;
|
46
|
private ScrolledForm scrolledForm;
|
47
|
private Button button_next;
|
48
|
private Button button_previous;
|
49
|
|
50
|
private Composite container;
|
51
|
|
52
|
private Label label_pageNumber;
|
53
|
|
54
|
protected OpenUrlReferencePage() {
|
55
|
super(NAME);
|
56
|
}
|
57
|
|
58
|
@Override
|
59
|
public void createControl(Composite parent) {
|
60
|
container = getWizard().getFormFactory().createComposite(parent, SWT.NULL);
|
61
|
|
62
|
container.setLayout(new GridLayout());
|
63
|
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
64
|
|
65
|
scrolledForm = getWizard().getFormFactory().createScrolledForm(container);
|
66
|
scrolledForm.getBody().setLayout(LayoutConstants.LAYOUT(3, false));
|
67
|
scrolledForm.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
|
68
|
|
69
|
Composite buttonContainer = getWizard().getFormFactory().createComposite(container);
|
70
|
buttonContainer.setLayout(new GridLayout(3, true));
|
71
|
buttonContainer.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false));
|
72
|
|
73
|
button_previous = getWizard().getFormFactory().createButton(buttonContainer, "Previous", SWT.PUSH);
|
74
|
button_previous.addSelectionListener(this);
|
75
|
button_previous.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true, false));
|
76
|
|
77
|
label_pageNumber = getWizard().getFormFactory().createLabel(buttonContainer, " ");
|
78
|
label_pageNumber.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
|
79
|
|
80
|
button_next = getWizard().getFormFactory().createButton(buttonContainer, "Next", SWT.PUSH);
|
81
|
button_next.addSelectionListener(this);
|
82
|
button_next.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, false));
|
83
|
|
84
|
scrolledForm.getBody().setLayoutData(LayoutConstants.FILL());
|
85
|
|
86
|
rootElement = new RootElement(getWizard().getFormFactory(), scrolledForm.getBody());
|
87
|
|
88
|
imageElement = getWizard().getFormFactory().createImageElement(rootElement, null, SWT.BORDER);
|
89
|
|
90
|
setControl(container);
|
91
|
}
|
92
|
|
93
|
public OpenUrlSelectorWizard getWizard(){
|
94
|
return (OpenUrlSelectorWizard) super.getWizard();
|
95
|
}
|
96
|
|
97
|
public void displayImage() {
|
98
|
URI imageUri = getWizard().getOpenUrlReferenceImageUri();
|
99
|
try {
|
100
|
imageElement.initImageUri(imageUri);
|
101
|
|
102
|
IRunnableWithProgress runnable = imageElement.getLoadImageRunnable(new Runnable(){
|
103
|
/* (non-Javadoc)
|
104
|
* @see java.lang.Runnable#run()
|
105
|
*/
|
106
|
@Override
|
107
|
public void run() {
|
108
|
container.layout();
|
109
|
|
110
|
configurePageBar();
|
111
|
}
|
112
|
});
|
113
|
|
114
|
getWizard().getContainer().run(true, false, runnable);
|
115
|
} catch (Exception e) {
|
116
|
MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage() + ": " + imageUri , e);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
private void configurePageBar() {
|
121
|
setTitle(getWizard().getOpenUrlReference().getTitleCache());
|
122
|
String pageNumber = getWizard().getOpenUrlReference().getPages();
|
123
|
label_pageNumber.setText(pageNumber);
|
124
|
button_previous.setEnabled(canPage(PREVIOUS));
|
125
|
button_next.setEnabled(canPage(NEXT));
|
126
|
}
|
127
|
|
128
|
@Override
|
129
|
public void widgetSelected(SelectionEvent e) {
|
130
|
if(e.getSource() == button_next){
|
131
|
pageResult(NEXT);
|
132
|
}else{
|
133
|
pageResult(PREVIOUS);
|
134
|
}
|
135
|
}
|
136
|
|
137
|
private void pageResult(int direction){
|
138
|
try {
|
139
|
List<OpenUrlReference> referenceList = getWizard().getOpenUrlServiceWrapper().doPage(getWizard().getOpenUrlReference(), direction);
|
140
|
OpenUrlReference openUrlReference = referenceList.iterator().next();
|
141
|
getWizard().setOpenUrlReference(openUrlReference);
|
142
|
displayImage();
|
143
|
setErrorMessage(null);
|
144
|
} catch (IllegalArgumentException e) {
|
145
|
setErrorMessage(String.format("Could not page: %s", e.getLocalizedMessage()));
|
146
|
} catch (Exception e) {
|
147
|
MessagingUtils.error(getClass(), e);
|
148
|
}
|
149
|
}
|
150
|
|
151
|
private boolean canPage(int direction){
|
152
|
try{
|
153
|
getWizard().getOpenUrlServiceWrapper().doPage(getWizard().getOpenUrlReference(), direction);
|
154
|
}catch (IllegalArgumentException e) {
|
155
|
return false;
|
156
|
}
|
157
|
return true;
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
public void widgetDefaultSelected(SelectionEvent e) {}
|
162
|
}
|