📄 mainframe.java
字号:
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(0, 0, 0, 0);
gdb.setConstraints(browseButton, c);
browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser(currentDirectory);
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setFileFilter(new DirectoryFilter());
int result = fc.showOpenDialog(ExtractDialog.this);
if (result == JFileChooser.APPROVE_OPTION) {
pathTextField.setText(fc.getSelectedFile().getPath());
}
}
});
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(20, 0, 0, 0);
gdb.setConstraints(okButton, c);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ExtractDialog.this.setVisible(false);
extractFiles();
}
}
);
c.gridx = 1;
c.gridy = 4;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(20, 0, 0, 0);
gdb.setConstraints(cancelButton, c);
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ExtractDialog.this.setVisible(false);
}
}
);
selectedRadioButton.addActionListener(this);
allRadioButton.addActionListener(this);
fileRadioButton.addActionListener(this);
overwriteCheckBox.addActionListener(this);
fileTextField.setBackground(Color.gray);
setSize(320, 250);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ExtractDialog.this.setVisible(false);
}
});
}
/**
* 导出用户指定的文件。
*/
public void extractFiles() {
String path = pathTextField.getText();
if (path == "") {
path = "." + File.separator;
}
if (!path.endsWith(File.separator)) {
path = path + File.separator;
}
switch (extractMode) {
case SELECTED_FILE: {
int[] rows = table.getSelectedRows();
ArrayList extractedFiles = new ArrayList();
ArrayList entries = new ArrayList();
for (int i = 0; i < rows.length; i++) {
String entryName = table.getEntryName(rows[i]);
String fileName = path + entryName;
if ( (!canOverWrite) && FileUtil.isFileExist(fileName)) {
int selection = JOptionPane.showConfirmDialog(this,
resource.getString("i_file") + " " + fileName + " " +
resource.getString("i_exist") +
resource.getString("i_overwrite"),
resource.getString("i_warning"),
JOptionPane.YES_NO_OPTION);
if (selection == JOptionPane.YES_OPTION) {
entries.add(entryName);
extractedFiles.add(fileName);
}
}
else {
entries.add(entryName);
extractedFiles.add(fileName);
}
}
String[] entryNames = new String[entries.size()];
String[] fileNames = new String[entries.size()];
entries.toArray(entryNames);
extractedFiles.toArray(fileNames);
currentZipFile.extractEntriesToFiles(entryNames, fileNames, true);
}
break;
case ALL_FILE: {
int count = table.getEntryCount();
ArrayList extractedFiles = new ArrayList();
ArrayList entries = new ArrayList();
for (int i = 0; i < count; i++) {
String entryName = table.getEntryName(i);
String fileName = path + entryName;
if ( (!canOverWrite) && FileUtil.isFileExist(fileName)) {
int selection = JOptionPane.showConfirmDialog(this,
resource.getString("i_file") + " " + fileName + " " +
resource.getString("i_exist") +
resource.getString("i_overwrite"),
resource.getString("i_warning"),
JOptionPane.YES_NO_OPTION);
if (selection == JOptionPane.YES_OPTION) {
entries.add(entryName);
extractedFiles.add(fileName);
}
}
else {
entries.add(entryName);
extractedFiles.add(fileName);
}
}
String[] entryNames = new String[entries.size()];
String[] fileNames = new String[entries.size()];
entries.toArray(entryNames);
extractedFiles.toArray(fileNames);
currentZipFile.extractEntriesToFiles(entryNames, fileNames, true);
}
break;
case USER_FILE: {
break;
}
}
}
/**
* 选项按钮的处理方法。
* @param evt 选中事件
*/
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == selectedRadioButton) {
fileTextField.setEnabled(false);
fileTextField.setBackground(Color.gray);
extractMode = SELECTED_FILE;
}
else if (source == allRadioButton) {
fileTextField.setEnabled(false);
fileTextField.setBackground(Color.gray);
extractMode = ALL_FILE;
}
else if (source == fileRadioButton) {
fileTextField.setEnabled(true);
fileTextField.setBackground(Color.white);
extractMode = USER_FILE;
}
else if (source == overwriteCheckBox) {
canOverWrite = overwriteCheckBox.isSelected();
}
}
}
/**
* 最近打开文件事件处理器。
*/
class RecentFileAction
implements ActionListener {
String filePath;
public RecentFileAction(String filePath) {
this.filePath = filePath;
}
public void actionPerformed(ActionEvent e) {
openFile(filePath);
}
}
/**
* 根据指定的路径打开文件。
* @param filePath 要打开的文件的文件名
*/
private void openFile(String filePath) {
currentZipFile = new JZJFile(filePath);
removeAllRows();
loadEntries();
enableThem();
}
/**
* 最近文件列表管理器。
*/
class RecentFilesManager {
ArrayList list;
HashMap menuItemMap;
int capacity;
int size;
JMenu menu;
public static final int MAX_CAPACITY = 20;
private static final int START_INDEX = 5;
private static final int PATH_LENGTH = 20;
/**
* 构造一个列表大小为10的RecentFilesManager。
* @param menu 列表菜单
*/
public RecentFilesManager(JMenu menu) {
this(10, menu, true);
}
/**
* 根据指定的列表大小构造一个RecentFilesManager。
* 如果指定的容量小于0则设置为缺省的10。
* @param capacity 列表容量
* @param menu 列表菜单
*/
public RecentFilesManager(int capacity, JMenu menu) {
this(capacity, menu, true);
}
/**
* 根据指定的列表大小和是否从参数选项里面读取列表构造一个RecentFilesManager。
* @param capacity 列表容量
* @param menu 列表菜单
* @param getFiles 是否从参数选项里面读取列表,为true是读取,否则不读取
*/
public RecentFilesManager(int capacity, JMenu menu, boolean getFiles) {
if (capacity < 0 || capacity > MAX_CAPACITY) {
capacity = 10;
}
list = new ArrayList(capacity);
menuItemMap = new HashMap(capacity * 2);
this.capacity = capacity;
size = 0;
this.menu = menu;
if (getFiles == true) {
getPreference();
}
}
/**
* 增加一项到最近文件列表中
* @param filePath 文件的绝对路径
*/
private void add(String filePath) {
list.add(0, filePath);
size++;
if (size == 1) {
menu.addSeparator();
}
JMenuItem menuItem = createMenuItem(filePath);
menuItemMap.put(filePath, menuItem);
menu.add(menuItem, START_INDEX + 1);
}
/**
* 从最近列表中删除一项
* @param filePath 文件的绝对路径
*/
private void remove(String filePath) {
list.remove(filePath);
size--;
if (size == 0) {
menu.remove(START_INDEX);
}
JMenuItem menuItem = (JMenuItem) menuItemMap.get(filePath);
menuItemMap.remove(filePath);
menu.remove(menuItem);
}
/**
* 将文件列表最后的一项删除。
*/
public void removeLast() {
if (size > 0) {
String lastFile = (String) list.get(size - 1);
remove(lastFile);
}
}
/**
* 添加一个文件到列表。
* @param filePath 文件的绝对路径
*/
public void addFile(String filePath) {
if (list.size() < capacity && filePath != null) {
if (!list.contains(filePath)) {
add(filePath);
refreshPreference();
}
else {
remove(filePath);
add(filePath);
refreshPreference();
}
}
else {
if (!list.contains(filePath)) {
removeLast();
add(filePath);
refreshPreference();
}
else {
remove(filePath);
add(filePath);
refreshPreference();
}
}
}
/**
* 清空文件列表。
*/
public void clear() {
if (size > 0) {
list.clear();
menuItemMap.clear();
for (int i = START_INDEX; i < START_INDEX + size; i++) {
menu.remove(i);
}
size = 0;
refreshPreference();
}
}
/**
* 重新设置列表的容量。
* 如果新的容量小于已有文件的数量则会删除那些最旧的文件项。
* @param capacity 新容量
*/
public void setCapacity(int capacity) {
if (capacity > 0) {
this.capacity = capacity;
if (size > capacity) {
for (int i = size; i > capacity; i++) {
removeLast();
}
size = capacity;
refreshPreference();
}
}
}
/**
* 返回文件列表数组,最新的文件在最前面。
* @return 文件列表数组
*/
public String[] getLists() {
String[] result = new String[size];
for (int i = 0; i < list.size(); i++) {
result[i] = (String) list.get(i);
}
return result;
}
/**
* 更新参数选项中的文件列表。
*/
private void refreshPreference() {
int i = 0;
for (; i < list.size(); i++) {
preference.put("recentfiles" + i, (String) list.get(i));
}
while (!preference.get("recentfiles" + i, "").equals("")) {
preference.remove("recentfiles" + i);
i++;
}
}
/**
* 从参数选项中读取文件列表。
*/
private void getPreference() {
int i = 0;
String filePath = preference.get("recentfiles" + i, "");
while (!filePath.equals("")) {
add(filePath);
i++;
filePath = preference.get("recentfiles" + i, "");
}
preference.remove("recentfiles" + i);
}
/**
* 创建一个最近文件菜单项
* @param filePath 文件的绝对路径
* @return 对应的菜单项
*/
private JMenuItem createMenuItem(String filePath) {
JMenuItem menuItem = new JMenuItem(getShortRecentName(filePath));
menuItem.addActionListener(new RecentFileAction(filePath));
menuItem.setToolTipText(filePath);
return menuItem;
}
/**
* 得到文件路径的缩短的形式。
* @param filePath 文件路径
* @return文件路径的缩短的形式
*/
private String getShortRecentName(String filePath) {
String result = filePath;
if (filePath.length() > PATH_LENGTH) {
String fileName = FileUtil.getFileName(filePath);
int length = fileName.length();
result = filePath.substring(0, 5);
result = result + "...";
result = result +
filePath.substring(filePath.length() - PATH_LENGTH + 5);
}
return result;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -