KickJava   Java API By Example, From Geeks To Geeks.

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


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.dnd;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.graphics.RGB;
16 import org.eclipse.swt.graphics.Rectangle;
17 import org.eclipse.swt.widgets.Canvas;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.themes.ColorUtil;
20
21 /**
22  * This class provides 'insertion' feedback to the User. It can be used to draw a
23  * 'bracket' based on the trim area's rectangle.
24  *
25  * @since 3.2
26  */

27 public class InsertCaret {
28     // Constants
29
private static final int width = 6; // the handle's 'thickness'
30
private static final int pctInset = 10; // The percentage of the area left at each 'end'
31

32     // Control info
33
private Canvas caretControl;
34     private Canvas end1;
35     private Canvas end2;
36     
37     // Colors
38
private Color baseColor;
39     private Color hilightColor;
40     private boolean isHighlight;
41     
42     /**
43      * Creates an affordance to indicate that the given trim area is a valid location for the
44      * trim being dragged.
45      *
46      * @param windowComposite The window to create the affordance as a child of
47      * @param trimRect The rectangle to show the affordance for
48      * @param swtSide The 'side' that the rectangle is on
49      * @param threshold The amount to offfset the affordance by
50      */

51     public InsertCaret(Composite parent, Rectangle trimRect, int swtSide, int threshold) {
52         // Use the SWT 'title' colors since they should always have a proper contrast
53
// and are 'related' (i.e. should look good together)
54
baseColor = parent.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
55         RGB background = parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND).getRGB();
56         RGB blended = ColorUtil.blend(baseColor.getRGB(), background);
57         hilightColor = new Color(parent.getDisplay(), blended);
58
59         //Create the caret control
60
createControl(parent, trimRect, swtSide, threshold);
61     }
62
63     /**
64      * Creates a control to show the 'area valid' affordance. The current implementation creates a
65      * simple rect half the length of the rect, centered and offset by the 'threshold' value.
66      *
67      * @param parent The control to used as the parent of the affordance control
68      * @param trimRect The trim rectangle
69      * @param swtSide The SWT side that the trim is on
70      * @param threshold The offset value
71      */

72     private void createControl(Composite parent, Rectangle trimRect, int swtSide, int threshold) {
73         int hDelta = trimRect.width/pctInset;
74         int vDelta = trimRect.height/pctInset;
75         caretControl = new Canvas (parent.getShell(), SWT.BORDER);
76         
77         end1 = new Canvas (parent.getShell(), SWT.BORDER);
78         end1.setSize(width, width);
79         end2 = new Canvas (parent.getShell(), SWT.BORDER);
80         end2.setSize(width, width);
81         
82         Rectangle bb;
83         switch (swtSide) {
84         case SWT.TOP:
85             caretControl.setSize(trimRect.width-(2*hDelta), width);
86             caretControl.setLocation(trimRect.x + hDelta, trimRect.y + trimRect.height + threshold);
87             bb = caretControl.getBounds();
88             end1.setLocation(bb.x, bb.y-width);
89             end2.setLocation((bb.x+bb.width)-width, bb.y-width);
90             break;
91         case SWT.BOTTOM:
92             caretControl.setSize(trimRect.width-(2*hDelta), width);
93             caretControl.setLocation(trimRect.x + hDelta, trimRect.y - threshold);
94             bb = caretControl.getBounds();
95             end1.setLocation(bb.x, bb.y+width);
96             end2.setLocation((bb.x+bb.width)-width, bb.y+width);
97             break;
98         case SWT.LEFT:
99             caretControl.setSize(width, trimRect.height -(2*vDelta));
100             caretControl.setLocation(trimRect.x + trimRect.width + threshold,
101                                     trimRect.y + vDelta);
102             bb = caretControl.getBounds();
103             end1.setLocation(bb.x-bb.width, bb.y);
104             end2.setLocation(bb.x-bb.width, (bb.y+bb.height)-width);
105             break;
106         case SWT.RIGHT:
107             caretControl.setSize(width, trimRect.height -(2*vDelta));
108             caretControl.setLocation(trimRect.x - threshold,
109                                     trimRect.y + vDelta);
110             bb = caretControl.getBounds();
111             end1.setLocation(bb.x+bb.width, bb.y);
112             end2.setLocation(bb.x+bb.width, (bb.y+bb.height)-width);
113             break;
114         }
115         
116         // Initially create as not hilighted
117
setHighlight(false);
118         caretControl.moveAbove(null);
119         end1.moveAbove(null);
120         end2.moveAbove(null);
121     }
122
123     /**
124      * Sets the hilight 'mode' for the control.
125      * @param highlight true if the caret should be drawn as 'hilighted'
126      */

127     public void setHighlight(boolean highlight) {
128         isHighlight = highlight;
129
130         // if we're displaying as a 'bar' then set the control's background to the
131
// appropriate value
132
if (isHighlight) {
133             caretControl.setBackground(hilightColor);
134             end1.setBackground(hilightColor);
135             end2.setBackground(hilightColor);
136         }
137         else {
138             caretControl.setBackground(baseColor);
139             end1.setBackground(baseColor);
140             end2.setBackground(baseColor);
141         }
142     }
143
144     public void dispose() {
145         // Dispose the control's resources (we don't have to dispose the
146
// 'bacseColor' because it's a system color
147
hilightColor.dispose();
148         
149         caretControl.dispose();
150         end1.dispose();
151         end2.dispose();
152     }
153 }
154
Popular Tags