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