]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/style/EditStyle.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / style / EditStyle.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 package org.simantics.modeling.ui.actions.style;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Dimension;\r
16 import java.awt.Font;\r
17 import java.awt.Point;\r
18 \r
19 import javax.swing.SwingUtilities;\r
20 import javax.swing.UIManager;\r
21 import javax.swing.UnsupportedLookAndFeelException;\r
22 \r
23 import org.eclipse.core.runtime.IProgressMonitor;\r
24 import org.eclipse.core.runtime.IStatus;\r
25 import org.eclipse.core.runtime.Status;\r
26 import org.eclipse.core.runtime.jobs.Job;\r
27 import org.eclipse.jface.dialogs.IDialogSettings;\r
28 import org.simantics.Simantics;\r
29 import org.simantics.db.ReadGraph;\r
30 import org.simantics.db.Resource;\r
31 import org.simantics.db.Session;\r
32 import org.simantics.db.WriteGraph;\r
33 import org.simantics.db.common.request.ReadRequest;\r
34 import org.simantics.db.common.request.WriteRequest;\r
35 import org.simantics.db.exception.DatabaseException;\r
36 import org.simantics.diagram.G2DUtils;\r
37 import org.simantics.diagram.stubs.DiagramResource;\r
38 import org.simantics.diagram.stubs.G2DResource;\r
39 import org.simantics.modeling.ui.Activator;\r
40 import org.simantics.ui.SimanticsUI;\r
41 import org.simantics.utils.strings.format.MetricsFormat;\r
42 import org.simantics.utils.ui.ErrorLogger;\r
43 \r
44 /**\r
45  * Style Edit\r
46  * \r
47  * TODO : should we have extension point for expanding styles?\r
48  * TODO : default ColorChooser is not suitable for this task\r
49  * TODO : how to store MetricsFormat template list\r
50  * \r
51  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
52  */\r
53 public class EditStyle {\r
54 \r
55     private static final String SECTION_AWT_STYLE_DIALOG = "AWTStyleDialog";\r
56     private static final String SETTING_DIALOG_HEIGHT    = "h";\r
57     private static final String SETTING_DIALOG_WIDTH     = "w";\r
58     private static final String SETTING_DIALOG_Y         = "y";\r
59     private static final String SETTING_DIALOG_X         = "x";\r
60 \r
61     public static void openStyleDialog(final Resource[] resources) {\r
62         if (resources.length == 0)\r
63             return;\r
64 \r
65         SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
66 \r
67             @Override\r
68             public void run(ReadGraph graph) throws DatabaseException {\r
69                 G2DResource g2d = G2DResource.getInstance(graph);\r
70                 DiagramResource dr = DiagramResource.getInstance(graph);\r
71                 boolean hasStyle = true;\r
72                 Color color = null;\r
73                 Font font = null;\r
74                 MetricsFormat format = null;\r
75 \r
76                 boolean hasColor = true;\r
77                 boolean hasFont = true;\r
78                 boolean hasFormat = true;\r
79 \r
80                 // Find what kind of styles selected objects support\r
81                 for (Resource r : resources) {\r
82                     if (!graph.isInstanceOf(r, dr.StyleProvider)) {\r
83                         hasStyle = false;\r
84                         break;\r
85                     }\r
86                     if (graph.isInstanceOf(r, dr.FontProvider)) {\r
87                         if (font == null) {\r
88                             Resource fontR = graph.getPossibleObject(r, g2d.HasFont);\r
89                             if (fontR != null)\r
90                                 font = G2DUtils.getFont(graph,fontR);\r
91                         }\r
92                     } else {\r
93                         hasFont = false;\r
94                     }\r
95                     if (graph.isInstanceOf(r, dr.ColorProvider)) {\r
96                         if (color == null) {\r
97                             Resource colorR = graph.getPossibleObject(r, g2d.HasColor);\r
98                             if (colorR != null)\r
99                                 color = G2DUtils.getColor(graph,colorR);\r
100                         }\r
101                     } else {\r
102                         hasColor = false;\r
103                     }\r
104                     if (graph.isInstanceOf(r, dr.FormatProvider)) {\r
105                         if (format == null) {\r
106                             Resource formatR = graph.getPossibleObject(r, dr.HasFormat);\r
107                             if (formatR != null)\r
108                                 format = G2DUtils.getMetricsFormat(graph,formatR);\r
109                         }\r
110                     } else {\r
111                         hasFormat = false;\r
112                     }\r
113                 }\r
114 \r
115                 if (!hasStyle)\r
116                     return; // TODO : show error\r
117 \r
118                 if (!hasFont && !hasColor && !hasFormat)\r
119                     return; // TODO : show error\r
120 \r
121                 final Font currentFont = font;\r
122                 final Color currentColor = color;\r
123                 final MetricsFormat currentFormat = format;\r
124                 final boolean useFont = hasFont;\r
125                 final boolean useColor = hasColor;\r
126                 final boolean useFormat = hasFormat;\r
127 \r
128                 Job job = new Job("Open Style Dialog") {\r
129                     @Override\r
130                     protected IStatus run(IProgressMonitor monitor) {\r
131                         monitor.beginTask("Open dialog", IProgressMonitor.UNKNOWN);\r
132                         SwingUtilities.invokeLater(new Runnable() {\r
133                             @Override\r
134                             public void run() {\r
135                                 setThread(Thread.currentThread());\r
136                                 try {\r
137                                     UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());\r
138                                     // there's no reason to show errors to user.\r
139                                 } catch (ClassNotFoundException e) {\r
140                                     ErrorLogger.defaultLogError(e);\r
141                                 } catch (InstantiationException e) {\r
142                                     ErrorLogger.defaultLogError(e);\r
143                                 } catch (IllegalAccessException e) {\r
144                                     ErrorLogger.defaultLogError(e);\r
145                                 } catch (UnsupportedLookAndFeelException e) {\r
146                                     ErrorLogger.defaultLogError(e);\r
147                                 }\r
148 \r
149                                 AWTStyleDialog dialog = new AWTStyleDialog(useFont, useColor, useFormat);\r
150                                 if (useColor && currentColor != null)\r
151                                     dialog.setStartColor(currentColor);\r
152                                 if (useFont && currentFont != null)\r
153                                     dialog.setStartFont(currentFont);\r
154                                 if (useFormat && currentFormat != null) {\r
155                                     dialog.setStartFormat(currentFormat);\r
156                                 }\r
157 \r
158                                 // Restore dialog settings\r
159                                 IDialogSettings ds = Activator.getDefault().getDialogSettings();\r
160                                 IDialogSettings sd = ds.getSection(SECTION_AWT_STYLE_DIALOG);\r
161                                 boolean restoredSettings = false;\r
162                                 if (sd == null) {\r
163                                     sd = ds.addNewSection(SECTION_AWT_STYLE_DIALOG);\r
164                                 } else {\r
165                                     try {\r
166                                         int x = sd.getInt(SETTING_DIALOG_X);\r
167                                         int y = sd.getInt(SETTING_DIALOG_Y);\r
168                                         int w = sd.getInt(SETTING_DIALOG_WIDTH);\r
169                                         int h = sd.getInt(SETTING_DIALOG_HEIGHT);\r
170                                         // Sanity check\r
171                                         if (w > 0 && h > 0) {\r
172                                             dialog.setLocation(x, y);\r
173                                             dialog.setSize(w, h);\r
174                                             restoredSettings = true;\r
175                                         }\r
176                                     } catch (NumberFormatException e) {\r
177                                         // Ignore.\r
178                                     }\r
179                                 }\r
180                                 if (!restoredSettings) {\r
181                                     dialog.setLocationByPlatform(true);\r
182                                     dialog.pack();\r
183                                 }\r
184                                 done(Status.OK_STATUS);\r
185                                 dialog.setVisible(true);\r
186 \r
187                                 // Save settings\r
188                                 Point loc = dialog.getLocation();\r
189                                 Dimension dim = dialog.getSize();\r
190                                 sd.put(SETTING_DIALOG_X, loc.x);\r
191                                 sd.put(SETTING_DIALOG_Y, loc.y);\r
192                                 sd.put(SETTING_DIALOG_WIDTH, dim.width);\r
193                                 sd.put(SETTING_DIALOG_HEIGHT, dim.height);\r
194 \r
195                                 if (!dialog.isCancelled()) {\r
196                                     // OK was pressed\r
197                                     final Font font = dialog.getFont();\r
198                                     final Color color = dialog.getColor();\r
199                                     final MetricsFormat format = dialog.getFormat();\r
200                                     Session session = Simantics.getSession();\r
201                                     session.markUndoPoint();\r
202                                     session.asyncRequest(new WriteRequest() {\r
203 \r
204                                         @Override\r
205                                         public void perform(WriteGraph graph) throws DatabaseException {\r
206                                             G2DResource g2d = G2DResource.getInstance(graph);\r
207                                             DiagramResource dr = DiagramResource.getInstance(graph);\r
208                                             // create style definitions\r
209                                             Resource fontResource = null;\r
210                                             if (useFont && font != null)\r
211                                                 fontResource = G2DUtils.createFont(graph, font);\r
212 \r
213                                             Resource colorResource = null;\r
214                                             if (useColor && color != null)\r
215                                                 colorResource = G2DUtils.createColor(graph, color);\r
216 \r
217                                             Resource formatResource = null;\r
218                                             if (useFormat)\r
219                                                 formatResource = G2DUtils.createMetricsFormat(graph, format);\r
220 \r
221                                             // use style definitions in selected objects\r
222                                             for (Resource r : resources) {\r
223                                                 if (useFont && fontResource != null && graph.isInstanceOf(r, dr.FontProvider)) {\r
224                                                     graph.deny(r, g2d.HasFont);\r
225                                                     graph.claim(r, g2d.HasFont, fontResource);\r
226                                                 }\r
227                                                 if (useColor && colorResource != null && graph.isInstanceOf(r, dr.ColorProvider)) {\r
228                                                     graph.deny(r, g2d.HasColor);\r
229                                                     graph.claim(r, g2d.HasColor, colorResource);\r
230                                                 }\r
231                                                 if (useFormat && formatResource != null && graph.isInstanceOf(r, dr.FormatProvider)) {\r
232                                                     graph.deny(r,dr.HasFormat);\r
233                                                     graph.claim(r, dr.HasFormat, formatResource);\r
234                                                 }\r
235                                             }\r
236                                         }\r
237                                     });\r
238                                 }\r
239                             }\r
240                         });\r
241                         return Job.ASYNC_FINISH;\r
242                     }\r
243                 };\r
244                 job.setUser(true);\r
245                 job.schedule();\r
246             }\r
247         });\r
248     }\r
249 \r
250 }\r