?? editortabpage.java
字號(hào):
package MulitePageEditor;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.io.*;
import java.text.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.undo.*;
//import 記事本.Notepad.RedoAction;
//import 記事本.Notepad.UndoAction;
//import 記事本.Notepad.UndoHandler;
/*******************************************************************************
* 多Tab頁(yè)文本編輯器的Tab類繼承自JScrollPane,為主窗口提供Tab頁(yè) 已支持并簡(jiǎn)化多頁(yè)實(shí)現(xiàn)難度,哈哈采用了我寫(xiě)的瀏覽器的技術(shù),不過(guò)C#
* 中有TabPage類,Java中的TabControl使用有點(diǎn)不一樣,好像組件都可加入其中 而C#只允許加入TabPage.
*
* java中的滾動(dòng)支持有點(diǎn)奇怪,可是使用也蠻容易的,就是感覺(jué)有點(diǎn)怪
*
* java中的異常處理能有效防止異常處理的濫用,如果你要使用try....catch
* 塊,try內(nèi)必須要有會(huì)拋出異常的代碼,否則,會(huì)不能通過(guò)編譯.不過(guò)這樣也有 限制作用,有時(shí)可能自己的代碼缺陷,而產(chǎn)生異常,卻不能捕獲,就變得不方便了
* 不過(guò)可以幫助程序員寫(xiě)出無(wú)bug的程序.有好處也有不好的地方吧.
*
* java中的屬性支持非常的不雅觀,沒(méi)C#,VB之類的語(yǔ)言那么來(lái)得優(yōu)雅,簡(jiǎn)單,雖然 編譯后還是都會(huì)有g(shù)et,set方法,
*
* java中的事件其實(shí)是采用了接口回調(diào)的一種方式處理,沒(méi)有C#委托回調(diào)機(jī)制那么好
* 還有java中能處理的事件好像有點(diǎn)少,也不是很直接,不過(guò)加上匿名內(nèi)部類,總算還過(guò) 得去.
*
* java的io處理有點(diǎn)過(guò)于靈活,使用有點(diǎn)難度,對(duì)于處理中文支持有點(diǎn)問(wèn)題,還沒(méi)找到
* 什么改變編碼的好的方法,試著用Charset解碼還是有問(wèn)題.不知道怎么解決,期待 解決.
*
* 時(shí)間: 2006-9-7 作者: 風(fēng)中過(guò)客 聲明: 這是第一次寫(xiě)的java程序,不當(dāng)之處還請(qǐng)指點(diǎn)
******************************************************************************/
public class EditorTabPage extends JScrollPane implements IEditorTabPage,
IDocumentChanged
{
private String _filename = null;
private JEditorPane editor = null;
/**
* 是否已經(jīng)改變了文檔內(nèi)容
*/
private boolean ischange=false;
private Font foreFont=new Font("宋體",Font.PLAIN,14);
private Color fontcolor=Color.BLACK;
private Color backcolor=Color.WHITE;
private Document editordocument=null;
public EditorTabPage()
{
super(); // 調(diào)用基類構(gòu)造器
editor = new JEditorPane();
editor.setFont(foreFont);
editordocument = editor.getDocument(); // 設(shè)置文檔模型
editordocument.addDocumentListener(new editordocument_documentAdapter(
this)); // 為文檔模型添加事件處理
editordocument.addUndoableEditListener(undoHandler);
this.getViewport().add(editor); // 為editor提供滾動(dòng)支持
resetUndoManager();
ischange=false;
}
public EditorTabPage(String filename)
{
this(); // 調(diào)用默認(rèn)構(gòu)造器
_filename = filename;
if (_filename.endsWith(".rtf") || _filename.endsWith(".html")
|| _filename.endsWith("htm"))
{
OpenFileName="file:\\" + _filename;
OpenRTFHtmlFile();
} else
{
OpenTXTAndElseFile();
}
// editordocument
// .removeDocumentListener(new editordocument_documentAdapter(this)); // 為文檔模型添加事件處理
// editordocument.removeUndoableEditListener(undoHandler);
editordocument = editor.getDocument();
editordocument.addDocumentListener(new editordocument_documentAdapter(
this)); // 為文檔模型添加事件處理
editordocument.addUndoableEditListener(undoHandler);
resetUndoManager();
ischange=false;
}
private void OpenTXTAndElseFile()
{
OpenTXTThread open = new OpenTXTThread();
open.start();
}
class OpenTXTThread extends Thread
{
public void run()
{
FileReader file = null;
BufferedReader br = null;
try
{
file = new FileReader(_filename);
br = new BufferedReader(file);
String line;
StringBuffer buffer = new StringBuffer();
while ((line = br.readLine()) != null)
{
buffer.append(line + "\n");
}
file.close();
br.close();
editor.setText(buffer.toString());
} catch (IOException ex)
{
JOptionPane.showMessageDialog(null, _filename
+ " 文件打開(kāi)時(shí)發(fā)生了IO異常,或許該文件不存在!\n" + ex.toString());
}
ischange=false;
}
}
private String OpenFileName;
private void OpenRTFHtmlFile()
{
OpenRTFThread open = new OpenRTFThread();
open.start();
}
class OpenRTFThread extends Thread
{
public void run()
{
try
{
editor.setPage(OpenFileName);
} catch (IOException ex)
{
JOptionPane.showMessageDialog(null, _filename
+ " 文件打開(kāi)時(shí)發(fā)生了IO異常,或許該文件不存在!\n" + ex.toString());
}
ischange=false;
}
}
private void OpenJRFFile()
{
}
class OpenJRFThread extends Thread
{
public void run()
{
}
}
class SaveJRFThread extends Thread
{
public void run()
{
}
}
/**
* 實(shí)現(xiàn)復(fù)制功能
*/
public void Copy()
{
editor.copy();
}
public void Cut()
{
editor.cut();
}
public void Delete()
{
editor.replaceSelection("");
}
public void DeleteAll()
{
editor.setText("");
}
public void Dispose()
{
this.remove(editor);
}
public void Find()
{
//TODO: 查找
}
public void InsertDataTime()
{
//TODO: 獲取當(dāng)前日期和時(shí)間
Calendar cl=Calendar.getInstance();
Date da=cl.getTime();
// DateFormat df=DateFormat.getDateInstance();
// System.out.println(/*df.format(da)*/da.toLocaleString()); //該方法以被廢棄
editor.replaceSelection(da.toLocaleString());
}
public void LoadFile(String filename)
{
//該方法已被廢棄
}
public void PageSetup()
{
//TODO:
}
public void Paste()
{
editor.paste();
}
public void Print()
{
//TODO:
}
public void PrintPreview()
{
//TODO:
}
public void Redo()
{
//return redoAction;
redoAction.actionPerformed(null);
}
private String savefilename;
public void SaveAsDocument()
{
JFileChooser save=new JFileChooser();
save.setMultiSelectionEnabled(false);
save.setDialogTitle("保存文件");
int result=save.showSaveDialog(this);
if(result==JFileChooser.APPROVE_OPTION) //按了確定了
{
String str=save.getSelectedFile().getPath();
_filename=str;
SaveDocument();
}
}
class Filter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
if(name.endsWith(".txt") || name.endsWith(".rtf"))
{
return true;
}
else
{
return false;
}
}
}
class SaveThread extends Thread
{
public void run()
{
String strdoc = editor.getText();
try
{
FileWriter file = new FileWriter(savefilename);
BufferedWriter writer = new BufferedWriter(file);
writer.write(strdoc);
writer.flush();
file.close();
writer.close();
} catch (IOException ex)
{
JOptionPane.showMessageDialog(null, savefilename + " 文件保存失敗!",
"錯(cuò)誤", JOptionPane.ERROR_MESSAGE);
}
}
}
public void SaveDocument()
{
if (_filename == null)
{
SaveAsDocument();
return;
}
savefilename = _filename;
if(savefilename==null)
{
JOptionPane.showMessageDialog(null, "保存的文件名為空,請(qǐng)重新選擇保存的文件名");
SaveAsDocument();
}
SaveThread save = new SaveThread();
save.start();
}
public void SelectAll()
{
editor.selectAll();
}
public void Undo()
{
undoAction.actionPerformed(null);
}
public Color getBackColor()
{
return backcolor;
}
public boolean IsDocChanged()
{
return ischange;
}
public Font getDocumentFont()
{
return foreFont;
}
public JEditorPane getEditor()
{
return editor;
}
public String getFileName()
{
return _filename;
}
public Color getFontColor()
{
return fontcolor;
}
public String getSelectText()
{
return editor.getSelectedText();
}
public boolean getIsDocumentChanged()
{
return ischange;
}
public void setBackColor(Color color)
{
backcolor=color;
editor.setBackground(backcolor);
}
public void setDocumentFont(Font font)
{
foreFont=font;
editor.setFont(foreFont);
}
public void setFontColor(Color color)
{
fontcolor=color;
editor.setForeground(fontcolor);
}
/**
* 文檔被改變
*/
public void editordocument_changedUpdate(DocumentEvent e)
{
ischange=true;
}
/**
* 文檔插入了文本
*/
public void editordocument_insertUpdate(DocumentEvent e)
{
ischange=true;
}
/**
* 文檔移除了文本
*/
public void editordocument_removeUpdate(DocumentEvent e)
{
ischange=true;
}
public Document getDocument()
{
return editordocument;
}
/***************************************************************************
* 撤銷和重做
**************************************************************************/
class UndoHandler implements UndoableEditListener
{
/**
* Messaged when the Document has created an edit, the edit is added to
* <code>undo</code>, an instance of UndoManager.
*/
public void undoableEditHappened(UndoableEditEvent e)
{
undo.addEdit(e.getEdit());
undoAction.update();
redoAction.update();
}
}
protected UndoManager undo = new UndoManager();
private UndoAction undoAction = new UndoAction();
private RedoAction redoAction = new RedoAction();
protected UndoableEditListener undoHandler = new UndoHandler();
class UndoAction extends AbstractAction
{
public UndoAction()
{
super("Undo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e)
{
try
{
undo.undo();
} catch (CannotUndoException ex)
{
System.out.println("Unable to undo: " + ex);
ex.printStackTrace();
}
update();
redoAction.update();
}
protected void update()
{
if (undo.canUndo())
{
setEnabled(true);
putValue(Action.NAME, undo.getUndoPresentationName());
} else
{
setEnabled(false);
putValue(Action.NAME, "Undo");
}
}
}
protected void resetUndoManager()
{
undo.discardAllEdits();
undoAction.update();
redoAction.update();
}
class RedoAction extends AbstractAction
{
public RedoAction()
{
super("Redo");
setEnabled(false);
}
public void actionPerformed(ActionEvent e)
{
try
{
undo.redo();
} catch (CannotRedoException ex)
{
System.out.println("Unable to redo: " + ex);
ex.printStackTrace();
}
update();
undoAction.update();
}
protected void update()
{
if (undo.canRedo())
{
setEnabled(true);
putValue(Action.NAME, undo.getRedoPresentationName());
} else
{
setEnabled(false);
putValue(Action.NAME, "Redo");
}
}
}
/** ********************************************************************** */
}
class editordocument_documentAdapter implements
javax.swing.event.DocumentListener // 文檔模型事件
{
EditorTabPage adaptee;
editordocument_documentAdapter(EditorTabPage adaptee)
{
this.adaptee = adaptee;
}
public void changedUpdate(DocumentEvent e)
{
adaptee.editordocument_changedUpdate(e);
}
public void insertUpdate(DocumentEvent e)
{
adaptee.editordocument_insertUpdate(e);
}
public void removeUpdate(DocumentEvent e)
{
adaptee.editordocument_removeUpdate(e);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -