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.diagram;
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.jface.layout.GridDataFactory;
16 import org.eclipse.jface.layout.GridLayoutFactory;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.ModifyEvent;
19 import org.eclipse.swt.events.ModifyListener;
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Group;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
30 import org.simantics.Simantics;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.Session;
34 import org.simantics.db.WriteGraph;
35 import org.simantics.db.common.request.IndexRoot;
36 import org.simantics.db.common.request.ReadRequest;
37 import org.simantics.db.common.request.WriteRequest;
38 import org.simantics.db.exception.DatabaseException;
39 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
40 import org.simantics.modeling.ui.preferences.DiagramPreferences;
41 import org.simantics.scl.commands.Commands;
42 import org.simantics.utils.page.PageDesc;
45 * A dialog for changing page settings
47 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
49 public class PageSettingsDialog extends Dialog {
51 private final Session session;
52 private final Resource diagramResource;
54 private PageDescComposite pdc;
55 private Text gridSizeText;
56 private Button showBordersButton;
57 private Button showMarginsButton;
59 private PageDesc pageDesc;
60 private double gridSize;
61 private double lastGridSize;
62 private boolean borders = false;
63 private boolean margins = false;
66 public PageSettingsDialog(Resource diagramResource, Shell parentShell) {
68 this.session = Simantics.getSession();
69 this.diagramResource = diagramResource;
74 protected void configureShell(Shell newShell) {
75 super.configureShell(newShell);
76 newShell.setText(Messages.PageSettingsDialog_PageSettings);
80 protected Control createDialogArea(Composite parent) {
81 Composite composite = (Composite) super.createDialogArea(parent);
83 Group group = new Group(composite, SWT.NONE);
84 group.setText(Messages.PageSettingsDialog_GridSnapping);
85 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(group);
86 Label label = new Label(group, SWT.NONE);
87 label.setText(Messages.PageSettingsDialog_GridSizeMM);
88 gridSizeText = new Text(group, SWT.SINGLE|SWT.BORDER);
89 GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(group);
90 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(gridSizeText);
92 Group pageGroup = new Group(composite, SWT.NONE);
93 pageGroup.setText(Messages.PageSettingsDialog_PageSize);
94 pdc = new PageDescComposite(pageGroup, SWT.NONE);
95 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(pageGroup);
96 GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(pageGroup);
97 GridDataFactory.fillDefaults().grab(true, true).span(1, 1).applyTo(pdc);
99 Group displayGroup = new Group(composite, SWT.NONE);
100 displayGroup.setText(Messages.PageSettingsDialog_Display);
101 showBordersButton = new Button(displayGroup, SWT.CHECK);
102 showBordersButton.setText(Messages.PageSettingsDialog_ShowPageBorders);
103 showMarginsButton = new Button(displayGroup, SWT.CHECK);
104 showMarginsButton.setText(Messages.PageSettingsDialog_ShowMargins);
105 GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(false).extendedMargins(12, 12, 12, 12).spacing(5, 4).applyTo(displayGroup);
107 GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(displayGroup);
109 gridSizeText.addModifyListener(new ModifyListener() {
111 public void modifyText(ModifyEvent e) {
112 String text = ((Text)e.widget).getText();
114 gridSize = Double.parseDouble(text);
115 if (gridSize < 1e-3 || gridSize > 1e3) {
116 gridSize = lastGridSize;
117 setGridSizeValid(false);
119 setGridSizeValid(true);
121 } catch (NumberFormatException err) {
122 gridSize = lastGridSize;
123 setGridSizeValid(false);
127 void setGridSizeValid(boolean valid) {
129 gridSizeText.setBackground(null);
130 gridSizeText.setForeground(null);
132 //gridSizeText.setBackground(gridSizeText.getDisplay().getSystemColor(SWT.COLOR_RED));
133 gridSizeText.setForeground(gridSizeText.getDisplay().getSystemColor(SWT.COLOR_RED));
138 showBordersButton.addSelectionListener(new SelectionAdapter() {
140 public void widgetSelected(SelectionEvent e) {
141 borders = ((Button)e.widget).getSelection();
145 showMarginsButton.addSelectionListener(new SelectionAdapter() {
147 public void widgetSelected(SelectionEvent e) {
148 margins = ((Button)e.widget).getSelection();
157 public void applySettings() {
158 if (pdc.getPageDesc() != null)
159 pageDesc = pdc.getPageDesc();
160 session.markUndoPoint();
161 session.asyncRequest(new WriteRequest() {
163 public void perform(WriteGraph graph) throws DatabaseException {
164 Resource model = graph.syncRequest(new IndexRoot(diagramResource));
165 Commands.get(graph, "Simantics/PageSettings/setPageDesc") //$NON-NLS-1$
166 .execute(graph, model, diagramResource, pageDesc.toRepr());
167 Commands.get(graph, "Simantics/PageSettings/setGridSize") //$NON-NLS-1$
168 .execute(graph, model, diagramResource, gridSize);
169 Commands.get(graph, "Simantics/PageSettings/setPageBordersVisible") //$NON-NLS-1$
170 .execute(graph, model, diagramResource, borders);
171 Commands.get(graph, "Simantics/PageSettings/setMarginsVisible") //$NON-NLS-1$
172 .execute(graph, model, diagramResource, margins);
177 private void loadValues() {
178 session.asyncRequest(new ReadRequest() {
181 public void run(ReadGraph graph) throws DatabaseException {
182 pageDesc = DiagramGraphUtil.getPageDesc(graph, diagramResource, PageDesc.DEFAULT);
183 gridSize = DiagramGraphUtil.getGridSize(graph, diagramResource, DiagramPreferences.DEFAULT_SNAP_GRID_SIZE);
184 lastGridSize = gridSize;
185 borders = DiagramGraphUtil.isPageBordersVisible(graph, diagramResource);
186 margins = DiagramGraphUtil.isMarginsVisible(graph, diagramResource);
187 Display.getDefault().asyncExec(new Runnable() {
197 private void applyValues() {
198 pdc.setPageDesc(pageDesc);
199 gridSizeText.setText(Double.toString(gridSize));
200 showBordersButton.setSelection(borders);
201 showMarginsButton.setSelection(margins);