⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jhelpcliptextpane.java

📁 为了下东西 随便发了个 datamining 的源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/**
 * Created on 2006/8/11
 *
 * @Author: Xiaojun Chen
 * Description: an editable textpanel with Undo、Redo,Copy、Paste、Cut
 * $Revision$ 1.0
 *
 */

package eti.bi.alphaminer.tools;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.text.Document;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import eti.bi.alphaminer.core.observer.Observer;
import eti.bi.alphaminer.jhelpcomponent.JHelpTextPane;
import eti.bi.alphaminer.tools.print.Printer;
import eti.bi.common.Locale.Resource;
import eti.bi.common.System.SysLog;

public class JHelpClipTextPane extends JHelpTextPane implements ClipboardOwner,
		UndoableEditListener, ActionListener, Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Clipboard strBoard = this.getToolkit().getSystemClipboard();

	private UndoManager undo = new UndoManager();

	private Document doc = getDocument();

	private JPopupMenu menu;
	private JMenuItem undoItem;
	private JMenuItem redoItem;
	private JMenuItem copyItem;
	private JMenuItem cutItem;
	private JMenuItem pasteItem;
	private JMenuItem searchItem;
	private JMenuItem printItem;
	
	private SearchDialog searchdialog;

	private String printtile;
	private String[] superscript;
	
	public JHelpClipTextPane(String printtile) {
		super();
		doc.addUndoableEditListener(this);
		addActionMap();
		createMenu();
		this.addMouseListener(this);
		Resource.registerInterest(this);
		this.printtile = printtile;
	}

	private void createMenu() {
		menu = new JPopupMenu();
		undoItem = new JMenuItem(Resource.srcStr("Clip_undo"));
		redoItem = new JMenuItem(Resource.srcStr("Clip_redo"));
		copyItem = new JMenuItem(Resource.srcStr("Clip_copy"));
		cutItem = new JMenuItem(Resource.srcStr("Clip_cut"));
		pasteItem = new JMenuItem(Resource.srcStr("Clip_paste"));
		searchItem = new JMenuItem(Resource.srcStr("SearchDialogSearch"));
		printItem = new JMenuItem(Resource.srcStr("Print"));
		
		undoItem.addActionListener(this);
		redoItem.addActionListener(this);
		copyItem.addActionListener(this);
		cutItem.addActionListener(this);
		pasteItem.addActionListener(this);
		searchItem.addActionListener(this);
		printItem.addActionListener(this);
		
		undoItem.setAccelerator(KeyStroke.getKeyStroke('Z', java.awt.event.KeyEvent.CTRL_MASK, false));
		redoItem.setAccelerator(KeyStroke.getKeyStroke('Y', java.awt.event.KeyEvent.CTRL_MASK, false));
		copyItem.setAccelerator(KeyStroke.getKeyStroke('C', java.awt.event.KeyEvent.CTRL_MASK, false));
		cutItem.setAccelerator(KeyStroke.getKeyStroke('T', java.awt.event.KeyEvent.CTRL_MASK, false));
		pasteItem.setAccelerator(KeyStroke.getKeyStroke('P', java.awt.event.KeyEvent.CTRL_MASK, false));
		searchItem.setAccelerator(KeyStroke.getKeyStroke('F', java.awt.event.KeyEvent.CTRL_MASK, false));
		printItem.setAccelerator(KeyStroke.getKeyStroke('P', java.awt.event.KeyEvent.CTRL_MASK, false));
		
		menu.add(undoItem);
		menu.add(redoItem);
		menu.add(new JPopupMenu.Separator());
		menu.add(copyItem);
		menu.add(cutItem);
		menu.add(pasteItem);
		menu.add(new JPopupMenu.Separator());
		menu.add(searchItem);
		menu.add(new JPopupMenu.Separator());
		menu.add(printItem);
	}

	
	/**
	 * set title for print
	 * @param aPrintTile a title for print
	 * */
	public void setPrintTile(String aPrintTile) {
		printtile = aPrintTile;
	}
	
	/**
	 * get the title for print
	 * */
	public String getPrintTile() {
		return printtile;
	}
	
	/**
	 * set the superscript for print
	 * */
	public void setSuperscript(String[] superscript) {
		this.superscript = superscript;
	}
	
	/**
	 * get the superscript for print
	 * */
	public String[] getSuperscript() {
		return superscript;
	}
	
	public void addActionMap() {
		
		getActionMap().put("Undo", new AbstractAction("Undo") {
			/**
			 * 
			 */
		
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent evt) {
				try {
					if (undo.canUndo()) {
						undo.undo();
					}
				} catch (CannotUndoException e) {
				}
			}
		});
		getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

		getActionMap().put("Redo", new AbstractAction("Redo") {
			/**
			 * 
			 */
		
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent evt) {
				try {
					if (undo.canRedo()) {
						undo.redo();
					}
				} catch (CannotRedoException e) {
				}
			}
		});
		
		getInputMap().put(KeyStroke.getKeyStroke("control R"), "Redo");

		getActionMap().put("Copy", new AbstractAction("Copy") {
			/**
			 * 
			 */
		
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent evt) {
				copy();
			}
		});
		
		getInputMap().put(KeyStroke.getKeyStroke("control C"), "Copy");

		getActionMap().put("Cut", new AbstractAction("Cut") {
			/**
			 * 
			 */
		
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent evt) {
				cut();
			}
		});
		getInputMap().put(KeyStroke.getKeyStroke("control X"), "Cut");

		getActionMap().put("Paste", new AbstractAction("Paste") {
			/**
			 * 
			 */
		
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent evt) {
				paste();
			}
		});
		getInputMap().put(KeyStroke.getKeyStroke("control V"), "Paste");
		
		getActionMap().put("findanreplace", new AbstractAction("findanreplace") {
			/**
			 * 
			 */
		
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent evt) {
				searchReplace();
			}
		});
		getInputMap().put(KeyStroke.getKeyStroke("control F"), "findanreplace");
		
		getActionMap().put("print", new AbstractAction("print") {
			/**
			 * 
			 */
		
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent evt) {
				print();
			}
		});
		getInputMap().put(KeyStroke.getKeyStroke("control P"), "print");
	}

	public void copy() {
		String sCopy = getSelectedText();
		if (sCopy != null) {
			StringSelection sSelection = new StringSelection(sCopy);
			strBoard.setContents(sSelection, this);
		}
	}

	public void cut() {
		String sCopy = getSelectedText();
		if (sCopy != null) {
			StringSelection sSelection = new StringSelection(sCopy);
			strBoard.setContents(sSelection, this);
			this.replaceSelection("");
		}
	}

	public void paste() {
		Transferable sTransf = strBoard.getContents(this);
		if (sTransf != null) {
			try {
				String sPaste = (String) sTransf
						.getTransferData(DataFlavor.stringFlavor);
				
				this.replaceSelection(sPaste);
			} catch (UnsupportedFlavorException e) {
			} catch (IOException ioe) {
			}
		}
	}

	public void lostOwnership(Clipboard clipboard, Transferable contents) {
		
	}

	public void undoableEditHappened(UndoableEditEvent e) {
		doc_addedit(e);
	}

	void doc_addedit(UndoableEditEvent e) {
		undo.addEdit(e.getEdit());
	}

	public boolean print() {
		try {
			Printer.printComponent(JHelpClipTextPane.this, printtile,superscript);
			return true;
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			SysLog.error(this, e1);
			return false;
		}
	}
	
	private void searchReplace() {
		if(searchdialog==null) {
			searchdialog = new SearchDialog(JHelpClipTextPane.this, JHelpClipTextPane.this, true);
		}
		searchdialog.toshow();
	}
	
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source==undoItem) {
			undo.undo();
		}
		else if(source==redoItem) {
			undo.redo();
		}
		else if(source==copyItem) {
			copy();
		}
		else if(source==cutItem) {
			cut();
		}
		else if(source==pasteItem) {
			paste();
		}
		else if(source==searchItem) {
			searchReplace();
		}
		else if(source==printItem) {
			print();
		}
	}

	/**
	 *  @see java.awt.event.MouseListener#mouseClicked(MouseEvent)
	 */
	public void mouseClicked(MouseEvent e) {
		if(e.getButton()==MouseEvent.BUTTON3&&e.getClickCount()==1) {
			undoItem.setEnabled(undo.canUndo());
			redoItem.setEnabled(undo.canRedo());
			copyItem.setEnabled(getSelectedText()!=null);
			cutItem.setEnabled(getSelectedText()!=null);
			
			pasteItem.setEnabled(false);
			Transferable sTransf = strBoard.getContents(this);
			if (sTransf != null) {
				try {
					String sPaste = (String) sTransf.getTransferData(DataFlavor.stringFlavor);
					pasteItem.setEnabled(sPaste!=null);
				} catch (UnsupportedFlavorException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			searchItem.setEnabled(getText()!=null);
			printItem.setEnabled(getText()!=null);
			
			menu.show(e.getComponent(), e.getX(), e.getY());
		}
	}

	public void sendNotify(String a_Message) {
		if(a_Message.equals(Resource.CHANGE_LOCALE)) {
			try {
				undoItem.setText(Resource.srcStr("Clip_undo"));
				redoItem.setText(Resource.srcStr("Clip_redo"));
				copyItem.setText(Resource.srcStr("Clip_copy"));
				cutItem.setText(Resource.srcStr("Clip_cut"));
				pasteItem.setText(Resource.srcStr("Clip_paste"));
				searchItem.setText(Resource.srcStr("SearchDialogSearch"));
				printItem.setText(Resource.srcStr("Print"));
			}
			catch(Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void sendNotify(int a_Message) {
		
	}

	public void sendNotify(int a_Message, Object a_Object) {
		
	}
	
	public void dispose() {
		Resource.removeIntereat(this);
		menu=null;
		undoItem = null;
		redoItem = null;
		copyItem = null;
		cutItem = null;
		pasteItem = null;
		strBoard = null;
		undo.die();
		doc = null;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -