-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.modeling.ui.actions.style;\r
-\r
-import java.awt.Color;\r
-import java.awt.Dimension;\r
-import java.awt.Font;\r
-import java.awt.Point;\r
-\r
-import javax.swing.SwingUtilities;\r
-import javax.swing.UIManager;\r
-import javax.swing.UnsupportedLookAndFeelException;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.core.runtime.jobs.Job;\r
-import org.eclipse.jface.dialogs.IDialogSettings;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.diagram.G2DUtils;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.diagram.stubs.G2DResource;\r
-import org.simantics.modeling.ui.Activator;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.strings.format.MetricsFormat;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-/**\r
- * Style Edit\r
- * \r
- * TODO : should we have extension point for expanding styles?\r
- * TODO : default ColorChooser is not suitable for this task\r
- * TODO : how to store MetricsFormat template list\r
- * \r
- * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
- */\r
-public class EditStyle {\r
-\r
- private static final String SECTION_AWT_STYLE_DIALOG = "AWTStyleDialog";\r
- private static final String SETTING_DIALOG_HEIGHT = "h";\r
- private static final String SETTING_DIALOG_WIDTH = "w";\r
- private static final String SETTING_DIALOG_Y = "y";\r
- private static final String SETTING_DIALOG_X = "x";\r
-\r
- public static void openStyleDialog(final Resource[] resources) {\r
- if (resources.length == 0)\r
- return;\r
-\r
- SimanticsUI.getSession().asyncRequest(new ReadRequest() {\r
-\r
- @Override\r
- public void run(ReadGraph graph) throws DatabaseException {\r
- G2DResource g2d = G2DResource.getInstance(graph);\r
- DiagramResource dr = DiagramResource.getInstance(graph);\r
- boolean hasStyle = true;\r
- Color color = null;\r
- Font font = null;\r
- MetricsFormat format = null;\r
-\r
- boolean hasColor = true;\r
- boolean hasFont = true;\r
- boolean hasFormat = true;\r
-\r
- // Find what kind of styles selected objects support\r
- for (Resource r : resources) {\r
- if (!graph.isInstanceOf(r, dr.StyleProvider)) {\r
- hasStyle = false;\r
- break;\r
- }\r
- if (graph.isInstanceOf(r, dr.FontProvider)) {\r
- if (font == null) {\r
- Resource fontR = graph.getPossibleObject(r, g2d.HasFont);\r
- if (fontR != null)\r
- font = G2DUtils.getFont(graph,fontR);\r
- }\r
- } else {\r
- hasFont = false;\r
- }\r
- if (graph.isInstanceOf(r, dr.ColorProvider)) {\r
- if (color == null) {\r
- Resource colorR = graph.getPossibleObject(r, g2d.HasColor);\r
- if (colorR != null)\r
- color = G2DUtils.getColor(graph,colorR);\r
- }\r
- } else {\r
- hasColor = false;\r
- }\r
- if (graph.isInstanceOf(r, dr.FormatProvider)) {\r
- if (format == null) {\r
- Resource formatR = graph.getPossibleObject(r, dr.HasFormat);\r
- if (formatR != null)\r
- format = G2DUtils.getMetricsFormat(graph,formatR);\r
- }\r
- } else {\r
- hasFormat = false;\r
- }\r
- }\r
-\r
- if (!hasStyle)\r
- return; // TODO : show error\r
-\r
- if (!hasFont && !hasColor && !hasFormat)\r
- return; // TODO : show error\r
-\r
- final Font currentFont = font;\r
- final Color currentColor = color;\r
- final MetricsFormat currentFormat = format;\r
- final boolean useFont = hasFont;\r
- final boolean useColor = hasColor;\r
- final boolean useFormat = hasFormat;\r
-\r
- Job job = new Job("Open Style Dialog") {\r
- @Override\r
- protected IStatus run(IProgressMonitor monitor) {\r
- monitor.beginTask("Open dialog", IProgressMonitor.UNKNOWN);\r
- SwingUtilities.invokeLater(new Runnable() {\r
- @Override\r
- public void run() {\r
- setThread(Thread.currentThread());\r
- try {\r
- UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());\r
- // there's no reason to show errors to user.\r
- } catch (ClassNotFoundException e) {\r
- ErrorLogger.defaultLogError(e);\r
- } catch (InstantiationException e) {\r
- ErrorLogger.defaultLogError(e);\r
- } catch (IllegalAccessException e) {\r
- ErrorLogger.defaultLogError(e);\r
- } catch (UnsupportedLookAndFeelException e) {\r
- ErrorLogger.defaultLogError(e);\r
- }\r
-\r
- AWTStyleDialog dialog = new AWTStyleDialog(useFont, useColor, useFormat);\r
- if (useColor && currentColor != null)\r
- dialog.setStartColor(currentColor);\r
- if (useFont && currentFont != null)\r
- dialog.setStartFont(currentFont);\r
- if (useFormat && currentFormat != null) {\r
- dialog.setStartFormat(currentFormat);\r
- }\r
-\r
- // Restore dialog settings\r
- IDialogSettings ds = Activator.getDefault().getDialogSettings();\r
- IDialogSettings sd = ds.getSection(SECTION_AWT_STYLE_DIALOG);\r
- boolean restoredSettings = false;\r
- if (sd == null) {\r
- sd = ds.addNewSection(SECTION_AWT_STYLE_DIALOG);\r
- } else {\r
- try {\r
- int x = sd.getInt(SETTING_DIALOG_X);\r
- int y = sd.getInt(SETTING_DIALOG_Y);\r
- int w = sd.getInt(SETTING_DIALOG_WIDTH);\r
- int h = sd.getInt(SETTING_DIALOG_HEIGHT);\r
- // Sanity check\r
- if (w > 0 && h > 0) {\r
- dialog.setLocation(x, y);\r
- dialog.setSize(w, h);\r
- restoredSettings = true;\r
- }\r
- } catch (NumberFormatException e) {\r
- // Ignore.\r
- }\r
- }\r
- if (!restoredSettings) {\r
- dialog.setLocationByPlatform(true);\r
- dialog.pack();\r
- }\r
- done(Status.OK_STATUS);\r
- dialog.setVisible(true);\r
-\r
- // Save settings\r
- Point loc = dialog.getLocation();\r
- Dimension dim = dialog.getSize();\r
- sd.put(SETTING_DIALOG_X, loc.x);\r
- sd.put(SETTING_DIALOG_Y, loc.y);\r
- sd.put(SETTING_DIALOG_WIDTH, dim.width);\r
- sd.put(SETTING_DIALOG_HEIGHT, dim.height);\r
-\r
- if (!dialog.isCancelled()) {\r
- // OK was pressed\r
- final Font font = dialog.getFont();\r
- final Color color = dialog.getColor();\r
- final MetricsFormat format = dialog.getFormat();\r
- Session session = Simantics.getSession();\r
- session.markUndoPoint();\r
- session.asyncRequest(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- G2DResource g2d = G2DResource.getInstance(graph);\r
- DiagramResource dr = DiagramResource.getInstance(graph);\r
- // create style definitions\r
- Resource fontResource = null;\r
- if (useFont && font != null)\r
- fontResource = G2DUtils.createFont(graph, font);\r
-\r
- Resource colorResource = null;\r
- if (useColor && color != null)\r
- colorResource = G2DUtils.createColor(graph, color);\r
-\r
- Resource formatResource = null;\r
- if (useFormat)\r
- formatResource = G2DUtils.createMetricsFormat(graph, format);\r
-\r
- // use style definitions in selected objects\r
- for (Resource r : resources) {\r
- if (useFont && fontResource != null && graph.isInstanceOf(r, dr.FontProvider)) {\r
- graph.deny(r, g2d.HasFont);\r
- graph.claim(r, g2d.HasFont, fontResource);\r
- }\r
- if (useColor && colorResource != null && graph.isInstanceOf(r, dr.ColorProvider)) {\r
- graph.deny(r, g2d.HasColor);\r
- graph.claim(r, g2d.HasColor, colorResource);\r
- }\r
- if (useFormat && formatResource != null && graph.isInstanceOf(r, dr.FormatProvider)) {\r
- graph.deny(r,dr.HasFormat);\r
- graph.claim(r, dr.HasFormat, formatResource);\r
- }\r
- }\r
- }\r
- });\r
- }\r
- }\r
- });\r
- return Job.ASYNC_FINISH;\r
- }\r
- };\r
- job.setUser(true);\r
- job.schedule();\r
- }\r
- });\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.modeling.ui.actions.style;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.Point;
+
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.dialogs.IDialogSettings;
+import org.simantics.Simantics;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.diagram.G2DUtils;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.stubs.G2DResource;
+import org.simantics.modeling.ui.Activator;
+import org.simantics.ui.SimanticsUI;
+import org.simantics.utils.strings.format.MetricsFormat;
+import org.simantics.utils.ui.ErrorLogger;
+
+/**
+ * Style Edit
+ *
+ * TODO : should we have extension point for expanding styles?
+ * TODO : default ColorChooser is not suitable for this task
+ * TODO : how to store MetricsFormat template list
+ *
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
+ */
+public class EditStyle {
+
+ private static final String SECTION_AWT_STYLE_DIALOG = "AWTStyleDialog";
+ private static final String SETTING_DIALOG_HEIGHT = "h";
+ private static final String SETTING_DIALOG_WIDTH = "w";
+ private static final String SETTING_DIALOG_Y = "y";
+ private static final String SETTING_DIALOG_X = "x";
+
+ public static void openStyleDialog(final Resource[] resources) {
+ if (resources.length == 0)
+ return;
+
+ SimanticsUI.getSession().asyncRequest(new ReadRequest() {
+
+ @Override
+ public void run(ReadGraph graph) throws DatabaseException {
+ G2DResource g2d = G2DResource.getInstance(graph);
+ DiagramResource dr = DiagramResource.getInstance(graph);
+ boolean hasStyle = true;
+ Color color = null;
+ Font font = null;
+ MetricsFormat format = null;
+
+ boolean hasColor = true;
+ boolean hasFont = true;
+ boolean hasFormat = true;
+
+ // Find what kind of styles selected objects support
+ for (Resource r : resources) {
+ if (!graph.isInstanceOf(r, dr.StyleProvider)) {
+ hasStyle = false;
+ break;
+ }
+ if (graph.isInstanceOf(r, dr.FontProvider)) {
+ if (font == null) {
+ Resource fontR = graph.getPossibleObject(r, g2d.HasFont);
+ if (fontR != null)
+ font = G2DUtils.getFont(graph,fontR);
+ }
+ } else {
+ hasFont = false;
+ }
+ if (graph.isInstanceOf(r, dr.ColorProvider)) {
+ if (color == null) {
+ Resource colorR = graph.getPossibleObject(r, g2d.HasColor);
+ if (colorR != null)
+ color = G2DUtils.getColor(graph,colorR);
+ }
+ } else {
+ hasColor = false;
+ }
+ if (graph.isInstanceOf(r, dr.FormatProvider)) {
+ if (format == null) {
+ Resource formatR = graph.getPossibleObject(r, dr.HasFormat);
+ if (formatR != null)
+ format = G2DUtils.getMetricsFormat(graph,formatR);
+ }
+ } else {
+ hasFormat = false;
+ }
+ }
+
+ if (!hasStyle)
+ return; // TODO : show error
+
+ if (!hasFont && !hasColor && !hasFormat)
+ return; // TODO : show error
+
+ final Font currentFont = font;
+ final Color currentColor = color;
+ final MetricsFormat currentFormat = format;
+ final boolean useFont = hasFont;
+ final boolean useColor = hasColor;
+ final boolean useFormat = hasFormat;
+
+ Job job = new Job("Open Style Dialog") {
+ @Override
+ protected IStatus run(IProgressMonitor monitor) {
+ monitor.beginTask("Open dialog", IProgressMonitor.UNKNOWN);
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ setThread(Thread.currentThread());
+ try {
+ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
+ // there's no reason to show errors to user.
+ } catch (ClassNotFoundException e) {
+ ErrorLogger.defaultLogError(e);
+ } catch (InstantiationException e) {
+ ErrorLogger.defaultLogError(e);
+ } catch (IllegalAccessException e) {
+ ErrorLogger.defaultLogError(e);
+ } catch (UnsupportedLookAndFeelException e) {
+ ErrorLogger.defaultLogError(e);
+ }
+
+ AWTStyleDialog dialog = new AWTStyleDialog(useFont, useColor, useFormat);
+ if (useColor && currentColor != null)
+ dialog.setStartColor(currentColor);
+ if (useFont && currentFont != null)
+ dialog.setStartFont(currentFont);
+ if (useFormat && currentFormat != null) {
+ dialog.setStartFormat(currentFormat);
+ }
+
+ // Restore dialog settings
+ IDialogSettings ds = Activator.getDefault().getDialogSettings();
+ IDialogSettings sd = ds.getSection(SECTION_AWT_STYLE_DIALOG);
+ boolean restoredSettings = false;
+ if (sd == null) {
+ sd = ds.addNewSection(SECTION_AWT_STYLE_DIALOG);
+ } else {
+ try {
+ int x = sd.getInt(SETTING_DIALOG_X);
+ int y = sd.getInt(SETTING_DIALOG_Y);
+ int w = sd.getInt(SETTING_DIALOG_WIDTH);
+ int h = sd.getInt(SETTING_DIALOG_HEIGHT);
+ // Sanity check
+ if (w > 0 && h > 0) {
+ dialog.setLocation(x, y);
+ dialog.setSize(w, h);
+ restoredSettings = true;
+ }
+ } catch (NumberFormatException e) {
+ // Ignore.
+ }
+ }
+ if (!restoredSettings) {
+ dialog.setLocationByPlatform(true);
+ dialog.pack();
+ }
+ done(Status.OK_STATUS);
+ dialog.setVisible(true);
+
+ // Save settings
+ Point loc = dialog.getLocation();
+ Dimension dim = dialog.getSize();
+ sd.put(SETTING_DIALOG_X, loc.x);
+ sd.put(SETTING_DIALOG_Y, loc.y);
+ sd.put(SETTING_DIALOG_WIDTH, dim.width);
+ sd.put(SETTING_DIALOG_HEIGHT, dim.height);
+
+ if (!dialog.isCancelled()) {
+ // OK was pressed
+ final Font font = dialog.getFont();
+ final Color color = dialog.getColor();
+ final MetricsFormat format = dialog.getFormat();
+ Session session = Simantics.getSession();
+ session.markUndoPoint();
+ session.asyncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ G2DResource g2d = G2DResource.getInstance(graph);
+ DiagramResource dr = DiagramResource.getInstance(graph);
+ // create style definitions
+ Resource fontResource = null;
+ if (useFont && font != null)
+ fontResource = G2DUtils.createFont(graph, font);
+
+ Resource colorResource = null;
+ if (useColor && color != null)
+ colorResource = G2DUtils.createColor(graph, color);
+
+ Resource formatResource = null;
+ if (useFormat)
+ formatResource = G2DUtils.createMetricsFormat(graph, format);
+
+ // use style definitions in selected objects
+ for (Resource r : resources) {
+ if (useFont && fontResource != null && graph.isInstanceOf(r, dr.FontProvider)) {
+ graph.deny(r, g2d.HasFont);
+ graph.claim(r, g2d.HasFont, fontResource);
+ }
+ if (useColor && colorResource != null && graph.isInstanceOf(r, dr.ColorProvider)) {
+ graph.deny(r, g2d.HasColor);
+ graph.claim(r, g2d.HasColor, colorResource);
+ }
+ if (useFormat && formatResource != null && graph.isInstanceOf(r, dr.FormatProvider)) {
+ graph.deny(r,dr.HasFormat);
+ graph.claim(r, dr.HasFormat, formatResource);
+ }
+ }
+ }
+ });
+ }
+ }
+ });
+ return Job.ASYNC_FINISH;
+ }
+ };
+ job.setUser(true);
+ job.schedule();
+ }
+ });
+ }
+
+}