1 /*
2  *
3  * Copyright (C) 2007,2008  Markko Merzin (markko.merzin@ut.ee)
4  *
5  * This file is part of ImageJ plugin Volumest.
6  * 
7  * Volumest plugin is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 *****************************************************************************
21 */
22package ee.ut.mrz.volumest;
23
24import java.awt.Color;
25import java.awt.event.MouseEvent;
26import java.io.File;
27import java.io.FileInputStream;
28import java.io.FileNotFoundException;
29import java.io.FileOutputStream;
30import java.io.IOException;
31import java.io.InputStream;
32import java.io.OutputStream;
33import java.util.InvalidPropertiesFormatException;
34import java.util.Properties;
35
36/**
37 * Holds runtime settings. Some settings are saved to file named Volumest.xml
38 * for later use, some are strictly runtime. 
39 * @author Markko Merzin, markko.merzin@ut.ee
40 */
41public class Settings {
42
43    /**
44     * Marks that grid is drawed with solid lines.
45     */
46    public static final int GRID_LINES = 0;
47    /**
48     * Marks that drid is drawed with right angles.
49     */
50    public static final int GRID_RIGHT_ANGLES = 1;
51    /**
52     * Marks that object is irregular.
53     */
54    public static final int IRREGULAR = 0;
55    /**
56     * Marks that object is ellipsoide-like.
57     */
58    public static final int REGULAR = 1;
59    /**
60     * Mnemonic for point activation.
61     */
62    public static final boolean ACTIVATE = true;
63    /**
64     * Mnemonic for point deactivation.
65     */
66    public static final boolean DEACTIVATE = false;
67    /**
68     * Allowed modifierkeys.
69     */
70    public static final int MODIFIER_KEYS = MouseEvent.ALT_DOWN_MASK | MouseEvent.CTRL_DOWN_MASK | MouseEvent.SHIFT_DOWN_MASK;
71    private static Settings instance = null;
72    private Properties runtimeprops;
73    private int objectType;
74    private double gridWidth;
75    private double sliceThickness;
76    private int numberOfSteps;
77
78
79    private Settings() {
80        readProperties();
81    }
82
83    protected static Settings getInstance() {
84        if (instance == null) {
85            instance = new Settings();
86        }
87        return instance;
88    }
89
90    public boolean isDontDisplayGrid() {
91        if (runtimeprops.getProperty("dontDisplayGrid").equals("0")) {
92            return false;
93        } else {
94            return true;
95        }
96    }
97
98    public void setDontDisplayGrid( boolean val ) {
99        if ( val == true ) {
00            runtimeprops.setProperty("dontDisplayGrid", "1");
01        } else {
02            runtimeprops.setProperty("dontDisplayGrid", "0");
03        }
04    }
05
06
07    
08    private void readProperties() {
09
10        // hardcoded default properties
11        Properties defaults = new Properties();
12
13        defaults.setProperty("gridColor", Integer.toString(Color.green.getRGB()));
14        defaults.setProperty("selectionColor", Integer.toString(Color.red.getRGB()));
15        defaults.setProperty("intersectionColor", Integer.toString(Color.yellow.getRGB()));
16        defaults.setProperty("gridType", Integer.toString(Settings.GRID_LINES));
17        defaults.setProperty("vertexLength", "100");
18        defaults.setProperty("gridLineThickness", "1");
19        defaults.setProperty("dragGranularity", "5");
20
21        defaults.setProperty("selectKeyMask", "0");
22        defaults.setProperty("selectMouseButton", Integer.toString(MouseEvent.BUTTON3));
23        defaults.setProperty("deselectKeyMask", Integer.toString(MouseEvent.CTRL_DOWN_MASK));
24        defaults.setProperty("deselectMouseButton", Integer.toString(MouseEvent.BUTTON3));
25
26        defaults.setProperty("intersectionKeyMask", "0");
27        defaults.setProperty("intersectionMouseButton", Integer.toString(MouseEvent.BUTTON2));
28        defaults.setProperty("deintersectionKeyMask", Integer.toString(MouseEvent.CTRL_DOWN_MASK));
29        defaults.setProperty("deintersectionMouseButton", Integer.toString(MouseEvent.BUTTON2));
30
31        defaults.setProperty("selectMarkKeyMask", "0");
32        defaults.setProperty("selectMarkMouseButton", Integer.toString(MouseEvent.BUTTON1));
33        defaults.setProperty("deselectDemarkKeyMask", Integer.toString(MouseEvent.CTRL_DOWN_MASK));
34        defaults.setProperty("deselectDemarkMouseButton", Integer.toString(MouseEvent.BUTTON1));
35        defaults.setProperty("dontDisplayGrid", "0");
36        defaults.setProperty("significantDigits", "5");
37        
38        defaults.setProperty("pilotCoefficent", "10");
39
40        String osName = "";
41        osName = System.getProperty("os.name");
42        String propsFile = "";
43
44        if (osName.indexOf("Windows", 0) > -1) {
45            String homeDir = System.getProperty("user.dir");
46            propsFile = homeDir + System.getProperty("file.separator") + "Volumest.xml";
47        } else {
48            String userHome = System.getProperty("user.home");
49            propsFile = userHome + System.getProperty("file.separator") + "Volumest.xml";
50        }
51
52
53        runtimeprops = new Properties(defaults);
54
55        File props = new File(propsFile);
56        if (props.exists()) {
57            try {
58                InputStream in = new FileInputStream(props);
59                runtimeprops.loadFromXML(in);
60            } catch (InvalidPropertiesFormatException ex) {
61                props.delete();
62            } catch (FileNotFoundException ex) {
63
64            } catch (IOException ex) {
65            }
66        }
67    }
68
69    protected void writeProperties() {
70        if (runtimeprops != null) {
71            String osName = "";
72            osName = System.getProperty("os.name");
73            String propsFile = "";
74
75            if (osName.indexOf("Windows", 0) > -1) {
76                String homeDir = System.getProperty("user.dir");
77                propsFile = homeDir + System.getProperty("file.separator") + "Volumest.xml";
78            } else {
79                String userHome = System.getProperty("user.home");
80                propsFile = userHome + System.getProperty("file.separator") + "Volumest.xml";
81            }
82
83            File props = new File(propsFile);
84            try {
85                OutputStream os = new FileOutputStream(props);
86                runtimeprops.storeToXML(os, "Volumest plugin properties. DO NOT EDIT!");
87            } catch (FileNotFoundException ex) {
88                // will not annoy user with windowed errors if writing fails.
89                ex.printStackTrace();
90            } catch (IOException ex) {
91                ex.printStackTrace();
92            }
93        }
94    }
95
96    public Color getGridColor() {
97        return Color.decode(runtimeprops.getProperty("gridColor"));
98    }
99
00    public void setGridColor(Color col) {
01        runtimeprops.setProperty("gridColor", Integer.toString(col.getRGB()));
02    }
03
04    public Color getSelectionColor() {
05        return Color.decode(runtimeprops.getProperty("selectionColor"));
06    }
07
08    public void setSelectionColor(Color col) {
09        runtimeprops.setProperty("selectionColor", Integer.toString(col.getRGB()));
10    }
11
12    public Color getIntersectionColor() {
13        return Color.decode(runtimeprops.getProperty("intersectionColor"));
14    }
15
16    public void setIntersectionColor(Color col) {
17        runtimeprops.setProperty("intersectionColor", Integer.toString(col.getRGB()));
18    }
19
20    public int getGridType() {
21        return Integer.parseInt(runtimeprops.getProperty("gridType"));
22    }
23
24    public void setGridType(int type) {
25        runtimeprops.setProperty("gridType", Integer.toString(type));
26    }
27
28    public int getVertexLength() {
29        return Integer.parseInt(runtimeprops.getProperty("vertexLength"));
30    }
31
32    public void setVertexLength(int len) {
33        runtimeprops.setProperty("vertexLength", Integer.toString(len));
34    }
35
36    public int getGridLineThickness() {
37        return Integer.parseInt(runtimeprops.getProperty("gridLineThickness"));
38    }
39
40    public void setGridLineThickness(int val) {
41        runtimeprops.setProperty("gridLineThickness", Integer.toString(val));
42    }
43
44    public double getGridWidth() {
45        return gridWidth;
46    }
47
48    public void setGridWidth(double gridWidth) {
49        this.gridWidth = gridWidth;
50    }
51
52    public int getDragGranularity() {
53        return Integer.parseInt(runtimeprops.getProperty("dragGranularity"));
54    }
55
56    public void setDragGranularity(int gran) {
57        runtimeprops.setProperty("dragGranularity", Integer.toString(gran));
58    }
59
60    public double getSliceThickness() {
61        return sliceThickness;
62    }
63
64    public void setSliceThickness(double sliceThickness) {
65        this.sliceThickness = sliceThickness;
66    }
67
68    public int getObjectType() {
69        return objectType;
70    }
71
72    public void setObjectType(int objectType) {
73        this.objectType = objectType;
74    }
75
76    public int getNumberOfSteps() {
77        return numberOfSteps;
78    }
79
80    public void setNumberOfSteps(int numberOfSteps) {
81        this.numberOfSteps = numberOfSteps;
82    }
83
84    public int getSelectKeyMask() {
85        return Integer.parseInt(runtimeprops.getProperty("selectKeyMask"));
86    }
87
88    public void setSelectKeyMask(int mask) {
89        runtimeprops.setProperty("selectKeyMask", Integer.toString(mask));
90    }
91
92    public int getSelectMouseButton() {
93        return Integer.parseInt(runtimeprops.getProperty("selectMouseButton"));
94    }
95
96    public void setSelectMouseButton(int button) {
97        runtimeprops.setProperty("selectMouseButton", Integer.toString(button));
98    }
99
00    public int getDeselectKeyMask() {
01        return Integer.parseInt(runtimeprops.getProperty("deselectKeyMask"));
02    }
03
04    public void setDeselectKeyMask(int mask) {
05        runtimeprops.setProperty("deselectKeyMask", Integer.toString(mask));
06    }
07
08    public int getDeselectMouseButton() {
09        return Integer.parseInt(runtimeprops.getProperty("deselectMouseButton"));
10    }
11
12    public void setDeselectMouseButton(int button) {
13        runtimeprops.setProperty("deselectMouseButton", Integer.toString(button));
14    }
15
16    public int getIntersectionKeyMask() {
17        return Integer.parseInt(runtimeprops.getProperty("intersectionKeyMask"));
18    }
19
20    public void setIntersectionKeyMask(int mask) {
21        runtimeprops.setProperty("intersectionKeyMask", Integer.toString(mask));
22    }
23
24    public int getIntersectionMouseButton() {
25        return Integer.parseInt(runtimeprops.getProperty("intersectionMouseButton"));
26    }
27
28    public void setIntersectionMouseButton(int button) {
29        runtimeprops.setProperty("intersectionMouseButton", Integer.toString(button));
30    }
31
32    public int getDeintersectionKeyMask() {
33        return Integer.parseInt(runtimeprops.getProperty("deintersectionKeyMask"));
34    }
35
36    public void setDeintersectionKeyMask(int mask) {
37        runtimeprops.setProperty("deintersectionKeyMask", Integer.toString(mask));
38    }
39
40    public int getDeintersectionMouseButton() {
41        return Integer.parseInt(runtimeprops.getProperty("deintersectionMouseButton"));
42    }
43
44    public void setDeintersectionMouseButton(int button) {
45        runtimeprops.setProperty("deintersectionMouseButton", Integer.toString(button));
46    }
47
48    public int getSelectMarkKeyMask() {
49        return Integer.parseInt(runtimeprops.getProperty("selectMarkKeyMask"));
50    }
51
52    public void setSelectMarkKeyMask(int mask) {
53        runtimeprops.setProperty("selectMarkKeyMask", Integer.toString(mask));
54    }
55
56    public int getSelectMarkMouseButton() {
57        return Integer.parseInt(runtimeprops.getProperty("selectMarkMouseButton"));
58    }
59
60    public void setSelectMarkMouseButton(int button) {
61        runtimeprops.setProperty("selectMarkMouseButton", Integer.toString(button));
62    }
63
64    public int getDeselectDemarkKeyMask() {
65        return Integer.parseInt(runtimeprops.getProperty("deselectDemarkKeyMask"));
66    }
67
68    public void setDeselectDemarkKeyMask(int mask) {
69        runtimeprops.setProperty("deselectDemarkKeyMask", Integer.toString(mask));
70    }
71
72    public int getDeselectDemarkMouseButton() {
73        return Integer.parseInt(runtimeprops.getProperty("deselectDemarkMouseButton"));
74    }
75
76    public void setDeselectDemarkMouseButton(int button) {
77        runtimeprops.setProperty("deselectDemarkMouseButton", Integer.toString(button));
78    }
79    
80    public int getSignificantDigits() {
81        return Integer.parseInt(runtimeprops.getProperty("significantDigits"));
82    }
83    
84    public void setSignificantDigits(int intValue) {
85        runtimeprops.setProperty("significantDigits", Integer.toString(intValue));
86    }
87    
88    public int getPilotCoefficent() {
89        return Integer.parseInt(runtimeprops.getProperty("pilotCoefficent"));
90    }
91    
92    public void setPilotCoefficent(int intValue) {
93        runtimeprops.setProperty("pilotCoefficent", Integer.toString(intValue));
94    }
95}
96