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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.actions.style;
\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
19 import javax.swing.SwingUtilities;
\r
20 import javax.swing.UIManager;
\r
21 import javax.swing.UnsupportedLookAndFeelException;
\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
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
51 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
53 public class EditStyle {
\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
61 public static void openStyleDialog(final Resource[] resources) {
\r
62 if (resources.length == 0)
\r
65 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
\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
74 MetricsFormat format = null;
\r
76 boolean hasColor = true;
\r
77 boolean hasFont = true;
\r
78 boolean hasFormat = true;
\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
86 if (graph.isInstanceOf(r, dr.FontProvider)) {
\r
88 Resource fontR = graph.getPossibleObject(r, g2d.HasFont);
\r
90 font = G2DUtils.getFont(graph,fontR);
\r
95 if (graph.isInstanceOf(r, dr.ColorProvider)) {
\r
96 if (color == null) {
\r
97 Resource colorR = graph.getPossibleObject(r, g2d.HasColor);
\r
99 color = G2DUtils.getColor(graph,colorR);
\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
116 return; // TODO : show error
\r
118 if (!hasFont && !hasColor && !hasFormat)
\r
119 return; // TODO : show error
\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
128 Job job = new Job("Open Style Dialog") {
\r
130 protected IStatus run(IProgressMonitor monitor) {
\r
131 monitor.beginTask("Open dialog", IProgressMonitor.UNKNOWN);
\r
132 SwingUtilities.invokeLater(new Runnable() {
\r
134 public void run() {
\r
135 setThread(Thread.currentThread());
\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
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
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
163 sd = ds.addNewSection(SECTION_AWT_STYLE_DIALOG);
\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
171 if (w > 0 && h > 0) {
\r
172 dialog.setLocation(x, y);
\r
173 dialog.setSize(w, h);
\r
174 restoredSettings = true;
\r
176 } catch (NumberFormatException e) {
\r
180 if (!restoredSettings) {
\r
181 dialog.setLocationByPlatform(true);
\r
184 done(Status.OK_STATUS);
\r
185 dialog.setVisible(true);
\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
195 if (!dialog.isCancelled()) {
\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
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
213 Resource colorResource = null;
\r
214 if (useColor && color != null)
\r
215 colorResource = G2DUtils.createColor(graph, color);
\r
217 Resource formatResource = null;
\r
219 formatResource = G2DUtils.createMetricsFormat(graph, format);
\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
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
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
241 return Job.ASYNC_FINISH;
\r