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