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.preferences;
14 import java.util.HashMap;
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;
27 * @author Tuukka Lehtonen
29 public final class DiagramPreferenceUtil {
31 public static final String PLUGIN_ID = Activator.PLUGIN_ID;
33 public static DiagramPreferences getDefaultPreferences() {
34 return getPreferences(DefaultScope.INSTANCE);
40 public static DiagramPreferences getPreferences() {
41 return getPreferences(InstanceScope.INSTANCE);
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);
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);
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);
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);
76 return new DiagramPreferences(node, map);
81 * @throws BackingStoreException
83 public static void setPreferences(DiagramPreferences prefs) {
84 _setPreferences(InstanceScope.INSTANCE, prefs);
89 * @throws BackingStoreException
91 public static void flushPreferences(DiagramPreferences prefs) throws BackingStoreException {
92 Preferences p = _setPreferences(InstanceScope.INSTANCE, prefs);
98 * @throws BackingStoreException
100 public static void setPreferences(IScopeContext context, DiagramPreferences prefs) {
101 _setPreferences(context, prefs);
106 * @throws BackingStoreException
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));
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);