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 / JStandardizedButton.java @ 298

History | View | Annotate | Download (6.19 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 JStandardizedButton extends JButton {
70

    
71
    private static final long serialVersionUID = -1635879317292710725L;
72

    
73
    // TODO this should be initialized from a properties file or so.
74
    private static int[][] buttonSizes =
75
        new int[][] { new int[] { 90, 23 }, new int[] { 110, 23 },
76
            new int[] { 135, 23 }, new int[] { 160, 23 } };
77

    
78
    private String enableText;
79
    private String toolTip;
80

    
81
    /**
82
     * Creates a new empty instance of org.gvsig.gui.beans.swing.JButton.
83
     */
84
    public JStandardizedButton() {
85
        super();
86
    }
87

    
88
    /**
89
     * Creates a new instance of org.gvsig.gui.beans.swing.JButton from an
90
     * {@link Action}.
91
     */
92
    public JStandardizedButton(Action action) {
93
        super(action);
94
    }
95

    
96
    /**
97
     * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing an
98
     * image.
99
     */
100
    public JStandardizedButton(Icon icon) {
101
        super(icon);
102
    }
103

    
104
    /**
105
     * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing a
106
     * text.
107
     * 
108
     * @param text
109
     */
110
    public JStandardizedButton(String text) {
111
        super();
112
        setText(text);
113
    }
114

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

    
127
    /**
128
     * Gets the text that appears in the tooltip when the button is disabled.
129
     * 
130
     * @return String
131
     */
132
    public String getEnableText() {
133
        return enableText;
134
    }
135

    
136
    @Override
137
    public Dimension getMaximumSize() {
138
        return getPreferredSize();
139
    }
140

    
141
    @Override
142
    public Dimension getMinimumSize() {
143
        return getPreferredSize();
144
    }
145

    
146
    @Override
147
    public Dimension getPreferredSize() {
148
        Dimension d = getUI().getMinimumSize(this);
149
        int oldWidth = (int) d.getWidth(), newWidth = oldWidth;
150
        int oldHeight = (int) d.getHeight(), newHeight = oldHeight;
151

    
152
        // figure out the suitable width
153
        for (int i = buttonSizes.length - 1; i >= 0; i--) {
154
            if (oldWidth < buttonSizes[i][0]) {
155
                newWidth = buttonSizes[i][0];
156
            }
157
        }
158

    
159
        // figure out the suitable height
160
        for (int i = buttonSizes.length - 1; i >= 0; i--) {
161
            if (oldHeight < buttonSizes[i][1]) {
162
                newHeight = buttonSizes[i][1];
163
            }
164
        }
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
    @Override
191
    public void setEnabled(boolean aFlag) {
192
        super.setEnabled(aFlag);
193
        if (aFlag) {
194
            setToolTipText(toolTip);
195
        } else {
196
            setToolTipText(enableText);
197
        }
198
    }
199

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

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