]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/preferences/DiagramPreferences.java
Option for exporting tg and pgraph with sharedlibrary
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / preferences / DiagramPreferences.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.preferences;
13
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
19 import org.simantics.g2d.page.DiagramDesc;
20 import org.simantics.utils.page.MarginUtils.Margin;
21 import org.simantics.utils.page.MarginUtils.Margins;
22 import org.simantics.utils.page.PageDesc;
23 import org.simantics.utils.page.PageOrientation;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public final class DiagramPreferences {
29
30     public static final String             P_SNAP_GRID_SIZE           = "grid.size";
31     public static final String             P_DEFAULT_PAGE_SIZE        = "page.default.size";
32     public static final String             P_DISPLAY_PAGE_SIZE        = "page.display.size";
33     public static final String             P_DISPLAY_MARGINS          = "page.display.margins";
34     public static final String             P_DISPLAY_GRID             = "page.display.grid";
35     public static final String             P_DISPLAY_RULER            = "page.display.ruler";
36
37     public static final Margin             MARGIN_10MM                = new Margin(0, 0, 10);
38
39     public static final Double             DEFAULT_SNAP_GRID_SIZE     = 1.0;
40     public static final PageOrientation    DEFAULT_PAGE_ORIENTATION   = PageOrientation.Landscape;
41     public static final Margins            DEFAULT_PAGE_MARGINS       = new Margins(MARGIN_10MM, MARGIN_10MM, MARGIN_10MM, MARGIN_10MM);
42     public static final PageDesc           DEFAULT_PAGE_SIZE          = PageDesc.DEFAULT.withOrientation(DEFAULT_PAGE_ORIENTATION).withMargins(DEFAULT_PAGE_MARGINS);
43     public static final Boolean            DEFAULT_DISPLAY_PAGE_SIZE  = Boolean.TRUE;
44     public static final Boolean            DEFAULT_DISPLAY_MARGINS    = Boolean.TRUE;
45     public static final Boolean            DEFAULT_DISPLAY_GRID       = Boolean.FALSE;
46     public static final Boolean            DEFAULT_DISPLAY_RULER      = Boolean.FALSE;
47
48     public final IEclipsePreferences       preferences;
49
50     private final Map<String, Object>      map;
51
52     public DiagramPreferences(IEclipsePreferences preferences, Map<String, Object> map) {
53         this.preferences = preferences;
54         this.map = map;
55         if (map == null)
56             map = Collections.emptyMap();
57     }
58
59     public DiagramPreferences withValue(String preference, Object value) {
60         Map<String, Object> newMap = new HashMap<String, Object>(map);
61         newMap.put(preference, value);
62         return new DiagramPreferences(preferences, newMap);
63     }
64
65     @SuppressWarnings("unchecked")
66     public <T> T get(String preference) {
67         T t = (T) map.get(preference);
68         if (t == null)
69             throw new IllegalStateException("no value for preference " + preference + " available");
70         return t;
71     }
72
73     public PageDesc getCompletePageDesc() {
74         PageDesc pd = get(P_DEFAULT_PAGE_SIZE);
75         return pd;
76 //        PageOrientation po = get(P_DEFAULT_PAGE_ORIENTATION);
77 //        Margins margins = get(P_DEFAULT_PAGE_MARGINS);
78 //        return pd.withOrientation(po).withMargins(margins);
79     }
80
81     public DiagramDesc getDiagramDesc() {
82         double gridSize = get(DiagramPreferences.P_SNAP_GRID_SIZE);
83         boolean bordersVisible = get(DiagramPreferences.P_DISPLAY_PAGE_SIZE);
84         boolean marginsVisible = get(DiagramPreferences.P_DISPLAY_MARGINS);
85         boolean gridVisible = get(DiagramPreferences.P_DISPLAY_GRID);
86         boolean rulerVisible = get(DiagramPreferences.P_DISPLAY_RULER);
87         return new DiagramDesc(getCompletePageDesc(), gridSize, bordersVisible, marginsVisible, gridVisible, rulerVisible);
88     }
89
90 }