1 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 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 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 98 public void paintControl(PaintEvent e) { 99 onPaint(e); 100 } 101 }); 102 103 setLayout(new Layout() { 106 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 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.x += 4; 133 size.y += 4; 134 return size; 135 } 136 }); 137 setVisible(false); 138 } 139 140 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 161 public void setText(String text) { 162 ((Text) messageField.getControl()).setText(text); 163 } 164 165 171 public void setImage(Image image) { 172 FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault(); 173 registry.registerFieldDecoration("messageImage", null, image); messageField.addFieldDecoration(registry 175 .getFieldDecoration("messageImage"), SWT.LEFT | SWT.TOP, false); 177 } 178 179 182 private void onPaint(PaintEvent e) { 183 Rectangle carea = getClientArea(); 184 e.gc.setForeground(getForeground()); 185 186 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 199 public void setFont(Font font) { 200 super.setFont(font); 201 ((Text) messageField.getControl()).setFont(font); 202 } 203 204 209 public void setToolTipText(String text) { 210 super.setToolTipText(text); 211 ((Text) messageField.getControl()).setToolTipText(text); 212 } 213 } 214 | Popular Tags |