Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / gui / panels / ImageSizePanel.java @ 29596

History | View | Annotate | Download (5.95 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.KeyEvent;
50
import java.awt.event.KeyListener;
51
import java.awt.event.MouseEvent;
52
import java.awt.event.MouseListener;
53
import java.util.ArrayList;
54

    
55
import javax.swing.Icon;
56
import javax.swing.JPanel;
57
import javax.swing.JToggleButton;
58

    
59
import org.gvsig.andami.PluginServices;
60
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
61
import org.gvsig.gui.beans.swing.JIncrementalNumberField;
62

    
63

    
64
/**
65
 * @author jaume dominguez faus - jaume.dominguez@iver.es
66
 */
67
public class ImageSizePanel extends JPanel implements KeyListener, MouseListener, ActionListener{
68
        private static final Icon iconSelected = PluginServices.getIconTheme().get("chain");
69
        private static final Icon iconUnselected = PluginServices.getIconTheme().get("broken-chain");
70

    
71
        private JIncrementalNumberField widthTxt;
72
        private JIncrementalNumberField heightTxt;
73
        private double width, height;
74
        private JToggleButton lock;
75
        private double ratio;
76
        private boolean performing;
77
        private boolean locked = true;
78
        private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>();
79
        
80
        public ImageSizePanel(){
81
                super();
82
                initialize();
83
        }
84

    
85

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

    
129
                lock.addMouseListener(this);
130
        }
131

    
132
        public void setImageSize(double width, double height) {
133
                this.width = width;
134
                widthTxt.setDouble(width);
135
                this.height = height;
136
                heightTxt.setDouble(height);
137
                this.ratio = width / height;
138
        }
139
        
140
        public void setImageSize(Dimension sz) {
141
                setImageSize(sz.getWidth(), sz.getHeight());
142
        }
143
        
144

    
145
        public double[] getImageDimension() {
146
                double[] d = new double[2];
147
                d[0] = width;
148
                d[1] = height;
149
                return d;
150
        }
151

    
152
        public void addActionListener(ActionListener l) {
153
                listeners.add(l);
154
        }
155

    
156
        public void keyPressed(KeyEvent e) { doIt(); }
157
        public void keyReleased(KeyEvent e) { doIt(); }
158
        public void keyTyped(KeyEvent e) { doIt(); }
159
        public void mouseClicked(MouseEvent e) { e.consume(); doIt(); }
160
        public void actionPerformed(ActionEvent e) { doIt(); }
161

    
162
        private void doIt() {
163
                if (!performing) {
164
                        performing = true;
165
                        if (locked != lock.isSelected()) {
166
                                locked = lock.isSelected();
167
                                ratio = widthTxt.getDouble() / heightTxt.getDouble();
168
                        } else if (locked && width != widthTxt.getDouble()){
169
                                width = widthTxt.getDouble();
170
                                height = width/ratio;
171
                                height = Math.round(height*100)/100;
172
                                heightTxt.setDouble(height);
173
                                fireActionPerformed();
174
                        } else if (locked && height != heightTxt.getHeight()) {
175
                                height = heightTxt.getDouble();
176
                                width = height*ratio;
177
                                width = Math.round(width*100)/100;
178
                                widthTxt.setDouble(width);
179
                                fireActionPerformed();
180

    
181
                        }
182

    
183
                        performing = false;
184
                }
185
        }
186

    
187

    
188
        private void fireActionPerformed() {
189
                for (int i = 0; i < listeners.size(); i++) {
190
                        ((ActionListener) listeners.get(i)).actionPerformed(
191
                                        new ActionEvent(this, 0, "IMAGE_RESIZED"));
192
                }
193
        }
194

    
195

    
196
        public void mouseEntered(MouseEvent e) { /* nothing */ }
197
        public void mouseExited(MouseEvent e) { /* nothing */ }
198
        public void mousePressed(MouseEvent e) {  /* nothing */ }
199
        public void mouseReleased(MouseEvent e) {  /* nothing */ }
200
}
201