Source code: com/port80/draw2d/imageviewer/views/ImageViewer.java
1 package com.port80.draw2d.imageviewer.views;
2
3 /*
4 * (c) Copyright IBM Corp. 2000, 2001.
5 * All Rights Reserved.
6 */
7
8 import org.eclipse.core.resources.IFile;
9 import org.eclipse.core.resources.IMarker;
10 import org.eclipse.core.runtime.IProgressMonitor;
11 import org.eclipse.draw2d.FigureCanvas;
12 import org.eclipse.draw2d.Label;
13 import org.eclipse.draw2d.geometry.Point;
14 import org.eclipse.draw2d.geometry.Rectangle;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.swt.SWTException;
17 import org.eclipse.swt.events.MouseAdapter;
18 import org.eclipse.swt.events.MouseEvent;
19 import org.eclipse.swt.events.MouseListener;
20 import org.eclipse.swt.graphics.Cursor;
21 import org.eclipse.swt.graphics.ImageData;
22 import org.eclipse.swt.graphics.ImageLoader;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.IEditorInput;
25 import org.eclipse.ui.IEditorSite;
26 import org.eclipse.ui.PartInitException;
27 import org.eclipse.ui.part.EditorPart;
28 import org.eclipse.ui.part.FileEditorInput;
29
30 import com.port80.eclipse.util.ColorFactory;
31 import com.port80.eclipse.util.CursorFactory;
32 import com.port80.util.Msg;
33 import com.port80.util.StopWatch;
34
35 /**
36 * An image viewer that construct visible region of the image on the fly.
37 */
38 public class ImageViewer extends EditorPart {
39
40 ////////////////////////////////////////////////////////////////////////
41
42 private static final String NAME = "ImageViewer";
43 private static final boolean DEBUG = false;
44
45 private static final Cursor fCursorWait = CursorFactory.getDefault().create("Wait");
46
47 ////////////////////////////////////////////////////////////////////////
48
49 private FigureCanvas fCanvas;
50 private Label fLabel;
51 //
52 private ImageData[] fImageData;
53 private ImageViewport fViewport;
54 private String fFilepath;
55 private float fLoadTime;
56
57 ////////////////////////////////////////////////////////////////////////
58
59 /**
60 * Creates a multi-page editor example.
61 */
62 public ImageViewer() {
63 super();
64 }
65
66 ////////////////////////////////////////////////////////////////////////
67
68 /**
69 * @see org.eclipse.ui.IEditorPart#init(IEditorSite, IEditorInput)
70 */
71 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
72 if (!(input instanceof FileEditorInput)) {
73 Msg.err("Expected FileEditorInput: input=" + input);
74 return;
75 }
76 setSite(site);
77 setInput(input);
78 IFile file = ((FileEditorInput) input).getFile();
79 fFilepath = file.getLocation().toString();
80 Msg.println(NAME + ".init(): filename=" + fFilepath);
81 }
82
83 /**
84 * @see org.eclipse.ui.IEditorPart#isDirty()
85 */
86 public boolean isDirty() {
87 return false;
88 }
89
90 /**
91 * @see org.eclipse.ui.IEditorPart#isSaveAsAllowed()
92 */
93 public boolean isSaveAsAllowed() {
94 return false;
95 }
96
97 /**
98 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(Composite)
99 */
100 public void createPartControl(Composite parent) {
101 fCanvas = new FigureCanvas(parent);
102 fCanvas.setBackground(ColorFactory.getDefault().create(0xffffff));
103 fCanvas.addMouseListener(new DefaultMouseHandler());
104 fViewport = new ImageViewport();
105 fCanvas.setContents(fViewport);
106 openFile(fFilepath);
107 }
108
109 /**
110 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
111 */
112 public void setFocus() {
113 }
114
115 /**
116 * @see org.eclipse.ui.IEditorPart#doSave(IProgressMonitor)
117 */
118 public void doSave(IProgressMonitor monitor) {
119 }
120
121 /**
122 * @see org.eclipse.ui.IEditorPart#doSaveAs()
123 */
124 public void doSaveAs() {
125 }
126
127 /**
128 * @see org.eclipse.ui.IEditorPart#gotoMarker(IMarker)
129 */
130 public void gotoMarker(IMarker marker) {
131 }
132
133 ////////////////////////////////////////////////////////////////////////
134
135 public void openFile(String filename) {
136 getSite().getShell().setCursor(fCursorWait);
137 fCanvas.setCursor(fCursorWait);
138 try {
139 ImageLoader loader = new ImageLoader();
140 // Read the new image(s) from the chosen file.
141 StopWatch timer = new StopWatch().start();
142 fImageData = loader.load(filename);
143 fLoadTime = timer.stop().elapsed();
144 if (DEBUG)
145 Msg.println(NAME + ".openFile(): load time=" + timer.toString());
146 if (fImageData.length > 0) {
147 // Display the first image in the file.
148 displayImage(fImageData[0]);
149 //resetScrollBars();
150 }
151 } catch (SWTException e) {
152 showErrorDialog(_("Load Error"), filename + e);
153 } finally {
154 getSite().getShell().setCursor(null);
155 fCanvas.setCursor(null);
156 }
157 }
158
159 public void displayImage(ImageData imagedata) {
160 fViewport.setImageData(imagedata);
161 fViewport.repaint();
162 }
163
164 ////////////////////////////////////////////////////////////////////////
165
166 private final void showErrorDialog(String title, String message) {
167 MessageDialog.openError(getSite().getShell(), title, message);
168 }
169
170 private static final String _(String s) {
171 return s;
172 }
173
174 FigureCanvas getCanvas() {
175 return fCanvas;
176 }
177
178 ImageData getImageData() {
179 return fImageData[0];
180 }
181
182 ////////////////////////////////////////////////////////////////////////
183
184 private class DefaultMouseHandler extends MouseAdapter implements MouseListener {
185 public void mouseDown(MouseEvent e) {
186 Rectangle bounds = getCanvas().getViewport().getBounds();
187 Point origin = getCanvas().getViewport().getViewLocation();
188 if (DEBUG)
189 Msg.println(
190 NAME
191 + ".DefaultMouseHandler(): origin="
192 + origin
193 + ", e.x="
194 + e.x
195 + ", e.y="
196 + e.y);
197 ImageData imagedata=getImageData();
198 int x = e.x + origin.x - bounds.width / 2;
199 int y = e.y + origin.y - bounds.height / 2;
200 if (x + bounds.width > imagedata.width)
201 x = imagedata.width - bounds.width;
202 if (y + bounds.height > imagedata.height)
203 y = imagedata.height - bounds.height;
204 if (x < 0)
205 x = 0;
206 if (y < 0)
207 y = 0;
208 getCanvas().scrollTo(x, y);
209 }
210 }
211
212 ////////////////////////////////////////////////////////////////////////
213
214 }