|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package net.sf.webphotos.action;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import java.awt.event.ActionEvent;  | 
  |  19 |     | 
   import java.io.*;  | 
  |  20 |     | 
   import java.util.HashSet;  | 
  |  21 |     | 
   import java.util.Iterator;  | 
  |  22 |     | 
   import javax.swing.AbstractAction;  | 
  |  23 |     | 
   import javax.swing.JOptionPane;  | 
  |  24 |     | 
   import javax.swing.JTable;  | 
  |  25 |     | 
   import net.sf.webphotos.Album;  | 
  |  26 |     | 
   import net.sf.webphotos.gui.PainelWebFotos;  | 
  |  27 |     | 
   import net.sf.webphotos.gui.util.TableModelFoto;  | 
  |  28 |     | 
   import net.sf.webphotos.gui.util.TableSorter;  | 
  |  29 |     | 
   import net.sf.webphotos.util.Util;  | 
  |  30 |     | 
   import org.apache.log4j.Logger;  | 
  |  31 |     | 
     | 
  |  32 |     | 
     | 
  |  33 |     | 
     | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
   public class AcaoExcluirFoto extends AbstractAction { | 
  |  38 |     | 
     | 
  |  39 |     | 
         | 
  |  40 |     | 
     | 
  |  41 |     | 
     | 
  |  42 |     | 
       private static final long serialVersionUID = -6690995860578985531L;  | 
  |  43 |    3 |        private static final Logger log = Logger.getLogger(AcaoExcluirFoto.class);  | 
  |  44 |     | 
       private JTable tbFotos;  | 
  |  45 |     | 
       private String larguraColunasFotos;  | 
  |  46 |     | 
     | 
  |  47 |     | 
         | 
  |  48 |     | 
     | 
  |  49 |     | 
     | 
  |  50 |     | 
     | 
  |  51 |     | 
     | 
  |  52 |     | 
     | 
  |  53 |    3 |        public AcaoExcluirFoto(JTable tabela) { | 
  |  54 |    3 |            tbFotos = tabela;  | 
  |  55 |    3 |            larguraColunasFotos = Util.getConfig().getString("colunas2"); | 
  |  56 |    3 |        }  | 
  |  57 |     | 
     | 
  |  58 |     | 
         | 
  |  59 |     | 
     | 
  |  60 |     | 
     | 
  |  61 |     | 
     | 
  |  62 |     | 
     | 
  |  63 |     | 
     | 
  |  64 |     | 
     | 
  |  65 |     | 
     | 
  |  66 |     | 
     | 
  |  67 |     | 
     | 
  |  68 |     | 
     | 
  |  69 |     | 
     | 
  |  70 |     | 
     | 
  |  71 |     | 
     | 
  |  72 |     | 
     | 
  |  73 |     | 
     | 
  |  74 |     | 
       @Override  | 
  |  75 |     | 
       public void actionPerformed(ActionEvent e) { | 
  |  76 |    0 |            HashSet<String> fotosID = new HashSet<String>();  | 
  |  77 |    0 |            HashSet<String> nomesArquivos = new HashSet<String>();  | 
  |  78 |    0 |            int[] linhasSelecionadas = tbFotos.getSelectedRows();  | 
  |  79 |    0 |            int numeroLinhasSelecionadas = tbFotos.getSelectedRowCount();  | 
  |  80 |    0 |            String msg = "";  | 
  |  81 |     | 
     | 
  |  82 |    0 |            if (tbFotos.getRowCount() == 1) { | 
  |  83 |    0 |                int retorno = JOptionPane.showConfirmDialog(null,  | 
  |  84 |     | 
                       "Esta é a última foto deste álbum.\nExcluir essa foto irá excluir seu álbum.",  | 
  |  85 |     | 
                       "Excluir álbum ?", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);  | 
  |  86 |    0 |                if (retorno == 0) { | 
  |  87 |     | 
                     | 
  |  88 |    0 |                    Util.out.println("remove album: " + Album.getAlbum().getAlbumID()); | 
  |  89 |    0 |                    return;  | 
  |  90 |     | 
               }  | 
  |  91 |     | 
                 | 
  |  92 |    0 |                return;  | 
  |  93 |     | 
           }  | 
  |  94 |     | 
             | 
  |  95 |    0 |            if (numeroLinhasSelecionadas > 20 || numeroLinhasSelecionadas == 0) { | 
  |  96 |    0 |                JOptionPane.showMessageDialog(null,  | 
  |  97 |     | 
                       "Você deve selecionar entre 1 e 20 fotos\npara serem excluídas", "Informação",  | 
  |  98 |     | 
                       JOptionPane.INFORMATION_MESSAGE);  | 
  |  99 |    0 |                return;  | 
  |  100 |     | 
           }  | 
  |  101 |     | 
     | 
  |  102 |     | 
             | 
  |  103 |    0 |            for (int i = 0; i < numeroLinhasSelecionadas; i++) { | 
  |  104 |    0 |                msg = msg + "\n" + tbFotos.getModel().getValueAt(linhasSelecionadas[i], 0) + " - " + tbFotos.getModel().getValueAt(linhasSelecionadas[i], 1);  | 
  |  105 |     | 
           }  | 
  |  106 |     | 
     | 
  |  107 |    0 |            if (numeroLinhasSelecionadas == 1) { | 
  |  108 |    0 |                msg = "Confirma a exclusão da foto ?\n" + msg;  | 
  |  109 |     | 
           } else { | 
  |  110 |    0 |                msg = "Confirma a exclusão de " + numeroLinhasSelecionadas + " fotos ?\n" + msg;  | 
  |  111 |     | 
           }  | 
  |  112 |    0 |            int confirmacao = JOptionPane.showConfirmDialog(null, msg, "Confirmação de exclusão", JOptionPane.WARNING_MESSAGE);  | 
  |  113 |     | 
     | 
  |  114 |     | 
             | 
  |  115 |    0 |            if (confirmacao == 0) { | 
  |  116 |     | 
                 | 
  |  117 |     | 
                 | 
  |  118 |    0 |                String indice = "";  | 
  |  119 |     | 
     | 
  |  120 |    0 |                for (int i = 0; i < numeroLinhasSelecionadas; i++) { | 
  |  121 |    0 |                    indice = tbFotos.getModel().getValueAt(linhasSelecionadas[i], 0).toString();  | 
  |  122 |     | 
     | 
  |  123 |     | 
                     | 
  |  124 |    0 |                    if (indice.toLowerCase().endsWith(".jpg")) { | 
  |  125 |    0 |                        nomesArquivos.add(indice);  | 
  |  126 |     | 
                   } else { | 
  |  127 |    0 |                        fotosID.add(indice);  | 
  |  128 |     | 
                   }  | 
  |  129 |     | 
               }  | 
  |  130 |     | 
     | 
  |  131 |    0 |                Album album = Album.getAlbum();  | 
  |  132 |     | 
     | 
  |  133 |     | 
                 | 
  |  134 |    0 |                if (nomesArquivos.size() > 0) { | 
  |  135 |     | 
                     | 
  |  136 |    0 |                    Util.out.println("nomesArquivos: " + nomesArquivos.toString()); | 
  |  137 |    0 |                    album.excluirFotos((String[]) nomesArquivos.toArray(new String[nomesArquivos.size()]));  | 
  |  138 |     | 
               }  | 
  |  139 |     | 
     | 
  |  140 |    0 |                if (fotosID.size() > 0) { | 
  |  141 |     | 
                     | 
  |  142 |    0 |                    Iterator<String> iter = fotosID.iterator();  | 
  |  143 |    0 |                    int[] fotoID = new int[fotosID.size()];  | 
  |  144 |    0 |                    int ct = 0;  | 
  |  145 |    0 |                    while (iter.hasNext()) { | 
  |  146 |    0 |                        fotoID[ct] = Integer.parseInt(iter.next().toString());  | 
  |  147 |    0 |                        ct++;  | 
  |  148 |     | 
                   }  | 
  |  149 |     | 
     | 
  |  150 |    0 |                    album.excluirFotos(fotoID);  | 
  |  151 |     | 
     | 
  |  152 |     | 
                     | 
  |  153 |    0 |                    String caminhoAlbum = Util.getFolder("albunsRoot").getPath() + File.separator + album.getAlbumID(); | 
  |  154 |     | 
                   try { | 
  |  155 |    0 |                        FileWriter out = new FileWriter(caminhoAlbum + File.separator + album.getAlbumID() + ".js");  | 
  |  156 |    0 |                        out.write(album.toJavaScript());  | 
  |  157 |    0 |                        out.flush();  | 
  |  158 |    0 |                        Util.out.println("escrevendo: " + album.toJavaScript()); | 
  |  159 |     | 
     | 
  |  160 |    0 |                    } catch (IOException ex) { | 
  |  161 |    0 |                        log.error(ex);  | 
  |  162 |    0 |                    }  | 
  |  163 |     | 
               }  | 
  |  164 |     | 
     | 
  |  165 |     | 
                 | 
  |  166 |     | 
                 | 
  |  167 |    0 |                TableModelFoto.getModel().update();  | 
  |  168 |    0 |                TableModelFoto.getModel().fireTableDataChanged();  | 
  |  169 |     | 
                 | 
  |  170 |     | 
                 | 
  |  171 |    0 |                tbFotos.setModel(new TableSorter(TableModelFoto.getModel(), tbFotos.getTableHeader()));  | 
  |  172 |     | 
     | 
  |  173 |     | 
                 | 
  |  174 |    0 |                Util.ajustaLargura(tbFotos, larguraColunasFotos);  | 
  |  175 |    0 |                tbFotos.repaint();  | 
  |  176 |     | 
     | 
  |  177 |     | 
                 | 
  |  178 |    0 |                PainelWebFotos.resetFoto();  | 
  |  179 |     | 
           }  | 
  |  180 |    0 |        }  | 
  |  181 |     | 
   }  |