001: /*
002: * Created on 10/08/2004
003: *
004: * ============================================================================
005: * GNU Lesser General Public License
006: * ============================================================================
007: *
008: * Swing Components - visit http://sf.net/projects/gfd
009: *
010: * Copyright (C) 2004 Igor Regis da Silva Simões
011: *
012: * This library is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU Lesser General Public
014: * License as published by the Free Software Foundation; either
015: * version 2.1 of the License, or (at your option) any later version.
016: *
017: * This library is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
020: * Lesser General Public License for more details.
021: *
022: * You should have received a copy of the GNU Lesser General Public
023: * License along with this library; if not, write to the Free Software
024: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
025: */
026:
027: package br.com.gfpshare.beans.aplicativos;
028:
029: import java.awt.Component;
030: import java.awt.GridBagConstraints;
031: import java.awt.GridBagLayout;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.util.ArrayList;
035:
036: import javax.swing.JButton;
037: import javax.swing.JFrame;
038: import javax.swing.JPanel;
039:
040: /**
041: * @author Igor Regis da Silva Simoes
042: */
043: public class PainelBotoes extends JPanel implements ActionListener,
044: Acoes {
045: private JButton jbAtualizar = new JButton();
046:
047: private JButton jbExcluir = new JButton();
048:
049: private JButton jbLimpar = new JButton();
050:
051: private JButton jbNovo = new JButton();
052:
053: private JButton jbPesquisar = new JButton();
054:
055: private JButton jbSalvar = new JButton();
056:
057: private JButton jbFiltrar = new JButton();
058:
059: private ArrayList botoes = new ArrayList(7);
060:
061: private boolean ocorreuErro = false;
062:
063: /**
064: * Creates new form BeanForm
065: */
066: public PainelBotoes() {
067: initComponents();
068: addActionListener(this );
069: }
070:
071: private void initComponents() {
072: GridBagConstraints gridBagConstraints;
073: this .setLayout(new GridBagLayout());
074: jbNovo.setIcon(IconeFactory.getIconeFactory().getImageIcon(
075: IconeFactory.NOVO_PEQUENA));
076: jbNovo.setMnemonic(PainelBotoesMessages.getMessages()
077: .getString("NovoMnemonic").charAt(0));
078: jbNovo.setToolTipText(PainelBotoesMessages.getMessages()
079: .getString("NovoToolTip"));
080: jbNovo.setText(PainelBotoesMessages.getMessages().getString(
081: "Novo")); //$NON-NLS-1$
082: jbNovo.setName(PainelBotoesMessages.getMessages().getString(
083: "Novo")); //$NON-NLS-1$
084: jbNovo.setActionCommand(NOVO_COMMAND);
085: gridBagConstraints = new GridBagConstraints();
086: gridBagConstraints.gridx = 0;
087: gridBagConstraints.gridy = 0;
088: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
089: this .add(jbNovo, gridBagConstraints);
090: botoes.add(jbNovo);
091:
092: jbSalvar.setIcon(IconeFactory.getIconeFactory().getImageIcon(
093: IconeFactory.SALVAR_PEQUENA));
094: jbSalvar.setMnemonic(PainelBotoesMessages.getMessages()
095: .getString("SalvarMnemonic").charAt(0));
096: jbSalvar.setToolTipText(PainelBotoesMessages.getMessages()
097: .getString("SalvarToolTip"));
098: jbSalvar.setText(PainelBotoesMessages.getMessages().getString(
099: "Salvar")); //$NON-NLS-1$
100: jbSalvar.setName(PainelBotoesMessages.getMessages().getString(
101: "Salvar")); //$NON-NLS-1$
102: jbSalvar.setEnabled(false);
103: jbSalvar.setActionCommand(SALVAR_COMMAND);
104: gridBagConstraints = new GridBagConstraints();
105: gridBagConstraints.gridx = 0;
106: gridBagConstraints.gridy = 1;
107: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
108: gridBagConstraints.weightx = 1.0;
109: this .add(jbSalvar, gridBagConstraints);
110: botoes.add(jbSalvar);
111:
112: jbExcluir.setIcon(IconeFactory.getIconeFactory().getImageIcon(
113: IconeFactory.EXCLUIR_PEQUENA));
114: jbExcluir.setMnemonic(PainelBotoesMessages.getMessages()
115: .getString("ExcluirMnemonic").charAt(0));
116: jbExcluir.setToolTipText(PainelBotoesMessages.getMessages()
117: .getString("ExcluirToolTip"));
118: jbExcluir.setText(PainelBotoesMessages.getMessages().getString(
119: "Excluir")); //$NON-NLS-1$
120: jbExcluir.setName(PainelBotoesMessages.getMessages().getString(
121: "Excluir")); //$NON-NLS-1$
122: jbExcluir.setEnabled(false);
123: jbExcluir.setActionCommand(EXCLUIR_COMMAND);
124: gridBagConstraints = new GridBagConstraints();
125: gridBagConstraints.gridx = 0;
126: gridBagConstraints.gridy = 2;
127: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
128: gridBagConstraints.weightx = 1.0;
129: this .add(jbExcluir, gridBagConstraints);
130: botoes.add(jbExcluir);
131:
132: jbLimpar.setIcon(IconeFactory.getIconeFactory().getImageIcon(
133: IconeFactory.LIMPAR_PEQUENA));
134: jbLimpar.setMnemonic(PainelBotoesMessages.getMessages()
135: .getString("LimparMnemonic").charAt(0));
136: jbLimpar.setToolTipText(PainelBotoesMessages.getMessages()
137: .getString("LimparToolTip"));
138: jbLimpar.setText(PainelBotoesMessages.getMessages().getString(
139: "Limpar")); //$NON-NLS-1$
140: jbLimpar.setName(PainelBotoesMessages.getMessages().getString(
141: "Limpar")); //$NON-NLS-1$
142: jbLimpar.setActionCommand(LIMPAR_COMMAND);
143: gridBagConstraints = new GridBagConstraints();
144: gridBagConstraints.gridx = 0;
145: gridBagConstraints.gridy = 3;
146: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
147: gridBagConstraints.weightx = 1.0;
148: this .add(jbLimpar, gridBagConstraints);
149: botoes.add(jbLimpar);
150:
151: jbPesquisar.setIcon(IconeFactory.getIconeFactory()
152: .getImageIcon(IconeFactory.PESQUISAR_PEQUENA));
153: jbPesquisar.setMnemonic(PainelBotoesMessages.getMessages()
154: .getString("PesquisarMnemonic").charAt(0));
155: jbPesquisar.setToolTipText(PainelBotoesMessages.getMessages()
156: .getString("PesquisarToolTip"));
157: jbPesquisar.setText(PainelBotoesMessages.getMessages()
158: .getString("Pesquisar")); //$NON-NLS-1$
159: jbPesquisar.setName(PainelBotoesMessages.getMessages()
160: .getString("Pesquisar")); //$NON-NLS-1$
161: jbPesquisar.setActionCommand(PESQUISAR_COMMAND);
162: gridBagConstraints = new GridBagConstraints();
163: gridBagConstraints.gridx = 0;
164: gridBagConstraints.gridy = 4;
165: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
166: gridBagConstraints.anchor = GridBagConstraints.NORTH;
167: gridBagConstraints.weightx = 1.0;
168: this .add(jbPesquisar, gridBagConstraints);
169: botoes.add(jbPesquisar);
170:
171: jbAtualizar.setIcon(IconeFactory.getIconeFactory()
172: .getImageIcon(IconeFactory.ATUALIZAR_PEQUENA));
173: jbAtualizar.setMnemonic(PainelBotoesMessages.getMessages()
174: .getString("AtualizarMnemonic").charAt(0));
175: jbAtualizar.setToolTipText(PainelBotoesMessages.getMessages()
176: .getString("AtualizarToolTip"));
177: jbAtualizar.setText(PainelBotoesMessages.getMessages()
178: .getString("Atualizar")); //$NON-NLS-1$
179: jbAtualizar.setName(PainelBotoesMessages.getMessages()
180: .getString("Atualizar")); //$NON-NLS-1$
181: jbAtualizar.setActionCommand(ATUALIZAR_COMMAND);
182: gridBagConstraints = new GridBagConstraints();
183: gridBagConstraints.gridx = 0;
184: gridBagConstraints.gridy = 5;
185: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
186: gridBagConstraints.anchor = GridBagConstraints.NORTH;
187: gridBagConstraints.weightx = 1.0;
188: this .add(jbAtualizar, gridBagConstraints);
189: botoes.add(jbAtualizar);
190:
191: jbFiltrar.setIcon(IconeFactory.getIconeFactory().getImageIcon(
192: IconeFactory.FILTARAR_PEQUENA));
193: jbFiltrar.setMnemonic(PainelBotoesMessages.getMessages()
194: .getString("FiltrarMnemonic").charAt(0));
195: jbFiltrar.setToolTipText(PainelBotoesMessages.getMessages()
196: .getString("FiltrarToolTip"));
197: jbFiltrar.setText(PainelBotoesMessages.getMessages().getString(
198: "Filtrar")); //$NON-NLS-1$
199: jbFiltrar.setName(PainelBotoesMessages.getMessages().getString(
200: "Filtrar")); //$NON-NLS-1$
201: jbFiltrar.setActionCommand(FILTRAR_COMMAND);
202: gridBagConstraints = new GridBagConstraints();
203: gridBagConstraints.gridx = 0;
204: gridBagConstraints.gridy = 6;
205: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
206: gridBagConstraints.anchor = GridBagConstraints.NORTH;
207: gridBagConstraints.weightx = 1.0;
208: gridBagConstraints.weighty = 1.0;
209: this .add(jbFiltrar, gridBagConstraints);
210: botoes.add(jbFiltrar);
211: }
212:
213: /**
214: * @param b
215: */
216: public void add(JButton b) {
217: int nUltimo = getComponentCount();
218: if (nUltimo > 0) {
219: java.awt.Component ultimo = getComponent(nUltimo - 1);
220: remove(ultimo);
221: GridBagConstraints gridBagConstraints = new GridBagConstraints();
222: gridBagConstraints.gridx = 0;
223: gridBagConstraints.gridy = nUltimo;
224: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
225: gridBagConstraints.anchor = GridBagConstraints.NORTH;
226: gridBagConstraints.weightx = 1.0;
227: super .add(ultimo, gridBagConstraints);
228: botoes.add(ultimo);
229: }
230: GridBagConstraints gridBagConstraints = new GridBagConstraints();
231: gridBagConstraints.gridx = 0;
232: gridBagConstraints.gridy = nUltimo + 1;
233: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
234: gridBagConstraints.anchor = GridBagConstraints.NORTH;
235: gridBagConstraints.weightx = 1.0;
236: gridBagConstraints.weighty = 1.0;
237: super .add(b, gridBagConstraints);
238: botoes.add(b);
239: }
240:
241: /**
242: * @see java.awt.Container#remove(java.awt.Component)
243: */
244: @Override
245: public void remove(Component comp) {
246: super .remove(comp);
247: botoes.remove(comp);
248: }
249:
250: /**
251: * @see java.awt.Container#remove(int)
252: */
253: @Override
254: public void remove(int index) {
255: super .remove(index);
256: botoes.remove(index);
257: }
258:
259: /**
260: * @see java.awt.Container#removeAll()
261: */
262: @Override
263: public void removeAll() {
264: super .removeAll();
265: botoes.removeAll(botoes);
266: }
267:
268: /**
269: * @param l
270: */
271: public void addActionListener(ActionListener l) {
272: if (l != this ) {
273: removeActionListener(this );
274: }
275:
276: for (int i = 0; i < botoes.size(); i++) {
277: ((JButton) botoes.get(i)).addActionListener(l);
278: }
279:
280: if (l != this ) {
281: addActionListener(this );
282: }
283: }
284:
285: /**
286: * @param l
287: */
288: public void removeActionListener(ActionListener l) {
289: for (int i = 0; i < botoes.size(); i++) {
290: ((JButton) botoes.get(i)).removeActionListener(l);
291: }
292: }
293:
294: /**
295: * @param botao
296: * @param l
297: */
298: public void addActionListener(String botao, ActionListener l) {
299: if (!botao.equalsIgnoreCase(SALVAR_COMMAND)
300: && !botao.equalsIgnoreCase(EXCLUIR_COMMAND)
301: && !botao.equalsIgnoreCase(NOVO_COMMAND)) {
302: removeActionListener(botao, this );
303: }
304:
305: for (int i = 0; i < botoes.size(); i++) {
306: if (((JButton) botoes.get(i)).getActionCommand()
307: .equalsIgnoreCase(botao))
308: ((JButton) botoes.get(i)).addActionListener(l);
309: }
310:
311: if (!botao.equalsIgnoreCase(SALVAR_COMMAND)
312: && !botao.equalsIgnoreCase(EXCLUIR_COMMAND)
313: && !botao.equalsIgnoreCase(NOVO_COMMAND)) {
314: for (int i = 0; i < botoes.size(); i++) {
315: if (((JButton) botoes.get(i)).getActionCommand()
316: .equalsIgnoreCase(botao))
317: ((JButton) botoes.get(i)).addActionListener(this );
318: }
319: }
320: }
321:
322: /**
323: * @param botao
324: * @param l
325: */
326: public void removeActionListener(String botao, ActionListener l) {
327: for (int i = 0; i < botoes.size(); i++) {
328: if (((JButton) botoes.get(i)).getActionCommand()
329: .equalsIgnoreCase(botao))
330: ((JButton) botoes.get(i)).removeActionListener(l);
331: }
332: }
333:
334: /**
335: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
336: */
337: public void actionPerformed(ActionEvent e) {
338: if (e.getActionCommand().equalsIgnoreCase(NOVO_COMMAND)) {
339: dadosCarregados(true);
340: jbSalvar.doClick();
341: } else if (e.getActionCommand()
342: .equalsIgnoreCase(SALVAR_COMMAND)
343: || e.getActionCommand().equalsIgnoreCase(
344: EXCLUIR_COMMAND)) {
345: if (!ocorreuErro)
346: jbLimpar.doClick();
347: else
348: dadosCarregados(false);
349:
350: ocorreuErro = false;
351: } else if (e.getActionCommand()
352: .equalsIgnoreCase(LIMPAR_COMMAND)) {
353: dadosCarregados(false);
354: }
355: }
356:
357: /**
358: * @param temDados
359: */
360: public void dadosCarregados(boolean temDados) {
361: jbNovo.setEnabled(!temDados);
362: jbPesquisar.setEnabled(!temDados);
363: jbFiltrar.setEnabled(!temDados);
364:
365: jbSalvar.setEnabled(temDados);
366: jbExcluir.setEnabled(temDados);
367: }
368:
369: /**
370: * @param args
371: * TODO Remover
372: */
373: public static void main(String[] args) {
374: JFrame f = new JFrame();
375: f.setSize(110, 350);
376: f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
377: PainelBotoes p = new PainelBotoes();
378: JButton teste = new JButton();
379: teste.setMnemonic('A');
380: teste.setText("Teste"); //$NON-NLS-1$
381: teste.setName("Teste"); //$NON-NLS-1$
382: teste.setActionCommand(ATUALIZAR_COMMAND);
383: p.add(teste);
384: p.validate();
385: f.setContentPane(p);
386: f.setVisible(true);
387: }
388:
389: /**
390: * Indica se ocorreu erro no tratamento dos dados que serão salvos pelo botão salvar
391: * @return booean
392: */
393: public boolean isOcorreuErro() {
394: return ocorreuErro;
395: }
396:
397: /**
398: * Indica se ocorreu erro no tratamento dos dados que serão salvos pelo botão salvar
399: * @param ocorreuErro
400: */
401: public void setOcorreuErro(boolean ocorreuErro) {
402: this .ocorreuErro = ocorreuErro;
403: }
404:
405: public void setEnableCommand(String command, boolean enable) {
406: if (command.equals(NOVO_COMMAND)) {
407: jbNovo.setVisible(enable);
408: } else if (command.equals(ATUALIZAR_COMMAND)) {
409: jbAtualizar.setVisible(enable);
410: } else if (command.equals(EXCLUIR_COMMAND)) {
411: jbExcluir.setVisible(enable);
412: } else if (command.equals(LIMPAR_COMMAND)) {
413: jbLimpar.setVisible(enable);
414: } else if (command.equals(FILTRAR_COMMAND)) {
415: jbFiltrar.setVisible(enable);
416: } else if (command.equals(PESQUISAR_COMMAND)) {
417: jbPesquisar.setVisible(enable);
418: }
419: }
420: }
|