package com.woxiaoe;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.font.TextAttribute;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import com.woxiaoe.Vo.FileAppend;
import com.woxiaoe.control.FontAction;
import com.woxiaoe.control.FontStyleAction;
import com.woxiaoe.control.MainAction;
import com.woxiaoe.control.MenuAction;
import com.woxiaoe.control.PopupMenuAction;
import com.woxiaoe.util.FileUtil;
import com.woxiaoe.util.FileUtilImpl;
import com.woxiaoe.util.Session;
public class MainFrame extends JFrame{
private JPanel mainPanel;
private JPopupMenu popupMenu;
private JMenuBar menuBar;
//文件菜单,编辑菜单,设置菜单,关于菜单
private JMenu fileMenu,editMenu,optionenu,helpMenu,lookMenu;
private JScrollPane editPane;
private JTextArea textArea;
public JTabbedPane tabbedPane;
//状态条
private JLabel stateLabel;
//字体选择
private JComboBox fontNameBox ,fontSizeBoxt;
//三种字体样式的按钮 和颜色选择按钮
private JButton boldBtn,italicBtn,underlineBtn,colorBtn;
private JToolBar toolBar;
private String[] FileMenuItemStr = {"新建-N","打开-O","保存-S","另存为-L","全部保存-A","","加密-E","解密-D","","退出-Q"};
private String[] editMenuItemStr = {"复制-C","剪切-X","粘贴-C","","背景-B","","自动换行-A"};
private String[] helpMenuItemStr ={"帮助-H","关于-A","自述文件-R"};
private String[] toolBarBtnItemStr = {"新建-pic/new.gif","打开-pic/open.gif","保存-pic/save.gif","加密-pic/decode.gif","解密-pic/encode.gif"};
private String[] popupMenuItemStr = {"复制(C)","剪切(X)","粘贴(V)","","背景","","保存","关闭"};
int fileNum = 1;
public Vector<JTextArea> textAreas = new Vector<JTextArea>();//储存当前的文本框
public Map<JTextArea,FileAppend> fileState = new HashMap<JTextArea, FileAppend>();
public int currentFileNum = 1;//当前文件数量
private MainAction action = new MainAction(this);
private PopupMenuAction popupAction = new PopupMenuAction();
private MenuAction menuAction = new MenuAction();
private FontAction fontAction = new FontAction();
private FontStyleAction fontStyleAction = new FontStyleAction();
private boolean wrap = true;//自动换行 默认为true;
//private boolean fileState = false;//文件状态
public MainFrame(){
this.setTitle("ENotePad");
this.setIconImage(new ImageIcon("pic/eclipse.gif").getImage());
this.setJMenuBar(buildMenu());
this.setLayout(new BorderLayout());
this.add(buildToolBar(),"North");
popupMenu = new JPopupMenu();
this.add(buildTabbedPane());
this.add(buildStateLabel(),"South");
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
size.setSize(size.getWidth(), size.getHeight()-25);
this.setSize(size);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
int result = JOptionPane.showConfirmDialog(null, "真的要退出?");
if(result == JOptionPane.YES_OPTION){
Session.exit();
}
}
});
Session.setAttribute("MainFrame", this);
}
private Component buildTabbedPane() {
if(this.tabbedPane == null) {
this.tabbedPane = new JTabbedPane();
tabbedPane.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent e) {
System.out.println("change");
updateItem();
}
});
tabbedPane.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
int num = e.getClickCount();
if(num == 2 && e.getButton() == 1){
createNewFile("新文件" + currentFileNum++,"");
}
}
});
}
return this.tabbedPane;
}
//新文件
public void createNewFile(String title,String text){
if(title.length() >13){//防止文件名过长 和文件没有后缀名的情况
title = title.substring(0,8) + "…" + (title.lastIndexOf(".")!= -1?title.substring(title.lastIndexOf(".")) : "");
}
tabbedPane.add(buildTextArea(text,title),title);
tabbedPane.setSelectedIndex(tabbedPane.getTabCount() -1 );
initTextPane();//初始化工作区间
};
private void initTextPane() {
FileAppend fileAppend = Session.getCurrentFileAppend();
JTextArea area = Session.getCurrentTextArea();
if(fileAppend.getColor() != null){
area.setBackground(fileAppend.getColor());
}
updateItem();
updateTextFont();
}
private Component buildTextArea(String text, String title) {
JTextArea area = new JTextArea(text);
area.addKeyListener(new KeyAdapter(){
@Override
public void keyTyped(KeyEvent e) {
changeState();
}
});
area.setLineWrap(wrap);//设置自动换行
JPopupMenu popupMenu = new JPopupMenu();
addPopup(area,popupMenu);
JScrollPane pane = new JScrollPane(area);
FileAppend fileAppend = null;
if(Session.getAttribute("fileAppend") != null){
fileAppend = (FileAppend) Session.getAttribute("fileAppend");
Session.setAttribute("fileAppend", null);
}else {
fileAppend = new FileAppend();
if(Session.getAttribute("path") != null){
fileAppend.setPath((String) Session.getAttribute("path"));
Session.setAttribute("path", null);
}
fileAppend.setFileName(title);
fileAppend.setState("");
fileState.put(area, fileAppend);
}
fileState.put(area, fileAppend);
textAreas.add(area);//加入到vector中
return pane;
}
private JMenuBar buildMenu() {
if(this.menuBar == null){
this.menuBar = new JMenuBar();
this.menuBar.add(bulidFileMenu());
this.menuBar.add(buildEditMenu());
this.menuBar.add(buildLookMenu());
this.menuBar.add(buildHelpMenu());
}
return this.menuBar;
}
private Component buildLookMenu() {
if(lookMenu == null){
lookMenu = new JMenu("风格");
UIManager.LookAndFeelInfo[] infos = Session.infos;
for(final UIManager.LookAndFeelInfo info : infos){
JMenuItem menuItem = new JMenuItem(info.getName());
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
UIManager.setLookAndFeel(info.getClassName());
SwingUtilities.updateComponentTreeUI(MainFrame.this);
} catch (ClassNotFoundException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} catch (InstantiationException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();