Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / panels / ImageSizePanel.java @ 34294

History | View | Annotate | Download (6.33 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.app.gui.panels;
42

    
43
import java.awt.BorderLayout;
44
import java.awt.Color;
45
import java.awt.Dimension;
46
import java.awt.Graphics;
47
import java.awt.event.ActionEvent;
48
import java.awt.event.ActionListener;
49
import java.awt.event.FocusEvent;
50
import java.awt.event.FocusListener;
51
import java.awt.event.KeyEvent;
52
import java.awt.event.KeyListener;
53
import java.awt.event.MouseEvent;
54
import java.awt.event.MouseListener;
55
import java.util.ArrayList;
56

    
57
import javax.swing.Icon;
58
import javax.swing.JPanel;
59
import javax.swing.JToggleButton;
60

    
61
import org.gvsig.andami.PluginServices;
62
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
63
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
64
import org.gvsig.i18n.Messages;
65

    
66

    
67
/**
68
 * @author jaume dominguez faus - jaume.dominguez@iver.es
69
 */
70
public class ImageSizePanel extends JPanel implements KeyListener, MouseListener, ActionListener, FocusListener{
71
        /**
72
         * 
73
         */
74
        private static final long serialVersionUID = 4810686805894198593L;
75
        private static final Icon iconSelected = PluginServices.getIconTheme().get("chain");
76
        private static final Icon iconUnselected = PluginServices.getIconTheme().get("broken-chain");
77

    
78
        private JIncrementalNumberField widthTxt;
79
        private JIncrementalNumberField heightTxt;
80
        private double width, height;
81
        private JToggleButton lock;
82
        private double ratio;
83
        private boolean performing;
84
        private boolean locked = true;
85
        private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();
86
        
87
        public ImageSizePanel(){
88
                super();
89
                initialize();
90
        }
91

    
92

    
93
        /**
94
         * This method initializes this
95
         *
96
         */
97
        private void initialize() {
98
                final GridBagLayoutPanel aux = new GridBagLayoutPanel();
99
                setLayout(new BorderLayout());
100
                aux.addComponent(Messages.getText("width")+":",
101
                                widthTxt  = new JIncrementalNumberField(null, 5, 0, Double.MAX_VALUE, 1));
102
                aux.addComponent(Messages.getText("height")+":",
103
                                heightTxt = new JIncrementalNumberField(null, 5, 0, Double.MAX_VALUE, 1));
104
                setLayout(new BorderLayout());
105
                add(aux, BorderLayout.CENTER);
106
                JPanel lockPanel = new JPanel(null) {
107
                        private static final long serialVersionUID = -415551033800811412L;
108
                        protected void paintComponent(Graphics g) {
109
                                int y1 = widthTxt.getY() + widthTxt.getHeight()/2-3;
110
                                int y2 = heightTxt.getY() + heightTxt.getHeight()/2+3;
111
                                g.setColor(Color.DARK_GRAY);
112
                                g.drawLine(2, y1, getWidth()/2+2, y1);
113
                                g.drawLine(getWidth()/2+2, y1, getWidth()/2+2, y2);
114
                                g.drawLine(2, y2, getWidth()/2+2, y2);
115
                                lock.setBounds(3, widthTxt.getY()+13, getWidth()-2, 22);
116
                        }
117
                };
118
                lockPanel.setPreferredSize(new Dimension(20, 20));
119
                lock = new JToggleButton() {
120
                        private static final long serialVersionUID = 1668046192113822412L;
121
                        protected void paintComponent(Graphics g) {
122
                                setIcon(isSelected() ? iconSelected : iconUnselected);
123
                                super.paintComponent(g);
124
                        }
125
                };
126
                lock.setSelected(locked);
127
                lockPanel.add(lock);
128
                add(lockPanel, BorderLayout.EAST);
129
                widthTxt.addKeyListener(this);
130
                widthTxt.addMouseListener(this);
131
                widthTxt.addActionListener(this);
132
                widthTxt.addFocusListener(this);
133
                heightTxt.addKeyListener(this);
134
                heightTxt.addMouseListener(this);
135
                heightTxt.addActionListener(this);
136
                heightTxt.addFocusListener(this);
137

    
138
                lock.addMouseListener(this);
139
        }
140

    
141
        public void setImageSize(double width, double height) {
142
                this.width = width;
143
                widthTxt.setDouble(width);
144
                this.height = height;
145
                heightTxt.setDouble(height);
146
                this.ratio = width / height;
147
        }
148
        
149
        public void setImageSize(Dimension sz) {
150
                setImageSize(sz.getWidth(), sz.getHeight());
151
        }
152
        
153

    
154
        public double[] getImageDimension() {
155
                double[] d = new double[2];
156
                d[0] = width;
157
                d[1] = height;
158
                return d;
159
        }
160

    
161
        public void addActionListener(ActionListener l) {
162
                listeners.add(l);
163
        }
164

    
165
        public void keyPressed(KeyEvent e) { doIt(); }
166
        public void keyReleased(KeyEvent e) { doIt(); }
167
        public void keyTyped(KeyEvent e) { doIt(); }
168
        public void mouseClicked(MouseEvent e) { e.consume(); doIt(); }
169
        public void actionPerformed(ActionEvent e) { doIt(); }
170

    
171
        private void doIt() {
172
                if (!performing) {
173
                        performing = true;
174
                        if (locked != lock.isSelected()) {
175
                                locked = lock.isSelected();
176
                                ratio = widthTxt.getDouble() / heightTxt.getDouble();
177
                        } else if (locked && width != widthTxt.getDouble()){
178
                                width = widthTxt.getDouble();
179
                                height = width/ratio;
180
                                height = Math.round(height*100)/100;
181
                                heightTxt.setDouble(height);
182
                                fireActionPerformed();
183
                        } else if (locked && height != heightTxt.getDouble()) {
184
                                height = heightTxt.getDouble();
185
                                width = height*ratio;
186
                                width = Math.round(width*100)/100;
187
                                widthTxt.setDouble(width);
188
                                fireActionPerformed();
189

    
190
                        }
191

    
192
                        performing = false;
193
                }
194
        }
195

    
196

    
197
        private void fireActionPerformed() {
198
                for (int i = 0; i < listeners.size(); i++) {
199
                        ((ActionListener) listeners.get(i)).actionPerformed(
200
                                        new ActionEvent(this, 0, "IMAGE_RESIZED"));
201
                }
202
        }
203

    
204

    
205
        public void mouseEntered(MouseEvent e) { /* nothing */ }
206
        public void mouseExited(MouseEvent e) { /* nothing */ }
207
        public void mousePressed(MouseEvent e) {  /* nothing */ }
208
        public void mouseReleased(MouseEvent e) {  /* nothing */ }
209

    
210

    
211
        public void focusGained(FocusEvent arg0) {
212
                // TODO Auto-generated method stub
213
                
214
        }
215

    
216

    
217
        public void focusLost(FocusEvent arg0) {
218
                doIt();
219
        }
220
}
221