Subversion Repositories public

Rev

Rev 85 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
85 luk 1
/*
2
 * ICConfig.java - jIncron configuration management
3
 *
4
 * Copyright (c) 2006 Lukas Jelinek, http://www.aiken.cz
5
 *
6
 * ==========================================================================
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License Version 2 as
10
 * published by the Free Software Foundation.
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
 * ==========================================================================
22
 */
23
 
24
package cz.aiken.util.jincron;
25
 
26
import java.io.*;
27
import java.util.*;
28
import java.awt.*;
29
import javax.swing.JOptionPane;
30
 
31
/**
32
 *
33
 * @author luk
34
 */
35
public class ICConfig {
36
 
37
    private static final String cfgFile = ".jincron.xml";
38
    private static Properties props = new Properties();
39
 
40
    static {
41
        init();
42
    }
43
 
44
    public static void load() {
45
        File f = new File(System.getProperty("user.home"), cfgFile);
46
        if (!f.exists())
47
            return;
48
 
49
        try {
50
            BufferedInputStream is = new BufferedInputStream(new FileInputStream(f));
51
            try {
52
                props.loadFromXML(is);
53
            } finally {
54
                is.close();
55
            }
56
        } catch (Exception e) {
57
            JOptionPane.showMessageDialog(null, "Cannot load configuration: " + e.getMessage(),
58
                    "Error", JOptionPane.ERROR_MESSAGE);
59
        }
60
    }
61
 
62
    public static void save() {
63
        File f = new File(System.getProperty("user.home"), cfgFile);
64
 
65
        try {
66
            BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(f));
67
            try {
68
                props.storeToXML(os, "jIncron configuration file");
69
            } finally {
70
                os.close();
71
            }
72
        } catch (Exception e) {
73
            JOptionPane.showMessageDialog(null, "Cannot save configuration: " + e.getMessage(),
74
                    "Error", JOptionPane.ERROR_MESSAGE);
75
        }
76
    }
77
 
78
    public static void init() {
79
        props.setProperty("window.width", Integer.toString(500));
80
        props.setProperty("window.height", Integer.toString(300));
81
        props.setProperty("window.x", Integer.toString(100));
82
        props.setProperty("window.y", Integer.toString(100));
83
    }
84
 
85
 
86
 
87
    public static int getInt(String key, int dflt) {
88
        try {
89
            return Integer.parseInt(props.getProperty(key));
90
        } catch (NumberFormatException e) {
91
            return dflt;
92
        }
93
    }
94
 
95
    public static void setInt(String key, int val) {
96
        props.setProperty(key, Integer.toString(val));
97
    }
98
 
99
 
100
 
101
    public static Dimension getWindowSize() {
102
        return new Dimension(getInt("window.width", 0), getInt("window.height", 0));
103
    }
104
 
105
    public static Point getWindowLocation() {
106
        return new Point(getInt("window.x", 0), getInt("window.y", 0));
107
    }
108
 
109
 
110
 
111
    public static void setWindowSize(Dimension d) {
112
        setInt("window.width", d.width);
113
        setInt("window.height", d.height);
114
    }
115
 
116
    public static void setWindowLocation(Point p) {
117
        setInt("window.x", p.x);
118
        setInt("window.y", p.y);
119
    }
120
}