📄 albumpanel.java
字号:
/*
* AlbumPanel.java
*
* Created on 2008年8月15日, 上午10:39
*/
package com.mwq.album;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import com.mwq.album.dao.Dao;
import com.mwq.album.frame.FindPhotoInfoDialog;
import com.mwq.album.frame.PlayDialog;
import com.mwq.album.frame.UpdatePhotoInfoDialog;
import com.mwq.album.mwing.BreviaryPhotoPanel;
import com.mwq.album.mwing.LanternSlidePanel;
import com.mwq.album.mwing.MLabel;
import com.mwq.album.mwing.MPanel;
import com.mwq.album.mwing.MTreeNode;
import com.mwq.album.mwing.PhotoPreviewButton;
/**
*
* @author Administrator
*/
public class AlbumPanel extends javax.swing.JPanel {
private static final Dao dao = Dao.getInstance();
private DefaultTreeModel treeModel;
private Object primaryItem;
private Thread loadPhotoThread;
/** Creates new form AlbumPanel */
public AlbumPanel() {
initComponents();
initAlbumTree();
photoPanel.add(new BreviaryPhotoPanel());
}
private javax.swing.filechooser.FileFilter swingFileFilter = new javax.swing.filechooser.FileFilter() {
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
String name = file.getName();
String type = name.substring(name.lastIndexOf("."))
.toUpperCase();
if (type.equals(".JPG") || type.equals(".JPEG")) {
return true;
}
return false;
}
}
public String getDescription() {
return "图像文件(.JPG;.JPEG)";
}
};
private java.io.FileFilter ioFileFilter = new java.io.FileFilter() {
public boolean accept(File file) {
if (file.isDirectory()) {
return false;
} else {
String name = file.getName();
String type = name.substring(name.lastIndexOf('.'))
.toUpperCase();
if (type.equals(".JPG") || type.equals(".JPEG")) {
return true;
} else {
return false;
}
}
}
};
Object[] photoInfo = { "", 0, "", "〈标题〉", "〈描述〉" };
private ShowModeComboBoxIL showModeComboBoxIL = new ShowModeComboBoxIL();
private void initAlbumTree() {
final MTreeNode root = new MTreeNode("album");
loadChildNode(root);
treeModel = new DefaultTreeModel(root, true);
albumTree.setModel(treeModel);
}
private class ShowModeComboBoxIL implements ItemListener {
private Object primaryItem;
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
int selectedIndex = seeModeComboBox.getSelectedIndex();
switch (selectedIndex) {
case 0:
photoPanel.remove(0);
photoPanel.add(new BreviaryPhotoPanel());
photoPanel.validate();
break;
case 1:
photoPanel.remove(0);
LanternSlidePanel panel = new LanternSlidePanel();
if (panel.getPhotoBoxPanel().getComponentCount() > 0) {
if (PhotoPreviewButton.getSelectedPhoto().size() > 0) {
panel.getShowPhotoLabel().setIcon(
new ImageIcon(PhotoPreviewButton
.getSelectedPhoto().firstElement()
.getPath()));
} else {
panel.getShowPhotoLabel()
.setIcon(
new ImageIcon(
((PhotoPreviewButton) panel
.getPhotoBoxPanel()
.getComponent(0))
.getPath()));
}
}
photoPanel.add(panel);
photoPanel.validate();
break;
default:
MPanel showPanel = (MPanel) photoPanel.getComponent(0);
Component[] photos = showPanel.getPhotoBoxPanel()
.getComponents();
if (photos.length > 0) {
new PlayDialog(null, true, photos).setVisible(true);
}
seeModeComboBox.removeItemListener(showModeComboBoxIL);
seeModeComboBox.setSelectedItem(primaryItem);
seeModeComboBox.addItemListener(showModeComboBoxIL);
}
} else {
primaryItem = e.getItem();
}
}
}
public static JTree getAlbumTree() {
return albumTree;
}
public static JPanel getPhotoPanel() {
return photoPanel;
}
private String getSelectedPath() {
String upLoadPath = "/img/album";
upLoadPath = AlbumFrame.class.getResource(upLoadPath).getPath();
TreePath selectionPath = albumTree.getSelectionPath();
if (selectionPath == null) {
return null;
} else {
Object[] treePath = selectionPath.getPath();
for (int i = 1; i < treePath.length; i++) {
upLoadPath += "/" + treePath[i];
}
return upLoadPath;
}
}
public static void loadChildNode(MTreeNode node) {
Vector albums = dao.selectAlbums(node.getId());
for (Iterator it = albums.iterator(); it.hasNext();) {
Vector album = (Vector) it.next();
node.add(new MTreeNode((Integer) album.get(0), album.get(2)));
}
node.setLoad(true);
}
private void addPhoto(File selectedFile, String upLoadPath) {
String photoName = upLoadPhoto(selectedFile, upLoadPath);// 上传图片并获得上传后的名称
photoInfo[0] = photoName;// 设置图片名称
photoInfo[2] = photoName.substring(0, 10);// 设置拍摄日期
dao.insertPhoto(photoInfo);// 保存信息到数据库
MPanel panel = (MPanel) photoPanel.getComponent(0);// 获得浏览方式面板
JPanel photoBox = panel.getPhotoBoxPanel();// 获得图片箱面板
photoBox.add(new PhotoPreviewButton(new File(upLoadPath + "/"
+ photoName)));// 添加缩略图
panel.validate();// 刷新浏览方式面板
}
private String upLoadPhoto(File selectedPhoto, String upLoadPath) {
String selectedPhotoName = selectedPhoto.getName().toLowerCase();// 获得图片名称
String photoType = selectedPhotoName.substring(selectedPhotoName
.lastIndexOf("."));// 获得图片格式
String upLoadPhotoName = getPhotoName() + photoType;// 定义上传后的图片名称
upLoadPath += "/" + upLoadPhotoName;// 定义上传路径
File upLoadPhoto = new File(upLoadPath);// 创建文件对象
if (!upLoadPhoto.getParentFile().exists()) {// 如果上传路径不存在
upLoadPhoto.getParentFile().mkdirs();// 创建该路径
}
try {
upLoadPhoto.createNewFile();// 创建文件对象
} catch (IOException e1) {
e1.printStackTrace();
}
try {
InputStream inStream = new FileInputStream(selectedPhoto);// 创建文件输入流对象
OutputStream outStream = new FileOutputStream(upLoadPhoto);// 创建文件输出流对象
int readBytes = 0; // 读取字节数
byte[] buffer = new byte[10240]; // 定义缓存数组
while ((readBytes = inStream.read(buffer, 0, 10240)) != -1) {// 从输入流读取数据到缓存数组中
outStream.write(buffer, 0, readBytes); // 将缓存数组中的数据输出到输出流
}
outStream.close();// 关闭输出流对象
inStream.close();// 关闭输入流对象
} catch (Exception e1) {
e1.printStackTrace();
}
return upLoadPhotoName;
}
private String getPhotoName() {
Date currentDate = new Date();// 创建日期对象
String date = String.format("%tF", currentDate);// 格式化日期为:2008-08-08
String time = String.format("%tT", currentDate);// 格式化时间为:16:07:05
String milliSecond = String.format("%tL", currentDate);// 格式化毫秒为:017
return date + " " + time.replace(':', '-') + " " + milliSecond;
}
private String getAlbumName(int fatherId, String title, String message) {
String name = null;// 相册名称
while (name == null) {
name = JOptionPane.showInputDialog(this, message, title,
JOptionPane.INFORMATION_MESSAGE);// 获得相册名称
if (name == null) {// 取消
break;
} else {// 确定
name = name.trim();// 去除首尾空格
if (name.length() == 0) {// 未输入
JOptionPane.showMessageDialog(this, "请输入相册名称!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);// 弹出提示输入框
name = null;
} else {// 已输入
Object node = dao
.selectOnlyValue("select id from tb_album where father_id="
+ fatherId + " and name='" + name + "'");// 在所属相册中查询同名相册
if (node != null) {// 已经存在
JOptionPane.showMessageDialog(this, "该相册名称已经存在!",
"友情提示", JOptionPane.INFORMATION_MESSAGE);// 弹出存在同名相册提示
name = null;
}
}
}
}
return name;// 返回相册名称
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
toolBar = new javax.swing.JToolBar();
addAlbumButton = new javax.swing.JButton();
addAlbumButton.setText("添加");
addAlbumButton.setToolTipText("添加相册");
addAlbumButton.setIcon(new ImageIcon(AlbumPanel.class.getResource("/img/album/addAlbum.png")));
updAlbumButton = new javax.swing.JButton();
updAlbumButton.setText("修改");
updAlbumButton.setToolTipText("修改相册");
updAlbumButton.setIcon(new ImageIcon(AlbumPanel.class.getResource("/img/album/updAlbum.png")));
delAlbumButton = new javax.swing.JButton();
delAlbumButton.setText("删除");
delAlbumButton.setToolTipText("删除相册");
delAlbumButton.setIcon(new ImageIcon(AlbumPanel.class.getResource("/img/album/delAlbum.png")));
cancelSelectedButton = new javax.swing.JButton();
cancelSelectedButton.setText("取消");
cancelSelectedButton.setToolTipText("取消相册的选中状态");
cancelSelectedButton.setIcon(new ImageIcon(AlbumPanel.class.getResource("/img/album/cancelAlbum.png")));
jSeparator1 = new javax.swing.JToolBar.Separator();
addPhotoButton = new javax.swing.JButton();
addPhotoButton.setText("添加");
addPhotoButton.setToolTipText("添加照片");
addPhotoButton.setIcon(new ImageIcon(AlbumPanel.class.getResource(
"/img/album/addPhoto.png")));
updPhotoButton = new javax.swing.JButton();
updPhotoButton.setText("修改");
updPhotoButton.setToolTipText("修改照片信息");
updPhotoButton.setIcon(new ImageIcon(AlbumPanel.class.getResource(
"/img/album/updPhoto.png")));
delPhotoButton = new javax.swing.JButton();
delPhotoButton.setText("删除");
delPhotoButton.setToolTipText("删除照片");
delPhotoButton.setIcon(new ImageIcon(AlbumPanel.class.getResource(
"/img/album/delAlbum.png")));
findPhotoButton = new javax.swing.JButton();
findPhotoButton.setText("搜索");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -