001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.jface.resource.ImageDescriptor;
013: import org.eclipse.jface.resource.JFaceResources;
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.events.PaintEvent;
016: import org.eclipse.swt.events.PaintListener;
017: import org.eclipse.swt.graphics.Cursor;
018: import org.eclipse.swt.graphics.GC;
019: import org.eclipse.swt.graphics.Image;
020: import org.eclipse.swt.graphics.Point;
021: import org.eclipse.swt.graphics.Rectangle;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.ui.plugin.AbstractUIPlugin;
024:
025: public class DragHandle extends Composite implements PaintListener {
026:
027: Cursor dragCursor;
028: Image handleImage;
029: ImageDescriptor descriptor;
030: private boolean isHorizontal;
031:
032: private static int margin = 2;
033:
034: public DragHandle(Composite parent) {
035: super (parent, SWT.NONE);
036:
037: dragCursor = new Cursor(parent.getDisplay(), SWT.CURSOR_SIZEALL);
038:
039: addPaintListener(this );
040:
041: descriptor = AbstractUIPlugin.imageDescriptorFromPlugin(
042: WorkbenchPlugin.PI_WORKBENCH, "icons/misc/handle.gif"); //$NON-NLS-1$
043:
044: handleImage = new Image(parent.getDisplay(), 4, 4);
045: GC context = new GC(handleImage);
046: context.setForeground(parent.getDisplay().getSystemColor(
047: SWT.COLOR_WIDGET_BACKGROUND));
048: context.drawPoint(0, 0);
049: context.drawPoint(2, 0);
050: context.drawPoint(3, 0);
051: context.drawPoint(3, 1);
052: context.drawPoint(0, 2);
053: context.drawPoint(3, 2);
054: context.drawPoint(0, 3);
055: context.drawPoint(1, 3);
056: context.drawPoint(2, 3);
057: context.drawPoint(3, 3);
058:
059: context.setForeground(parent.getDisplay().getSystemColor(
060: SWT.COLOR_WIDGET_NORMAL_SHADOW));
061: context.drawPoint(1, 0);
062: context.drawPoint(0, 1);
063:
064: context.setForeground(parent.getDisplay().getSystemColor(
065: SWT.COLOR_WIDGET_DARK_SHADOW));
066: context.drawPoint(1, 1);
067:
068: context.setForeground(parent.getDisplay().getSystemColor(
069: SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
070: context.drawPoint(1, 2);
071: context.drawPoint(2, 1);
072: context.drawPoint(2, 2);
073:
074: context.dispose();
075:
076: setCursor(dragCursor);
077: }
078:
079: public void paintControl(PaintEvent e) {
080: Point size = getSize();
081:
082: if (handleImage != null) {
083: Rectangle ibounds = handleImage.getBounds();
084:
085: int x = ((size.x - 2 * margin) % ibounds.width) / 2
086: + margin;
087: int y = ((size.y - 2 * margin) % ibounds.height) / 2
088: + margin;
089:
090: for (;;) {
091: e.gc.drawImage(handleImage, x, y);
092: if (isHorizontal) {
093: x += ibounds.width;
094: if (x + ibounds.width > size.x - margin) {
095: break;
096: }
097: } else {
098: y += ibounds.height;
099: if (y + ibounds.height > size.y - margin) {
100: break;
101: }
102: }
103: }
104: }
105: }
106:
107: public Point computeSize(int wHint, int hHint, boolean changed) {
108: Point result = new Point(wHint, hHint);
109:
110: Rectangle ibounds = handleImage.getBounds();
111:
112: if (wHint == SWT.DEFAULT) {
113: result.x = ibounds.width + 2 * margin;
114: }
115:
116: if (hHint == SWT.DEFAULT) {
117: result.y = ibounds.height + 2 * margin;
118: }
119:
120: return result;
121: }
122:
123: public void setHorizontal(boolean isHorizontal) {
124: this .isHorizontal = isHorizontal;
125: }
126:
127: public void dispose() {
128: if (isDisposed()) {
129: return;
130: }
131: super.dispose();
132: dragCursor.dispose();
133: handleImage.dispose();
134: JFaceResources.getResources().destroyImage(descriptor);
135: }
136: }
|