]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
9f69f7b379f395bd57a2dd6d716e6830338614ea
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * 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 package org.simantics.sysdyn.ui.menu;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Font;\r
16 \r
17 import org.eclipse.jface.action.ContributionItem;\r
18 import org.eclipse.jface.action.IContributionItem;\r
19 import org.eclipse.swt.SWT;\r
20 import org.eclipse.swt.events.SelectionEvent;\r
21 import org.eclipse.swt.events.SelectionListener;\r
22 import org.eclipse.swt.widgets.Menu;\r
23 import org.eclipse.swt.widgets.MenuItem;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.Resource;\r
26 import org.simantics.db.WriteGraph;\r
27 import org.simantics.db.common.request.WriteRequest;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 import org.simantics.diagram.G2DUtils;\r
30 import org.simantics.diagram.stubs.G2DResource;\r
31 import org.simantics.sysdyn.ui.properties.widgets.CustomFontDialog;\r
32 import org.simantics.ui.SimanticsUI;\r
33 import org.simantics.ui.contribution.DynamicMenuContribution;\r
34 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
35 \r
36 /**\r
37  * Context menu contribution for modifying fonts and font colors in diagram elements\r
38  * \r
39  * @author Teemu Lempinen\r
40  *\r
41  */\r
42 public class FontContextMenuContribution extends DynamicMenuContribution {\r
43 \r
44         @Override\r
45         protected IContributionItem[] getContributionItems(final ReadGraph graph,\r
46                         Object[] selection) throws DatabaseException {\r
47                 if (selection.length == 0)\r
48                         return new IContributionItem[0];\r
49 \r
50                 return new IContributionItem[] { new ContributionItem() {\r
51 \r
52                         @Override\r
53                         public void fill(Menu menu, int index) {\r
54 \r
55                                 G2DResource G2D = G2DResource.getInstance(graph);\r
56 \r
57                                 Object[] selections = getSelectedObjects();\r
58 \r
59                                 Font font = null;\r
60                                 Color color = null;\r
61 \r
62                                 /*\r
63                                  *  Find a common font and color for the selected elements.\r
64                                  *  \r
65                                  *  If a common font or color is not found, the initial value in dialog is empty.\r
66                                  */\r
67                                 try {\r
68                                         for(Object o : selections) {\r
69                                                 Resource element = ResourceAdaptionUtils\r
70                                                                 .adaptToResource(o);\r
71                                                 if(element != null) {\r
72                                                         Resource fontResource = graph.getPossibleObject(element, G2D.HasFont);\r
73                                                         if(fontResource != null) { \r
74                                                                 Font newFont = G2DUtils.getFont(graph, fontResource);\r
75                                                                 if(font == null) {\r
76                                                                         font = newFont;\r
77                                                                 }\r
78 \r
79                                                                 if(font != null && !font.equals(newFont)) {\r
80                                                                         font = null;\r
81                                                                         break;\r
82                                                                 }\r
83                                                         }\r
84                                                 }\r
85                                         }\r
86                                 } catch (DatabaseException e) {\r
87                                         e.printStackTrace();\r
88                                 }\r
89 \r
90                                 try {\r
91                                         for(Object o : selections) {\r
92                                                 Resource element = ResourceAdaptionUtils\r
93                                                                 .adaptToResource(o);\r
94                                                 if(element != null) {\r
95                                                     Resource colorResource = graph.getPossibleObject(element, G2D.HasColor);\r
96                                                     if(colorResource != null ) {\r
97                                                         Color newColor = G2DUtils.getColor(graph, colorResource);\r
98                                                         if(color == null)\r
99                                                             color = newColor;\r
100 \r
101                                                         if(color != null && !color.equals(newColor))  {\r
102                                                             color = null;\r
103                                                             break;\r
104                                                         }\r
105                                                     }                    \r
106                                                 }\r
107                                         }\r
108                                 } catch (DatabaseException e) {\r
109                                 }\r
110 \r
111                                 // Create the menu item with a selection listener\r
112                                 MenuItem item;\r
113                                 item = new MenuItem(menu, SWT.PUSH);\r
114 \r
115                                 item.setText("Font...");\r
116 \r
117                                 item.addSelectionListener(new FontSelectionListener(selections, font, color));\r
118                         }\r
119                 } \r
120                 };\r
121 \r
122         }\r
123 \r
124         /**\r
125          * Selection listener for font context menu action\r
126          * @author Teemu Lempinen\r
127          *\r
128          */\r
129         class FontSelectionListener implements SelectionListener {\r
130 \r
131                 private Font font;\r
132                 private Color color;\r
133                 private Object[] selections;\r
134 \r
135                 /**\r
136                  * Font selection listener for context menu action in diagram\r
137                  * \r
138                  * @param selections Selected elements\r
139                  * @param font Possible common font for the selected elements\r
140                  * @param color Possible common color for the selected elements\r
141                  */\r
142                 public FontSelectionListener(Object[] selections, Font font, Color color) {\r
143                         this.selections = selections;\r
144                         this.font = font;\r
145                         this.color = color;\r
146                 }\r
147 \r
148                 @Override\r
149                 public void widgetSelected(SelectionEvent e) {\r
150                         // Create the dialog\r
151                         CustomFontDialog dialog = new CustomFontDialog(e.widget.getDisplay().getActiveShell(), "Sample");\r
152 \r
153                         // Set possible font and color defaults\r
154                         if(font != null)\r
155                                 dialog.setAWTFont(font);\r
156 \r
157                         if(color != null)\r
158                                 dialog.setColor(color);\r
159 \r
160                         // Open dialog\r
161                         dialog.open();\r
162 \r
163                         // Get results\r
164                         final Font resultFont = dialog.getAWTFont();\r
165                         final Color resultColor = dialog.getAWTColor();\r
166 \r
167                         // Apply results to all selected elements\r
168                         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
169 \r
170                                 @Override\r
171                                 public void perform(WriteGraph graph) throws DatabaseException {\r
172                                         G2DResource G2D = G2DResource.getInstance(graph);\r
173 \r
174                                         for(Object o : selections) {\r
175                                                 Resource element = ResourceAdaptionUtils\r
176                                                                 .adaptToResource(o);\r
177                                                 if(resultFont != null) {\r
178                                                         graph.deny(element, G2D.HasFont);\r
179                                                         graph.claim(element, G2D.HasFont, G2DUtils.createFont(graph, resultFont));\r
180                                                 }\r
181 \r
182                                                 if(resultColor != null) {\r
183                                                         graph.deny(element, G2D.HasColor);\r
184                                                         graph.claim(element, G2D.HasColor, G2DUtils.createColor(graph, resultColor));\r
185                                                 }\r
186                                         }\r
187                                 }\r
188                         });\r
189                 }\r
190 \r
191                 @Override\r
192                 public void widgetDefaultSelected(SelectionEvent e) {\r
193                 }\r
194 \r
195         }\r
196 }\r