Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / buttonsPanel / ButtonsPanel.java @ 10795

History | View | Annotate | Download (5.41 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
        
53
        public static final int BUTTONS_ACCEPT = 1;
54
        public static final int BUTTONS_ACCEPTCANCEL = 2;
55
        public static final int BUTTONS_ACCEPTCANCELAPPLY = 3;
56
        public static final int BUTTONS_YESNO = 4;
57
        public static final int BUTTONS_CLOSE = 5;
58
        public static final int BUTTONS_EXIT = 6;
59
        public static final int BUTTONS_NONE = 7;
60

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

    
103
        public void addActionListener(ActionListener listener) {
104
          if (!actionCommandListeners.contains(listener))
105
            actionCommandListeners.add(listener);
106
        }
107

    
108
        public void removeActionListener(ActionListener listener) {
109
          actionCommandListeners.remove(listener);
110
        }
111

    
112
        private void callActionCommandListeners(String buttonID) {
113
          Iterator acIterator = actionCommandListeners.iterator();
114
          while (acIterator.hasNext()) {
115
            ActionListener listener = (ActionListener) acIterator.next();
116
            listener.actionPerformed(new ActionEvent(this, eventId, buttonID));
117
          }
118
          eventId++;
119
        }
120

    
121
        /**
122
         * A?adir el boton Aceptar.
123
         */
124
        public void addAccept() {
125
                addButton(Messages.getText("aceptar"), BUTTON_ACCEPT);
126
        }
127
        
128
        /**
129
         * A?adir el boton Cancelar.
130
         */
131
        public void addCancel() {
132
                addButton(Messages.getText("cancelar"), BUTTON_CANCEL);
133
        }
134
        
135
        /**
136
         * A?adir el boton S?.
137
         */
138
        public void addYes() {
139
                addButton(Messages.getText("si"), BUTTON_YES);
140
        }
141

    
142
        /**
143
         * A?adir el boton No.
144
         */
145
        public void addNo() {
146
                addButton(Messages.getText("no"), BUTTON_NO);
147
        }
148

    
149
        /**
150
         * A?adir el boton Aplicar.
151
         */
152
        public void addApply() {
153
                addButton(Messages.getText("aplicar"), BUTTON_APPLY);
154
        }
155

    
156
        /**
157
         * A?adir el boton Cerrar.
158
         */
159
        public void addClose() {
160
                addButton(Messages.getText("cerrar"), BUTTON_CLOSE);
161
        }
162

    
163
        /**
164
         * A?adir el boton Salir.
165
         */
166
        public void addExit() {
167
                addButton(Messages.getText("salir"), BUTTON_EXIT);
168
        }
169

    
170
        /**
171
         * A?adimos un bot?n definido por el usuario.
172
         * 
173
         * @param text Texto que contendr? el bot?n
174
         * @param id Entero para identificar los eventos del bot?n
175
         */
176
        public void addButton(String text, int id) {
177
                JButton button = new JButton();
178
                buttonsList.add(button);
179
                button.setText(text);
180
                button.setActionCommand(id + "");
181
                button.addActionListener(new ActionListener() {
182
      public void actionPerformed(ActionEvent e) {
183
              callActionCommandListeners(e.getActionCommand());
184
      }
185
                });
186
        
187
                add(button);
188
        }
189
        
190
        /**
191
         * Obtener un bot?n por su Entero
192
         * @param id N?mero del disparador del bot?n
193
         * @return El bot?n especificado o <code>null</code> si no se encontr? el bot?n.
194
         */
195
        public JButton getButton(int id) {
196
                Iterator acIterator = buttonsList.iterator();
197
          while (acIterator.hasNext()) {
198
                  JButton button = (JButton) acIterator.next();
199
                  if (button.getActionCommand().compareTo(id + "") == 0)
200
                          return button;
201
                 }
202
          return null;
203
        }
204
}