1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets;
\r
14 import java.awt.Color;
\r
15 import java.awt.Font;
\r
16 import java.util.LinkedHashMap;
\r
17 import java.util.Map;
\r
19 import org.eclipse.jface.dialogs.Dialog;
\r
20 import org.eclipse.jface.layout.GridDataFactory;
\r
21 import org.eclipse.jface.layout.GridLayoutFactory;
\r
22 import org.eclipse.jface.resource.FontDescriptor;
\r
23 import org.eclipse.jface.resource.JFaceResources;
\r
24 import org.eclipse.jface.resource.LocalResourceManager;
\r
25 import org.eclipse.nebula.widgets.tablecombo.TableCombo;
\r
26 import org.eclipse.swt.SWT;
\r
27 import org.eclipse.swt.events.SelectionEvent;
\r
28 import org.eclipse.swt.events.SelectionListener;
\r
29 import org.eclipse.swt.graphics.FontData;
\r
30 import org.eclipse.swt.graphics.Image;
\r
31 import org.eclipse.swt.graphics.RGB;
\r
32 import org.eclipse.swt.graphics.Rectangle;
\r
33 import org.eclipse.swt.widgets.Composite;
\r
34 import org.eclipse.swt.widgets.Control;
\r
35 import org.eclipse.swt.widgets.Display;
\r
36 import org.eclipse.swt.widgets.Group;
\r
37 import org.eclipse.swt.widgets.Label;
\r
38 import org.eclipse.swt.widgets.Shell;
\r
39 import org.eclipse.swt.widgets.Table;
\r
40 import org.eclipse.swt.widgets.TableItem;
\r
41 import org.simantics.utils.ui.gfx.ColorImageDescriptor;
\r
44 * Custom dialog for selecting font and font color. Similar to SWT FontDialog.
\r
45 * @author Teemu Lempinen
\r
48 public class CustomFontDialog extends Dialog {
\r
50 private Map<String, Integer> systemColors = createColorMap();
\r
52 private FontData resultSWTFontData;
\r
53 private Font awtFont;
\r
54 private Font resultAWTFont;
\r
55 private Color color;
\r
56 private Color resultAWTColor;
\r
58 private FontSelectionComposite vpc;
\r
59 private TableCombo tc;
\r
61 private String example = "Example";
\r
62 private Label sample;
\r
63 private Group sampleGroup;
\r
66 private LocalResourceManager resourceManager;
\r
69 // Default color map
\r
70 protected static Map<String, Integer> createColorMap() {
\r
71 LinkedHashMap<String, Integer> colors = new LinkedHashMap<String, Integer>();
\r
72 colors.put("Black", SWT.COLOR_BLACK);
\r
73 colors.put("White", SWT.COLOR_WHITE);
\r
74 colors.put("Blue", SWT.COLOR_BLUE);
\r
75 colors.put("Dark Blue", SWT.COLOR_DARK_BLUE);
\r
76 colors.put("Red", SWT.COLOR_RED);
\r
77 colors.put("Dark Red", SWT.COLOR_DARK_RED);
\r
78 colors.put("Yellow", SWT.COLOR_YELLOW);
\r
79 colors.put("Dark Yellow", SWT.COLOR_DARK_YELLOW);
\r
80 colors.put("Gray", SWT.COLOR_GRAY);
\r
81 colors.put("Dark Gray", SWT.COLOR_DARK_GRAY);
\r
82 colors.put("Green", SWT.COLOR_GREEN);
\r
83 colors.put("Dark Green", SWT.COLOR_DARK_GREEN);
\r
84 colors.put("Cyan", SWT.COLOR_CYAN);
\r
85 colors.put("Dark Cyan", SWT.COLOR_DARK_CYAN);
\r
86 colors.put("Magenta", SWT.COLOR_MAGENTA);
\r
87 colors.put("Dark Magenta", SWT.COLOR_DARK_MAGENTA);
\r
92 * Creates a font dialog with sample text
\r
93 * @param parentShell
\r
94 * @param example Sample text in the dialog. Null example => "Example"
\r
96 public CustomFontDialog(Shell parentShell, String example) {
\r
99 this.example = example;
\r
103 * Sets the initial font for this dialog
\r
104 * @param awtFont Current AWT font
\r
106 public void setAWTFont(java.awt.Font awtFont) {
\r
107 this.awtFont = awtFont;
\r
108 this.resultAWTFont = awtFont;
\r
112 * Get selected font as AWT font
\r
115 public java.awt.Font getAWTFont() {
\r
116 return resultAWTFont;
\r
120 * Get selected font as SWT font dta
\r
123 public FontData getSWTFontData() {
\r
124 return resultSWTFontData;
\r
128 * Set initial color for this dialog
\r
129 * @param color AWT color
\r
131 public void setColor(Color color) {
\r
132 this.color = color;
\r
133 this.resultAWTColor = color;
\r
137 * Get selected color as AWT color
\r
140 public Color getAWTColor() {
\r
141 return resultAWTColor;
\r
145 * Get selected color as RGB
\r
148 public RGB getRGB() {
\r
154 * Creates font choosing area
\r
155 * @param parent Parent composite
\r
157 protected void createFontChooser(Composite parent) {
\r
158 vpc = new FontSelectionComposite(parent, SWT.NONE);
\r
159 vpc.setFont(awtFont, false);
\r
160 GridDataFactory.fillDefaults().span(2, 1).applyTo(vpc);
\r
162 vpc.addFontModifiedListener(new FontModifyListener() {
\r
165 public void swtFontDataChanged(FontData fd) {
\r
166 sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(fd)));
\r
167 sampleGroup.layout();
\r
171 public void awtFontChanged(Font font) {
\r
178 * Creates a TableCombo for choosing a color for a font
\r
179 * @param parent Parent composite
\r
181 protected void createColorChooser(Composite parent) {
\r
182 // Create a ResourceManager to dispose images when the widget is disposed.
\r
183 this.resourceManager = new LocalResourceManager(JFaceResources.getResources(), parent);
\r
185 Composite colorComposite = new Composite(parent, SWT.NONE);
\r
186 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(colorComposite);
\r
187 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(colorComposite);
\r
189 Label label = new Label(colorComposite, SWT.NONE);
\r
190 GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL).applyTo(label);
\r
191 label.setText("Color: ");
\r
193 tc = new TableCombo(colorComposite, SWT.BORDER | SWT.READ_ONLY);
\r
194 GridDataFactory.fillDefaults().hint(170, SWT.DEFAULT).applyTo(tc);
\r
196 tc.defineColumns(2);
\r
197 tc.setDisplayColumnIndex(1);
\r
198 tc.setToolTipText("this is tooltip");
\r
200 createColorItems(tc.getTable());
\r
202 if(this.color != null) {
\r
203 for(int i = 0; i < tc.getTable().getItemCount(); i++) {
\r
204 TableItem ti = tc.getTable().getItem(i);
\r
205 RGB rgb = (RGB) ti.getData();
\r
206 if(rgb.red == this.color.getRed() &&
\r
207 rgb.green == this.color.getGreen() &&
\r
208 rgb.blue == this.color.getBlue()) {
\r
209 tc.setText(ti.getText(1));
\r
210 tc.setForeground(resourceManager.createColor(rgb));
\r
217 tc.addSelectionListener(new SelectionListener() {
\r
220 public void widgetSelected(SelectionEvent e) {
\r
221 TableItem[] selection = tc.getTable().getSelection();
\r
222 if(selection.length == 1) {
\r
223 rgb = (RGB) selection[0].getData();
\r
224 org.eclipse.swt.graphics.Color swtColor = resourceManager.createColor(rgb);
\r
225 sample.setForeground(swtColor);
\r
226 tc.setForeground(swtColor);
\r
228 tc.getTextControl().setSelection(0);
\r
229 tc.getParent().forceFocus();
\r
233 public void widgetDefaultSelected(SelectionEvent e) {
\r
239 * Creates a sample text area
\r
240 * @param parent Parent composite
\r
242 protected void createSampleArea(Composite parent) {
\r
243 sampleGroup = new Group(parent, SWT.NONE);
\r
244 sampleGroup.setText("Sample");
\r
245 GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 70).applyTo(sampleGroup);
\r
246 GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(sampleGroup);
\r
248 sample = new Label(sampleGroup, SWT.NONE);
\r
249 sample.setText(example);
\r
250 if(awtFont != null) {
\r
251 sample.setFont(resourceManager.createFont(FontDescriptor.createFrom(toSwtFontData(awtFont))));
\r
254 sample.setForeground(resourceManager.createColor(rgb));
\r
256 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).applyTo(sample);
\r
260 protected Control createDialogArea(Composite parent)
\r
262 Composite composite = ( Composite )super.createDialogArea(parent);
\r
263 composite.getShell().setText("Choose Font");
\r
265 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(2).applyTo(composite);
\r
267 // Init SWT RGB, if AWT color has been set
\r
269 this.rgb = new RGB(color.getRed(), color.getGreen(), color.getBlue());
\r
271 // Font selection composite
\r
272 createFontChooser(composite);
\r
275 createColorChooser(composite);
\r
278 createSampleArea(composite);
\r
280 //Set the dialog position in the middle of the monitor
\r
281 setDialogLocationToMonitorCenter();
\r
287 protected void cancelPressed() {
\r
288 resultAWTFont = awtFont;
\r
289 resultAWTColor = color;
\r
290 if(resultAWTFont != null)
\r
291 resultSWTFontData = toSwtFontData(resultAWTFont);
\r
293 setReturnCode(CANCEL);
\r
298 protected void okPressed() {
\r
299 resultAWTFont = vpc.getAWTFont();
\r
300 resultSWTFontData = toSwtFontData(resultAWTFont);
\r
302 TableItem[] selection = tc.getTable().getSelection();
\r
303 if(selection.length == 1) {
\r
304 RGB rgb = (RGB) selection[0].getData();
\r
305 resultAWTColor = new Color(rgb.red, rgb.green, rgb.blue);
\r
313 * Sets the dialog location to the middle of the screen
\r
315 protected void setDialogLocationToMonitorCenter() {
\r
316 Rectangle monitorArea = getShell().getDisplay().getPrimaryMonitor().getBounds();
\r
317 Rectangle shellArea = getShell().getBounds();
\r
318 int x = monitorArea.x + (monitorArea.width - shellArea.width)/2;
\r
319 int y = monitorArea.y + (monitorArea.height - shellArea.height)/2;
\r
320 getShell().setLocation(x,y);
\r
324 * Builds SWT FontData from AWT font. Simple conversion.
\r
326 * @param font AWT font
\r
327 * @param height Height for the created data (or -1 if inherited directly from awt font, size matching not guaranteed)
\r
330 protected static FontData toSwtFontData(Font font) {
\r
331 FontData fontData = new FontData();
\r
332 fontData.setName(font.getFontName());
\r
333 fontData.setStyle(font.getStyle());
\r
334 fontData.setHeight(font.getSize());
\r
339 * Creates color items for color combo
\r
342 protected void createColorItems(Table table) {
\r
347 Display display = Display.getCurrent();
\r
348 for(String text : systemColors.keySet()) {
\r
349 code = systemColors.get(text);
\r
350 color = display.getSystemColor(code).getRGB();
\r
351 image = resourceManager.createImage(new ColorImageDescriptor(color.red, color.green, color.blue, 25, 15, false));
\r
353 ti = new TableItem(table, SWT.NONE);
\r
354 ti.setImage(0, image);
\r
355 ti.setText(1, text);
\r
356 ti.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
\r