KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dnd > DragBorder


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

11 package org.eclipse.ui.internal.dnd;
12
13 import org.eclipse.jface.util.Geometry;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.PaintEvent;
16 import org.eclipse.swt.events.PaintListener;
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.graphics.RGB;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.widgets.Canvas;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.ui.themes.ColorUtil;
25
26 /**
27  * Utility class that wraps a given control with a black 'border'. Moving the
28  * border control will cause the given control to move to stay within its bounds.
29  *
30  * @since 3.2
31  *
32  */

33 public class DragBorder {
34     // Controls
35
private Composite clientControl = null;
36     private Control dragControl = null;
37     private Canvas border = null;
38
39     // Colors
40
private Color baseColor;
41     private Color hilightColor;
42     private boolean isHighlight;
43
44     /**
45      * Construct a new DragBorder.
46      *
47      * @param client The client window that the border must stay within
48      * @param toDrag The control to be placed 'inside' the border
49      */

50     public DragBorder(Composite client, Control toDrag, boolean provideFrame) {
51         clientControl = client;
52         dragControl = toDrag;
53         Point dragSize = toDrag.getSize();
54         
55         // Create a control large enough to 'contain' the dragged control
56
border = new Canvas(dragControl.getParent(), SWT.NONE);
57         border.setSize(dragSize.x+2, dragSize.y+2);
58         
59         // Use the SWT 'title' colors since they should always have a proper contrast
60
// and are 'related' (i.e. should look good together)
61
baseColor = border.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
62         RGB background = border.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB();
63         RGB blended = ColorUtil.blend(baseColor.getRGB(), background);
64         hilightColor = new Color(border.getDisplay(), blended);
65         
66         // Ensure the border is visible and the control is 'above' it...
67
border.moveAbove(null);
68         dragControl.moveAbove(null);
69         
70         if (provideFrame) {
71             border.addPaintListener(new PaintListener() {
72                 public void paintControl(PaintEvent e) {
73                     if (isHighlight) {
74                         e.gc.setForeground(hilightColor);
75                     }
76                     else {
77                         e.gc.setForeground(baseColor);
78                     }
79                     
80                     // Draw a rectangle as our 'border'
81
Rectangle bb = border.getBounds();
82                     e.gc.drawRectangle(0,0,bb.width-1, bb.height-1);
83                 }
84             });
85         }
86     }
87     
88     
89     /**
90      * Move the border (and its 'contained' control to a new position. The new
91      * position will be adjusted to lie entirely within the client area of the
92      * <code>clientControl</code>.
93      *
94      * @param newPos The new position for the border
95      * @param alignment The location of the cursor relative to the border being dragged.
96      * Current implementation only recognizes SWT.TOP & SWT.BOTTOM (which implies SWT.LEFT)
97      * and SWT.CENTER (which centers teh dragged border on the cursor.
98      */

99     public void setLocation(Point newPos, int alignment) {
100         // Move the border but ensure that it is still inside the Client area
101
if (alignment == SWT.CENTER) {
102             Point size = border.getSize();
103             border.setLocation(newPos.x - (size.x/2), newPos.y - (size.y/2));
104         }
105         else if (alignment == SWT.TOP) {
106             border.setLocation(newPos.x, newPos.y);
107         } else {
108             border.setLocation(newPos.x, newPos.y - border.getSize().y);
109         }
110         
111         // Force the control to remain inside the shell
112
Rectangle bb = border.getBounds();
113         Rectangle cr = clientControl.getClientArea();
114         Geometry.moveInside(bb,cr);
115         
116         // Ensure that the controls are the 'topmost' controls
117
border.moveAbove(null);
118         dragControl.moveAbove(null);
119         
120         // OK, now move the drag control and the border to their new locations
121
dragControl.setLocation(bb.x+1, bb.y+1);
122         border.setBounds(bb);
123     }
124
125     /**
126      * Sets the hilight 'mode' for the control.
127      * @param highlight true if the border should be drawn as 'hilighted'
128      */

129     public void setHighlight(boolean highlight) {
130         isHighlight = highlight;
131         border.redraw();
132     }
133
134     /**
135      * Dispose the controls owned by the border.
136      */

137     public void dispose() {
138         hilightColor.dispose();
139         border.dispose();
140     }
141
142
143     /**
144      * @return The bounds of the border's control.
145      */

146     public Rectangle getBounds() {
147         return border.getBounds();
148     }
149 }
150
Popular Tags