KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > DragHandle


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import org.eclipse.jface.resource.ImageDescriptor;
14 import org.eclipse.jface.resource.JFaceResources;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.PaintEvent;
17 import org.eclipse.swt.events.PaintListener;
18 import org.eclipse.swt.graphics.Cursor;
19 import org.eclipse.swt.graphics.GC;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.plugin.AbstractUIPlugin;
25
26 public class DragHandle extends Composite implements PaintListener {
27     
28     Cursor dragCursor;
29     Image handleImage;
30     ImageDescriptor descriptor;
31     private boolean isHorizontal;
32
33     private static int margin = 2;
34     
35 public DragHandle(Composite parent) {
36     super(parent, SWT.NONE);
37     
38     dragCursor = new Cursor(parent.getDisplay(),
39             SWT.CURSOR_SIZEALL);
40     
41     addPaintListener(this);
42     
43     descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(WorkbenchPlugin.PI_WORKBENCH, "icons/misc/handle.gif"); //$NON-NLS-1$
44

45     handleImage = new Image(parent.getDisplay(), 4, 4);
46     GC context = new GC(handleImage);
47     context.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
48     context.drawPoint(0,0);
49     context.drawPoint(2,0);
50     context.drawPoint(3,0);
51     context.drawPoint(3,1);
52     context.drawPoint(0,2);
53     context.drawPoint(3,2);
54     context.drawPoint(0,3);
55     context.drawPoint(1,3);
56     context.drawPoint(2,3);
57     context.drawPoint(3,3);
58     
59     context.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
60     context.drawPoint(1,0);
61     context.drawPoint(0,1);
62     
63     context.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
64     context.drawPoint(1,1);
65     
66     context.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
67     context.drawPoint(1,2);
68     context.drawPoint(2,1);
69     context.drawPoint(2,2);
70     
71     context.dispose();
72     
73     setCursor(dragCursor);
74 }
75
76 public void paintControl(PaintEvent e) {
77     Point size = getSize();
78     
79     if (handleImage != null) {
80         Rectangle ibounds = handleImage.getBounds();
81         
82         
83         int x = ((size.x - 2 * margin) % ibounds.width) / 2 + margin;
84         int y = ((size.y - 2 * margin) % ibounds.height) / 2 + margin;
85
86         for (;;) {
87             e.gc.drawImage(handleImage, x, y);
88             if (isHorizontal) {
89                 x += ibounds.width;
90                 if (x + ibounds.width > size.x - margin) {
91                     break;
92                 }
93             } else {
94                 y += ibounds.height;
95                 if (y + ibounds.height > size.y - margin) {
96                     break;
97                 }
98             }
99         }
100     }
101 }
102 public Point computeSize(int wHint, int hHint, boolean changed) {
103     Point result = new Point(wHint, hHint);
104     
105     Rectangle ibounds = handleImage.getBounds();
106     
107     if (wHint == SWT.DEFAULT) {
108         result.x = ibounds.width + 2 * margin;
109     }
110     
111     if (hHint == SWT.DEFAULT) {
112         result.y = ibounds.height + 2 * margin;
113     }
114     
115     return result;
116 }
117
118 public void setHorizontal(boolean isHorizontal) {
119     this.isHorizontal = isHorizontal;
120 }
121
122 public void dispose() {
123     if (isDisposed()) {
124         return;
125     }
126     super.dispose();
127     dragCursor.dispose();
128     handleImage.dispose();
129     JFaceResources.getResources().destroyImage(descriptor);
130 }
131 }
132
Popular Tags