1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.actions.style;
14 import java.awt.Color;
15 import java.awt.Dimension;
17 import java.awt.Point;
19 import javax.swing.SwingUtilities;
20 import javax.swing.UIManager;
21 import javax.swing.UnsupportedLookAndFeelException;
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;
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
51 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
53 public class EditStyle {
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";
61 public static void openStyleDialog(final Resource[] resources) {
62 if (resources.length == 0)
65 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
68 public void run(ReadGraph graph) throws DatabaseException {
69 G2DResource g2d = G2DResource.getInstance(graph);
70 DiagramResource dr = DiagramResource.getInstance(graph);
71 boolean hasStyle = true;
74 MetricsFormat format = null;
76 boolean hasColor = true;
77 boolean hasFont = true;
78 boolean hasFormat = true;
80 // Find what kind of styles selected objects support
81 for (Resource r : resources) {
82 if (!graph.isInstanceOf(r, dr.StyleProvider)) {
86 if (graph.isInstanceOf(r, dr.FontProvider)) {
88 Resource fontR = graph.getPossibleObject(r, g2d.HasFont);
90 font = G2DUtils.getFont(graph,fontR);
95 if (graph.isInstanceOf(r, dr.ColorProvider)) {
97 Resource colorR = graph.getPossibleObject(r, g2d.HasColor);
99 color = G2DUtils.getColor(graph,colorR);
104 if (graph.isInstanceOf(r, dr.FormatProvider)) {
105 if (format == null) {
106 Resource formatR = graph.getPossibleObject(r, dr.HasFormat);
108 format = G2DUtils.getMetricsFormat(graph,formatR);
116 return; // TODO : show error
118 if (!hasFont && !hasColor && !hasFormat)
119 return; // TODO : show error
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;
128 Job job = new Job("Open Style Dialog") {
130 protected IStatus run(IProgressMonitor monitor) {
131 monitor.beginTask("Open dialog", IProgressMonitor.UNKNOWN);
132 SwingUtilities.invokeLater(new Runnable() {
135 setThread(Thread.currentThread());
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);
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);
158 // Restore dialog settings
159 IDialogSettings ds = Activator.getDefault().getDialogSettings();
160 IDialogSettings sd = ds.getSection(SECTION_AWT_STYLE_DIALOG);
161 boolean restoredSettings = false;
163 sd = ds.addNewSection(SECTION_AWT_STYLE_DIALOG);
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);
171 if (w > 0 && h > 0) {
172 dialog.setLocation(x, y);
173 dialog.setSize(w, h);
174 restoredSettings = true;
176 } catch (NumberFormatException e) {
180 if (!restoredSettings) {
181 dialog.setLocationByPlatform(true);
184 done(Status.OK_STATUS);
185 dialog.setVisible(true);
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);
195 if (!dialog.isCancelled()) {
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() {
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);
213 Resource colorResource = null;
214 if (useColor && color != null)
215 colorResource = G2DUtils.createColor(graph, color);
217 Resource formatResource = null;
219 formatResource = G2DUtils.createMetricsFormat(graph, format);
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);
227 if (useColor && colorResource != null && graph.isInstanceOf(r, dr.ColorProvider)) {
228 graph.deny(r, g2d.HasColor);
229 graph.claim(r, g2d.HasColor, colorResource);
231 if (useFormat && formatResource != null && graph.isInstanceOf(r, dr.FormatProvider)) {
232 graph.deny(r,dr.HasFormat);
233 graph.claim(r, dr.HasFormat, formatResource);
241 return Job.ASYNC_FINISH;