svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / gui / panels / ImageSizePanel.java @ 40560
History | View | Annotate | Download (6.07 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40560 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40560 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40560 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40560 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.app.gui.panels; |
||
25 | |||
26 | import java.awt.BorderLayout; |
||
27 | import java.awt.Color; |
||
28 | import java.awt.Dimension; |
||
29 | import java.awt.Graphics; |
||
30 | import java.awt.event.ActionEvent; |
||
31 | import java.awt.event.ActionListener; |
||
32 | import java.awt.event.FocusEvent; |
||
33 | import java.awt.event.FocusListener; |
||
34 | import java.awt.event.KeyEvent; |
||
35 | import java.awt.event.KeyListener; |
||
36 | import java.awt.event.MouseEvent; |
||
37 | import java.awt.event.MouseListener; |
||
38 | import java.util.ArrayList; |
||
39 | |||
40 | import javax.swing.Icon; |
||
41 | import javax.swing.JPanel; |
||
42 | import javax.swing.JToggleButton; |
||
43 | |||
44 | import org.gvsig.andami.PluginServices; |
||
45 | import org.gvsig.gui.beans.swing.GridBagLayoutPanel; |
||
46 | import org.gvsig.gui.beans.swing.JIncrementalNumberField; |
||
47 | import org.gvsig.i18n.Messages; |
||
48 | |||
49 | |||
50 | /**
|
||
51 | * @author jaume dominguez faus - jaume.dominguez@iver.es
|
||
52 | */
|
||
53 | public class ImageSizePanel extends JPanel implements KeyListener, MouseListener, ActionListener, FocusListener{ |
||
54 | /**
|
||
55 | *
|
||
56 | */
|
||
57 | private static final long serialVersionUID = 4810686805894198593L; |
||
58 | private static final Icon iconSelected = PluginServices.getIconTheme().get("chain"); |
||
59 | private static final Icon iconUnselected = PluginServices.getIconTheme().get("broken-chain"); |
||
60 | |||
61 | private JIncrementalNumberField widthTxt;
|
||
62 | private JIncrementalNumberField heightTxt;
|
||
63 | private double width, height; |
||
64 | private JToggleButton lock; |
||
65 | private double ratio; |
||
66 | private boolean performing; |
||
67 | private boolean locked = true; |
||
68 | private ArrayList<ActionListener> listeners = new ArrayList<ActionListener>(); |
||
69 | |||
70 | public ImageSizePanel(){
|
||
71 | super();
|
||
72 | initialize(); |
||
73 | } |
||
74 | |||
75 | |||
76 | /**
|
||
77 | * This method initializes this
|
||
78 | *
|
||
79 | */
|
||
80 | private void initialize() { |
||
81 | final GridBagLayoutPanel aux = new GridBagLayoutPanel(); |
||
82 | setLayout(new BorderLayout()); |
||
83 | aux.addComponent(Messages.getText("width")+":", |
||
84 | widthTxt = new JIncrementalNumberField(null, 5, 0, Double.MAX_VALUE, 1)); |
||
85 | aux.addComponent(Messages.getText("height")+":", |
||
86 | heightTxt = new JIncrementalNumberField(null, 5, 0, Double.MAX_VALUE, 1)); |
||
87 | setLayout(new BorderLayout()); |
||
88 | add(aux, BorderLayout.CENTER);
|
||
89 | JPanel lockPanel = new JPanel(null) { |
||
90 | private static final long serialVersionUID = -415551033800811412L; |
||
91 | protected void paintComponent(Graphics g) { |
||
92 | int y1 = widthTxt.getY() + widthTxt.getHeight()/2-3; |
||
93 | int y2 = heightTxt.getY() + heightTxt.getHeight()/2+3; |
||
94 | g.setColor(Color.DARK_GRAY);
|
||
95 | g.drawLine(2, y1, getWidth()/2+2, y1); |
||
96 | g.drawLine(getWidth()/2+2, y1, getWidth()/2+2, y2); |
||
97 | g.drawLine(2, y2, getWidth()/2+2, y2); |
||
98 | lock.setBounds(3, widthTxt.getY()+13, getWidth()-2, 22); |
||
99 | } |
||
100 | }; |
||
101 | lockPanel.setPreferredSize(new Dimension(20, 20)); |
||
102 | lock = new JToggleButton() { |
||
103 | private static final long serialVersionUID = 1668046192113822412L; |
||
104 | protected void paintComponent(Graphics g) { |
||
105 | setIcon(isSelected() ? iconSelected : iconUnselected); |
||
106 | super.paintComponent(g);
|
||
107 | } |
||
108 | }; |
||
109 | lock.setSelected(locked); |
||
110 | lockPanel.add(lock); |
||
111 | add(lockPanel, BorderLayout.EAST);
|
||
112 | widthTxt.addKeyListener(this);
|
||
113 | widthTxt.addMouseListener(this);
|
||
114 | widthTxt.addActionListener(this);
|
||
115 | widthTxt.addFocusListener(this);
|
||
116 | heightTxt.addKeyListener(this);
|
||
117 | heightTxt.addMouseListener(this);
|
||
118 | heightTxt.addActionListener(this);
|
||
119 | heightTxt.addFocusListener(this);
|
||
120 | |||
121 | lock.addMouseListener(this);
|
||
122 | } |
||
123 | |||
124 | public void setImageSize(double width, double height) { |
||
125 | this.width = width;
|
||
126 | widthTxt.setDouble(width); |
||
127 | this.height = height;
|
||
128 | heightTxt.setDouble(height); |
||
129 | this.ratio = width / height;
|
||
130 | } |
||
131 | |||
132 | public void setImageSize(Dimension sz) { |
||
133 | setImageSize(sz.getWidth(), sz.getHeight()); |
||
134 | } |
||
135 | |||
136 | |||
137 | public double[] getImageDimension() { |
||
138 | double[] d = new double[2]; |
||
139 | d[0] = width;
|
||
140 | d[1] = height;
|
||
141 | return d;
|
||
142 | } |
||
143 | |||
144 | public void addActionListener(ActionListener l) { |
||
145 | listeners.add(l); |
||
146 | } |
||
147 | |||
148 | public void keyPressed(KeyEvent e) { doIt(); } |
||
149 | public void keyReleased(KeyEvent e) { doIt(); } |
||
150 | public void keyTyped(KeyEvent e) { doIt(); } |
||
151 | public void mouseClicked(MouseEvent e) { e.consume(); doIt(); } |
||
152 | public void actionPerformed(ActionEvent e) { doIt(); } |
||
153 | |||
154 | private void doIt() { |
||
155 | if (!performing) {
|
||
156 | performing = true;
|
||
157 | if (locked != lock.isSelected()) {
|
||
158 | locked = lock.isSelected(); |
||
159 | ratio = widthTxt.getDouble() / heightTxt.getDouble(); |
||
160 | } else if (locked && width != widthTxt.getDouble()){ |
||
161 | width = widthTxt.getDouble(); |
||
162 | height = width/ratio; |
||
163 | height = Math.round(height*100)/100; |
||
164 | heightTxt.setDouble(height); |
||
165 | fireActionPerformed(); |
||
166 | } else if (locked && height != heightTxt.getDouble()) { |
||
167 | height = heightTxt.getDouble(); |
||
168 | width = height*ratio; |
||
169 | width = Math.round(width*100)/100; |
||
170 | widthTxt.setDouble(width); |
||
171 | fireActionPerformed(); |
||
172 | |||
173 | } |
||
174 | |||
175 | performing = false;
|
||
176 | } |
||
177 | } |
||
178 | |||
179 | |||
180 | private void fireActionPerformed() { |
||
181 | for (int i = 0; i < listeners.size(); i++) { |
||
182 | ((ActionListener) listeners.get(i)).actionPerformed(
|
||
183 | new ActionEvent(this, 0, "IMAGE_RESIZED")); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | |||
188 | public void mouseEntered(MouseEvent e) { /* nothing */ } |
||
189 | public void mouseExited(MouseEvent e) { /* nothing */ } |
||
190 | public void mousePressed(MouseEvent e) { /* nothing */ } |
||
191 | public void mouseReleased(MouseEvent e) { /* nothing */ } |
||
192 | |||
193 | |||
194 | public void focusGained(FocusEvent arg0) { |
||
195 | // TODO Auto-generated method stub
|
||
196 | |||
197 | } |
||
198 | |||
199 | |||
200 | public void focusLost(FocusEvent arg0) { |
||
201 | doIt(); |
||
202 | } |
||
203 | } |