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 /*
17 * PainelFoto.java
18 *
19 * Created on 17 de Janeiro de 2006, 11:54
20 */
21
22 package net.sf.webphotos.gui.component;
23
24 import net.sf.webphotos.gui.Visualizador;
25 import net.sf.webphotos.util.Util;
26
27 /**
28 * Painel de apresentação da Foto selecionada.
29 * Ao clicar nele, uma janela de popup é aberta.
30 * Para saber mais sobre essa janela de visualização procure sobre a classe {@link net.sf.webphotos.gui.Visualizador Visualizador}.
31 * @author guilherme
32 */
33 public class PainelFoto extends javax.swing.JPanel {
34
35 /**
36 * Distancia em Pixels entre a figura e a Borda do componente
37 */
38 public static final int FOLGA = 1;
39
40 private java.awt.Image foto;
41 private int tamMax=200;
42 private float novaLargura;
43 private float novaAltura;
44 private float x,y;
45 private String nomeArquivo;
46 private boolean mostrandoMsgErro;
47 private javax.swing.ImageIcon ico=new javax.swing.ImageIcon();
48
49 /**
50 * Construtor da classe.
51 * Chama o método initComponents() que tem a função de gerar uma janela popup para apresentar a foto.
52 */
53 public PainelFoto() {
54 initComponents();
55 }
56
57 /** This method is called from within the constructor to
58 * initialize the form.
59 * WARNING: Do NOT modify this code. The content of this method is
60 * always regenerated by the Form Editor.
61 */
62 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
63 private void initComponents() {
64
65 lblPainelFoto = new javax.swing.JLabel();
66 lblFotoInfo = new javax.swing.JLabel();
67
68 setMaximumSize(new java.awt.Dimension(210, 2147483647));
69 setMinimumSize(new java.awt.Dimension(210, 10));
70 setPreferredSize(new java.awt.Dimension(210, 210));
71 setLayout(new java.awt.BorderLayout());
72
73 lblPainelFoto.setToolTipText("Clique para visualizar a foto");
74 lblPainelFoto.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
75 lblPainelFoto.addMouseListener(new java.awt.event.MouseAdapter() {
76 public void mouseClicked(java.awt.event.MouseEvent evt) {
77 lblPainelFotoMouseClicked(evt);
78 }
79 public void mouseEntered(java.awt.event.MouseEvent evt) {
80 lblPainelFotoMouseEntered(evt);
81 }
82 public void mouseExited(java.awt.event.MouseEvent evt) {
83 lblPainelFotoMouseExited(evt);
84 }
85 });
86 add(lblPainelFoto, java.awt.BorderLayout.CENTER);
87
88 lblFotoInfo.setFont(new java.awt.Font("SansSerif", 0, 10)); // NOI18N
89 lblFotoInfo.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
90 lblFotoInfo.setText("??x??");
91 add(lblFotoInfo, java.awt.BorderLayout.SOUTH);
92 }// </editor-fold>//GEN-END:initComponents
93
94 private void lblPainelFotoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblPainelFotoMouseClicked
95 if(getFoto() != null) {
96 String foto=getNomeArquivo().replaceFirst("_b","_d");
97 Visualizador fotoMaior=new Visualizador(foto,null,"zOOm");
98 }
99 }//GEN-LAST:event_lblPainelFotoMouseClicked
100
101 private void lblPainelFotoMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblPainelFotoMouseExited
102 setCursor(null);
103 }//GEN-LAST:event_lblPainelFotoMouseExited
104
105 private void lblPainelFotoMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblPainelFotoMouseEntered
106 setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
107 }//GEN-LAST:event_lblPainelFotoMouseEntered
108
109 /**
110 * Limpa a caixa onde é apresentada a foto.
111 * Checa se não existe mensagem de erro, caso exista, usa o método {@link java.awt.Component#repaint() repaint()} e preenche com a cor cinza.
112 * Caso a foto não exista, apenas encerra.
113 * E por último, caso a foto exista, seta ela como nula e usa o repaint() para preencher o espaço da foto.
114 */
115 public void clear() {
116 if(mostrandoMsgErro) {
117 mostrandoMsgErro=false;
118 java.awt.Graphics2D g2=(java.awt.Graphics2D) this.getGraphics();
119 g2.setPaint(java.awt.Color.GRAY);
120 g2.fillRect(0,0,200,200);
121 repaint();
122 }
123 if(getFoto()==null) {
124 return;
125 }
126 setFoto(null);
127 repaint();
128 }
129
130 /**
131 * Carrega uma imagem.
132 * Seta a variável nomeArquivo e faz o load da foto.
133 * @param caminhoImagem Caminho da imagem.
134 */
135 public void loadImage(String caminhoImagem) {
136 setNomeArquivo(caminhoImagem);
137 System.out.println ("Lendo:" + caminhoImagem);
138 this.loadImage();
139 }
140
141 /**
142 * Carrega uma imagem.
143 * Como não recebe o caminho do arquivo, busca a foto pelo valor armazenado no variável nomeArquivo.
144 * Checa se a foto foi <I>lida</I> corretamente, e redimensiona a altura e largura.
145 */
146 public void loadImage() {
147 try {
148 ico.setImage(java.awt.Toolkit.getDefaultToolkit().getImage(getNomeArquivo()));
149 } catch (Exception e) {
150 System.out.println("Erro:");
151 e.printStackTrace();
152 }
153 System.out.println ("Fim toolkit");
154
155 // verifica se a imagem foi lida corretamente
156 int status=ico.getImageLoadStatus();
157 if(status != java.awt.MediaTracker.COMPLETE) {
158 lblPainelFoto.setIcon(null);
159 repaint();
160 mostrandoMsgErro=true;
161 return;
162 }
163 System.out.println ("obtendo informacoes");
164
165 setFoto(ico.getImage());
166
167 float largura=getFoto().getWidth(this);
168 float altura=getFoto().getHeight(this);
169 tamMax = lblPainelFoto.getWidth();
170 System.out.println("Tamanho: "+tamMax);
171
172 // redimensiona
173 if(largura > altura) {
174 // orientação horizontal
175 novaLargura=tamMax;
176 novaAltura=-1;
177 } else if (largura < altura) {
178 // orientação vertical
179 novaLargura=-1;
180 novaAltura=tamMax;
181 } else {
182 // foto quadrada
183 novaLargura=tamMax;
184 novaAltura=tamMax;
185 }
186
187 System.out.println ("Fim do redimensionamento");
188 lblPainelFoto.setIcon(new javax.swing.ImageIcon(getFoto().getScaledInstance((int) novaLargura - (FOLGA*2), (int) novaAltura - (FOLGA*2), java.awt.Image.SCALE_SMOOTH)));
189 System.out.println ("Fim repintagem");
190 }
191
192 // Variables declaration - do not modify//GEN-BEGIN:variables
193 private javax.swing.JLabel lblFotoInfo;
194 private javax.swing.JLabel lblPainelFoto;
195 // End of variables declaration//GEN-END:variables
196
197 /**
198 * Retorna uma foto através do objeto {@link java.awt.Image Image} chamado foto.
199 * @return Retorna uma foto.
200 */
201 public java.awt.Image getFoto() {
202 return foto;
203 }
204
205 /**
206 * Seta o objeto {@link java.awt.Image Image} foto através de outro objeto recebido como parâmetro.
207 * @param foto Foto.
208 */
209 public void setFoto(java.awt.Image foto) {
210 this.foto = foto;
211 }
212
213 /**
214 * Retorna o nome ou caminho do arquivo. No caso, de uma foto.
215 * @return Retorna um nome de arquivo.
216 */
217 public String getNomeArquivo() {
218 return nomeArquivo;
219 }
220
221 /**
222 * Seta o nome ou caminho do arquivo. No caso, de uma foto.
223 * @param nomeArquivo Nome do arquivo.
224 */
225 public void setNomeArquivo(String nomeArquivo) {
226 this.nomeArquivo = nomeArquivo;
227 }
228
229 /**
230 * Retorna o <I>label</I> da foto.
231 * @return Retorna um label.
232 */
233 public javax.swing.JLabel getLblFotoInfo() {
234 return lblFotoInfo;
235 }
236
237 /**
238 * Seta o label da foto.
239 * @param lblFotoInfo Label correspondente a foto.
240 */
241 public void setLblFotoInfo(javax.swing.JLabel lblFotoInfo) {
242 this.lblFotoInfo = lblFotoInfo;
243 }
244
245 /**
246 * Retorna a dimensão <I>default</I> através da classe {@link java.awt.Dimension Dimension}.
247 * Passa thumbs como parâmetros para setar os valores de dimensão.
248 * @return Retorna o default de dimensão.
249 */
250 public static java.awt.Dimension getDefaultSize() {
251 return new java.awt.Dimension(Util.getConfig().getInt("thumbnail2"), Util.getConfig().getInt("thumbnail2"));
252 }
253
254 }