Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libCorePlugin / src / com / iver / core / preferences / general / ResolutionPage.java @ 11021

History | View | Annotate | Download (5.71 KB)

1
package com.iver.core.preferences.general;
2

    
3
import java.awt.BasicStroke;
4
import java.awt.Color;
5
import java.awt.Dimension;
6
import java.awt.Graphics;
7
import java.awt.Graphics2D;
8
import java.awt.Toolkit;
9
import java.awt.event.ActionEvent;
10
import java.awt.event.ActionListener;
11
import java.text.NumberFormat;
12
import java.util.prefs.Preferences;
13

    
14
import javax.swing.BorderFactory;
15
import javax.swing.ImageIcon;
16
import javax.swing.JComboBox;
17
import javax.swing.JLabel;
18
import javax.swing.JPanel;
19
import javax.swing.JTextField;
20
import javax.swing.border.Border;
21

    
22
import org.gvsig.gui.beans.swing.JButton;
23

    
24
import com.iver.andami.PluginServices;
25
import com.iver.andami.preferences.AbstractPreferencePage;
26
import com.iver.andami.preferences.StoreException;
27

    
28

    
29
/**
30
 * Page to calculate correctly all the scales of screen.  In it we introduce the values of our screen just as we see it.
31
 *
32
 * @author Vicente Caballero Navarro
33
 */
34
public class ResolutionPage extends AbstractPreferencePage {
35
        private static Preferences prefs = Preferences.userRoot().node( "gvsig.configuration.screen" );
36
        private ImageIcon icon;
37
        private JPanel pTestMeasure;
38
        private JTextField txtResolution;
39
        private JTextField txtMeasure;
40
        private JComboBox cmbUnits;
41
        private JButton btnRefresh;
42
        /**
43
     * This is the default constructor
44
     */
45
    public ResolutionPage() {
46
        super();
47
        setParentID(GeneralPage.id);
48
        icon = new ImageIcon(this.getClass().getClassLoader().getResource("images/resolution.png"));
49
        addComponent(PluginServices.getText(this, "resolution") + ":",
50
                            txtResolution = new JTextField("", 15));
51

    
52
        pTestMeasure = new TestMeasurePanel();
53
        addComponent(pTestMeasure);
54
        addComponent(new JLabel(PluginServices.getText(this,"the_length_of_the_line_above_is")+":"));
55
        cmbUnits=new JComboBox();
56
        cmbUnits.addItem(PluginServices.getText(this,"centimeters"));
57
        cmbUnits.addItem(PluginServices.getText(this,"inches"));
58
        cmbUnits.addActionListener(new ActionListener() {
59
                        public void actionPerformed(ActionEvent arg0) {
60
                                /*double d=Double.parseDouble(txtMeasure.getText().replace(',','.'));
61
                                if (cmbUnits.getSelectedIndex()==0) {
62
                                        txtResolution.setText(String.valueOf((int)((210*2.45)/d)));
63
                                }else {
64
                                        txtResolution.setText(String.valueOf((int)(210/d)));
65
                                }
66
*/
67
                        }
68
        });
69
        txtMeasure=new JTextField();
70
        addComponent(txtMeasure,cmbUnits);
71
        btnRefresh=new JButton(PluginServices.getText(this,"button.resolution.calculate"));
72
        btnRefresh.addActionListener(new ActionListener() {
73
                        public void actionPerformed(ActionEvent arg0) {
74

    
75
                                double d=Double.parseDouble(txtMeasure.getText().replace(',','.'));
76
                                if (cmbUnits.getSelectedIndex()==0) {
77
                                        txtResolution.setText(String.valueOf((int)((210*2.54)/d)));
78
                                }else {
79
                                        txtResolution.setText(String.valueOf((int)(210/d)));
80
                                }
81

    
82

    
83
                        }
84

    
85
        });
86
        addComponent(btnRefresh);
87

    
88
        initialize();
89
    }
90
    class TestMeasurePanel extends JPanel{
91

    
92
            public TestMeasurePanel() {
93
                    setPreferredSize(new Dimension(250,60));
94
            Border border=BorderFactory.createTitledBorder(PluginServices.getText(this, "test_measure"));
95
            setBorder(border);
96
            }
97
                protected void paintComponent(Graphics g) {
98
                        super.paintComponent(g);
99
                        ((Graphics2D)g).setStroke(new BasicStroke(2));
100
                        g.setColor(Color.black);
101
                        g.drawLine(20,30,230,30);
102
                        g.drawLine(20,20,20,40);
103
                        g.drawLine(230,20,230,40);
104
                }
105

    
106
    }
107
    /**
108
     * This method initializes this
109
     */
110
    private void initialize() {
111
        this.setSize(394, 248);
112
    }
113

    
114
    /**
115
     * DOCUMENT ME!
116
     *
117
     * @throws StoreException DOCUMENT ME!
118
     */
119
    public void storeValues() throws StoreException {
120
       int dpi=Integer.parseInt(txtResolution.getText());
121
       prefs.putInt("dpi",dpi);
122
    }
123

    
124
    /**
125
     * DOCUMENT ME!
126
     */
127
    public void setChangesApplied() {
128
            setChanged(false);
129
    }
130

    
131
    /**
132
     * DOCUMENT ME!
133
     *
134
     * @return DOCUMENT ME!
135
     */
136
    public String getID() {
137
            return this.getClass().getName();
138
    }
139

    
140
    /**
141
     * DOCUMENT ME!
142
     *
143
     * @return DOCUMENT ME!
144
     */
145
    public String getTitle() {
146
            return PluginServices.getText(this, "options.configuration.screen");
147
    }
148

    
149
    /**
150
     * DOCUMENT ME!
151
     *
152
     * @return DOCUMENT ME!
153
     */
154
    public JPanel getPanel() {
155
       return this;
156
    }
157

    
158
    /**
159
     * DOCUMENT ME!
160
     */
161
    public void initializeValues() {
162
            Toolkit kit = Toolkit.getDefaultToolkit();
163
            double dpi = kit.getScreenResolution();
164
            int resDPI=prefs.getInt("dpi",(int)dpi);
165

    
166
                txtResolution.setText(String.valueOf(resDPI));
167
                txtMeasure.setText(String.valueOf(format(210*2.54/((double)resDPI))));
168
                cmbUnits.setSelectedIndex(0);
169
        }
170

    
171
    /**
172
     * DOCUMENT ME!
173
     */
174
    public void initializeDefaults() {
175
            Toolkit kit = Toolkit.getDefaultToolkit();
176
                int dpi = kit.getScreenResolution();
177
                txtResolution.setText(String.valueOf(dpi));
178
                txtMeasure.setText(String.valueOf(format(210*2.45/dpi)));
179
                cmbUnits.setSelectedIndex(0);
180
    }
181

    
182
    /**
183
     * DOCUMENT ME!
184
     *
185
     * @return DOCUMENT ME!
186
     */
187
    public ImageIcon getIcon() {
188
        return icon;
189
    }
190

    
191
    /**
192
     * DOCUMENT ME!
193
     *
194
     * @return DOCUMENT ME!
195
     */
196
    public boolean isValueChanged() {
197
            return super.hasChanged();
198
    }
199
    /**
200
     * DOCUMENT ME!
201
     *
202
     * @param d DOCUMENT ME!
203
     *
204
     * @return DOCUMENT ME!
205
     */
206
    public String format(double d) {
207
        NumberFormat nf = NumberFormat.getInstance();
208

    
209
        if ((d % (long) d) != 0) {
210
            nf.setMaximumFractionDigits(2);
211
        } else {
212
            nf.setMaximumFractionDigits(0);
213
        }
214

    
215
        return nf.format(d); //(Double.valueOf(s).doubleValue());
216
    }
217
}  //  @jve:decl-index=0:visual-constraint="10,10"