Project

General

Profile

Download (5.66 KB) Statistics
| Branch: | Tag: | Revision:
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.ui.openurl;
12

    
13
import java.net.URI;
14
import java.util.List;
15

    
16
import org.eclipse.jface.operation.IRunnableWithProgress;
17
import org.eclipse.jface.wizard.WizardPage;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.events.SelectionListener;
21
import org.eclipse.swt.layout.GridData;
22
import org.eclipse.swt.layout.GridLayout;
23
import org.eclipse.swt.widgets.Button;
24
import org.eclipse.swt.widgets.Composite;
25
import org.eclipse.swt.widgets.Label;
26
import org.eclipse.ui.forms.widgets.ScrolledForm;
27

    
28
import eu.etaxonomy.cdm.ext.openurl.OpenUrlReference;
29
import eu.etaxonomy.taxeditor.model.MessagingUtils;
30
import eu.etaxonomy.taxeditor.ui.element.ImageElement;
31
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
32
import eu.etaxonomy.taxeditor.ui.element.RootElement;
33

    
34
/**
35
 * @author n.hoffmann
36
 * @created Jan 31, 2011
37
 * @version 1.0
38
 */
39
public class OpenUrlReferencePage extends WizardPage implements SelectionListener {
40

    
41
	public static final String NAME = "OpenUrlReferencePage";
42

    
43
	private static final int NEXT = 1;
44
	private static final int PREVIOUS = -1; 
45
	
46
	private ImageElement imageElement;
47
	private RootElement rootElement;
48
	private ScrolledForm scrolledForm;
49
	private Button button_next;
50
	private Button button_previous;
51

    
52
	private Composite container;
53

    
54
	private Label label_pageNumber; 
55
	
56
	/**
57
	 * @param pageName
58
	 */
59
	protected OpenUrlReferencePage() {
60
		super(NAME);
61
	}
62

    
63
	/* (non-Javadoc)
64
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
65
	 */
66
	@Override
67
	public void createControl(Composite parent) {
68
		container = getWizard().getFormFactory().createComposite(parent, SWT.NULL);
69
		
70
		container.setLayout(new GridLayout());
71
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
72
		
73
		scrolledForm = getWizard().getFormFactory().createScrolledForm(container);
74
		scrolledForm.getBody().setLayout(LayoutConstants.LAYOUT(3, false));
75
		scrolledForm.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
76
		
77
		Composite buttonContainer = getWizard().getFormFactory().createComposite(container);
78
		buttonContainer.setLayout(new GridLayout(3, true));
79
		buttonContainer.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false));
80
		
81
		button_previous = getWizard().getFormFactory().createButton(buttonContainer, "Previous", SWT.PUSH);
82
		button_previous.addSelectionListener(this);
83
		button_previous.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true, false));
84
		
85
		label_pageNumber = getWizard().getFormFactory().createLabel(buttonContainer, "              "); 
86
		label_pageNumber.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false));
87
		
88
		button_next = getWizard().getFormFactory().createButton(buttonContainer, "Next", SWT.PUSH);
89
		button_next.addSelectionListener(this);
90
		button_next.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, false));
91
		
92
		scrolledForm.getBody().setLayoutData(LayoutConstants.FILL());
93
		
94
		rootElement = new RootElement(getWizard().getFormFactory(), scrolledForm.getBody());
95
		
96
		imageElement = getWizard().getFormFactory().createImageElement(rootElement, null, SWT.BORDER);
97
		
98
		setControl(container);
99
	}
100

    
101
	public OpenUrlSelectorWizard getWizard(){
102
		return (OpenUrlSelectorWizard) super.getWizard();
103
	}
104

    
105
	/**
106
	 * @param jpegImage
107
	 */
108
	public void displayImage() {
109
		URI imageUri = getWizard().getOpenUrlReferenceImageUri();
110
		try {
111
			imageElement.initImageUri(imageUri);
112
			
113
			IRunnableWithProgress runnable = imageElement.getLoadImageRunnable(new Runnable(){
114
				/* (non-Javadoc)
115
				 * @see java.lang.Runnable#run()
116
				 */
117
				@Override
118
				public void run() {
119
					container.layout();
120
					
121
					configurePageBar();
122
				}
123
			});
124
		
125
			getWizard().getContainer().run(true, false, runnable);
126
		} catch (Exception e) {
127
			MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage()  + ": " + imageUri , e);
128
		}
129
	}
130

    
131
	/**
132
	 * 
133
	 */
134
	private void configurePageBar() {
135
		setTitle(getWizard().getOpenUrlReference().getTitleCache());
136
		String pageNumber = getWizard().getOpenUrlReference().getPages();
137
		label_pageNumber.setText(pageNumber);
138
		button_previous.setEnabled(canPage(PREVIOUS));
139
		button_next.setEnabled(canPage(NEXT));
140
	}
141

    
142
	/* (non-Javadoc)
143
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
144
	 */
145
	@Override
146
	public void widgetSelected(SelectionEvent e) {
147
		if(e.getSource() == button_next){
148
			pageResult(NEXT);
149
		}else{
150
			pageResult(PREVIOUS);
151
		}
152
	}
153
	
154
	private void pageResult(int direction){
155
		try {
156
			List<OpenUrlReference> referenceList = getWizard().getOpenUrlServiceWrapper().doPage(getWizard().getOpenUrlReference(), direction);
157
			OpenUrlReference openUrlReference = referenceList.iterator().next();
158
			getWizard().setOpenUrlReference(openUrlReference);
159
			displayImage();
160
			setErrorMessage(null);
161
		} catch (IllegalArgumentException e) {
162
			setErrorMessage(String.format("Could not page: %s", e.getLocalizedMessage()));
163
		} catch (Exception e) {
164
			MessagingUtils.error(getClass(), e);
165
		}
166
	}
167
	
168
	private boolean canPage(int direction){
169
		try{
170
			getWizard().getOpenUrlServiceWrapper().doPage(getWizard().getOpenUrlReference(), direction);
171
		}catch (IllegalArgumentException e) {
172
			return false;
173
		}
174
		return true;
175
	}
176

    
177
	/* (non-Javadoc)
178
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
179
	 */
180
	@Override
181
	public void widgetDefaultSelected(SelectionEvent e) {}
182
}
(4-4/7)