1
|
1 package de.mpg.mpiwg.itgroup.digilib.digiImage;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.io.InputStream;
|
|
5 import java.net.MalformedURLException;
|
|
6 import java.net.URL;
|
|
7 import java.util.ArrayList;
|
|
8 import java.util.Arrays;
|
|
9 import java.util.List;
|
|
10 import java.util.Map;
|
|
11
|
|
12 import org.apache.commons.lang.StringUtils;
|
20
|
13 import org.eclipse.core.runtime.IAdaptable;
|
|
14 import org.eclipse.jface.viewers.ISelection;
|
|
15 import org.eclipse.jface.viewers.ISelectionChangedListener;
|
|
16 import org.eclipse.jface.viewers.ISelectionProvider;
|
|
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
|
|
18 import org.eclipse.jface.viewers.StructuredSelection;
|
1
|
19 import org.eclipse.swt.graphics.Image;
|
|
20 import org.eclipse.swt.widgets.Composite;
|
|
21 import org.eclipse.swt.widgets.Label;
|
20
|
22 import org.eclipse.ui.views.properties.IPropertySource;
|
1
|
23
|
28
|
24 import de.mpg.mpiwg.itgroup.digilib.core.plugin.Activator;
|
20
|
25 import de.mpg.mpiwg.itgroup.digilib.digiImage.properties.DigiImageProperties;
|
9
|
26 import de.mpg.mpiwg.itgroup.digilib.manipulator.extensions.ZoomAreaRectangleListener;
|
21
|
27 import de.mpg.mpiwg.itgroup.digilib.plugin.editors.DigilibLinkEditorObservable;
|
26
|
28 import de.mpg.mpiwg.itgroup.digilib.plugin.preferences.PluginPreferencePage;
|
1
|
29
|
|
30
|
|
31
|
|
32
|
9
|
33 /**
|
|
34 * Refers to an Digilib-Scaler Object either an image or an folder. The object contains an swt-label object, so that it can be embedded e.g. in Eclipse.
|
|
35 * @author dwinter
|
|
36 *
|
|
37 */
|
27
|
38 public class DigiImage implements IDigiImage, IAdaptable{
|
1
|
39
|
9
|
40 /**
|
|
41 * Implemens a connection to Digilib.
|
|
42 */
|
|
43
|
22
|
44
|
|
45
|
20
|
46 private List<ISelectionChangedListener> selectionChangeListener = new ArrayList<ISelectionChangedListener>();
|
|
47
|
9
|
48 /* some parameters should not occur in the URL constructed by {@link #createUrlFromParameter(DigiImageParameter)} if set to negative values.
|
|
49 *
|
|
50 */
|
|
51 private static String[] omitIfNegativeArray = {"dw","dh","ddpi","ddpix","ddpiy"};
|
1
|
52 private static List<String> omitIfNegative = Arrays.asList(omitIfNegativeArray);
|
9
|
53
|
|
54
|
22
|
55 //private String baseUrl="http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler";
|
1
|
56
|
9
|
57 /**
|
|
58 * Get the Url of the digilib scaler, set to http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler, if not changed.
|
|
59 * TODO should be part of a configuration/preference file
|
|
60 * @return
|
|
61 */
|
|
62 public String getBaseUrl() {
|
|
63 return baseUrl;
|
|
64 }
|
|
65
|
|
66 /**
|
|
67 * Setze die Basis url des Digilib-Scalers
|
|
68 * @param baseUrl
|
|
69 */
|
|
70 public void setBaseUrl(String baseUrl) {
|
|
71 this.baseUrl = baseUrl;
|
|
72 }
|
|
73
|
1
|
74 private Label label;
|
9
|
75
|
|
76 /* (non-Javadoc)
|
|
77 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getLabel()
|
|
78 */
|
1
|
79 public Label getLabel() {
|
|
80 return label;
|
|
81 }
|
|
82
|
9
|
83 /* (non-Javadoc)
|
|
84 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#setLabel(org.eclipse.swt.widgets.Label)
|
|
85 */
|
1
|
86 public void setLabel(Label label) {
|
|
87 this.label = label;
|
|
88 }
|
|
89
|
9
|
90 //private URL url;
|
1
|
91 private Composite parent;
|
9
|
92
|
1
|
93 private DigiImageParameter dp;
|
22
|
94
|
|
95 private String baseUrl;
|
1
|
96
|
|
97
|
9
|
98 /**
|
|
99 *
|
|
100 * @param parent Composite where the image should be become a child of.
|
|
101 * @param style SWT Style how to display the image as a composite. For more the the style parameter at @see import org.eclipse.swt.widgets.Label
|
|
102 * @param dp Set of Parameters which define how the image should be displayed.
|
|
103 */
|
1
|
104 public DigiImage(Composite parent, int style, DigiImageParameter dp) {
|
|
105 //super(parent,style);
|
|
106 label = new Label(parent, style);
|
|
107 this.parent = parent;
|
|
108 this.dp=dp;
|
23
|
109 this.baseUrl = Activator.getDefault().getPreferenceStore().getString(PluginPreferencePage.baseUrl_id);
|
1
|
110 try {
|
|
111 setNewURL(createUrlFromParameter(dp));
|
|
112 } catch (MalformedURLException e) {
|
|
113 // TODO Auto-generated catch block
|
|
114 e.printStackTrace();
|
|
115 }
|
|
116
|
|
117 }
|
|
118
|
9
|
119 /**
|
|
120 * Sets the complete URL of the manipulated image and changes the image object in the composite.
|
|
121 * @param url
|
|
122 */
|
1
|
123 private void setNewURL(URL url) {
|
9
|
124 //this.url = url;
|
|
125
|
1
|
126
|
|
127 InputStream is;
|
|
128 try {
|
|
129 is = url.openStream();
|
|
130 } catch (IOException e) {
|
|
131 // TODO Auto-generated catch block
|
|
132 e.printStackTrace();
|
|
133 return;
|
|
134 }
|
|
135 Image img = new Image(parent.getDisplay(),is);
|
|
136 //Label label = new Label(parent, SWT.None);
|
|
137 label.setImage(img);
|
|
138
|
|
139 label.setVisible(true);
|
|
140 }
|
|
141
|
|
142
|
9
|
143 /* (non-Javadoc)
|
|
144 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getParent()
|
|
145 */
|
1
|
146 public Composite getParent() {
|
|
147 return parent;
|
|
148 }
|
|
149
|
|
150
|
9
|
151 /**
|
|
152 * Creates the URL to digilib from the given parameter set. For the documentation @see {@link http://developer.berlios.de/docman/display_doc.php?docid=106&group_id=251}.
|
|
153 * @param dp
|
|
154 * @return
|
|
155 * @throws MalformedURLException
|
|
156 */
|
1
|
157 private URL createUrlFromParameter(DigiImageParameter dp) throws MalformedURLException{
|
9
|
158
|
20
|
159 Map<String, String> qa = dp.createMapFromParameters(omitIfNegative);
|
1
|
160
|
|
161 List<String> queryArray = new ArrayList<String>();
|
|
162 for(String key:qa.keySet()){
|
|
163 queryArray.add(key+"="+qa.get(key));
|
|
164 }
|
|
165 String queryString = StringUtils.join(queryArray,"&");
|
|
166
|
|
167 String stringUrl=baseUrl+"?"+queryString;
|
25
|
168
|
1
|
169 return new URL(stringUrl);
|
|
170 }
|
20
|
171
|
|
172
|
1
|
173
|
9
|
174 /* (non-Javadoc)
|
|
175 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getParameter()
|
|
176 */
|
1
|
177 public DigiImageParameter getParameter() {
|
|
178
|
|
179 return dp;
|
|
180 }
|
|
181
|
9
|
182 /* (non-Javadoc)
|
|
183 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#setParameter(de.mpg.mpiwg.itgroup.digilib.digiImage.DigiImageParameter)
|
|
184 */
|
1
|
185 public void setParameter(DigiImageParameter dp) {
|
|
186 this.dp=dp;
|
|
187
|
|
188 }
|
|
189
|
9
|
190 /* (non-Javadoc)
|
|
191 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#redraw()
|
|
192 */
|
1
|
193 public void redraw(){
|
|
194 try {
|
|
195 setNewURL(createUrlFromParameter(dp));
|
20
|
196 setSelection(new StructuredSelection(this));
|
21
|
197 DigilibLinkEditorObservable.INSTANCE.imageHasChanged(this);
|
25
|
198
|
1
|
199 } catch (MalformedURLException e) {
|
|
200 // TODO Auto-generated catch block
|
|
201 e.printStackTrace();
|
|
202 }
|
|
203 }
|
|
204
|
9
|
205
|
|
206 private ZoomAreaRectangleListener rectangleListener=null;
|
1
|
207
|
9
|
208 /* (non-Javadoc)
|
|
209 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getRectangleListener()
|
|
210 */
|
|
211 public ZoomAreaRectangleListener getRectangleListener() {
|
|
212
|
1
|
213 return rectangleListener;
|
|
214 }
|
|
215
|
9
|
216 /* (non-Javadoc)
|
|
217 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#setRectangleListener(de.mpg.mpiwg.itgroup.digilib.manipulator.extensions.RectangleListener)
|
|
218 */
|
|
219 public void setRectangleListener(ZoomAreaRectangleListener rectangleListener) {
|
1
|
220 this.rectangleListener = rectangleListener;
|
|
221 }
|
|
222
|
21
|
223 /* (non-Javadoc)
|
|
224 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
|
24
|
225 *
|
21
|
226 * Digiimage shows properties if asked to.
|
|
227 */
|
24
|
228 @SuppressWarnings("rawtypes")
|
20
|
229 public Object getAdapter(Class adapter) {
|
|
230 if (adapter == IPropertySource.class)
|
|
231 return new DigiImageProperties(this);
|
25
|
232
|
20
|
233 return null;
|
|
234
|
|
235 }
|
|
236
|
21
|
237 /* (non-Javadoc)
|
|
238 * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener)
|
|
239 * Digiimage registeres selelection changers
|
|
240 */
|
20
|
241 public void addSelectionChangedListener(ISelectionChangedListener listener) {
|
|
242 selectionChangeListener.add(listener);
|
|
243
|
|
244 }
|
|
245
|
21
|
246 /* (non-Javadoc)
|
|
247 * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection()
|
|
248 * Selection is the object itself.
|
|
249 */
|
20
|
250 public ISelection getSelection() {
|
21
|
251
|
20
|
252 return new StructuredSelection(this);
|
|
253 }
|
|
254
|
|
255 public void removeSelectionChangedListener(
|
|
256 ISelectionChangedListener listener) {
|
|
257 selectionChangeListener.remove(listener);
|
|
258
|
|
259 }
|
|
260
|
21
|
261 /* (non-Javadoc)
|
|
262 * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection)
|
|
263 * Informs all listeners that selection has changed.
|
|
264 */
|
20
|
265 public void setSelection(ISelection selection) {
|
|
266
|
|
267 for (ISelectionChangedListener sl: selectionChangeListener){
|
|
268 SelectionChangedEvent event = new SelectionChangedEvent(this, selection);
|
|
269 sl.selectionChanged(event);
|
|
270
|
|
271
|
|
272 }
|
|
273
|
|
274 }
|
|
275
|
|
276
|
25
|
277
|
1
|
278 }
|
|
279
|
|
280
|