Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / panels / ImageSizePanel.java @ 34002

History | View | Annotate | Download (6.25 KB)

1 14821 jmvivo
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2 11559 jaume
 *
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 14821 jmvivo
 *   Av. Blasco Ib��ez, 50
24 11559 jaume
 *   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 29596 jpiera
package org.gvsig.app.gui.panels;
42 11559 jaume
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 29986 vcaballero
import java.awt.event.FocusEvent;
50
import java.awt.event.FocusListener;
51 11559 jaume
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 29596 jpiera
import org.gvsig.andami.PluginServices;
62 11559 jaume
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
63 11734 jaume
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
64 11559 jaume
65
66
/**
67
 * @author jaume dominguez faus - jaume.dominguez@iver.es
68
 */
69 29986 vcaballero
public class ImageSizePanel extends JPanel implements KeyListener, MouseListener, ActionListener, FocusListener{
70 14821 jmvivo
        private static final Icon iconSelected = PluginServices.getIconTheme().get("chain");
71
        private static final Icon iconUnselected = PluginServices.getIconTheme().get("broken-chain");
72 11559 jaume
73 11734 jaume
        private JIncrementalNumberField widthTxt;
74
        private JIncrementalNumberField heightTxt;
75 11559 jaume
        private double width, height;
76
        private JToggleButton lock;
77
        private double ratio;
78
        private boolean performing;
79
        private boolean locked = true;
80 18623 jdominguez
        private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();
81
82 11559 jaume
        public ImageSizePanel(){
83
                super();
84
                initialize();
85
        }
86
87
88
        /**
89
         * This method initializes this
90
         *
91
         */
92
        private void initialize() {
93
                final GridBagLayoutPanel aux = new GridBagLayoutPanel();
94
                setLayout(new BorderLayout());
95
                aux.addComponent(PluginServices.getText(this, "width")+":",
96 11734 jaume
                                widthTxt  = new JIncrementalNumberField(null, 5, 0, Double.MAX_VALUE, 1));
97 11559 jaume
                aux.addComponent(PluginServices.getText(this, "height")+":",
98 11734 jaume
                                heightTxt = new JIncrementalNumberField(null, 5, 0, Double.MAX_VALUE, 1));
99 11559 jaume
                setLayout(new BorderLayout());
100
                add(aux, BorderLayout.CENTER);
101
                JPanel lockPanel = new JPanel(null) {
102
                        private static final long serialVersionUID = -415551033800811412L;
103
                        protected void paintComponent(Graphics g) {
104
                                int y1 = widthTxt.getY() + widthTxt.getHeight()/2-3;
105
                                int y2 = heightTxt.getY() + heightTxt.getHeight()/2+3;
106
                                g.setColor(Color.DARK_GRAY);
107
                                g.drawLine(2, y1, getWidth()/2+2, y1);
108
                                g.drawLine(getWidth()/2+2, y1, getWidth()/2+2, y2);
109
                                g.drawLine(2, y2, getWidth()/2+2, y2);
110
                                lock.setBounds(3, widthTxt.getY()+13, getWidth()-2, 22);
111
                        }
112
                };
113
                lockPanel.setPreferredSize(new Dimension(20, 20));
114
                lock = new JToggleButton() {
115
                        private static final long serialVersionUID = 1668046192113822412L;
116
                        protected void paintComponent(Graphics g) {
117
                                setIcon(isSelected() ? iconSelected : iconUnselected);
118
                                super.paintComponent(g);
119
                        }
120
                };
121
                lock.setSelected(locked);
122
                lockPanel.add(lock);
123
                add(lockPanel, BorderLayout.EAST);
124
                widthTxt.addKeyListener(this);
125
                widthTxt.addMouseListener(this);
126
                widthTxt.addActionListener(this);
127 29986 vcaballero
                widthTxt.addFocusListener(this);
128 11559 jaume
                heightTxt.addKeyListener(this);
129
                heightTxt.addMouseListener(this);
130
                heightTxt.addActionListener(this);
131 29986 vcaballero
                heightTxt.addFocusListener(this);
132 11559 jaume
133
                lock.addMouseListener(this);
134
        }
135
136 18623 jdominguez
        public void setImageSize(double width, double height) {
137 11559 jaume
                this.width = width;
138
                widthTxt.setDouble(width);
139
                this.height = height;
140
                heightTxt.setDouble(height);
141
                this.ratio = width / height;
142
        }
143 18623 jdominguez
144
        public void setImageSize(Dimension sz) {
145
                setImageSize(sz.getWidth(), sz.getHeight());
146
        }
147
148 11559 jaume
149
        public double[] getImageDimension() {
150
                double[] d = new double[2];
151
                d[0] = width;
152
                d[1] = height;
153
                return d;
154
        }
155
156
        public void addActionListener(ActionListener l) {
157 18623 jdominguez
                listeners.add(l);
158 11559 jaume
        }
159
160
        public void keyPressed(KeyEvent e) { doIt(); }
161
        public void keyReleased(KeyEvent e) { doIt(); }
162
        public void keyTyped(KeyEvent e) { doIt(); }
163
        public void mouseClicked(MouseEvent e) { e.consume(); doIt(); }
164
        public void actionPerformed(ActionEvent e) { doIt(); }
165
166
        private void doIt() {
167
                if (!performing) {
168
                        performing = true;
169
                        if (locked != lock.isSelected()) {
170
                                locked = lock.isSelected();
171
                                ratio = widthTxt.getDouble() / heightTxt.getDouble();
172
                        } else if (locked && width != widthTxt.getDouble()){
173
                                width = widthTxt.getDouble();
174
                                height = width/ratio;
175
                                height = Math.round(height*100)/100;
176
                                heightTxt.setDouble(height);
177
                                fireActionPerformed();
178 29986 vcaballero
                        } else if (locked && height != heightTxt.getDouble()) {
179 11559 jaume
                                height = heightTxt.getDouble();
180
                                width = height*ratio;
181
                                width = Math.round(width*100)/100;
182
                                widthTxt.setDouble(width);
183
                                fireActionPerformed();
184
185
                        }
186
187
                        performing = false;
188
                }
189
        }
190
191
192
        private void fireActionPerformed() {
193
                for (int i = 0; i < listeners.size(); i++) {
194 18623 jdominguez
                        ((ActionListener) listeners.get(i)).actionPerformed(
195
                                        new ActionEvent(this, 0, "IMAGE_RESIZED"));
196 11559 jaume
                }
197
        }
198
199
200
        public void mouseEntered(MouseEvent e) { /* nothing */ }
201
        public void mouseExited(MouseEvent e) { /* nothing */ }
202
        public void mousePressed(MouseEvent e) {  /* nothing */ }
203
        public void mouseReleased(MouseEvent e) {  /* nothing */ }
204 29986 vcaballero
205
206
        public void focusGained(FocusEvent arg0) {
207
                // TODO Auto-generated method stub
208
209
        }
210
211
212
        public void focusLost(FocusEvent arg0) {
213
                doIt();
214
        }
215 11559 jaume
}