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