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