📄 albumpanel.java
字号:
findPhotoButton.setToolTipText("搜索照片");
findPhotoButton.setIcon(new ImageIcon(AlbumPanel.class.getResource(
"/img/album/findPhoto.png")));
saveButton = new javax.swing.JButton();
saveButton.setText("保存");
saveButton.setToolTipText("保存照片到指定位置");
saveButton.setIcon(new ImageIcon(AlbumPanel.class.getResource(
"/img/album/savePhoto.png")));
jSeparator2 = new javax.swing.JToolBar.Separator();
seeModeComboBox = new javax.swing.JComboBox();
splitPane = new javax.swing.JSplitPane();
albumPanel = new javax.swing.JPanel();
albumScrollPane = new javax.swing.JScrollPane();
albumTree = new javax.swing.JTree();
photoPanel = new javax.swing.JPanel();
setLayout(new java.awt.BorderLayout());
toolBar.setFloatable(false);
toolBar.setRollover(true);
addAlbumButton.setFocusable(false);
addAlbumButton
.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
addAlbumButton
.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
addAlbumButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addAlbumButtonActionPerformed(evt);
}
});
toolBar.add(addAlbumButton);
updAlbumButton.setFocusable(false);
updAlbumButton
.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
updAlbumButton
.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
updAlbumButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updAlbumButtonActionPerformed(evt);
}
});
toolBar.add(updAlbumButton);
delAlbumButton.setFocusable(false);
delAlbumButton
.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
delAlbumButton
.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
delAlbumButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delAlbumButtonActionPerformed(evt);
}
});
toolBar.add(delAlbumButton);
cancelSelectedButton.setFocusable(false);
cancelSelectedButton
.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
cancelSelectedButton
.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
cancelSelectedButton
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelSelectedButtonActionPerformed(evt);
}
});
toolBar.add(cancelSelectedButton);
toolBar.add(jSeparator1);
addPhotoButton.setFocusable(false);
addPhotoButton
.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
addPhotoButton
.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
addPhotoButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addPhotoButtonActionPerformed(evt);
}
});
toolBar.add(addPhotoButton);
updPhotoButton.setFocusable(false);
updPhotoButton
.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
updPhotoButton
.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
updPhotoButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
updPhotoButtonActionPerformed(evt);
}
});
toolBar.add(updPhotoButton);
delPhotoButton.setFocusable(false);
delPhotoButton
.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
delPhotoButton
.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
delPhotoButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delPhotoButtonActionPerformed(evt);
}
});
toolBar.add(delPhotoButton);
findPhotoButton.setFocusable(false);
findPhotoButton
.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
findPhotoButton
.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
findPhotoButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
findPhotoButtonActionPerformed(evt);
}
});
toolBar.add(findPhotoButton);
saveButton.setFocusable(false);
saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
toolBar.add(saveButton);
toolBar.add(jSeparator2);
seeModeComboBox.setModel(new javax.swing.DefaultComboBoxModel(
new String[] { "缩略图", "幻灯片", "播放器" }));
seeModeComboBox.setMaximumSize(new java.awt.Dimension(70, 30));
seeModeComboBox.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
seeModeComboBoxItemStateChanged(evt);
}
});
toolBar.add(seeModeComboBox);
add(toolBar, java.awt.BorderLayout.PAGE_START);
splitPane.setDividerLocation(190);
albumPanel.setBackground(new java.awt.Color(255, 255, 255));
albumScrollPane.setBorder(null);
albumScrollPane
.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
albumTree.setRootVisible(false);
albumTree
.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
public void treeCollapsed(
javax.swing.event.TreeExpansionEvent evt) {
}
public void treeExpanded(
javax.swing.event.TreeExpansionEvent evt) {
albumTreeTreeExpanded(evt);
}
});
albumTree
.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(
javax.swing.event.TreeSelectionEvent evt) {
albumTreeValueChanged(evt);
}
});
albumScrollPane.setViewportView(albumTree);
splitPane.setLeftComponent(albumPanel);
javax.swing.GroupLayout albumPanelLayout = new javax.swing.GroupLayout(
albumPanel);
albumPanelLayout.setHorizontalGroup(albumPanelLayout
.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
albumPanelLayout.createSequentialGroup()
.addContainerGap().addComponent(
albumScrollPane,
GroupLayout.DEFAULT_SIZE, 22,
Short.MAX_VALUE).addContainerGap()));
albumPanelLayout.setVerticalGroup(albumPanelLayout.createParallelGroup(
GroupLayout.Alignment.LEADING).addGroup(
albumPanelLayout.createSequentialGroup().addContainerGap()
.addComponent(albumScrollPane,
GroupLayout.DEFAULT_SIZE, 7, Short.MAX_VALUE)
.addContainerGap()));
albumPanel.setLayout(albumPanelLayout);
photoPanel.setLayout(new java.awt.BorderLayout());
splitPane.setRightComponent(photoPanel);
add(splitPane, java.awt.BorderLayout.CENTER);
}// </editor-fold>
private void addAlbumButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
MTreeNode lastSelectedNode = (MTreeNode) albumTree
.getLastSelectedPathComponent();// 获得被选中的相册节点
int fatherId = (lastSelectedNode == null ? 0 : lastSelectedNode.getId());// 定义所属相册的编号
String albumName = getAlbumName(fatherId, "添加相册", "请输入相册名称:");// 获得相册名称
if (albumName != null) {// 输入了相册名称
int id = dao.insertAlbum(fatherId, albumName);// 保存到数据库
if (lastSelectedNode == null) {// 添加顶级相册
DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel
.getRoot();// 获得相册树的根节点
root.add(new MTreeNode(id, albumName));// 添加相册节点
treeModel.reload();// 刷新树模型
} else {// 添加子相册
TreePath selectionPath = albumTree.getSelectionPath();// 获得选中节点路径对象
if (albumTree.isExpanded(selectionPath)) {// 已经展开
lastSelectedNode.add(new MTreeNode(id, albumName));// 添加相册节点
treeModel.reload(lastSelectedNode);// 刷新指定节点
} else {// 尚未展开
albumTree.expandPath(selectionPath);// 则展开该节点
}
}
}
}
private void updAlbumButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
MTreeNode lastSelectedNode = (MTreeNode) albumTree
.getLastSelectedPathComponent(); // 获得被选中的相册节点
if (lastSelectedNode == null) {// 未选中任何相册
JOptionPane.showMessageDialog(this, "请选择要修改的相册!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);// 提示选中要修改的相册
} else {// 存在选中的相册
String albumName = getAlbumName(lastSelectedNode.getId(), "修改相册",
"请输入相册“" + lastSelectedNode.getUserObject() + "”的新名称:");// 获得相册名称
if (albumName != null) {// 输入了相册名称
System.out.println(getSelectedPath());
dao.updateAlbum(lastSelectedNode.getId(), albumName);// 将修改同步到数据库
File album = new File(getSelectedPath());
album
.renameTo(new File(getSelectedPath().replace(
lastSelectedNode.getUserObject().toString(),
albumName)));
lastSelectedNode.setUserObject(albumName);// 修改树节点
treeModel.reload(lastSelectedNode);// 刷新树节点
}
}
}
private void delAlbumButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
MTreeNode lastSelectedNode = (MTreeNode) albumTree
.getLastSelectedPathComponent(); // 获得被选中的相册节点
if (lastSelectedNode == null) { // 未选中任何相册
JOptionPane.showMessageDialog(this, "请选择要删除的相册!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);// 提示选中要删除的相册
} else {// 存在选中的相册
int i = JOptionPane.showConfirmDialog(this, "确定要删除相册“"
+ lastSelectedNode.getUserObject() + "”?", "友情提示",
JOptionPane.YES_NO_OPTION);// 询问是否真的删除
if (i == 0) {// 确定要删除
File album = new File(getSelectedPath());
album.renameTo(new File(getSelectedPath().replace(
lastSelectedNode.getUserObject().toString(),
"dddddddddddddddddddd")));
dao.deleteAlbum(lastSelectedNode.getId());// 从数据库中删除
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) lastSelectedNode
.getParent();// 获得父节点
parentNode.remove(lastSelectedNode);// 移除该节点
treeModel.reload(parentNode);// 刷新父节点
}
}
}
private void cancelSelectedButtonActionPerformed(
java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
albumTree.clearSelection();
}
private void addPhotoButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
MTreeNode selectedNode = (MTreeNode) albumTree
.getLastSelectedPathComponent();// 获得选中的节点
if (selectedNode == null) {// 未选中任何相册
JOptionPane.showMessageDialog(this, "请选择要添加照片的相册!", "友情提示",
JOptionPane.INFORMATION_MESSAGE);// 弹出选择相册提示
} else {// 存在选中的相册
JFileChooser fileChooser = new JFileChooser();// 创建文件选择器对象
fileChooser
.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// 设置选择模式
fileChooser.setFileFilter(swingFileFilter);// 设置文件过滤器
int sign = fileChooser.showOpenDialog(this);// 弹出文件选择框
if (sign == JFileChooser.APPROVE_OPTION) {// 选择了文件
photoInfo[1] = selectedNode.getId();
final String upLoadPath = getSelectedPath();
File selectedFile = fileChooser.getSelectedFile();// 获得选择的文件对象
if (selectedFile.isDirectory()) {// 选择的为文件夹
final File[] files = selectedFile.listFiles(ioFileFilter);// 获得满足条件的图片文件
new Thread() {// 创建并开启一个线程
@Override
public void run() {// 重构该方法
for (int i = 0; i < files.length; i++) {// 遍历图片文件数组
addPhoto(files[i], upLoadPath);// 添加图片
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -