]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/jface/FontCellEditor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / jface / FontCellEditor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  *\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.utils.ui.jface;\r
17 \r
18 import org.eclipse.jface.viewers.DialogCellEditor;\r
19 import org.eclipse.swt.SWT;\r
20 import org.eclipse.swt.graphics.Color;\r
21 import org.eclipse.swt.graphics.FontData;\r
22 import org.eclipse.swt.widgets.Composite;\r
23 import org.eclipse.swt.widgets.Control;\r
24 import org.eclipse.swt.widgets.FontDialog;\r
25 import org.eclipse.swt.widgets.Label;\r
26 \r
27 public class FontCellEditor extends DialogCellEditor {\r
28 \r
29     private Label fontLabel;\r
30         \r
31     public FontCellEditor() {\r
32         super();\r
33     }\r
34 \r
35     public FontCellEditor(Composite parent, int style) {\r
36         super(parent, style);\r
37     }\r
38 \r
39 \r
40 \r
41     public FontCellEditor(Composite parent) {\r
42         super(parent);\r
43     }\r
44 \r
45     @Override\r
46     protected Control createContents(Composite cell) {\r
47         fontLabel = new Label(cell, SWT.LEFT);\r
48         Color bg = cell.getBackground();\r
49         fontLabel.setBackground(bg);\r
50         return fontLabel;\r
51     }\r
52     \r
53 \r
54     @Override\r
55     protected Object openDialogBox(Control cellEditorWindow) {\r
56         \r
57         FontDialog fontDialog = new FontDialog(cellEditorWindow.getShell());\r
58         FontData fontData = (FontData) getValue();\r
59         if (fontData!=null)\r
60             fontDialog.setFontList(new FontData[] {fontData});\r
61         return fontDialog.open();\r
62     }\r
63 \r
64     @Override\r
65     protected void updateContents(Object value) {\r
66         FontData fontData = (FontData) value;\r
67         if (fontData==null) {\r
68             fontLabel.setText("select font...");\r
69             return;\r
70         }\r
71         \r
72         StringBuilder sb = new StringBuilder();\r
73         sb.append( fontData.getName() );\r
74         sb.append( ", "+fontData.getHeight() );\r
75         if ( (fontData.getStyle() & SWT.BOLD)>0 )\r
76             sb.append(", Bold");\r
77         if ( (fontData.getStyle() & SWT.ITALIC)>0 )\r
78             sb.append(", Italic");\r
79         fontLabel.setText(sb.toString());\r
80     }\r
81     \r
82     \r
83 }\r