--- /dev/null
+/*******************************************************************************\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.diagram;\r
+\r
+import org.eclipse.jface.dialogs.Dialog;\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Group;\r
+import org.eclipse.swt.widgets.Label;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.eclipse.swt.widgets.Text;\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.IndexRoot;\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.synchronization.graph.DiagramGraphUtil;\r
+import org.simantics.modeling.ui.preferences.DiagramPreferences;\r
+import org.simantics.scl.commands.Commands;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.page.PageDesc;\r
+\r
+/**\r
+ * A dialog for changing page settings\r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ */\r
+public class PageSettingsDialog extends Dialog {\r
+\r
+ private final Session session;\r
+ private final Resource diagramResource;\r
+\r
+ private PageDescComposite pdc;\r
+ private Text gridSizeText;\r
+ private Button showBordersButton;\r
+ private Button showMarginsButton;\r
+\r
+ private PageDesc pageDesc;\r
+ private double gridSize;\r
+ private double lastGridSize;\r
+ private boolean borders = false;\r
+ private boolean margins = false;\r
+\r
+\r
+ public PageSettingsDialog(Resource diagramResource, Shell parentShell) {\r
+ super(parentShell);\r
+ this.session = SimanticsUI.getSession();\r
+ this.diagramResource = diagramResource;\r
+ }\r
+\r
+\r
+ @Override\r
+ protected void configureShell(Shell newShell) {\r
+ super.configureShell(newShell);\r
+ newShell.setText("Page Settings");\r
+ }\r
+\r
+ @Override\r
+ protected Control createDialogArea(Composite parent) {\r
+ Composite composite = (Composite) super.createDialogArea(parent);\r
+\r
+ Group group = new Group(composite, SWT.NONE);\r
+ group.setText("Grid Snapping");\r
+ GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(group);\r
+ Label label = new Label(group, SWT.NONE);\r
+ label.setText("Grid size (mm)");\r
+ gridSizeText = new Text(group, SWT.SINGLE|SWT.BORDER);\r
+ GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(group);\r
+ GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(gridSizeText);\r
+\r
+ Group pageGroup = new Group(composite, SWT.NONE);\r
+ pageGroup.setText("Page size");\r
+ pdc = new PageDescComposite(pageGroup, SWT.NONE);\r
+ GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(pageGroup);\r
+ GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(pageGroup);\r
+ GridDataFactory.fillDefaults().grab(true, true).span(1, 1).applyTo(pdc);\r
+\r
+ Group displayGroup = new Group(composite, SWT.NONE);\r
+ displayGroup.setText("Display");\r
+ showBordersButton = new Button(displayGroup, SWT.CHECK);\r
+ showBordersButton.setText("Show page borders");\r
+ showMarginsButton = new Button(displayGroup, SWT.CHECK);\r
+ showMarginsButton.setText("Show margins");\r
+ GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(displayGroup);\r
+\r
+ GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(displayGroup);\r
+\r
+ gridSizeText.addModifyListener(new ModifyListener() {\r
+ @Override\r
+ public void modifyText(ModifyEvent e) {\r
+ String text = ((Text)e.widget).getText();\r
+ try {\r
+ gridSize = Double.parseDouble(text);\r
+ if (gridSize < 1e-3 || gridSize > 1e3) {\r
+ gridSize = lastGridSize;\r
+ setGridSizeValid(false);\r
+ } else {\r
+ setGridSizeValid(true);\r
+ }\r
+ } catch (NumberFormatException err) {\r
+ gridSize = lastGridSize;\r
+ setGridSizeValid(false);\r
+ }\r
+ }\r
+\r
+ void setGridSizeValid(boolean valid) {\r
+ if (valid) {\r
+ gridSizeText.setBackground(null);\r
+ gridSizeText.setForeground(null);\r
+ } else {\r
+ //gridSizeText.setBackground(gridSizeText.getDisplay().getSystemColor(SWT.COLOR_RED));\r
+ gridSizeText.setForeground(gridSizeText.getDisplay().getSystemColor(SWT.COLOR_RED));\r
+ }\r
+ }\r
+ });\r
+\r
+ showBordersButton.addSelectionListener(new SelectionAdapter() {\r
+ @Override\r
+ public void widgetSelected(SelectionEvent e) {\r
+ borders = ((Button)e.widget).getSelection();\r
+ }\r
+ });\r
+\r
+ showMarginsButton.addSelectionListener(new SelectionAdapter() {\r
+ @Override\r
+ public void widgetSelected(SelectionEvent e) {\r
+ margins = ((Button)e.widget).getSelection();\r
+ }\r
+ });\r
+\r
+ loadValues();\r
+\r
+ return composite;\r
+ }\r
+\r
+ public void applySettings() {\r
+ if (pdc.getPageDesc() != null)\r
+ pageDesc = pdc.getPageDesc();\r
+ session.markUndoPoint();\r
+ session.asyncRequest(new WriteRequest() {\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ Resource model = graph.syncRequest(new IndexRoot(diagramResource));\r
+ Commands.get(graph, "Simantics/PageSettings/setPageDesc")\r
+ .execute(graph, model, diagramResource, pageDesc.toRepr());\r
+ Commands.get(graph, "Simantics/PageSettings/setGridSize")\r
+ .execute(graph, model, diagramResource, gridSize);\r
+ Commands.get(graph, "Simantics/PageSettings/setPageBordersVisible")\r
+ .execute(graph, model, diagramResource, borders);\r
+ Commands.get(graph, "Simantics/PageSettings/setMarginsVisible")\r
+ .execute(graph, model, diagramResource, margins);\r
+ }\r
+ });\r
+ }\r
+\r
+ private void loadValues() {\r
+ session.asyncRequest(new ReadRequest() {\r
+\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ pageDesc = DiagramGraphUtil.getPageDesc(graph, diagramResource, PageDesc.DEFAULT);\r
+ gridSize = DiagramGraphUtil.getGridSize(graph, diagramResource, DiagramPreferences.DEFAULT_SNAP_GRID_SIZE);\r
+ lastGridSize = gridSize;\r
+ borders = DiagramGraphUtil.isPageBordersVisible(graph, diagramResource);\r
+ margins = DiagramGraphUtil.isMarginsVisible(graph, diagramResource);\r
+ Display.getDefault().asyncExec(new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ applyValues();\r
+ }\r
+ });\r
+ }\r
+ });\r
+ }\r
+\r
+ private void applyValues() {\r
+ pdc.setPageDesc(pageDesc);\r
+ gridSizeText.setText(Double.toString(gridSize));\r
+ showBordersButton.setSelection(borders);\r
+ showMarginsButton.setSelection(margins);\r
+ }\r
+\r
+}\r