package com.luckystar.filesearch.views;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import com.luckystar.filesearch.bean.FileSearchBean;
import com.luckystar.filesearch.bean.FileSearchResult;
import com.luckystar.filesearch.constants.FileSearchItemType;
import com.luckystar.filesearch.handlers.event.FileSearchListener;
import com.luckystar.filesearch.handlers.thread.FileSearchThread;
import com.luckystar.filesearch.util.ComponentCreater;
import com.luckystar.filesearch.util.ConfigsReader;
/**
* 本地文件搜索。
*
* @author luckystar2008
* @date 2013/6/5
*/
public class FileSearchFrame extends BaseFrame {
private static final long serialVersionUID = 4583642816299965009L;
public static String frameTitle = "极速文件搜索工具";
public static int frameWidth = 590;
public static int frameHeight = 420;
final ConfigsReader cr = ConfigsReader.getInstance();
JLabel statusLabel = new JLabel("初始化完成");
private JLabel bgL = new JLabel();
JPopupMenu pm = new JPopupMenu();
FileSearchBean fsb = null;
FileSearchResult fsr = new FileSearchResult();
FileSearchThread fst = null;
JPanel p2 = null;
JTable table = null;
JButton searchBtn = new JButton("搜索");
String searchDir = (String) cr.getValue("search_dir");
JButton searchDirBtn = new JButton(
searchDir.length() > 34 ? searchDir.substring(0, 34) : searchDir);
// 读取搜索的文件扩展名
String extension = (String) cr.getValue("extension");
JTextField searchKeyWordField = new JTextField(12);
JTextField extensionField = new JTextField(6);
// 是否停止搜索
boolean isStopSearch = false;
public FileSearchFrame() {
initFrame();
}
private void initFrame() {
setTitle(frameTitle);
setSize(frameWidth, frameHeight);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 菜单栏
// =========================构建菜单=======================
JMenuBar menuBar = new JMenuBar();
JMenu systemMenu = new JMenu("程序设置 ");
JMenu aboutMenu = new JMenu("帮助 ");
JMenu systemThemeMenu = new JMenu("主题设置 ");
List<JMenuItem> themes = new ComponentCreater(fsr, this)
.createThemeItems();
for (int i = 0; i < themes.size(); i++) {
systemThemeMenu.add(themes.get(i));
}
JMenuItem systemSkinMenu = new JMenuItem("皮肤设置 ");
systemSkinMenu.addActionListener(new FileSearchListener(this, fsr,
FileSearchItemType.SKIN_SET));
systemMenu.add(systemThemeMenu);
systemMenu.add(systemSkinMenu);
menuBar.add(systemMenu);
JMenuItem aboutItem = new JMenuItem("关于 ");
aboutItem.addActionListener(new FileSearchListener(this, fsr,
FileSearchItemType.ABOUT));
aboutMenu.add(aboutItem);
menuBar.add(aboutMenu);
// ======================== End 构建菜单===========================
// 读取配置文件,获取配置信息
JPanel panel = (JPanel) getContentPane();
panel.setLayout(new BorderLayout());
panel.setOpaque(false);
JLabel searchKeyWordLabel = new JLabel("搜索关键字:");
JLabel extensionLabel = new JLabel("扩展名:");
if (extension != null && extension.replace(" ", "").length() > 0) {
extensionField.setText(extension);
}
String[][] data = new String[15][2];
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 2; j++) {
data[i][j] = "";
}
}
// 让单元格不可编辑。
TableModel tm = new DefaultTableModel(data,
new String[] { "编号", "文件名" }) {
private static final long serialVersionUID = 910167193409868478L;
@Override
public boolean isCellEditable(int i, int j) {
return false;
}
};
table = new JTable(tm);
table.setOpaque(false);
table.addMouseListener(new FileSearchListener(this, fsr, null));
table.setAutoscrolls(true);
// table.setBackground(new Color(198, 211, 247));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
TableColumn tc = table.getColumnModel().getColumn(0);
tc.setPreferredWidth(50);
TableColumn tc1 = table.getColumnModel().getColumn(1);
tc1.setPreferredWidth(frameWidth - 50);
table.setRowHeight(20);
JMenuItem openFileItem = new JMenuItem("打开文件");
JMenuItem openDirItem = new JMenuItem("打开目录");
openFileItem.addActionListener(new FileSearchListener(this, fsr,
FileSearchItemType.OPEN_FILE));
openDirItem.addActionListener(new FileSearchListener(this, fsr,
FileSearchItemType.OPEN_DIRECTORY));
pm.add(openFileItem);
pm.addSeparator();
pm.add(openDirItem);
table.add(pm);
table.setSelectionBackground(new Color(255, 144, 9));
JScrollPane jsp = new JScrollPane(table);
jsp.setPreferredSize(new Dimension(frameWidth - 20, 300));
// jsp.setBackground(new Color(198, 211, 247));
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 1));
p1.add(menuBar);
p2 = new BackgroundPanel(new BackgroundComponent(bgL, this.getWidth(),
this.getHeight()));
p2.setLayout(new FlowLayout(FlowLayout.LEADING, 3, 3));
p2.add(searchKeyWordLabel);
p2.add(searchKeyWordField);
p2.add(extensionLabel);
p2.add(extensionField);
p2.add(searchBtn);
p2.add(searchDirBtn);
p2.add(jsp);
p2.add(statusLabel);
panel.add(p1, BorderLayout.NORTH);
panel.add(p2, BorderLayout.CENTER);
searchBtn.addActionListener(new FileSearchListener(this, fsr,
FileSearchItemType.SEARCH_BTN));
searchDirBtn.addActionListener(new FileSearchListener(this, fsr,
FileSearchItemType.SEARCH_DIR_BTN));
addMouseListener(new FileSearchListener(this, fsr, null));
}
// 设置背景图片
public void setBackground(Icon icon) {
this.bgL.setIcon(icon);
p2.repaint();
}
// getter and setters
public boolean isStopSearch() {
return isStopSearch;
}
public void setStopSearch(boolean isStopSearch) {
this.isStopSearch = isStopSearch;
}
public JTable getTable() {
return table;
}
public void setTable(JTable table) {
this.table = table;
}
public JLabel getStatusLabel() {
return statusLabel;
}
public void setStatusLabel(JLabel statusLabel) {
this.statusLabel = statusLabel;
}
public JButton getSearchBtn() {
return searchBtn;
}
public void setSearchBtn(JButton searchBtn) {
this.searchBtn = searchBtn;
}
public String getSearchDir() {
return searchDir;
}
public void setSearchDir(String searchDir) {
this.searchDir = searchDir;
}
public JButton getSearchDirBtn() {
return searchDirBtn;
}
public void setSearchDirBtn(JButton searchDirBtn) {
this.searchDirBtn = searchDirBtn;
}
public String getExtension() {
return extension;
}
public JTextField getSearchKeyWordField() {
return searchKeyWordField;
}
public void setSearchKeyWordField(JTextField searchKeyWordField) {
this.searchKeyWordField = searchKeyWordField;
}
public JTextField getExtensionField() {
return extensionField;
}
public void setExtensionField(JTextField extensionField) {
this.extensionField = extensionField;
}
public JPopupMenu getPm() {
return pm;
}
public void setPm(JPopupMenu pm) {
this.pm = pm;
}
public JLabel getBgL() {
return bgL;
}
public void setBgL(JLabel bgL) {
this.bgL = bgL;
}
public FileSearchBean getFsb() {
return fsb;
}
public void setFsb(FileSearchBean fsb) {
评论0
最新资源