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 @ 230

History | View | Annotate | Download (7.92 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
                                        System.out.println("focus lost " + e.toString());
131
                    if (txtRotation.getText().endsWith(".")) {
132
                        return;
133
                    }
134
                    try {
135
                        setRotation(Double.parseDouble(txtRotation.getText()));
136
                    } catch (NumberFormatException e1) {
137
                            String oldText = parsedText;
138
                            parsedText = txtRotation.getText();
139
                            if (!parsedText.equals(oldText)) { // don't show more than once for the same string
140
                                    JOptionPane.showMessageDialog(
141
                                                    (Component) PluginServices.getMainFrame(),
142
                                                    PluginServices.getText(this, "numero_incorrecto"));
143
                            }
144
                    }
145
                    getPRotationView().repaint();                                }
146
                                
147
                                public void focusGained(FocusEvent e) {
148
                                        System.out.println("focus gained" + e.toString());
149
                                }
150
                        });
151
            txtRotation.addKeyListener(new java.awt.event.KeyAdapter() {
152

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

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

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

    
213
    /**
214
     * @param args
215
     */
216
    public static void main(String[] args) {
217

    
218
    }
219

    
220
    /**
221
     * This is the default constructor
222
     */
223
    public JPRotation() {
224
        super();
225
        initialize();
226
    }
227

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

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