--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.ui.preferences;\r
+\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;\r
+import org.simantics.g2d.page.DiagramDesc;\r
+import org.simantics.utils.page.MarginUtils.Margin;\r
+import org.simantics.utils.page.MarginUtils.Margins;\r
+import org.simantics.utils.page.PageDesc;\r
+import org.simantics.utils.page.PageOrientation;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class DiagramPreferences {\r
+\r
+ public static final String P_SNAP_GRID_SIZE = "grid.size";\r
+ public static final String P_DEFAULT_PAGE_SIZE = "page.default.size";\r
+ public static final String P_DISPLAY_PAGE_SIZE = "page.display.size";\r
+ public static final String P_DISPLAY_MARGINS = "page.display.margins";\r
+ public static final String P_DISPLAY_GRID = "page.display.grid";\r
+ public static final String P_DISPLAY_RULER = "page.display.ruler";\r
+\r
+ public static final Margin MARGIN_10MM = new Margin(0, 0, 10);\r
+\r
+ public static final Double DEFAULT_SNAP_GRID_SIZE = 1.0;\r
+ public static final PageOrientation DEFAULT_PAGE_ORIENTATION = PageOrientation.Landscape;\r
+ public static final Margins DEFAULT_PAGE_MARGINS = new Margins(MARGIN_10MM, MARGIN_10MM, MARGIN_10MM, MARGIN_10MM);\r
+ public static final PageDesc DEFAULT_PAGE_SIZE = PageDesc.DEFAULT.withOrientation(DEFAULT_PAGE_ORIENTATION).withMargins(DEFAULT_PAGE_MARGINS);\r
+ public static final Boolean DEFAULT_DISPLAY_PAGE_SIZE = Boolean.TRUE;\r
+ public static final Boolean DEFAULT_DISPLAY_MARGINS = Boolean.TRUE;\r
+ public static final Boolean DEFAULT_DISPLAY_GRID = Boolean.FALSE;\r
+ public static final Boolean DEFAULT_DISPLAY_RULER = Boolean.FALSE;\r
+\r
+ public final IEclipsePreferences preferences;\r
+\r
+ private final Map<String, Object> map;\r
+\r
+ public DiagramPreferences(IEclipsePreferences preferences, Map<String, Object> map) {\r
+ this.preferences = preferences;\r
+ this.map = map;\r
+ if (map == null)\r
+ map = Collections.emptyMap();\r
+ }\r
+\r
+ public DiagramPreferences withValue(String preference, Object value) {\r
+ Map<String, Object> newMap = new HashMap<String, Object>(map);\r
+ newMap.put(preference, value);\r
+ return new DiagramPreferences(preferences, newMap);\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ public <T> T get(String preference) {\r
+ T t = (T) map.get(preference);\r
+ if (t == null)\r
+ throw new IllegalStateException("no value for preference " + preference + " available");\r
+ return t;\r
+ }\r
+\r
+ public PageDesc getCompletePageDesc() {\r
+ PageDesc pd = get(P_DEFAULT_PAGE_SIZE);\r
+ return pd;\r
+// PageOrientation po = get(P_DEFAULT_PAGE_ORIENTATION);\r
+// Margins margins = get(P_DEFAULT_PAGE_MARGINS);\r
+// return pd.withOrientation(po).withMargins(margins);\r
+ }\r
+\r
+ public DiagramDesc getDiagramDesc() {\r
+ double gridSize = get(DiagramPreferences.P_SNAP_GRID_SIZE);\r
+ boolean bordersVisible = get(DiagramPreferences.P_DISPLAY_PAGE_SIZE);\r
+ boolean marginsVisible = get(DiagramPreferences.P_DISPLAY_MARGINS);\r
+ boolean gridVisible = get(DiagramPreferences.P_DISPLAY_GRID);\r
+ boolean rulerVisible = get(DiagramPreferences.P_DISPLAY_RULER);\r
+ return new DiagramDesc(getCompletePageDesc(), gridSize, bordersVisible, marginsVisible, gridVisible, rulerVisible);\r
+ }\r
+\r
+}\r