KickJava   Java API By Example, From Geeks To Geeks.

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


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  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.PaintEvent;
15 import org.eclipse.swt.events.PaintListener;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.GC;
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.swt.widgets.CoolBar;
25 import org.eclipse.swt.widgets.Layout;
26 import org.eclipse.swt.widgets.ToolBar;
27 import org.eclipse.ui.themes.ColorUtil;
28
29 /**
30  * Draws a styled frame around its contained controls.
31  * This class is intended to be used to wrap various
32  * trim elements that appear in the workbench.
33  *
34  * Currently this class expects a <b>single</b> child
35  * control.
36  *
37  * @since 3.3
38  *
39  */

40 public class TrimFrame {
41     private static int blend = 40;
42     
43     Canvas canvas = null;
44     
45     public TrimFrame(Composite parent) {
46         createControl(parent);
47     }
48
49     private void createControl(Composite parent) {
50         dispose();
51         canvas = new Canvas(parent, SWT.NONE);
52         canvas.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
53         
54         // paint the border
55
canvas.addPaintListener(new PaintListener() {
56             private void drawLine (GC gc, int x1, int y1, int x2, int y2, boolean flipXY) {
57                 if (flipXY) {
58                     int tmp = x1;
59                     x1 = y1;
60                     y1 = tmp;
61                     tmp = x2;
62                     x2 = y2;
63                     y2 = tmp;
64                 }
65                 
66                  gc.drawLine(x1, y1, x2, y2);
67             }
68             
69             public void paintControl(PaintEvent e) {
70                 Canvas canvas = (Canvas)e.widget;
71                 Control child = canvas.getChildren ()[0];
72                 
73                 // Are we horizontally or vertically aligned
74
boolean flipXY = false;
75                 if (child instanceof ToolBar && (((ToolBar)child).getStyle() & SWT.VERTICAL) != 0)
76                     flipXY = true;
77                 else if (child instanceof CoolBar && (((CoolBar)child).getStyle() & SWT.VERTICAL) != 0)
78                     flipXY = true;
79                 
80                 Rectangle bb = canvas.getBounds();
81                 int maxX = bb.width-1;
82                 int maxY = bb.height-1;
83                  
84                 if (flipXY) {
85                     int tmp = maxX;
86                     maxX = maxY;
87                     maxY = tmp;
88                 }
89                 
90                 Color white = e.gc.getDevice ().getSystemColor(SWT.COLOR_WHITE);
91                 Color shadow = e.gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
92                 RGB outerRGB = ColorUtil.blend(white.getRGB(), shadow.getRGB(), blend);
93                 Color outerColor = new Color(e.gc.getDevice(), outerRGB);
94                 
95                 // Draw the 'outer' bits
96
e.gc.setForeground(outerColor);
97                 
98                 // Top Line and curve
99
drawLine(e.gc, 1, 0, maxX-5, 0, flipXY);
100                 drawLine(e.gc, maxX-4, 1, maxX-3, 1, flipXY);
101                 drawLine(e.gc, maxX-2, 2, maxX-2, 2, flipXY);
102                 drawLine(e.gc, maxX-1, 3, maxX-1, 4, flipXY);
103                 
104                 // Bottom line and curve
105
drawLine(e.gc, 1, maxY, maxX-5, maxY, flipXY);
106                 drawLine( e.gc, maxX-4, maxY-1, maxX-3, maxY-1, flipXY);
107                 drawLine(e.gc, maxX-2, maxY-2, maxX-2, maxY-2, flipXY);
108                 drawLine(e.gc, maxX-1, maxY-3, maxX-1, maxY-4, flipXY);
109                 
110                 // Left & Right edges
111
drawLine(e.gc, 0, 1, 0, maxY-1, flipXY);
112                 drawLine(e.gc, maxX, 5, maxX, maxY-5, flipXY);
113                 
114                 // Dispose the color since we created it...
115
outerColor.dispose();
116                 
117                 // Draw the 'inner' curve
118
e.gc.setForeground (white);
119
120                 drawLine(e.gc, 1, 1, maxX-5, 1, flipXY);
121                 drawLine(e.gc, maxX-4, 2, maxX-3, 2, flipXY);
122                 drawLine(e.gc, maxX-3, 3, maxX-2, 3, flipXY);
123                 drawLine( e.gc, maxX-2, 4, maxX-2, 4, flipXY);
124                 
125                 drawLine(e.gc, 1, maxY-1, maxX-5, maxY-1, flipXY);
126                 drawLine(e.gc, maxX-4, maxY-2, maxX-3, maxY-2, flipXY);
127                 drawLine( e.gc, maxX-3, maxY-3, maxX-2, maxY-3, flipXY);
128                 drawLine(e.gc, maxX-2, maxY-4, maxX-2, maxY-4, flipXY);
129                 
130                 // Left and Right sides
131
drawLine(e.gc, 1, 1, 1, maxY-1, flipXY);
132                 drawLine(e.gc, maxX-1, 5, maxX-1, maxY-5, flipXY);
133             }
134         });
135         
136         // provide a layout that provides enough extra space to
137
// draw the border and to place the child conrol in the
138
// correct location
139
canvas.setLayout(new Layout() {
140
141             protected Point computeSize(Composite composite, int wHint,
142                     int hHint, boolean changed) {
143                 Control[] children = composite.getChildren();
144                 
145                 if (children.length == 0)
146                     return new Point(0,0);
147
148                 Point innerSize = children[0].computeSize(hHint, wHint, changed);
149                 innerSize.x += 4;
150                 innerSize.y += 4;
151                 
152                 Control child = children[0];
153                 if (child instanceof CoolBar && (((CoolBar)child).getStyle() & SWT.VERTICAL) != 0)
154                     innerSize.y += 3;
155                 else
156                     innerSize.x += 3;
157                 
158                 return innerSize;
159             }
160
161             protected void layout(Composite composite, boolean flushCache) {
162                 Control[] children = composite.getChildren();
163                 if (children.length == 0)
164                     return;
165
166                 children[0].setLocation(2, 2);
167             }
168         });
169     }
170
171     /**
172      * Dispose the frame
173      */

174     private void dispose() {
175         if (canvas != null && !canvas.isDisposed())
176             canvas.dispose();
177     }
178     
179     /**
180      * @return The border canvas
181      */

182     public Composite getComposite() {
183         return canvas;
184     }
185 }
186
187
Popular Tags