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.Collections;
15 import java.util.HashMap;
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;
26 * @author Tuukka Lehtonen
28 public final class DiagramPreferences {
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";
37 public static final Margin MARGIN_10MM = new Margin(0, 0, 10);
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;
48 public final IEclipsePreferences preferences;
50 private final Map<String, Object> map;
52 public DiagramPreferences(IEclipsePreferences preferences, Map<String, Object> map) {
53 this.preferences = preferences;
56 map = Collections.emptyMap();
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);
65 @SuppressWarnings("unchecked")
66 public <T> T get(String preference) {
67 T t = (T) map.get(preference);
69 throw new IllegalStateException("no value for preference " + preference + " available");
73 public PageDesc getCompletePageDesc() {
74 PageDesc pd = get(P_DEFAULT_PAGE_SIZE);
76 // PageOrientation po = get(P_DEFAULT_PAGE_ORIENTATION);
77 // Margins margins = get(P_DEFAULT_PAGE_MARGINS);
78 // return pd.withOrientation(po).withMargins(margins);
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);