📄 mainwnd.java
字号:
/*
* Created on 2003-9-10
*
* Zilin Du
*/
package org.zilin.gui;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
/**
* @author D6VAA1
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class MainWnd {
private Shell shell;
private Display display;
private Menu menu;
private ToolBar toolBar;
private CLabel statusLine;
private Table table;
static final String appName = "SWT JavaZip 1.0";
MenuItem newMnu, openMnu, closeMnu, exitMnu;
MenuItem addMnu, deleteMnu, extractMnu, viewMnu;
MenuItem configMnu, aboutMnu;
ToolItem newBtn, openBtn, closeBtn, exitBtn;
ToolItem addBtn, deleteBtn, extractBtn, viewBtn;
ToolItem configBtn, aboutBtn;
private Image unknownImg;
private SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-M-d H:mm");
/**
*
*/
public MainWnd() {
display = new Display();
shell = new Shell(display);
initWindow();
unknownImg = new Image(display, "images/unknown.gif");
}
private Image loadImage(String name) {
ImageData imageData = new ImageData(name);
if (imageData.transparentPixel >= 0) {
ImageData maskData = imageData.getTransparencyMask();
return new Image(Display.getCurrent(), imageData, maskData);
} else
return new Image(Display.getCurrent(), imageData);
}
public void initWindow() {
shell.setText(appName);
shell.setImage(loadImage("images/javazip.gif"));
GridLayout gd = new GridLayout();
gd.marginWidth = gd.marginHeight = 1;
shell.setLayout(gd);
addMenuBar();
addSeparator();
addToolBar();
addSeparator();
addContents();
addStatusLine();
}
private MenuItem createMenuItem(Menu m, String t, String img, int a) {
MenuItem item = new MenuItem(m, 0);
item.setText(t);
if (img != null)
item.setImage(loadImage(img));
if (a != 0)
item.setAccelerator(a);
return item;
}
public void addMenuBar() {
menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem item;
Menu submenu;
item = new MenuItem(menu, SWT.CASCADE);
item.setText("File");
submenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(submenu);
newMnu =
createMenuItem(
submenu,
"&New ...\tCtrl+N",
"images/menu_new16.gif",
SWT.CTRL + 'N');
newMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
newFile();
}
});
openMnu =
createMenuItem(
submenu,
"&Open ...\tCtrl+O",
"images/menu_open16.gif",
SWT.CTRL + 'O');
openMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
openFile();
}
});
closeMnu =
createMenuItem(
submenu,
"&Close\tCtrl+C",
"images/menu_close16.gif",
SWT.CTRL + 'O');
closeMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
closeFile();
}
});
item = new MenuItem(submenu, SWT.SEPARATOR);
exitMnu =
createMenuItem(
submenu,
"&Exit\tCtrl+X",
"images/menu_exit16.gif",
SWT.CTRL + 'X');
exitMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
exit();
}
});
item = new MenuItem(menu, SWT.CASCADE);
item.setText("Actions");
submenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(submenu);
addMnu =
createMenuItem(
submenu,
"&Add ...\tShift+A",
"images/menu_add16.gif",
SWT.SHIFT + 'A');
addMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
add();
}
});
deleteMnu =
createMenuItem(
submenu,
"&Delete ...\tShift+D",
"images/menu_delete16.gif",
SWT.SHIFT + 'D');
deleteMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
delete();
}
});
extractMnu =
createMenuItem(
submenu,
"&Extract\tShift+E",
"images/menu_extract16.gif",
SWT.SHIFT + 'E');
extractMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
extract();
}
});
viewMnu =
createMenuItem(
submenu,
"&View\tShift+V",
"images/menu_view16.gif",
SWT.SHIFT + 'V');
viewMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
view();
}
});
item = new MenuItem(menu, SWT.CASCADE);
item.setText("Options");
submenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(submenu);
configMnu =
createMenuItem(
submenu,
"&Configuration...",
"images/menu_config16.gif",
0);
configMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
config();
}
});
item = new MenuItem(menu, SWT.CASCADE);
item.setText("Help");
submenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(submenu);
aboutMnu =
createMenuItem(
submenu,
"&About JavaZip...",
"images/menu_about16.gif",
0);
aboutMnu.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
about();
}
});
}
public void addSeparator() {
new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(
new GridData(GridData.FILL_HORIZONTAL));
}
private ToolItem createToolItem(ToolBar bar, String t, String img) {
ToolItem item = new ToolItem(bar, SWT.NONE);
item.setText(t);
item.setImage(loadImage(img));
return item;
}
public void addToolBar() {
toolBar = new ToolBar(shell, SWT.FLAT);
toolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
ToolItem item;
newBtn = createToolItem(toolBar, "New", "images/tb_new.gif");
newBtn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
newFile();
}
});
openBtn = createToolItem(toolBar, "Open", "images/tb_open.gif");
openBtn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
openFile();
}
});
item = new ToolItem(toolBar, SWT.SEPARATOR);
addBtn = createToolItem(toolBar, "Add", "images/tb_add.gif");
addBtn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
add();
}
});
deleteBtn = createToolItem(toolBar, "Delete", "images/tb_delete.gif");
deleteBtn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
delete();
}
});
extractBtn =
createToolItem(toolBar, "Extract", "images/tb_extract.gif");
extractBtn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
extract();
}
});
viewBtn = createToolItem(toolBar, "View", "images/tb_view.gif");
viewBtn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
view();
}
});
item = new ToolItem(toolBar, SWT.SEPARATOR);
configBtn =
createToolItem(toolBar, "Configuration", "images/tb_config.gif");
configBtn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
config();
}
});
item = new ToolItem(toolBar, SWT.SEPARATOR);
aboutBtn = createToolItem(toolBar, "About", "images/tb_about.gif");
aboutBtn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
about();
}
});
item = new ToolItem(toolBar, SWT.SEPARATOR);
exitBtn = createToolItem(toolBar, "Exit", "images/tb_exit.gif");
exitBtn.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
exit();
}
});
}
public void addContents() {
table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
table.setLayoutData(
new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL));
table.setHeaderVisible(true);
TableColumn col;
col = new TableColumn(table, SWT.LEFT);
col.setText("Name");
col.setWidth(200);
col = new TableColumn(table, SWT.LEFT);
col.setText("Modified");
col.setWidth(100);
col = new TableColumn(table, SWT.RIGHT);
col.setText("Size");
col.setWidth(60);
col = new TableColumn(table, SWT.CENTER);
col.setText("Ratio");
col.setWidth(40);
col = new TableColumn(table, SWT.RIGHT);
col.setText("Packed");
col.setWidth(60);
col = new TableColumn(table, SWT.LEFT);
col.setText("Path");
col.setWidth(300);
table.setFocus();
}
public void addStatusLine() {
statusLine = new CLabel(shell, SWT.SHADOW_IN);
statusLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
}
public void newFile() {
}
public void loadZipFile(String zipFileName) {
table.removeAll();
try {
ZipFile file = new ZipFile(zipFileName);
for (Enumeration enum = file.entries(); enum.hasMoreElements();) {
ZipEntry entry = (ZipEntry) enum.nextElement();
if (entry.isDirectory() || entry.getName().equals("..\\")) {
continue;
} else {
String name = entry.getName();
Date modifiedTime = new Date(entry.getTime());
long size = entry.getSize();
long compressedSize = entry.getCompressedSize();
long ratio = (size - compressedSize) * 100 / size;
//convert all "/" to "\"
name = name.replace('/', '\\');
String baseName, path, extension;
int idx = name.lastIndexOf('\\');
if (idx > 0) {
baseName = name.substring(idx + 1);
path = name.substring(0, idx + 1);
} else {
baseName = name;
path = "";
}
idx = baseName.lastIndexOf('.');
if (idx > 0)
extension = baseName.substring(idx + 1);
else
extension = "";
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, baseName);
Program p = Program.findProgram(extension);
if (p != null) {
ImageData data = p.getImageData();
if (data != null)
item.setImage(
0,
new Image(shell.getDisplay(), data));
else
item.setImage(0, unknownImg);
} else
item.setImage(0, unknownImg);
item.setText(1, dataFormat.format(modifiedTime));
item.setText(2, String.valueOf(size));
item.setText(3, String.valueOf(ratio) + "%");
item.setText(4, String.valueOf(compressedSize));
item.setText(5, path);
}
}
} catch (IOException e) {
System.out.println(e);
}
}
public void openFile() {
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setFilterNames(
new String[] { "Zip Files (*.zip)", "All Files (*.*)" });
dialog.setFilterExtensions(new String[] { "*.zip", "*.*" });
if (dialog.open() != null) {
final String name = dialog.getFilterPath() + File.separator + dialog.getFileName();
display.syncExec(new Runnable() {
public void run() {
loadZipFile(name);
}
});
}
}
public void closeFile() {
}
public void exit() {
shell.close();
}
public void add() {
}
public void delete() {
}
public void extract() {
}
public void view() {
}
public void config() {
}
public void about() {
AboutWnd wnd = new AboutWnd(shell);
wnd.run();
}
public void run() {
shell.pack();
shell.setSize(500, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
Display.getCurrent().dispose();
}
public static void main(String[] args) {
MainWnd ui = new MainWnd();
ui.run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -