| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AcaoPesquisa |
|
| 4.0;4 |
| 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.JComboBox; | |
| 21 | import javax.swing.JTable; | |
| 22 | import javax.swing.JTextField; | |
| 23 | import net.sf.webphotos.Album; | |
| 24 | import net.sf.webphotos.gui.PainelWebFotos; | |
| 25 | import net.sf.webphotos.gui.util.TableModelAlbum; | |
| 26 | import net.sf.webphotos.util.Util; | |
| 27 | import org.apache.commons.configuration.Configuration; | |
| 28 | ||
| 29 | /** | |
| 30 | * Pesquisa elementos no programa. Possui um método que organiza buscas através | |
| 31 | * da quantidade de parâmetros passados ao construtor, com o campo de texto ou | |
| 32 | * com o ID da categoria. Ao terminar a pesquisa, apresenta a quantidade de | |
| 33 | * registros encontrados. | |
| 34 | */ | |
| 35 | public class AcaoPesquisa extends AbstractAction { | |
| 36 | ||
| 37 | /** | |
| 38 | * | |
| 39 | */ | |
| 40 | private static final long serialVersionUID = -3050139194212291060L; | |
| 41 | private JComboBox lstCategoriasPesquisa; | |
| 42 | private JTextField txtPesquisa; | |
| 43 | 0 | private Configuration c = Util.getConfig(); |
| 44 | ||
| 45 | /** | |
| 46 | * Construtro da classe. Recebe três parâmetros. Uma lista inicial de | |
| 47 | * categorias, um campo de texto com um valor a ser pesquisado e uma tabela | |
| 48 | * de albúm. Seta esses três valores em variáveis da classe que serão | |
| 49 | * utilizadas posteriormente. | |
| 50 | * | |
| 51 | * @param lst Lista de categorias. | |
| 52 | * @param txt Campo de texto com valor a ser pesquisado. | |
| 53 | * @param tb Tabela de albúm. | |
| 54 | */ | |
| 55 | 0 | public AcaoPesquisa(JComboBox lst, JTextField txt, JTable tb) { |
| 56 | 0 | lstCategoriasPesquisa = lst; |
| 57 | 0 | txtPesquisa = txt; |
| 58 | 0 | } |
| 59 | ||
| 60 | /** | |
| 61 | * Faz as pesquisas ao banco utilizando os valores de ID ou com o campo de | |
| 62 | * texto de pesquisa. Faz a busca com essas variáveis de quatro maneiras | |
| 63 | * diferentes. Com categoria sem pesquisa, sem categoria e pesquisa, com | |
| 64 | * categoria e pesquisa, e sem categoria com pesquisa. Após definir o tipo | |
| 65 | * de busca, trata possíveis exceções e atualiza o modelo do albúm. | |
| 66 | * Apresenta o número de registros encontrados e tira o cursor do modo de | |
| 67 | * espera. | |
| 68 | * | |
| 69 | * @param e Evento da ação de pesquisa. | |
| 70 | */ | |
| 71 | @Override | |
| 72 | public void actionPerformed(ActionEvent e) { | |
| 73 | String sql; | |
| 74 | // ação pesquisar (clique em botao pesquisar ou tecla ENTER | |
| 75 | 0 | PainelWebFotos.setCursorWait(true); |
| 76 | 0 | int categoriaID = Album.getAlbum().getLstCategoriasID(lstCategoriasPesquisa.getSelectedItem().toString()); |
| 77 | 0 | String strCategoriaID = Integer.toString(categoriaID); |
| 78 | 0 | String pesquisa = txtPesquisa.getText(); |
| 79 | ||
| 80 | // com categoria sem pesquisa | |
| 81 | // apresenta todos os registros dessa categoria | |
| 82 | 0 | if (pesquisa.length() == 0 && categoriaID != -1) { |
| 83 | 0 | sql = c.getString("sql5"); |
| 84 | 0 | sql = sql.replaceFirst("\\?2", strCategoriaID); |
| 85 | ||
| 86 | // sem categoria sem pesquisa | |
| 87 | // apresenta os registros conforme sql1 | |
| 88 | 0 | } else if (pesquisa.length() == 0 && categoriaID == -1) { |
| 89 | 0 | sql = c.getString("sql1"); |
| 90 | ||
| 91 | // com categoria e com pesquisa | |
| 92 | 0 | } else if (pesquisa.length() > 0 && categoriaID != -1) { |
| 93 | 0 | sql = c.getString("sql4"); |
| 94 | // substitui ?1 pela chave de pesquisa | |
| 95 | 0 | sql = sql.replaceFirst("\\?1", pesquisa); |
| 96 | // substitui ?2 pela categoriaID | |
| 97 | 0 | sql = sql.replaceFirst("\\?2", strCategoriaID); |
| 98 | ||
| 99 | // sem categoria com pesquisa | |
| 100 | } else { | |
| 101 | 0 | sql = c.getString("sql3"); |
| 102 | 0 | sql = sql.replaceFirst("\\?1", pesquisa); |
| 103 | } | |
| 104 | ||
| 105 | 0 | TableModelAlbum.getModel().update(sql); |
| 106 | 0 | TableModelAlbum.getModel().fireTableDataChanged(); |
| 107 | ||
| 108 | 0 | PainelWebFotos.apresentaNumReg(); |
| 109 | 0 | PainelWebFotos.setCursorWait(false); |
| 110 | 0 | } |
| 111 | } |