Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / gui / JPRotation.java @ 233

History | View | Annotate | Download (7.75 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.app.project.documents.layout.fframes.gui;
23

    
24
import java.awt.BorderLayout;
25
import java.awt.Component;
26
import java.awt.event.FocusEvent;
27
import java.awt.event.FocusListener;
28
import java.awt.event.KeyEvent;
29

    
30
import javax.swing.JButton;
31
import javax.swing.JOptionPane;
32
import javax.swing.JPanel;
33
import javax.swing.JTextField;
34

    
35
import org.gvsig.andami.PluginServices;
36

    
37
public class JPRotation extends JPanel {
38

    
39
    private static final long serialVersionUID = 5537314374490402138L;
40
    private JPRotationView pRotationView = null;
41
    private JPanel pButtons = null;
42
    private JButton bLeft = null;
43
    private JTextField txtRotation = null;
44
    private String parsedText = null;
45
    private JButton bRight = null;
46

    
47
    public void setRotation(double rot) {
48
        getPRotationView().setRotation(rot);
49
        setTextRotation(rot);
50
    }
51

    
52
    private void setTextRotation(double d) {
53
        String s = String.valueOf(d);
54
        if (s.endsWith(".0")) {
55
            txtRotation.setText(s.substring(0, s.length() - 2));
56
        } else {
57
            txtRotation.setText(s);
58
        }
59
    }
60

    
61
    public double getRotation() {
62
        return getPRotationView().getRotation();
63
    }
64

    
65
    /**
66
     * This method initializes pRotationView
67
     * 
68
     * @return javax.swing.JPanel
69
     */
70
    private JPRotationView getPRotationView() {
71
        if (pRotationView == null) {
72
            pRotationView = new JPRotationView();
73
            pRotationView.setForeground(java.awt.Color.black);
74
            pRotationView.setBackground(java.awt.SystemColor.controlShadow);
75
            pRotationView.setPreferredSize(new java.awt.Dimension(80, 50));
76
        }
77
        return pRotationView;
78
    }
79

    
80
    /**
81
     * This method initializes pButtons
82
     * 
83
     * @return javax.swing.JPanel
84
     */
85
    private JPanel getPButtons() {
86
        if (pButtons == null) {
87
            pButtons = new JPanel();
88
            pButtons.add(getBLeft(), null);
89
            pButtons.add(getTxtRotation(), null);
90
            pButtons.add(getBRight(), null);
91
        }
92
        return pButtons;
93
    }
94

    
95
    /**
96
     * This method initializes bLeft
97
     * 
98
     * @return javax.swing.JButton
99
     */
100
    private JButton getBLeft() {
101
        if (bLeft == null) {
102
            bLeft = new JButton();
103
            bLeft.setPreferredSize(new java.awt.Dimension(24, 24));
104
            bLeft.setIcon(PluginServices.getIconTheme().get(
105
                "left-rotation-icon"));
106
            bLeft.addActionListener(new java.awt.event.ActionListener() {
107

    
108
                public void actionPerformed(java.awt.event.ActionEvent e) {
109
                    setRotation(Double.parseDouble(txtRotation.getText()) + 1);
110
                    getTxtRotation().setText(txtRotation.getText());
111
                    getPRotationView().repaint();
112
                }
113
            });
114
        }
115
        return bLeft;
116
    }
117

    
118
    /**
119
     * This method initializes txtRotation
120
     * 
121
     * @return javax.swing.JTextField
122
     */
123
    private JTextField getTxtRotation() {
124
        if (txtRotation == null) {
125
            txtRotation = new JTextField();
126
            txtRotation.setPreferredSize(new java.awt.Dimension(45, 24));
127
            txtRotation.addFocusListener(new FocusListener() {
128
                                
129
                                public void focusLost(FocusEvent e) {
130
                    if (txtRotation.getText().endsWith(".")) {
131
                        return;
132
                    }
133
                    try {
134
                        setRotation(Double.parseDouble(txtRotation.getText()));
135
                    } catch (NumberFormatException e1) {
136
                            String oldText = parsedText;
137
                            parsedText = txtRotation.getText();
138
                            if (!parsedText.equals(oldText)) { // don't show more than once for the same string
139
                                    JOptionPane.showMessageDialog(
140
                                                    (Component) PluginServices.getMainFrame(),
141
                                                    PluginServices.getText(this, "numero_incorrecto"));
142
                            }
143
                    }
144
                    getPRotationView().repaint();                                }
145
                                
146
                                public void focusGained(FocusEvent e) {
147
                                }
148
                        });
149
            txtRotation.addKeyListener(new java.awt.event.KeyAdapter() {
150

    
151
                public void keyReleased(java.awt.event.KeyEvent e) {
152
                        String text = txtRotation.getText();
153
                        if (text.endsWith(".")) {
154
                                return;
155
                        }
156
                                        Double value = getDouble(text);
157
                                        if (value!=null) {
158
                                                setRotation(value);
159
                                                getPRotationView().repaint();
160
                                        }
161
                                        else if (e.getKeyCode()==KeyEvent.VK_ENTER) {
162
                            String oldText = parsedText;
163
                            parsedText = txtRotation.getText();
164
                            if (!parsedText.equals(oldText)) { // don't show more than once for the same string
165
                                    JOptionPane.showMessageDialog(
166
                                                    (Component) PluginServices.getMainFrame(),
167
                                                    PluginServices.getText(this, "numero_incorrecto"));
168
                            }
169
                                        }
170
                }
171
            });
172
            setTextRotation(getRotation());
173
        }
174
        return txtRotation;
175
    }
176
    
177
    protected Double getDouble(String strValue) {
178
                // allow comma separated decimal values
179
                String text = strValue.replaceAll(",", ".");
180
                try {
181
                        return Double.parseDouble(text);
182
                } catch (NumberFormatException e1) {
183
                }
184
                return null;
185
    }
186

    
187
    /**
188
     * This method initializes bRight
189
     * 
190
     * @return javax.swing.JButton
191
     */
192
    private JButton getBRight() {
193
        if (bRight == null) {
194
            bRight = new JButton();
195
            bRight.setPreferredSize(new java.awt.Dimension(24, 24));
196
            bRight.setIcon(PluginServices.getIconTheme().get(
197
                "right-rotation-icon"));
198
            bRight.addActionListener(new java.awt.event.ActionListener() {
199

    
200
                public void actionPerformed(java.awt.event.ActionEvent e) {
201
                    setRotation(Double.parseDouble(txtRotation.getText()) - 1);
202
                    getTxtRotation().setText(txtRotation.getText());
203
                    getPRotationView().repaint();
204
                }
205
            });
206
        }
207
        return bRight;
208
    }
209

    
210
    /**
211
     * @param args
212
     */
213
    public static void main(String[] args) {
214

    
215
    }
216

    
217
    /**
218
     * This is the default constructor
219
     */
220
    public JPRotation() {
221
        super();
222
        initialize();
223
    }
224

    
225
    /**
226
     * This method initializes this
227
     * 
228
     * @return void
229
     */
230
    private void initialize() {
231
        this.setLayout(new BorderLayout());
232
        this.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
233
            PluginServices.getText(this, "grados"),
234
            javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
235
            javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
236
        this.add(getPRotationView(), java.awt.BorderLayout.NORTH);
237
        this.add(getPButtons(), java.awt.BorderLayout.SOUTH);
238
    }
239

    
240
} // @jve:decl-index=0:visual-constraint="10,26"