]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/PageSettingsDialog.java
Improved Copy Visible Data usability in time series chart editor
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / PageSettingsDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagram;
13
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;
43
44 /**
45  * A dialog for changing page settings
46  * 
47  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
48  */
49 public class PageSettingsDialog extends Dialog {
50
51     private final Session session;
52     private final Resource diagramResource;
53
54     private PageDescComposite pdc;
55     private Text gridSizeText;
56     private Button showBordersButton;
57     private Button showMarginsButton;
58
59     private PageDesc pageDesc;
60     private double gridSize;
61     private double lastGridSize;
62     private boolean borders = false;
63     private boolean margins = false;
64
65
66     public PageSettingsDialog(Resource diagramResource, Shell parentShell) {
67         super(parentShell);
68         this.session = Simantics.getSession();
69         this.diagramResource = diagramResource;
70     }
71
72
73     @Override
74     protected void configureShell(Shell newShell) {
75         super.configureShell(newShell);
76         newShell.setText(Messages.PageSettingsDialog_PageSettings);
77     }
78
79     @Override
80     protected Control createDialogArea(Composite parent) {
81         Composite composite = (Composite) super.createDialogArea(parent);
82
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);
91
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);
98
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);
106
107         GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(displayGroup);
108
109         gridSizeText.addModifyListener(new ModifyListener() {
110             @Override
111             public void modifyText(ModifyEvent e) {
112                 String text = ((Text)e.widget).getText();
113                 try {
114                     gridSize = Double.parseDouble(text);
115                     if (gridSize < 1e-3 || gridSize > 1e3) {
116                         gridSize = lastGridSize;
117                         setGridSizeValid(false);
118                     } else {
119                         setGridSizeValid(true);
120                     }
121                 } catch (NumberFormatException err) {
122                     gridSize = lastGridSize;
123                     setGridSizeValid(false);
124                 }
125             }
126
127             void setGridSizeValid(boolean valid) {
128                 if (valid) {
129                     gridSizeText.setBackground(null);
130                     gridSizeText.setForeground(null);
131                 } else {
132                     //gridSizeText.setBackground(gridSizeText.getDisplay().getSystemColor(SWT.COLOR_RED));
133                     gridSizeText.setForeground(gridSizeText.getDisplay().getSystemColor(SWT.COLOR_RED));
134                 }
135             }
136         });
137
138         showBordersButton.addSelectionListener(new SelectionAdapter() {
139             @Override
140             public void widgetSelected(SelectionEvent e) {
141                 borders = ((Button)e.widget).getSelection();
142             }
143         });
144
145         showMarginsButton.addSelectionListener(new SelectionAdapter() {
146             @Override
147             public void widgetSelected(SelectionEvent e) {
148                 margins = ((Button)e.widget).getSelection();
149             }
150         });
151
152         loadValues();
153
154         return composite;
155     }
156
157     public void applySettings() {
158         if (pdc.getPageDesc() != null)
159             pageDesc = pdc.getPageDesc();
160         session.markUndoPoint();
161         session.asyncRequest(new WriteRequest() {
162             @Override
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);
173             }
174         });
175     }
176
177     private void loadValues() {
178         session.asyncRequest(new ReadRequest() {
179
180             @Override
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() {
188                     @Override
189                     public void run() {
190                         applyValues();
191                     }
192                 });
193             }
194         });
195     }
196
197     private void applyValues() {
198         pdc.setPageDesc(pageDesc);
199         gridSizeText.setText(Double.toString(gridSize));
200         showBordersButton.setSelection(borders);
201         showMarginsButton.setSelection(margins);
202     }
203
204 }