]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/preferences/DiagramPreferenceUtil.java
Option for exporting tg and pgraph with sharedlibrary
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / preferences / DiagramPreferenceUtil.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.HashMap;
15 import java.util.Map;
16
17 import org.eclipse.core.runtime.preferences.DefaultScope;
18 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
19 import org.eclipse.core.runtime.preferences.IScopeContext;
20 import org.eclipse.core.runtime.preferences.InstanceScope;
21 import org.osgi.service.prefs.BackingStoreException;
22 import org.osgi.service.prefs.Preferences;
23 import org.simantics.modeling.ui.Activator;
24 import org.simantics.utils.page.PageDesc;
25
26 /**
27  * @author Tuukka Lehtonen
28  */
29 public final class DiagramPreferenceUtil {
30
31     public static final String PLUGIN_ID = Activator.PLUGIN_ID;
32
33     public static DiagramPreferences getDefaultPreferences() {
34         return getPreferences(DefaultScope.INSTANCE);
35     }
36
37     /**
38      * @return
39      */
40     public static DiagramPreferences getPreferences() {
41         return getPreferences(InstanceScope.INSTANCE);
42     }
43
44     /**
45      * @return
46      */
47     public static DiagramPreferences getPreferences(IScopeContext context) {
48         IEclipsePreferences node = context.getNode(PLUGIN_ID);
49         double gridSize = node.getDouble(DiagramPreferences.P_SNAP_GRID_SIZE, DiagramPreferences.DEFAULT_SNAP_GRID_SIZE);
50         PageDesc pageDesc = getPageDesc(node);
51         //System.out.println("GET pagedesc: " + pageDesc);
52         //String pos = node.get(DiagramPreferences.P_DEFAULT_PAGE_ORIENTATION, DiagramPreferences.DEFAULT_PAGE_ORIENTATION.toString());
53         //PageOrientation po = PageOrientation.valueOf(pos);
54
55 //        Margins margins = DiagramPreferences.DEFAULT_PAGE_MARGINS;
56 //        String marginString = node.get(DiagramPreferences.P_DEFAULT_PAGE_MARGINS, null);
57 //        if (marginString != null)
58 //            margins = PageDesc.deserializeMargins(marginString, margins);
59 //        pageDesc = pageDesc.withMargins(margins);
60
61         boolean displayPage = node.getBoolean(DiagramPreferences.P_DISPLAY_PAGE_SIZE, DiagramPreferences.DEFAULT_DISPLAY_PAGE_SIZE);
62         boolean displayMargins = node.getBoolean(DiagramPreferences.P_DISPLAY_MARGINS, DiagramPreferences.DEFAULT_DISPLAY_MARGINS);
63         boolean displayGrid = node.getBoolean(DiagramPreferences.P_DISPLAY_MARGINS, DiagramPreferences.DEFAULT_DISPLAY_GRID);
64         boolean displayRuler = node.getBoolean(DiagramPreferences.P_DISPLAY_MARGINS, DiagramPreferences.DEFAULT_DISPLAY_RULER);
65
66         Map<String, Object> map = new HashMap<String, Object>();
67         map.put(DiagramPreferences.P_SNAP_GRID_SIZE, gridSize);
68         map.put(DiagramPreferences.P_DEFAULT_PAGE_SIZE, pageDesc);
69 //        map.put(DiagramPreferences.P_DEFAULT_PAGE_ORIENTATION, po);
70 //        map.put(DiagramPreferences.P_DEFAULT_PAGE_MARGINS, margins);
71         map.put(DiagramPreferences.P_DISPLAY_PAGE_SIZE, displayPage);
72         map.put(DiagramPreferences.P_DISPLAY_MARGINS, displayMargins);
73         map.put(DiagramPreferences.P_DISPLAY_GRID, displayGrid);
74         map.put(DiagramPreferences.P_DISPLAY_RULER, displayRuler);
75
76         return new DiagramPreferences(node, map);
77     }
78
79     /**
80      * @return
81      * @throws BackingStoreException
82      */
83     public static void setPreferences(DiagramPreferences prefs) {
84         _setPreferences(InstanceScope.INSTANCE, prefs);
85     }
86
87     /**
88      * @return
89      * @throws BackingStoreException
90      */
91     public static void flushPreferences(DiagramPreferences prefs) throws BackingStoreException {
92         Preferences p = _setPreferences(InstanceScope.INSTANCE, prefs);
93         p.flush();
94     }
95
96     /**
97      * @return
98      * @throws BackingStoreException
99      */
100     public static void setPreferences(IScopeContext context, DiagramPreferences prefs) {
101         _setPreferences(context, prefs);
102     }
103
104     /**
105      * @return
106      * @throws BackingStoreException
107      */
108     private static IEclipsePreferences _setPreferences(IScopeContext context, DiagramPreferences prefs) {
109         IEclipsePreferences node = context.getNode(PLUGIN_ID);
110         //System.out.println("SET pagedesc: " + prefs.get(DiagramPreferences.P_DEFAULT_PAGE_SIZE));
111         node.putDouble(DiagramPreferences.P_SNAP_GRID_SIZE, (Double) prefs.get(DiagramPreferences.P_SNAP_GRID_SIZE));
112         node.put(DiagramPreferences.P_DEFAULT_PAGE_SIZE, PageDesc.serialize((PageDesc) prefs.get(DiagramPreferences.P_DEFAULT_PAGE_SIZE)));
113 //        node.put(DiagramPreferences.P_DEFAULT_PAGE_ORIENTATION, prefs.get(DiagramPreferences.P_DEFAULT_PAGE_ORIENTATION).toString());
114 //        node.put(DiagramPreferences.P_DEFAULT_PAGE_MARGINS, PageDesc.serialize((Margins) prefs.get(DiagramPreferences.P_DEFAULT_PAGE_MARGINS)));
115         node.putBoolean(DiagramPreferences.P_DISPLAY_PAGE_SIZE, (Boolean) prefs.get(DiagramPreferences.P_DISPLAY_PAGE_SIZE));
116         node.putBoolean(DiagramPreferences.P_DISPLAY_MARGINS, (Boolean) prefs.get(DiagramPreferences.P_DISPLAY_MARGINS));
117         node.putBoolean(DiagramPreferences.P_DISPLAY_GRID, (Boolean) prefs.get(DiagramPreferences.P_DISPLAY_GRID));
118         node.putBoolean(DiagramPreferences.P_DISPLAY_RULER, (Boolean) prefs.get(DiagramPreferences.P_DISPLAY_RULER));
119         return node;
120     }
121
122     private static PageDesc getPageDesc(IEclipsePreferences node) {
123         String desc = node.get(DiagramPreferences.P_DEFAULT_PAGE_SIZE, null);
124         return PageDesc.deserialize(desc, DiagramPreferences.DEFAULT_PAGE_SIZE);
125     }
126
127 }