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;
|
1
|
34
|
|
35
|
|
36
|
|
37
|
9
|
38 /**
|
|
39 * 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.
|
|
40 * @author dwinter
|
|
41 *
|
|
42 */
|
20
|
43 public class DigiImage implements IDigiImage, IAdaptable, ISelectionProvider{
|
1
|
44
|
9
|
45 /**
|
|
46 * Implemens a connection to Digilib.
|
|
47 */
|
|
48
|
20
|
49 private List<ISelectionChangedListener> selectionChangeListener = new ArrayList<ISelectionChangedListener>();
|
|
50
|
9
|
51 /* some parameters should not occur in the URL constructed by {@link #createUrlFromParameter(DigiImageParameter)} if set to negative values.
|
|
52 *
|
|
53 */
|
|
54 private static String[] omitIfNegativeArray = {"dw","dh","ddpi","ddpix","ddpiy"};
|
1
|
55 private static List<String> omitIfNegative = Arrays.asList(omitIfNegativeArray);
|
9
|
56
|
|
57
|
|
58 private String baseUrl="http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler";
|
1
|
59
|
9
|
60 /**
|
|
61 * Get the Url of the digilib scaler, set to http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler, if not changed.
|
|
62 * TODO should be part of a configuration/preference file
|
|
63 * @return
|
|
64 */
|
|
65 public String getBaseUrl() {
|
|
66 return baseUrl;
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * Setze die Basis url des Digilib-Scalers
|
|
71 * @param baseUrl
|
|
72 */
|
|
73 public void setBaseUrl(String baseUrl) {
|
|
74 this.baseUrl = baseUrl;
|
|
75 }
|
|
76
|
1
|
77 private Label label;
|
9
|
78
|
|
79 /* (non-Javadoc)
|
|
80 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getLabel()
|
|
81 */
|
1
|
82 public Label getLabel() {
|
|
83 return label;
|
|
84 }
|
|
85
|
9
|
86 /* (non-Javadoc)
|
|
87 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#setLabel(org.eclipse.swt.widgets.Label)
|
|
88 */
|
1
|
89 public void setLabel(Label label) {
|
|
90 this.label = label;
|
|
91 }
|
|
92
|
9
|
93 //private URL url;
|
1
|
94 private Composite parent;
|
9
|
95
|
1
|
96 private DigiImageParameter dp;
|
|
97
|
|
98
|
9
|
99 /**
|
|
100 *
|
|
101 * @param parent Composite where the image should be become a child of.
|
|
102 * @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
|
|
103 * @param dp Set of Parameters which define how the image should be displayed.
|
|
104 */
|
1
|
105 public DigiImage(Composite parent, int style, DigiImageParameter dp) {
|
|
106 //super(parent,style);
|
|
107 label = new Label(parent, style);
|
|
108 this.parent = parent;
|
|
109 this.dp=dp;
|
|
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;
|
|
168 return new URL(stringUrl);
|
|
169 }
|
20
|
170
|
|
171
|
1
|
172
|
9
|
173 /* (non-Javadoc)
|
|
174 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getParameter()
|
|
175 */
|
1
|
176 public DigiImageParameter getParameter() {
|
|
177
|
|
178 return dp;
|
|
179 }
|
|
180
|
9
|
181 /* (non-Javadoc)
|
|
182 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#setParameter(de.mpg.mpiwg.itgroup.digilib.digiImage.DigiImageParameter)
|
|
183 */
|
1
|
184 public void setParameter(DigiImageParameter dp) {
|
|
185 this.dp=dp;
|
|
186
|
|
187 }
|
|
188
|
9
|
189 /* (non-Javadoc)
|
|
190 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#redraw()
|
|
191 */
|
1
|
192 public void redraw(){
|
|
193 try {
|
|
194 setNewURL(createUrlFromParameter(dp));
|
20
|
195 setSelection(new StructuredSelection(this));
|
1
|
196 } catch (MalformedURLException e) {
|
|
197 // TODO Auto-generated catch block
|
|
198 e.printStackTrace();
|
|
199 }
|
|
200 }
|
|
201
|
9
|
202
|
|
203 private ZoomAreaRectangleListener rectangleListener=null;
|
1
|
204
|
9
|
205 /* (non-Javadoc)
|
|
206 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#getRectangleListener()
|
|
207 */
|
|
208 public ZoomAreaRectangleListener getRectangleListener() {
|
|
209
|
1
|
210 return rectangleListener;
|
|
211 }
|
|
212
|
9
|
213 /* (non-Javadoc)
|
|
214 * @see de.mpg.mpiwg.itgroup.digilib.digiImage.IDigiImage#setRectangleListener(de.mpg.mpiwg.itgroup.digilib.manipulator.extensions.RectangleListener)
|
|
215 */
|
|
216 public void setRectangleListener(ZoomAreaRectangleListener rectangleListener) {
|
1
|
217 this.rectangleListener = rectangleListener;
|
|
218 }
|
|
219
|
20
|
220 public Object getAdapter(Class adapter) {
|
|
221 if (adapter == IPropertySource.class)
|
|
222 return new DigiImageProperties(this);
|
|
223 return null;
|
|
224
|
|
225 }
|
|
226
|
|
227 public void addSelectionChangedListener(ISelectionChangedListener listener) {
|
|
228 selectionChangeListener.add(listener);
|
|
229
|
|
230 }
|
|
231
|
|
232 public ISelection getSelection() {
|
|
233 // TODO Auto-generated method stub
|
|
234 return new StructuredSelection(this);
|
|
235 }
|
|
236
|
|
237 public void removeSelectionChangedListener(
|
|
238 ISelectionChangedListener listener) {
|
|
239 selectionChangeListener.remove(listener);
|
|
240
|
|
241 }
|
|
242
|
|
243 public void setSelection(ISelection selection) {
|
|
244
|
|
245 for (ISelectionChangedListener sl: selectionChangeListener){
|
|
246
|
|
247 SelectionChangedEvent event = new SelectionChangedEvent(this, selection);
|
|
248 sl.selectionChanged(event);
|
|
249
|
|
250
|
|
251 }
|
|
252
|
|
253 }
|
|
254
|
|
255
|
1
|
256 }
|
|
257
|
|
258
|