Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / buttonbar / ButtonBarContainer.java @ 12121

History | View | Annotate | Download (4.59 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
package org.gvsig.gui.beans.buttonbar;
20

    
21
import java.awt.FlowLayout;
22
import java.util.ArrayList;
23

    
24
import javax.swing.ImageIcon;
25
import javax.swing.JButton;
26
import javax.swing.JPanel;
27

    
28
public class ButtonBarContainer extends JPanel {
29
        private static final long serialVersionUID = -2556987128553063939L;
30
        private int       wComp              = 400;
31
        private int       hComp              = 26;
32
        private String    pathToImages       = "images/";
33
        private ArrayList buttons            = new ArrayList();
34
        private boolean   disableAllControls = false;
35
        private boolean[] buttonsState       = null;
36

    
37
        /**
38
         * This is the default constructor
39
         */
40
        public ButtonBarContainer() {
41
                super();
42
                initialize();
43
        }
44

    
45
        /**
46
         * This method initializes this
47
         * 
48
         * @return void
49
         */
50
        private void initialize() {
51
                FlowLayout flowLayout = new FlowLayout();
52
                flowLayout.setHgap(0);
53
                flowLayout.setVgap(0);
54
                this.setLayout(flowLayout);
55
                this.setSize(wComp, hComp);
56
                }
57

    
58

    
59
        /**
60
         * A?ade un boton al ArrayList de los botones.
61
         * 
62
         * @param iconName: nombre del icono asignado al boton. La imagen tendr?a que
63
         *                                         estar dentro de la carpeta "images/"
64
         * @param tip: tip del boton;
65
         * @param order: orden que ocupar? el boton dentro del control
66
         */
67
        public void addButton(String iconName, String tip, int order){
68
                JButton bt = new JButton();
69

    
70
                bt.setPreferredSize(new java.awt.Dimension(22, 22));
71
                try{
72
                        if (iconName != null)
73
                                bt.setIcon(new ImageIcon(getClass().getResource(pathToImages + iconName)));
74
                }catch(NullPointerException exc){
75
                        //El icono no existe -> No se a?ade ninguno
76
                }
77

    
78
                if(tip != null)
79
                        bt.setToolTipText(tip);
80

    
81
                buttons.add(order, bt);
82
                addList();
83

    
84
        }
85

    
86
        /**
87
         * Elimina el bot?n correspondiente al indice que le pasamos.
88
         * @param index
89
         */
90
        public void delButton(int index){
91
                buttons.remove(index);
92
                this.removeAll();
93
                addList();
94
        }
95

    
96
        /**
97
         * A?ade en el panel los botones que tenemos en el ArrayList.
98
         *
99
         */
100
        public void addList(){
101
                for(int i = 0 ; i < buttons.size() ; i++){
102
                        this.add((JButton)buttons.get(i));
103
                }
104
        }
105

    
106

    
107
        /**
108
         * Esta funci?n deshabilita todos los controles y guarda sus valores
109
         * de habilitado o deshabilitado para que cuando se ejecute restoreControlsValue
110
         * se vuelvan a quedar como estaba
111
         */
112
        public void disableAllControls(){
113
                if(!disableAllControls){
114
                        disableAllControls = true;
115

    
116
                        buttonsState = new boolean[buttons.size()];
117

    
118

    
119

    
120
                        for (int i = 0 ; i < buttons.size() ; i++){
121

    
122
                                //Salvamos los estados
123
                                buttonsState[i] = ((JButton)buttons.get(i)).isEnabled();
124
                                //Desactivamos controles
125
                                ((JButton)buttons.get(i)).setEnabled(false);
126
                        }
127
                }
128
        }
129

    
130
        /**
131
         * Esta funci?n deja los controles como estaban al ejecutar la funci?n 
132
         * disableAllControls
133
         */
134
        public void restoreControlsValue(){
135
                if(disableAllControls){
136
                        disableAllControls = false;
137

    
138
                        for(int i = 0 ; i < buttons.size() ; i++){
139
                                ((JButton)buttons.get(i)).setEnabled(buttonsState[i]);
140
                        }
141
                }
142
        }
143

    
144
        /**
145
         * M?todo para acceder a los botones del control;
146
         * @param index
147
         * @return
148
         */
149
        public JButton getButton(int index){
150
                return (JButton)buttons.get(index);
151
        }
152

    
153
        /**
154
         * M?todo para establecer la posici?n de los botones dentro del control.
155
         * @param align: "left" o "right"
156
         */
157
        public void setButtonAlignment(String align){
158
                FlowLayout layout = new FlowLayout();
159
                layout.setHgap(2);
160
                layout.setVgap(0);
161

    
162
                if (align.equals("right"))
163
                        layout.setAlignment(FlowLayout.RIGHT);
164
                else
165
                        layout.setAlignment(FlowLayout.LEFT);
166

    
167
                this.setLayout(layout);
168
        }
169

    
170
        public void setComponentBorder(boolean br){
171
                if(br)
172
                        this.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
173
                if(!br)
174
                        this.setBorder(javax.swing.BorderFactory.createEmptyBorder());
175
        }
176

    
177
}