Statistics
| Revision:

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

History | View | Annotate | Download (4.5 KB)

1
package org.gvsig.gui.beans.buttonsPanel;
2

    
3
import java.awt.FlowLayout;
4
import java.awt.event.ActionEvent;
5
import java.awt.event.ActionListener;
6
import java.util.ArrayList;
7
import java.util.Iterator;
8

    
9
import javax.swing.JButton;
10
import javax.swing.JPanel;
11

    
12
import org.gvsig.gui.beans.messages.Messages;
13
/**
14
 * <code>ButtonsPanel</code> ofrece un widget con un conjunto de botones
15
 * preestablecidos, aunque tambi?n se pueden a?adir botones con el m?todo
16
 * {@link #addButton(String, int)}
17
 * 
18
 * @version 03/15/07
19
 * @author Borja S?nchez Zamorano (borja.sanchez@iver.es)
20
 */
21
public class ButtonsPanel extends JPanel {
22
        private ArrayList buttonsList = new ArrayList();
23
        static private int eventId = Integer.MIN_VALUE;
24
        private ArrayList actionCommandListeners = new ArrayList();
25

    
26
        public static final int BUTTON_ACCEPT = 1;
27
        public static final int BUTTON_CANCEL = 2;
28
        public static final int BUTTON_APPLY = 3;
29
        public static final int BUTTON_YES = 4;
30
        public static final int BUTTON_NO = 5;
31
        public static final int BUTTON_CLOSE = 6;
32
        public static final int BUTTON_EXIT = 7;
33
        
34
        public static final int BUTTONS_ACCEPT = 1;
35
        public static final int BUTTONS_ACCEPTCANCEL = 2;
36
        public static final int BUTTONS_ACCEPTCANCELAPPLY = 3;
37
        public static final int BUTTONS_YESNO = 4;
38
        public static final int BUTTONS_CLOSE = 5;
39
        public static final int BUTTONS_EXIT = 6;
40
        public static final int BUTTONS_NONE = 7;
41

    
42
        /**
43
         * Crea un ButtonsPanel con un Layout por defecto.
44
         */
45
        public ButtonsPanel() {
46
                setLayout(new java.awt.FlowLayout(FlowLayout.RIGHT));
47
        }
48
        
49
        /**
50
         * Crea un ButtonsPanel con un Layout por defecto.
51
         * 
52
         * @param items Que botones vamos a usar en la creaci?n.
53
         */
54
        public ButtonsPanel(int items) {
55
                setLayout(new java.awt.FlowLayout(FlowLayout.RIGHT));
56
                switch (items) {
57
                        case BUTTONS_ACCEPT:
58
                                addAccept();
59
                                break;
60
                        case BUTTONS_ACCEPTCANCEL:
61
                                addAccept();
62
                                addCancel();
63
                                break;
64
                        case BUTTONS_ACCEPTCANCELAPPLY:
65
                                addAccept();
66
                                addCancel();
67
                                addApply();
68
                                break;
69
                        case BUTTONS_YESNO:
70
                                addYes();
71
                                addNo();
72
                                break;
73
                        case BUTTONS_CLOSE:
74
                                addClose();
75
                                break;
76
                        case BUTTONS_EXIT:
77
                                addExit();
78
                                break;
79
                        case BUTTONS_NONE:
80
                                break;
81
                }
82
        }
83

    
84
        public void addActionListener(ActionListener listener) {
85
          if (!actionCommandListeners.contains(listener))
86
            actionCommandListeners.add(listener);
87
        }
88

    
89
        public void removeActionListener(ActionListener listener) {
90
          actionCommandListeners.remove(listener);
91
        }
92

    
93
        private void callActionCommandListeners(String buttonID) {
94
          Iterator acIterator = actionCommandListeners.iterator();
95
          while (acIterator.hasNext()) {
96
            ActionListener listener = (ActionListener) acIterator.next();
97
            listener.actionPerformed(new ActionEvent(this, eventId, buttonID));
98
          }
99
          eventId++;
100
        }
101

    
102
        /**
103
         * A?adir el boton Aceptar.
104
         */
105
        public void addAccept() {
106
                addButton(Messages.getText("aceptar"), BUTTON_ACCEPT);
107
        }
108
        
109
        /**
110
         * A?adir el boton Cancelar.
111
         */
112
        public void addCancel() {
113
                addButton(Messages.getText("cancelar"), BUTTON_CANCEL);
114
        }
115
        
116
        /**
117
         * A?adir el boton S?.
118
         */
119
        public void addYes() {
120
                addButton(Messages.getText("si"), BUTTON_YES);
121
        }
122

    
123
        /**
124
         * A?adir el boton No.
125
         */
126
        public void addNo() {
127
                addButton(Messages.getText("no"), BUTTON_NO);
128
        }
129

    
130
        /**
131
         * A?adir el boton Aplicar.
132
         */
133
        public void addApply() {
134
                addButton(Messages.getText("aplicar"), BUTTON_APPLY);
135
        }
136

    
137
        /**
138
         * A?adir el boton Cerrar.
139
         */
140
        public void addClose() {
141
                addButton(Messages.getText("cerrar"), BUTTON_CLOSE);
142
        }
143

    
144
        /**
145
         * A?adir el boton Salir.
146
         */
147
        public void addExit() {
148
                addButton(Messages.getText("salir"), BUTTON_EXIT);
149
        }
150

    
151
        /**
152
         * A?adimos un bot?n definido por el usuario.
153
         * 
154
         * @param text Texto que contendr? el bot?n
155
         * @param id Entero para identificar los eventos del bot?n
156
         */
157
        public void addButton(String text, int id) {
158
                JButton button = new JButton();
159
                buttonsList.add(button);
160
                button.setText(text);
161
                button.setActionCommand(id + "");
162
                button.addActionListener(new ActionListener() {
163
      public void actionPerformed(ActionEvent e) {
164
              callActionCommandListeners(e.getActionCommand());
165
      }
166
                });
167
        
168
                add(button);
169
        }
170
        
171
        /**
172
         * Obtener un bot?n por su Entero
173
         * @param id N?mero del disparador del bot?n
174
         * @return El bot?n especificado o <code>null</code> si no se encontr? el bot?n.
175
         */
176
        public JButton getButton(int id) {
177
                Iterator acIterator = buttonsList.iterator();
178
          while (acIterator.hasNext()) {
179
                  JButton button = (JButton) acIterator.next();
180
                  if (button.getActionCommand().compareTo(id + "") == 0)
181
                          return button;
182
                 }
183
          return null;
184
        }
185
}