package com.ticmy.notepad.main;
import java.util.Observable;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import com.ticmy.notepad.constant.CommonValue;
import com.ticmy.notepad.listener.CopyMenuListener;
import com.ticmy.notepad.listener.CreateMenuListener;
import com.ticmy.notepad.listener.CutMenuListener;
import com.ticmy.notepad.listener.DelMenuListener;
import com.ticmy.notepad.listener.ExitMenuListener;
import com.ticmy.notepad.listener.OpenMenuListener;
import com.ticmy.notepad.listener.PasteMenuListener;
import com.ticmy.notepad.listener.SaveMenuListener;
import com.ticmy.notepad.listener.SelectAllMenuListener;
import com.ticmy.notepad.util.ReadOnlyObservable;
import com.ticmy.notepad.util.ReadOnlyObserver;
import com.ticmy.notepad.util.Util;
public class MainFrame {
private final Observable readOnlyObservable = new ReadOnlyObservable();
private final Display display = Display.getDefault();
private Shell shell = null;
private StyledText text = null;
public MainFrame() {
buildShell();
buildTextArea();
buildPopupMenu();
buildMenu();
}
private void buildShell() {
shell = new Shell(display);
FillLayout fillLayout = new FillLayout();
fillLayout.marginHeight = 0;
fillLayout.marginWidth = 0;
fillLayout.spacing = 0;
shell.setLayout(fillLayout);
shell.setText("记事本");
shell.setImage(new Image(display, this.getClass().getResourceAsStream(CommonValue.imagePath + "shellicon.gif")));
//居中显示
Rectangle clientArea = display.getClientArea();
int clientHeight = clientArea.height;
int clientWidth = clientArea.width;
int x, y;
if(clientWidth > CommonValue.width) {
x = (clientWidth - CommonValue.width) / 2;
} else {
x = 0;
CommonValue.width = clientWidth;
}
if(clientHeight > CommonValue.height) {
y = (clientHeight - CommonValue.height) / 2;
} else {
y=0;
CommonValue.height = clientHeight;
}
shell.setBounds(x, y, CommonValue.width, CommonValue.height);
//关闭的时候提示是否保存
shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
Util.saveCurrent(text);
}
});
}
private void buildPopupMenu() {
Menu popup = new Menu(shell, SWT.POP_UP);
MenuItem cutMenu = new MenuItem(popup, SWT.PUSH);
cutMenu.setText("剪切(&T)");
cutMenu.addSelectionListener(new CutMenuListener(text));
readOnlyObservable.addObserver(new ReadOnlyObserver(cutMenu));
MenuItem copyMenu = new MenuItem(popup, SWT.PUSH);
copyMenu.setText("复制(&C)");
copyMenu.addSelectionListener(new CopyMenuListener(text));
MenuItem pasteMenu = new MenuItem(popup, SWT.PUSH);
pasteMenu.setText("粘贴(&P)");
pasteMenu.addSelectionListener(new PasteMenuListener(text));
readOnlyObservable.addObserver(new ReadOnlyObserver(pasteMenu));
MenuItem deleteMenu = new MenuItem(popup, SWT.PUSH);
deleteMenu.setText("删除(&D)");
deleteMenu.addSelectionListener(new DelMenuListener(text));
readOnlyObservable.addObserver(new ReadOnlyObserver(deleteMenu));
new MenuItem(popup, SWT.SEPARATOR);
MenuItem selectAllMenu = new MenuItem(popup, SWT.PUSH);
selectAllMenu.setText("全选(&A)");
selectAllMenu.addSelectionListener(new SelectAllMenuListener(text));
text.setMenu(popup);
}
private void buildTextArea() {
text = new StyledText(shell, SWT.MULTI|SWT.LEFT|SWT.H_SCROLL|SWT.V_SCROLL);
FontData fd = new FontData("Fixedsys", 11, SWT.NORMAL);
Font font = new Font(display, fd);
text.setFont(font);
text.setTabs(4);
text.setWordWrap(true);
text.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
CommonValue.currentFileInfo.setModify(true);
String savePath = CommonValue.currentFileInfo.getSavePath();
shell.setText("*" + (Util.isEmpty(savePath) ? "记事本" : savePath));
}
});
}
private void buildMenu() {
Menu menuBar = new Menu(shell, SWT.BAR);
buildFileMenu(menuBar);
buildEditMenu(menuBar);
buildFormatMenu(menuBar);
buildHelpMenu(menuBar);
shell.setMenuBar(menuBar);
}
private void buildHelpMenu(Menu menuBar) {
//帮助菜单
MenuItem helpMenu = new MenuItem(menuBar, SWT.CASCADE);
helpMenu.setText("帮助(&H)");
Menu helpSubMenu = new Menu(helpMenu);
helpMenu.setMenu(helpSubMenu);
MenuItem aboutMenu = new MenuItem(helpSubMenu, SWT.NULL);
aboutMenu.setText("关于(&A)");
aboutMenu.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
MessageBox msg = new MessageBox(shell, SWT.ICON_INFORMATION);
msg.setText("关于记事本");
msg.setMessage("swt记事本");
msg.open();
}
});
}
private void buildFormatMenu(Menu menuBar) {
//格式菜单
MenuItem formatMenu = new MenuItem(menuBar, SWT.CASCADE);
formatMenu.setText("格式(&O)");
Menu formatSubMenu = new Menu(formatMenu);
formatMenu.setMenu(formatSubMenu);
MenuItem autoWrapMenu = new MenuItem(formatSubMenu, SWT.CHECK);
autoWrapMenu.setText("自动换行(&W)");
autoWrapMenu.setSelection(true);
autoWrapMenu.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
MenuItem menuItem = (MenuItem)e.widget;
boolean selection = menuItem.getSelection();
text.setWordWrap(selection);//这会改变是否修改的属性
}
});
MenuItem fontMenu = new MenuItem(formatSubMenu, SWT.NULL);
fontMenu.setText("字体(&F)");
fontMenu.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FontDialog fontDialog = new FontDialog(shell, SWT.NONE);
Font oldFont = text.getFont();
if(oldFont != null) {
fontDialog.setFontList(oldFont.getFontData());
}
FontData fontData = fontDialog.open();
if(fontData == null) {
return;
}
Font newFont = new Font(display, fontData);
text.setFont(newFont);
if(oldFont != null) {
oldFont.dispose();
}
}
});
}
private void buildEditMenu(Menu menuBar) {
//构建Edit菜单
MenuItem editMenu = new MenuItem(menuBar, SWT.CASCADE);
editMenu.setText("编辑(&E)");
Menu editSubMenu = new Menu(editMenu);
editMenu.setMenu(editSubMenu);
MenuItem selectAllMenu = new MenuItem(editSubMenu, SWT.NULL);
selectAllMenu.setText("全选(&A) Ctrl+A");
selectAllMenu.setAccelerator(SWT.CTRL + 'A');
selectAllMenu.addSelectionListener(new SelectAllMenuListener(text));
MenuItem cutMenu = new MenuItem(editSubMenu, SWT.NULL);
cutMenu.setText("剪切(&T) Ctrl+X");
cutMenu.setAccelerator(SWT.CTRL + 'X');
cutMenu.addSelectionListener(new CutMenuListener(text));
readOnlyObservable.addObserver(new ReadOnlyObserver(cutMenu));
MenuItem copyMenu = new MenuItem(editSubMenu, SWT.NULL);
copyMenu.setText("复制(&C) Ctrl+C");
copyMenu.setAccelerator(SWT.CTRL + 'C');
copyMenu.addSelectionListener(new CopyMenuListener(text));
MenuItem pasteMenu = new MenuI
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源推荐
资源评论















资源评论

- qq_270027652015-07-03很好很好的资源,感谢楼主
- mrmraz2014-09-25功能挺齐全的,适合新手
- 沐沐wei2014-05-30很不错,对我这个新手很有帮助
- wesley52013142013-06-14还不错 对于新手的我足够了
- fengxunhuanying2013-05-07基本功能还算齐全呀!不过对于菜单上格式中的复制剪切,没有选中文字时该功能应该是不可用的,代码没有做到。求分享更好的。
龙四
- 粉丝: 739
- 资源: 19

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
资源目录









































































安全验证
文档复制为VIP权益,开通VIP直接复制
