Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / buttonsPanel / ButtonsPanel.java @ 10818

History | View | Annotate | Download (5.89 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.buttonsPanel;
20

    
21
import java.awt.FlowLayout;
22
import java.awt.event.ActionEvent;
23
import java.awt.event.ActionListener;
24
import java.util.ArrayList;
25
import java.util.Iterator;
26

    
27
import javax.swing.JButton;
28
import javax.swing.JPanel;
29

    
30
import org.gvsig.gui.beans.messages.Messages;
31
/**
32
 * <code>ButtonsPanel</code> ofrece un widget con un conjunto de botones
33
 * preestablecidos, aunque tambi?n se pueden a?adir botones con el m?todo
34
 * {@link #addButton(String, int)}
35
 * 
36
 * @version 15/03/2007
37
 * @author Borja Sanchez Zamorano (borja.sanchez@iver.es)
38
 */
39
public class ButtonsPanel extends JPanel {
40
        private static final long serialVersionUID = -7264041442217894577L;
41
        private ArrayList buttonsList = new ArrayList();
42
        static private int eventId = Integer.MIN_VALUE;
43
        private ArrayList actionCommandListeners = new ArrayList();
44

    
45
        public static final int BUTTON_ACCEPT = 1;
46
        public static final int BUTTON_CANCEL = 2;
47
        public static final int BUTTON_APPLY = 3;
48
        public static final int BUTTON_YES = 4;
49
        public static final int BUTTON_NO = 5;
50
        public static final int BUTTON_CLOSE = 6;
51
        public static final int BUTTON_EXIT = 7;
52
        public static final int BUTTON_SEEDETAILS = 8;
53
        public static final int BUTTON_HIDEDETAILS = 9;
54
        
55
        public static final int BUTTONS_ACCEPT = 1;
56
        public static final int BUTTONS_ACCEPTCANCEL = 2;
57
        public static final int BUTTONS_ACCEPTCANCELAPPLY = 3;
58
        public static final int BUTTONS_CANCEL = 4;
59
        public static final int BUTTONS_YESNO = 5;
60
        public static final int BUTTONS_CLOSE = 6;
61
        public static final int BUTTONS_EXIT = 7;
62
        public static final int BUTTONS_NONE = 8;
63

    
64
        /**
65
         * Crea un ButtonsPanel con un Layout por defecto.
66
         */
67
        public ButtonsPanel() {
68
                setLayout(new java.awt.FlowLayout(FlowLayout.RIGHT));
69
        }
70

    
71
        /**
72
         * Crea un ButtonsPanel con un Layout por defecto.
73
         * 
74
         * @param items Que botones vamos a usar en la creaci?n.
75
         */
76
        public ButtonsPanel(int items) {
77
                setLayout(new java.awt.FlowLayout(FlowLayout.RIGHT));
78
                switch (items) {
79
                        case BUTTONS_ACCEPT:
80
                                addAccept();
81
                                break;
82
                        case BUTTONS_ACCEPTCANCEL:
83
                                addAccept();
84
                                addCancel();
85
                                break;
86
                        case BUTTONS_ACCEPTCANCELAPPLY:
87
                                addAccept();
88
                                addCancel();
89
                                addApply();
90
                                break;
91
                        case BUTTONS_CANCEL:
92
                                addCancel();
93
                                break;
94
                        case BUTTONS_YESNO:
95
                                addYes();
96
                                addNo();
97
                                break;
98
                        case BUTTONS_CLOSE:
99
                                addClose();
100
                                break;
101
                        case BUTTONS_EXIT:
102
                                addExit();
103
                                break;
104
                        case BUTTONS_NONE:
105
                                break;
106
                }
107
        }
108

    
109
        public void addActionListener(ActionListener listener) {
110
          if (!actionCommandListeners.contains(listener))
111
            actionCommandListeners.add(listener);
112
        }
113

    
114
        public void removeActionListener(ActionListener listener) {
115
          actionCommandListeners.remove(listener);
116
        }
117

    
118
        private void callActionCommandListeners(String buttonID) {
119
          Iterator acIterator = actionCommandListeners.iterator();
120
          while (acIterator.hasNext()) {
121
            ActionListener listener = (ActionListener) acIterator.next();
122
            listener.actionPerformed(new ActionEvent(this, eventId, buttonID));
123
          }
124
          eventId++;
125
        }
126

    
127
        /**
128
         * A?adir el boton Aceptar.
129
         */
130
        public void addAccept() {
131
                addButton(Messages.getText("aceptar"), BUTTON_ACCEPT);
132
        }
133
        
134
        /**
135
         * A?adir el boton Cancelar.
136
         */
137
        public void addCancel() {
138
                addButton(Messages.getText("cancelar"), BUTTON_CANCEL);
139
        }
140
        
141
        /**
142
         * A?adir el boton S?.
143
         */
144
        public void addYes() {
145
                addButton(Messages.getText("si"), BUTTON_YES);
146
        }
147

    
148
        /**
149
         * A?adir el boton No.
150
         */
151
        public void addNo() {
152
                addButton(Messages.getText("no"), BUTTON_NO);
153
        }
154

    
155
        /**
156
         * A?adir el boton Aplicar.
157
         */
158
        public void addApply() {
159
                addButton(Messages.getText("aplicar"), BUTTON_APPLY);
160
        }
161

    
162
        /**
163
         * A?adir el boton Cerrar.
164
         */
165
        public void addClose() {
166
                addButton(Messages.getText("cerrar"), BUTTON_CLOSE);
167
        }
168

    
169
        /**
170
         * A?adir el boton Salir.
171
         */
172
        public void addExit() {
173
                addButton(Messages.getText("salir"), BUTTON_EXIT);
174
        }
175

    
176
        /**
177
         * A?adir el boton Ver detalles.
178
         */
179
        public void addSeeDetails() {
180
                addButton(Messages.getText("verdetalles"), BUTTON_SEEDETAILS);
181
        }
182
        
183
        /**
184
         * A?adir el boton Ocultar detalles.
185
         */
186
        public void addHideDetails() {
187
                addButton(Messages.getText("ocultardetalles"), BUTTON_HIDEDETAILS);
188
        }
189
        
190
        /**
191
         * A?adimos un bot?n definido por el usuario.
192
         * 
193
         * @param text Texto que contendr? el bot?n
194
         * @param id Entero para identificar los eventos del bot?n
195
         */
196
        public void addButton(String text, int id) {
197
                JButton button = new JButton();
198
                buttonsList.add(button);
199
                button.setText(text);
200
                button.setActionCommand(id + "");
201
                button.addActionListener(new ActionListener() {
202
      public void actionPerformed(ActionEvent e) {
203
              callActionCommandListeners(e.getActionCommand());
204
      }
205
                });
206
        
207
                add(button);
208
        }
209
        
210
        /**
211
         * Obtener un bot?n por su Entero
212
         * @param id N?mero del disparador del bot?n
213
         * @return El bot?n especificado o <code>null</code> si no se encontr? el bot?n.
214
         */
215
        public JButton getButton(int id) {
216
                Iterator acIterator = buttonsList.iterator();
217
          while (acIterator.hasNext()) {
218
                  JButton button = (JButton) acIterator.next();
219
                  if (button.getActionCommand().compareTo(id + "") == 0)
220
                          return button;
221
                 }
222
          return null;
223
        }
224
}