View Javadoc

1   /**
2    * Copyright 2008 WebPhotos
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.webphotos.action;
17  
18  import java.awt.event.ActionEvent;
19  import javax.swing.AbstractAction;
20  import javax.swing.JOptionPane;
21  import javax.swing.JTable;
22  import net.sf.webphotos.Album;
23  import net.sf.webphotos.gui.PainelWebFotos;
24  import net.sf.webphotos.gui.util.TableModelAlbum;
25  import net.sf.webphotos.gui.util.TableSorter;
26  import net.sf.webphotos.util.Util;
27  
28  /**
29   * Exclui albúns. Não possui dados próprios, por isso seu construtor é vazio. O
30   * método que realiza a ação, instancia dados apenas para verificação e chama o
31   * método de exclusão da classe Album no pacote webphotos.
32   */
33  public class AcaoExcluirAlbum extends AbstractAction {
34  
35      /**
36       *
37       */
38      private static final long serialVersionUID = 6469549565678592107L;
39  
40      /**
41       * Construtor da classe. Incialmente vazio, pois a classe não possui dados.
42       */
43      public AcaoExcluirAlbum() {
44      }
45  
46      /**
47       * Método responsável pela ação de exclusão de albúns. Inicia uma tabela de
48       * albúns para buscar as linhas selecionadas e checa quais e quantos foram
49       * selecionados. Faz um controle de exclusão de no máximo 20 albúns por vez.
50       * Lista os albúns selecionados ao usuário e pede uma confirmação de
51       * exclusão. Caso o usuário confirme, exclui os albúns selecionados através
52       * da função
53       * {@link net.sf.webphotos.Album#excluirAlbuns(int[]) excluirAlbuns(albunsID)}
54       * em {@link net.sf.webphotos.Album} e atualiza a lista e a aréa dos albúns no programa.
55       *
56       * @param e Evento de ação de exclusão de albúns.
57       */
58      @Override
59      public void actionPerformed(ActionEvent e) {
60  
61          JTable tbAlbuns = PainelWebFotos.getTbAlbuns();
62          String larguraColunas = Util.getConfig().getString("colunas1");
63  
64          // descobre os álbuns selecionados
65          int[] linhasSelecionadas = tbAlbuns.getSelectedRows();
66          int numeroLinhasSelecionadas = tbAlbuns.getSelectedRowCount();
67          String msg = "";
68  
69          // permite somente a exclusão de 20 álbuns de cada vez
70          if (numeroLinhasSelecionadas > 20 || numeroLinhasSelecionadas == 0) {
71              JOptionPane.showMessageDialog(null,
72                      "Você deve selecionar entre 1 e 20 álbuns\npara serem excluídos", "Informação",
73                      JOptionPane.INFORMATION_MESSAGE);
74              return;
75          }
76  
77          // solicita a confirmação da exclusão, listando os álbuns selecionados
78          for (int i = 0; i < numeroLinhasSelecionadas; i++) {
79              msg = msg + "\n" + TableModelAlbum.getModel().getValueAt(linhasSelecionadas[i], 0) + " - " + TableModelAlbum.getModel().getValueAt(linhasSelecionadas[i], 2);
80          }
81  
82          if (numeroLinhasSelecionadas == 1) {
83              msg = "Confirma a exclusão do álbum ?\n" + msg;
84          } else {
85              msg = "Confirma a exclusão de " + numeroLinhasSelecionadas + " álbuns ?\n" + msg;
86          }
87  
88          int confirmacao = JOptionPane.showConfirmDialog(null, msg, "Confirmação de exclusão", JOptionPane.WARNING_MESSAGE);
89  
90          // caso o usuário confirme, executa a exclusão (a cargo de Album)
91          if (confirmacao == 0) {
92              int[] albunsID = new int[numeroLinhasSelecionadas];
93              for (int i = 0; i < numeroLinhasSelecionadas; i++) {
94                  albunsID[i] = Integer.parseInt(TableModelAlbum.getModel().getValueAt(linhasSelecionadas[i], 0).toString());
95              }
96  
97              // executa a exclusão
98              Album.getAlbum().excluirAlbuns(albunsID);
99  
100             if (Util.getConfig().getBoolean("autoTransferir")) {
101                 Thread t = new Thread(new net.sf.webphotos.gui.util.FtpClient());
102                 t.start();
103             }
104 
105             TableModelAlbum.getModel().update();
106             TableModelAlbum.getModel().fireTableDataChanged();
107             //TableModelAlbum.getModel().addMouseListener(tbAlbuns);
108 
109             if (TableModelAlbum.getModel().getRowCount() > 1) {
110                 tbAlbuns.removeRowSelectionInterval(0, TableModelAlbum.getModel().getRowCount() - 1);
111             }
112 
113             tbAlbuns.setModel(new TableSorter(TableModelAlbum.getModel(), tbAlbuns.getTableHeader()));
114             Util.ajustaLargura(tbAlbuns, larguraColunas);
115             tbAlbuns.repaint();
116             PainelWebFotos.apresentaNumReg();
117 
118             // reseta área de álbum
119             PainelWebFotos.resetAlbum();
120             PainelWebFotos.alteracaoFinalizada();
121         }
122     }
123 }