📄 fileproxy.java
字号:
package net.sf.component.simplenote;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.util.TousleUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.PaintObjectEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
/**
* 定义文件链接信息
* 也可由形如<a href=aaa.doc width=120 height=160>测试.doc</a>的字串构成
* @author levin
* 2007.03.15
*/
public class FileProxy extends ElementProxy{
public String name;
private Control control; //要显示的,可以是个链接,也可以是个带图片的链接
public transient File originalFile; //原始文件,可能不存在
public static int MARGIN = 5;
public FileProxy(String src, String name, int width, int height) {
super();
this.src = src;
this.name = name;
this.width = width;
this.height = height;
}
//由文件生成
public FileProxy(File f){
this.name=f.getName();
originalFile=f;
//构造一个link,判断一下大小
Shell shell=Display.getCurrent().getActiveShell() == null ? new Shell(Display.getCurrent()):Display.getCurrent().getActiveShell();
Link link=new Link(shell,SWT.NULL);
link.setText(this.name);
link.pack();
Rectangle size = link.getBounds();
this.width=size.width;
this.height=size.height;
link.dispose();
}
//由html生成
public FileProxy(String html){
//防止解析出错
this("","NO_FILE",10,10);
try{
Pattern p=Pattern.compile("<a\\s+href=([a-z\\.A-Z/0-9]*)\\s+width=([0-9]*)\\s+height=([0-9]*)>(.*)</a>");
Matcher m=p.matcher(html);
if(m.find()){
this.src=m.group(1);
this.width=Integer.parseInt(m.group(2));
this.height=Integer.parseInt(m.group(3));
this.name=m.group(4);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public FileProxy() {
}
//取定义的html
public String getHtml(){
return "<a href="+src+" width="+width+" height="+height+">"+name+"</a>";
}
public int[] getMetrics(){
int ascent = 2*height/3;
int descent = height - ascent;
return new int[]{ascent + MARGIN, descent + MARGIN, width + 2*MARGIN};
}
public void save(String basePath){
try {
if(originalFile == null)
return ;
File file=createEmbedFile(basePath,originalFile.getName().substring(originalFile.getName().indexOf(".")));
TousleUtil.copyIntput2Output(new FileInputStream(originalFile), new FileOutputStream(file));
//设置imageProxy的src属性
src="embed/"+file.getName();
} catch (Exception e) {
e.printStackTrace();
}
}
public void draw(String basePath,PaintObjectEvent event,Composite parent){
Control control = getControl(basePath, parent);
Point pt = control.getSize();
int x = event.x + FileProxy.MARGIN;
int y = event.y + event.ascent - 2*pt.y/3;
control.setLocation(x, y);
}
public void dispose(){
if(control != null){
control.dispose();
control =null;
}
}
public String getPlaceholder(){
return "\uFFFD";
}
private Control getControl(final String basePath,final Composite parent){
if(control == null){
control = new Link(parent,SWT.NULL);
control.setSize(width, height);
control.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
Link link = ((Link)control);
link.setText("<a>"+name+"</a>");
//左键打开,右键另存
link.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
File f= originalFile == null ? new File(basePath,src) : originalFile;
Program.launch(f.getAbsolutePath());
}
});
link.addMouseListener(new MouseAdapter(){
@Override
public void mouseUp(MouseEvent e) {
//右键另存为
if(e.button > 1){
saveFileAs(basePath,parent);
}
}
});
}
return control;
}
private void saveFileAs(String basePath,Composite parent) {
final FileDialog fd=new FileDialog(parent.getShell(),SWT.SAVE);
File f= src == null ? originalFile : new File(basePath,src);
fd.setFileName(name);
String fname=fd.open();
if (fname != null) {
try {
// 提示是否覆盖
if (!new File(fname).exists() || MessageDialog.openConfirm(fd.getParent(), "文件覆盖确认", "文件已经存在,是否覆盖?")) {
TousleUtil.copyIntput2Output(new FileInputStream(f), new FileOutputStream(fname));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -