Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / JButton.java @ 33355

History | View | Annotate | Download (4.84 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.gui.beans.swing;
42

    
43
import java.awt.Dimension;
44

    
45
import javax.swing.Icon;
46

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

    
76
        // TODO this should be initialized from a properties file or so.
77
        private static int[][] buttonSizes = new int[][] {
78
                        new int[] { 90, 23},
79
                        new int[] {110, 23},
80
                        new int[] {135, 23},
81
                        new int[] {160, 23}
82
        };
83

    
84
        private String enableText;
85
        private String toolTip;
86

    
87

    
88

    
89
        /**
90
         * Creates a new empty instance of org.gvsig.gui.beans.swing.JButton.
91
         */
92
        public JButton() {
93
                super();
94
        }
95

    
96
        /**
97
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing a text.
98
         * @param text
99
         */
100
        public JButton(String text) {
101
                super();
102
                setText(text);
103
        }
104

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

    
116
        /**
117
         * Creates a new instance of org.gvsig.gui.beans.swing.JButton containing an image.
118
         */
119
        public JButton(Icon icon) {
120
                super(icon);
121
        }
122

    
123

    
124
        /**
125
         * Gets the text that appears in the tooltip when the button is disabled.
126
         * @return String
127
         */
128
        public String getEnableText() {
129
                return enableText;
130
        }
131
        /**
132
         * Sets the text that appears in the tooltip when the button is disabled.
133
         * @param enableText The enableText to set.
134
         */
135
        public void setEnableText(String enableText) {
136
                this.enableText = enableText;
137
        }
138

    
139

    
140
        public void setEnabled(boolean aFlag) {
141
                super.setEnabled(aFlag);
142
                if (aFlag){
143
                        setToolTipText(toolTip);
144
                }else{
145
                        setToolTipText(enableText);
146
                }
147
        }
148

    
149
        /**
150
         * Sets the text that appears in the tooltip when the button is enabled.
151
         */
152
        public void setToolTip(String text) {
153
                toolTip = text;
154
        }
155

    
156
        public void setText(String text) {
157
                super.setText(text);
158
                Dimension d = getUI().getMinimumSize(this);
159
                int oldWidth = (int) d.getWidth(), newWidth = oldWidth;
160
                int oldHeight = (int) d.getHeight(), newHeight = oldHeight;
161

    
162
                // figure out the suitable width
163
                for (int i = buttonSizes.length-1; i >= 0 ; i--)
164
                        if (oldWidth < buttonSizes[i][0])
165
                                newWidth = buttonSizes[i][0];
166

    
167
                // figure out the suitable height
168
                for (int i = buttonSizes.length-1; i >= 0 ; i--)
169
                        if (oldHeight < buttonSizes[i][1])
170
                                newHeight = buttonSizes[i][1];
171

    
172
                Dimension sz = new Dimension(newWidth, newHeight);
173
                super.setSize(sz);
174
                super.setPreferredSize(sz);
175
        }
176

    
177
}