📄 xeditor.java
字号:
update(); redoAction.update(); } protected void update() { if(undo.canUndo()) { setEnabled(true); putValue(Action.NAME, undo.getUndoPresentationName()); } else { setEnabled(false); putValue(Action.NAME, "Undo"); } } }*/ /*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"); } } }*/ public static void main(String[] arg) { class Trymain implements CountListener,ERCListener{ Trymain(){} public void setCountText(String cou){} public void dispPopMen(MouseEvent e){} } JFrame jf=new JFrame("try it."); JScrollPane scroPan = new JScrollPane(); jf.setSize(400,300); Xeditor xe=new Xeditor(); Trymain tm=new Trymain(); xe.addCountListener(tm); xe.addERCListener(tm); scroPan.getViewport().add(xe, null); jf.getContentPane().add(scroPan); JPanel panel=new JPanel(); //panel.add(undoBtn); //panel.add(redoBtn); jf.getContentPane().add(panel,BorderLayout.SOUTH); //jf.getContentPane().add(jtf,BorderLayout.SOUTH); //FileDialog fd=new FileDialog(jf); //fd.setBounds(250,200,200,140); jf.show(); } static class PlainEditorKit extends Ekit implements ViewFactory { /** * Creates a copy of the editor kit. This * allows an implementation to serve as a prototype * for others, so that they can be quickly created. * * @return the copy */ public Object clone() { return new PlainEditorKit(); } /** * Fetches a factory that is suitable for producing * views of any models that are produced by this * kit. The default is to have the UI produce the * factory, so this method has no implementation. * * @return the view factory */ public ViewFactory getViewFactory() { return this; } /** * Creates a view from the given structural element of a * document. * * @param elem the piece of the document to build a view of * @return the view * @see View */ public View create(Element elem) { Document doc = elem.getDocument(); Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/); if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) { // build a view that support bidi return createI18N(elem); } else { return new WrappedPlainView(elem); } } View createI18N(Element elem) { String kind = elem.getName(); if (kind != null) { if (kind.equals(AbstractDocument.ContentElementName)) { return new PlainParagraph(elem); } else if (kind.equals(AbstractDocument.ParagraphElementName)){ return new BoxView(elem, View.Y_AXIS); } } return null; } /** * Paragraph for representing plain-text lines that support * bidirectional text. */ static class PlainParagraph extends javax.swing.text.ParagraphView { PlainParagraph(Element elem) { super(elem); layoutPool = new LogicalView(elem); layoutPool.setParent(this); } protected void setPropertiesFromAttributes() { Component c = getContainer(); if ((c != null) && (! c.getComponentOrientation().isLeftToRight())) { setJustification(StyleConstants.ALIGN_RIGHT); } else { setJustification(StyleConstants.ALIGN_LEFT); } } /** * Fetch the constraining span to flow against for * the given child index. */ public int getFlowSpan(int index) { Component c = getContainer(); if (c instanceof JTextArea) { JTextArea area = (JTextArea) c; if (! area.getLineWrap()) { // no limit if unwrapped return Integer.MAX_VALUE; } } return super.getFlowSpan(index); } protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) { SizeRequirements req = super.calculateMinorAxisRequirements(axis, r); Component c = getContainer(); if (c instanceof JTextArea) { JTextArea area = (JTextArea) c; if (! area.getLineWrap()) { // min is pref if unwrapped req.minimum = req.preferred; } } return req; } /** * This class can be used to represent a logical view for * a flow. It keeps the children updated to reflect the state * of the model, gives the logical child views access to the * view hierarchy, and calculates a preferred span. It doesn't * do any rendering, layout, or model/view translation. */ static class LogicalView extends CompositeView { LogicalView(Element elem) { super(elem); } protected int getViewIndexAtPosition(int pos) { Element elem = getElement(); if (elem.getElementCount() > 0) { return elem.getElementIndex(pos); } return 0; } protected boolean updateChildren(DocumentEvent.ElementChange ec, DocumentEvent e, ViewFactory f) { return false; } protected void loadChildren(ViewFactory f) { Element elem = getElement(); if (elem.getElementCount() > 0) { super.loadChildren(f); } else { View v = new GlyphView(elem); append(v); } } public float getPreferredSpan(int axis) { if( getViewCount() != 1 ) throw new Error("One child view is assumed."); View v = getView(0); //((GlyphView)v).setGlyphPainter(null); return v.getPreferredSpan(axis); } /** * Forward the DocumentEvent to the given child view. This * is implemented to reparent the child to the logical view * (the children may have been parented by a row in the flow * if they fit without breaking) and then execute the * superclass behavior. * * @param v the child view to forward the event to. * @param e the change information from the associated document * @param a the current allocation of the view * @param f the factory to use to rebuild if the view has * children * @see #forwardUpdate * @since 1.3 */ protected void forwardUpdateToView(View v, DocumentEvent e, Shape a, ViewFactory f) { v.setParent(this); super.forwardUpdateToView(v, e, a, f); } // The following methods don't do anything useful, they // simply keep the class from being abstract. public void paint(Graphics g, Shape allocation) { } protected boolean isBefore(int x, int y, Rectangle alloc) { return false; } protected boolean isAfter(int x, int y, Rectangle alloc) { return false; } protected View getViewAtPoint(int x, int y, Rectangle alloc) { return null; } protected void childAllocation(int index, Rectangle a) { } } } } private void jbInit() throws Exception { this.setFont(new java.awt.Font("DialogInput", 0, 12)); this.setForeground(new Color(0, 0, 100)); this.setMargin(new Insets(0,6,0,0)); this.addMouseListener(this); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -