KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > dialogs > ImageAndMessageArea


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.jface.dialogs;
13
14 import org.eclipse.jface.fieldassist.DecoratedField;
15 import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
16 import org.eclipse.jface.fieldassist.TextControlCreator;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.PaintEvent;
20 import org.eclipse.swt.events.PaintListener;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.swt.graphics.Rectangle;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Layout;
30 import org.eclipse.swt.widgets.Text;
31
32 /**
33  * Instances of this class provide a message area to display a message and an
34  * associated image.
35  * <p>
36  * This class is not intended to be extended by clients.
37  * </p>
38  *
39  * @since 3.2
40  * @deprecated As of 3.3, this class is no longer necessary.
41  *
42  */

43 public class ImageAndMessageArea extends Composite {
44
45     private int BORDER_MARGIN = IDialogConstants.HORIZONTAL_SPACING / 2;
46
47     private DecoratedField messageField;
48
49     private Composite container;
50
51     /**
52      * Constructs a new ImageAndMessageArea with an empty decorated field. Calls
53      * to <code>setText(String text)</code> and
54      * <code>setImage(Image image)</code> are required in order to fill the
55      * message area. Also, the instance will be invisible when initially
56      * created.
57      * <p>
58      * The style bit <code>SWT.WRAP</code> should be used if a larger message
59      * area is desired.
60      * </p>
61      *
62      * @param parent
63      * the parent composite
64      * @param style
65      * the SWT style bits. Using SWT.WRAP will create a larger
66      * message area.
67      */

68     public ImageAndMessageArea(Composite parent, int style) {
69         super(parent, style);
70         container = new Composite(this, style);
71         GridLayout glayout = new GridLayout();
72         glayout.numColumns = 2;
73         glayout.marginWidth = 0;
74         glayout.marginHeight = 0;
75         glayout.marginTop = BORDER_MARGIN;
76         glayout.marginBottom = BORDER_MARGIN;
77         container.setLayout(glayout);
78
79         messageField = new DecoratedField(container, SWT.READ_ONLY | style,
80                 new TextControlCreator());
81         setFont(JFaceResources.getDialogFont());
82
83         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
84         int lineHeight = ((Text) messageField.getControl()).getLineHeight();
85         if ((style & SWT.WRAP) > 0)
86             gd.heightHint = 2 * lineHeight;
87         else
88             gd.heightHint = lineHeight;
89
90         messageField.getLayoutControl().setLayoutData(gd);
91
92         addPaintListener(new PaintListener() {
93             /*
94              * (non-Javadoc)
95              *
96              * @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent)
97              */

98             public void paintControl(PaintEvent e) {
99                 onPaint(e);
100             }
101         });
102
103         // sets the layout and size to account for the BORDER_MARGIN between
104
// the border drawn around the container and the decorated field.
105
setLayout(new Layout() {
106             /*
107              * (non-Javadoc)
108              *
109              * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite,
110              * boolean)
111              */

112             public void layout(Composite parent, boolean changed) {
113                 Rectangle carea = getClientArea();
114                 container.setBounds(carea.x + BORDER_MARGIN, carea.y
115                         + BORDER_MARGIN, carea.width - (2 * BORDER_MARGIN),
116                         carea.height - (2 * BORDER_MARGIN));
117             }
118
119             /*
120              * (non-Javadoc)
121              *
122              * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite,
123              * int, int, boolean)
124              */

125             public Point computeSize(Composite parent, int wHint, int hHint,
126                     boolean changed) {
127                 Point size;
128                 size = container.computeSize(wHint, hHint, changed);
129
130                 // size set to account for the BORDER_MARGIN on
131
// all sides of the decorated field
132
size.x += 4;
133                 size.y += 4;
134                 return size;
135             }
136         });
137         setVisible(false);
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.eclipse.swt.widgets.Control#setBackground(org.eclipse.swt.graphics.Color)
144      */

145     public void setBackground(Color bg) {
146         super.setBackground(bg);
147         messageField.getLayoutControl().setBackground(bg);
148         messageField.getControl().setBackground(bg);
149         container.setBackground(bg);
150     }
151
152     /**
153      * Sets the text in the decorated field which will be displayed in the
154      * message area.
155      *
156      * @param text
157      * the text to be displayed in the message area
158      *
159      * @see org.eclipse.swt.widgets.Text#setText(String string)
160      */

161     public void setText(String JavaDoc text) {
162         ((Text) messageField.getControl()).setText(text);
163     }
164
165     /**
166      * Adds an image to decorated field to be shown in the message area.
167      *
168      * @param image
169      * desired image to be shown in the ImageAndMessageArea
170      */

171     public void setImage(Image image) {
172         FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
173         registry.registerFieldDecoration("messageImage", null, image); //$NON-NLS-1$
174
messageField.addFieldDecoration(registry
175                 .getFieldDecoration("messageImage"), //$NON-NLS-1$
176
SWT.LEFT | SWT.TOP, false);
177     }
178
179     /**
180      * Draws the message area composite with rounded corners.
181      */

182     private void onPaint(PaintEvent e) {
183         Rectangle carea = getClientArea();
184         e.gc.setForeground(getForeground());
185
186         // draws the polyline to be rounded in a 2 pixel squared area
187
e.gc.drawPolyline(new int[] { carea.x, carea.y + carea.height - 1,
188                 carea.x, carea.y + 2, carea.x + 2, carea.y,
189                 carea.x + carea.width - 3, carea.y, carea.x + carea.width - 1,
190                 carea.y + 2, carea.x + carea.width - 1,
191                 carea.y + carea.height - 1 });
192     }
193
194     /*
195      * (non-Javadoc)
196      *
197      * @see org.eclipse.swt.widgets.Control#setFont(org.eclipse.swt.graphics.Font)
198      */

199     public void setFont(Font font) {
200         super.setFont(font);
201         ((Text) messageField.getControl()).setFont(font);
202     }
203
204     /*
205      * (non-Javadoc)
206      *
207      * @see org.eclipse.swt.widgets.Control#setToolTipText(java.lang.String)
208      */

209     public void setToolTipText(String JavaDoc text) {
210         super.setToolTipText(text);
211         ((Text) messageField.getControl()).setToolTipText(text);
212     }
213 }
214
Popular Tags