Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / usability / button / JStandardizedToolButton.java @ 168

History | View | Annotate | Download (5.76 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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.tools.swing.impl.usability.button;
42

    
43
import java.awt.Dimension;
44

    
45
import javax.swing.Action;
46
import javax.swing.Icon;
47
import javax.swing.JButton;
48

    
49
/**
50
 * According to the gvSIG's GUI style sheet all the buttons in the application
51
 * will have a normative size. No smaller than a concrete size, and big enough
52
 * to contain the text and avoiding the "..." characters. The button will grow
53
 * up in width by a set of widths defined in this style sheet, always choosing
54
 * the smallest width that can contain the text. If the biggest width is not
55
 * enought for this purpose then the button will automatically grow up to the
56
 * smallest necessary width to fit the text.<br>
57
 * <p>
58
 * The button resizing is based on the <b>setText(String txt)</b> method.
59
 * However, it is possible to use a custom size if you invoke one of
60
 * <b>setSize(..)</b>, <b>setBorders(...)</b> or <b>setPreferredSize(...)</b>
61
 * after invoking the <b>setText(...)<b> method.
62
 * <p>
63
 * This class is just a standard javax.swing.JButton that handles this issue.
64
 * </p>
65
 * 
66
 * @author jaume dominguez faus - jaume.dominguez@iver.es
67
 * 
68
 */
69
public class JStandardizedToolButton extends JButton {
70
        private static final long serialVersionUID = -1635879317292710725L;
71

    
72
        // TODO this should be initialized from a properties file or so.
73
        private static int[][] buttonSizes = new int[][] { 
74
            new int[] {  22, 22 },
75
            new int[] {  44, 22 },
76
            new int[] {  66, 22 },
77
            new int[] {  88, 22 },
78
            new int[] { 110, 22 }, 
79
            new int[] { 132, 22 }, 
80
            new int[] { 154, 22 } };
81

    
82
        private String enableText;
83
        private String toolTip;
84

    
85
        /**
86
         * Creates a new empty instance of org.gvsig.gui.beans.swing.JButton.
87
         */
88
        public JStandardizedToolButton() {
89
                super();
90
        }
91

    
92
        /**
93
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton from an
94
         * {@link Action}.
95
         */
96
        public JStandardizedToolButton(Action action) {
97
                super(action);
98
        }
99

    
100
        /**
101
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing an
102
         * image.
103
         */
104
        public JStandardizedToolButton(Icon icon) {
105
                super(icon);
106
        }
107

    
108
        /**
109
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing a
110
         * text.
111
         * 
112
         * @param text
113
         */
114
        public JStandardizedToolButton(String text) {
115
                super();
116
                setText(text);
117
        }
118

    
119
        /**
120
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing an
121
         * image and a text.
122
         * 
123
         * @param text
124
         * @param icon
125
         */
126
        public JStandardizedToolButton(String text, Icon icon) {
127
                super(icon);
128
                setText(text);
129
        }
130

    
131
        /**
132
         * Gets the text that appears in the tooltip when the button is disabled.
133
         * 
134
         * @return String
135
         */
136
        public String getEnableText() {
137
                return enableText;
138
        }
139

    
140
        @Override
141
        public Dimension getMaximumSize() {
142
                return getPreferredSize();
143
        }
144

    
145
        @Override
146
        public Dimension getMinimumSize() {
147
                return getPreferredSize();
148
        }
149

    
150
        @Override
151
        public Dimension getPreferredSize() {
152
                Dimension d = getUI().getMinimumSize(this);
153
                int oldWidth = (int) d.getWidth(), newWidth = oldWidth;
154
                int oldHeight = (int) d.getHeight(), newHeight = oldHeight;
155

    
156
                // figure out the suitable width
157
                for (int i = buttonSizes.length - 1; i >= 0; i--)
158
                        if (oldWidth < buttonSizes[i][0])
159
                                newWidth = buttonSizes[i][0];
160

    
161
                // figure out the suitable height
162
                for (int i = buttonSizes.length - 1; i >= 0; i--)
163
                        if (oldHeight < buttonSizes[i][1])
164
                                newHeight = buttonSizes[i][1];
165

    
166
                return new Dimension(newWidth, newHeight);
167
        }
168

    
169
        // public void setText(String text) {
170
        // super.setText(text);
171
        // Dimension d = getUI().getMinimumSize(this);
172
        // int oldWidth = (int) d.getWidth(), newWidth = oldWidth;
173
        // int oldHeight = (int) d.getHeight(), newHeight = oldHeight;
174
        //
175
        // // figure out the suitable width
176
        // for (int i = buttonSizes.length - 1; i >= 0; i--)
177
        // if (oldWidth < buttonSizes[i][0])
178
        // newWidth = buttonSizes[i][0];
179
        //
180
        // // figure out the suitable height
181
        // for (int i = buttonSizes.length - 1; i >= 0; i--)
182
        // if (oldHeight < buttonSizes[i][1])
183
        // newHeight = buttonSizes[i][1];
184
        //
185
        // Dimension sz = new Dimension(newWidth, newHeight);
186
        // super.setSize(sz);
187
        // super.setPreferredSize(sz);
188
        // }
189

    
190
        public void setEnabled(boolean aFlag) {
191
                super.setEnabled(aFlag);
192
                if (aFlag) {
193
                        setToolTipText(toolTip);
194
                } else {
195
                        setToolTipText(enableText);
196
                }
197
        }
198

    
199
        /**
200
         * Sets the text that appears in the tooltip when the button is disabled.
201
         * 
202
         * @param enableText
203
         *            The enableText to set.
204
         */
205
        public void setEnableText(String enableText) {
206
                this.enableText = enableText;
207
        }
208

    
209
        /**
210
         * Sets the text that appears in the tooltip when the button is enabled.
211
         */
212
        public void setToolTip(String text) {
213
                toolTip = text;
214
        }
215
}