View | Details | Raw Unified | Return to bug 155323 | Differences between
and this patch

Collapse All | Expand All

(-)plugin.properties (+1 lines)
Lines 23-28 Link Here
23
KeysPreference.Description = Preferences related to keys, accelerator key bindings, key configurations and commands.
23
KeysPreference.Description = Preferences related to keys, accelerator key bindings, key configurations and commands.
24
24
25
Editors.WelcomeEditor = Welcome
25
Editors.WelcomeEditor = Welcome
26
Editors.ImageViewerEditor = Image Viewer
26
ImportExportWizards.Category.Basic = General
27
ImportExportWizards.Category.Basic = General
27
ExportWizards.FileSystem = File System
28
ExportWizards.FileSystem = File System
28
ExportWizards.Preferences = Preferences
29
ExportWizards.Preferences = Preferences
(-)plugin.xml (+7 lines)
Lines 187-192 Link Here
187
            contributorClass="org.eclipse.ui.internal.ide.dialogs.WelcomeEditorActionContributor"
187
            contributorClass="org.eclipse.ui.internal.ide.dialogs.WelcomeEditorActionContributor"
188
            id="org.eclipse.ui.internal.ide.dialogs.WelcomeEditor">
188
            id="org.eclipse.ui.internal.ide.dialogs.WelcomeEditor">
189
      </editor>
189
      </editor>
190
      <editor
191
            id="org.eclipse.ui.internal.ide.ImageViewerEditor"
192
            class="org.eclipse.ui.internal.imageviewer.ImageViewerEditor"
193
            icon="$nl$/icons/full/obj16/image_editor.gif"
194
            default="true"
195
            name="%Editors.ImageViewerEditor"
196
            extensions="gif,jpg,jpeg,png,ico,bmp"/>
190
   </extension>
197
   </extension>
191
   <extension
198
   <extension
192
         point="org.eclipse.ui.elementFactories">
199
         point="org.eclipse.ui.elementFactories">
(-)src/org/eclipse/ui/internal/imageviewer/ImageCanvas.java (+327 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Eugene Kuleshov - initial API and implementation (bug 155323)
10
 *******************************************************************************/
11
12
package org.eclipse.ui.internal.imageviewer;
13
14
import org.eclipse.swt.SWT;
15
import org.eclipse.swt.events.ControlAdapter;
16
import org.eclipse.swt.events.ControlEvent;
17
import org.eclipse.swt.events.MouseAdapter;
18
import org.eclipse.swt.events.MouseEvent;
19
import org.eclipse.swt.events.MouseMoveListener;
20
import org.eclipse.swt.events.PaintEvent;
21
import org.eclipse.swt.events.PaintListener;
22
import org.eclipse.swt.events.SelectionAdapter;
23
import org.eclipse.swt.events.SelectionEvent;
24
import org.eclipse.swt.graphics.Color;
25
import org.eclipse.swt.graphics.Cursor;
26
import org.eclipse.swt.graphics.GC;
27
import org.eclipse.swt.graphics.Image;
28
import org.eclipse.swt.graphics.ImageData;
29
import org.eclipse.swt.graphics.PaletteData;
30
import org.eclipse.swt.widgets.Canvas;
31
import org.eclipse.swt.widgets.Composite;
32
import org.eclipse.swt.widgets.ScrollBar;
33
34
/**
35
 * TODO
36
 * 
37
 * @since 3.3
38
 */
39
public class ImageCanvas extends Canvas {
40
41
  private Image image;
42
43
  private int clientw;
44
  private int clienth;
45
  private int imagex;
46
  private int imagey;
47
  private int imagew;
48
  private int imageh;
49
  private int scrolly;
50
  private int scrollx;
51
  private int mousex;
52
  private int mousey;
53
54
  private Color backgroundColor;
55
56
  private boolean listenForMouseMovement = false;
57
  private Cursor cursorHand;
58
  private Cursor cursorArrow;
59
60
  private double zoomScale = 1;
61
62
  /**
63
   * TODO
64
   * 
65
   * @param parent
66
   * @param style
67
   */
68
  public ImageCanvas(Composite parent, int style) {
69
    super(parent, style | SWT.NO_BACKGROUND | SWT.V_SCROLL | SWT.H_SCROLL);
70
71
    backgroundColor = getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
72
    cursorHand = new Cursor(getDisplay(), SWT.CURSOR_HAND);
73
    cursorArrow = new Cursor(getDisplay(), SWT.CURSOR_ARROW);
74
75
    this.addControlListener(new ControlAdapter() {
76
      public void controlResized(ControlEvent e) {
77
        updateCanvas();
78
      }
79
    });
80
81
    this.addMouseListener(new MouseAdapter() {
82
      public void mouseDown(MouseEvent e) {
83
        listenForMouseMovement = true;
84
        mousex = e.x;
85
        mousey = e.y;
86
        setCursor(cursorHand);
87
      }
88
89
      public void mouseUp(MouseEvent e) {
90
        listenForMouseMovement = false;
91
        setCursor(cursorArrow);
92
      }
93
    });
94
95
    this.addMouseMoveListener(new MouseMoveListener() {
96
      public void mouseMove(MouseEvent e) {
97
        if (listenForMouseMovement) {
98
          followMouse(e);
99
        }
100
      }
101
    });
102
103
    this.addPaintListener(new PaintListener() {
104
      public void paintControl(PaintEvent event) {
105
        paint(event.gc);
106
      }
107
    });
108
109
    getHorizontalBar().setEnabled(true);
110
    getHorizontalBar().addSelectionListener(new SelectionAdapter() {
111
      public void widgetSelected(SelectionEvent event) {
112
        updateHorizontalScroll((ScrollBar) event.widget);
113
      }
114
    });
115
116
    getVerticalBar().setEnabled(true);
117
    getVerticalBar().addSelectionListener(new SelectionAdapter() {
118
      public void widgetSelected(SelectionEvent event) {
119
        updateVerticalScroll((ScrollBar) event.widget);
120
      }
121
    });
122
  }
123
124
  /**
125
   * TODO
126
   * 
127
   * @param image
128
   */
129
  public void setImage(Image image) {
130
    if (this.image != null) {
131
      image.dispose();
132
    }
133
134
    this.image = image;
135
    updateCanvas();
136
  }
137
138
  /**
139
   * TODO
140
   */
141
  public void zoomFit() {
142
    zoomScale = 0;
143
  }
144
145
  /**
146
   * TODO
147
   */
148
  public void zoomIn() {
149
    zoomScale *= 2;
150
  }
151
152
  /**
153
   * TODO
154
   */
155
  public void zoomOut() {
156
    zoomScale /= 2;
157
  }
158
159
  /**
160
   * TODO
161
   */
162
  public void zoomOriginal() {
163
    zoomScale = 1;
164
  }
165
166
  public void dispose() {
167
    super.dispose();
168
    if (image != null && !image.isDisposed()) {
169
      image.dispose();
170
    }
171
  }
172
173
  /**
174
   * TODO
175
   */
176
  public void rotate() {
177
    ImageData originalData = image.getImageData();
178
    PaletteData originalPalette = originalData.palette;
179
    ImageData tmpData;
180
    PaletteData tmpPalette;
181
182
    if (originalPalette.isDirect)
183
      tmpPalette = new PaletteData(originalPalette.redMask,
184
          originalPalette.greenMask, originalPalette.blueMask);
185
    else
186
      tmpPalette = new PaletteData(originalPalette.getRGBs());
187
188
    tmpData = new ImageData(originalData.height, originalData.width,
189
        originalData.depth, tmpPalette);
190
191
    tmpData.transparentPixel = originalData.transparentPixel;
192
193
    for (int i = 0; i < originalData.width; i++) {
194
      for (int k = 0; k < originalData.height; k++) {
195
        tmpData.setPixel(k, originalData.width - 1 - i, originalData.getPixel(
196
            i, k));
197
      }
198
    }
199
200
    if (image != null) {
201
      image.dispose();
202
    }
203
    image = new Image(getDisplay(), tmpData);
204
    updateCanvas();
205
  }
206
207
  private void updateCanvas() {
208
    clientw = getClientArea().width;
209
    clienth = getClientArea().height;
210
    if (clientw < 1) {
211
      clientw = 1;
212
    }
213
    if (clienth < 1) {
214
      clienth = 1;
215
    }
216
217
    if (image == null) {
218
      return;
219
    }
220
221
    imageh = image.getBounds().height;
222
    imagew = image.getBounds().width;
223
224
    updateScrollVisibility();
225
    getVerticalBar().setSelection(0);
226
    getHorizontalBar().setSelection(0);
227
    imagex = clientw / 2 - imagew / 2;
228
    imagey = clienth / 2 - imageh / 2;
229
230
    if (imagex < 0) {
231
      imagex = 0;
232
    }
233
    if (imagey < 0) {
234
      imagey = 0;
235
    }
236
237
    scrollx = getHorizontalBar().getSelection();
238
    scrolly = getVerticalBar().getSelection();
239
240
    ScrollBar vertical = getVerticalBar();
241
    vertical.setMaximum(imageh);
242
    vertical.setThumb(Math.min(clienth, imageh));
243
    vertical.setIncrement(40);
244
    vertical.setPageIncrement(clienth);
245
246
    ScrollBar horizontal = getHorizontalBar();
247
    horizontal.setMaximum(imagew);
248
    horizontal.setThumb(Math.min(clientw, imagew));
249
    horizontal.setIncrement(40);
250
    horizontal.setPageIncrement(clientw);
251
252
    redraw();
253
  }
254
255
  private void followMouse(MouseEvent e) {
256
    if (clientw < imagew) {
257
      int mouseDiffX = (mousex - e.x);
258
      mousex = e.x;
259
      imagex -= mouseDiffX;
260
      getHorizontalBar().setSelection(
261
          getHorizontalBar().getSelection() + mouseDiffX);
262
      int minx = clientw - imagew;
263
      int maxx = 0;
264
      if (imagex < minx) {
265
        imagex = minx;
266
      }
267
      if (imagex > maxx) {
268
        imagex = maxx;
269
      }
270
      scrollx = getHorizontalBar().getSelection();
271
    }
272
    if (clienth < imageh) {
273
      int mouseDiffY = (mousey - e.y);
274
      mousey = e.y;
275
      imagey -= mouseDiffY;
276
      getVerticalBar().setSelection(
277
          getVerticalBar().getSelection() + mouseDiffY);
278
      int miny = clienth - imageh;
279
      int maxy = 0;
280
      if (imagey < miny) {
281
        imagey = miny;
282
      }
283
      if (imagey > maxy) {
284
        imagey = maxy;
285
      }
286
287
      scrolly = getVerticalBar().getSelection();
288
    }
289
290
    redraw();
291
  }
292
293
  void paint(GC gc) {
294
    Image backImage = new Image(getDisplay(), clientw, clienth);
295
296
    GC backGC = new GC(backImage);
297
    backGC.setBackground(backgroundColor);
298
    backGC.setClipping(getClientArea());
299
    backGC.fillRectangle(getClientArea());
300
301
    if (image != null) {
302
      backGC.drawImage(image, imagex, imagey);
303
    }
304
305
    gc.drawImage(backImage, 0, 0);
306
    backGC.dispose();
307
    backImage.dispose();
308
  }
309
310
  private void updateScrollVisibility() {
311
    getHorizontalBar().setVisible(clientw < imagew);
312
    getVerticalBar().setVisible(clienth < imageh);
313
  }
314
315
  private void updateVerticalScroll(ScrollBar bar) {
316
    imagey -= bar.getSelection() - scrolly;
317
    scrolly = bar.getSelection();
318
    redraw();
319
  }
320
321
  private void updateHorizontalScroll(ScrollBar bar) {
322
    imagex -= bar.getSelection() - scrollx;
323
    scrollx = bar.getSelection();
324
    redraw();
325
  }
326
327
}
(-)src/org/eclipse/ui/internal/imageviewer/ImageViewerEditor.java (+142 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2007 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     Eugene Kuleshov - initial API and implementation (bug 155323)
10
 *******************************************************************************/
11
12
package org.eclipse.ui.internal.imageviewer;
13
14
import java.text.MessageFormat;
15
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.Status;
20
import org.eclipse.core.runtime.jobs.Job;
21
import org.eclipse.jface.action.IStatusLineManager;
22
import org.eclipse.swt.SWT;
23
import org.eclipse.swt.events.DisposeEvent;
24
import org.eclipse.swt.events.DisposeListener;
25
import org.eclipse.swt.events.FocusEvent;
26
import org.eclipse.swt.events.FocusListener;
27
import org.eclipse.swt.graphics.Image;
28
import org.eclipse.swt.graphics.ImageData;
29
import org.eclipse.swt.layout.FillLayout;
30
import org.eclipse.swt.widgets.Composite;
31
import org.eclipse.ui.IEditorInput;
32
import org.eclipse.ui.IEditorSite;
33
import org.eclipse.ui.IStorageEditorInput;
34
import org.eclipse.ui.part.EditorPart;
35
36
/**
37
 * TODO
38
 * 
39
 * @since 3.3
40
 */
41
public class ImageViewerEditor extends EditorPart {
42
43
  private ImageCanvas imageCanvas;
44
  private Image image;
45
46
  public void createPartControl(final Composite parent) {
47
    final IEditorInput editorInput = getEditorInput();
48
    setPartName(editorInput.getName());
49
50
    new Job("Loading image") { //$NON-NLS-1$
51
      protected IStatus run(IProgressMonitor monitor) {
52
        if (editorInput instanceof IStorageEditorInput) {
53
          IStorageEditorInput storageEditorInput = (IStorageEditorInput) editorInput;
54
          try {
55
            image = new Image(parent.getDisplay(), storageEditorInput
56
                .getStorage().getContents());
57
            parent.getDisplay().asyncExec(new Runnable() {
58
              public void run() {
59
                imageCanvas.setImage(image);
60
                updateStatus();
61
              }
62
            });
63
          } catch (CoreException ex) {
64
            // show the error
65
          }
66
        }
67
        return Status.OK_STATUS;
68
      }
69
    }.schedule();
70
71
    Composite composite = new Composite(parent, SWT.NONE);
72
    composite.setLayout(new FillLayout());
73
74
    composite.addFocusListener(new FocusListener() {
75
      public void focusGained(FocusEvent e) {
76
        updateStatus();
77
      }
78
79
      public void focusLost(FocusEvent e) {
80
        IStatusLineManager statusLineManager = getEditorSite().getActionBars()
81
            .getStatusLineManager();
82
        statusLineManager.setMessage(""); //$NON-NLS-1$
83
      }
84
    });
85
86
    this.imageCanvas = new ImageCanvas(composite, SWT.NONE);
87
88
    parent.addDisposeListener(new DisposeListener() {
89
      public void widgetDisposed(DisposeEvent e) {
90
        if (imageCanvas != null && !imageCanvas.isDisposed()) {
91
          imageCanvas.dispose();
92
        }
93
        if (image != null && !image.isDisposed()) {
94
          image.dispose();
95
        }
96
      }
97
    });
98
  }
99
100
  public void setPartName(String partName) {
101
    super.setPartName(partName);
102
  }
103
104
  public void init(IEditorSite site, IEditorInput input) {
105
    setSite(site);
106
    setInput(input);
107
  }
108
109
  public void setFocus() {
110
    if (imageCanvas != null) {
111
      imageCanvas.setFocus();
112
    }
113
    updateStatus();
114
  }
115
116
  public void doSave(IProgressMonitor monitor) {
117
  }
118
119
  public void doSaveAs() {
120
  }
121
122
  public boolean isDirty() {
123
    return false;
124
  }
125
126
  public boolean isSaveAsAllowed() {
127
    return false;
128
  }
129
130
  private void updateStatus() {
131
    if (image != null) {
132
      ImageData data = image.getImageData();
133
      MessageFormat mf = new MessageFormat("{0} | size: {1} x {2} | depth: {3}"); //$NON-NLS-1$
134
      String message = mf.format(new Object[] {getPartName(), new Integer(data.width), new Integer(data.height), new Integer(data.depth)});
135
136
      IStatusLineManager statusLineManager = getEditorSite().getActionBars()
137
          .getStatusLineManager();
138
      statusLineManager.setMessage(message);
139
    }
140
  }
141
142
}

Return to bug 155323