Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / buttonbar / ButtonBarContainer.java @ 11445

History | View | Annotate | Download (4.58 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, hComp = 26;
31
        private String                                                 pathToImages = "images/";
32
        private ArrayList                                         buttons = new ArrayList();
33
        private boolean                                         disableAllControls = false;
34
        private boolean[]                                         buttonsState = null;
35
        
36
        /**
37
         * This is the default constructor
38
         */
39
        public ButtonBarContainer() {
40
                super();
41
                initialize();
42
        }
43

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

    
57
        
58
        /**
59
         * A?ade un boton al ArrayList de los botones.
60
         * 
61
         * @param iconName: nombre del icono asignado al boton. La imagen tendr?a que
62
         *                                         estar dentro de la carpeta "images/"
63
         * @param tip: tip del boton;
64
         * @param order: orden que ocupar? el boton dentro del control
65
         */
66
        public void addButton(String iconName, String tip, int order){
67
                JButton bt = new JButton();
68
                
69
                bt.setPreferredSize(new java.awt.Dimension(22, 22));
70
                try{
71
                        if (iconName != null)
72
                                bt.setIcon(new ImageIcon(getClass().getResource(pathToImages + iconName)));
73
                }catch(NullPointerException exc){
74
                        //El icono no existe -> No se a?ade ninguno
75
                }
76
                
77
                if(tip != null)
78
                        bt.setToolTipText(tip);
79
                
80
                buttons.add(order, bt);
81
                addList();
82
                
83
        }
84
                
85
        /**
86
         * Elimina el bot?n correspondiente al indice que le pasamos.
87
         * @param index
88
         */
89
        public void delButton(int index){
90
                buttons.remove(index);
91
                this.removeAll();
92
                addList();
93
        }
94
        
95
        /**
96
         * A?ade en el panel los botones que tenemos en el ArrayList.
97
         *
98
         */
99
        public void addList(){
100
                for(int i = 0 ; i < buttons.size() ; i++){
101
                        this.add((JButton)buttons.get(i));
102
                }
103
        }
104
        
105
        
106
        /**
107
         * Esta funci?n deshabilita todos los controles y guarda sus valores
108
         * de habilitado o deshabilitado para que cuando se ejecute restoreControlsValue
109
         * se vuelvan a quedar como estaba
110
         */
111
        public void disableAllControls(){
112
                if(!disableAllControls){
113
                        disableAllControls = true;
114
                        
115
                        buttonsState = new boolean[buttons.size()];
116
                        
117
                        
118
                        
119
                        for (int i = 0 ; i < buttons.size() ; i++){
120
                                
121
                                //Salvamos los estados
122
                                buttonsState[i] = ((JButton)buttons.get(i)).isEnabled();
123
                                //Desactivamos controles
124
                                ((JButton)buttons.get(i)).setEnabled(false);
125
                        }
126
                }
127
        }
128
        
129
        /**
130
         * Esta funci?n deja los controles como estaban al ejecutar la funci?n 
131
         * disableAllControls
132
         */
133
        public void restoreControlsValue(){
134
                if(disableAllControls){
135
                        disableAllControls = false;
136
                        
137
                        for(int i = 0 ; i < buttons.size() ; i++){
138
                                ((JButton)buttons.get(i)).setEnabled(buttonsState[i]);
139
                        }
140
                }
141
        }
142
        
143
        /**
144
         * M?todo para acceder a los botones del control;
145
         * @param index
146
         * @return
147
         */
148
        public JButton getButton(int index){
149
                return (JButton)buttons.get(index);
150
        }
151

    
152
        /**
153
         * M?todo para establecer la posici?n de los botones dentro del control.
154
         * @param align: "left" o "right"
155
         */
156
        public void setButtonAlignment(String align){
157
                FlowLayout layout = new FlowLayout();
158
                layout.setHgap(2);
159
                layout.setVgap(0);
160
                
161
                if (align.equals("right"))
162
                        layout.setAlignment(FlowLayout.RIGHT);
163
                else
164
                        layout.setAlignment(FlowLayout.LEFT);
165
                
166
                this.setLayout(layout);
167
        }
168
        
169
        public void setComponentBorder(boolean br){
170
                if(br)
171
                        this.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
172
                if(!br)
173
                        this.setBorder(javax.swing.BorderFactory.createEmptyBorder());                
174
        }
175
        
176
}