KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > OutputEditorKit


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * OutputEditorKit.java
21  *
22  * Created on May 9, 2004, 4:34 PM
23  */

24
25 package org.netbeans.core.output2;
26
27 import java.awt.Rectangle JavaDoc;
28 import javax.swing.JEditorPane JavaDoc;
29 import javax.swing.event.ChangeEvent JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import javax.swing.text.DefaultEditorKit JavaDoc;
33 import javax.swing.text.Element JavaDoc;
34 import javax.swing.text.JTextComponent JavaDoc;
35 import javax.swing.text.Position JavaDoc;
36 import org.openide.util.Exceptions;
37
38 /**
39  * A simple editor kit which provides instances of ExtPlainView/ExtWrappedPlainView as its views.
40  *
41  * @author Tim Boudreau
42  */

43 final class OutputEditorKit extends DefaultEditorKit JavaDoc implements javax.swing.text.ViewFactory JavaDoc, ChangeListener JavaDoc {
44     private final boolean wrapped;
45     private final JTextComponent JavaDoc comp;
46
47     /** Creates a new instance of OutputEditorKit */
48     OutputEditorKit(boolean wrapped, JTextComponent JavaDoc comp) {
49         this.comp = comp;
50         this.wrapped = wrapped;
51     }
52
53     public WrappedTextView view() {
54         return lastWrappedView;
55     }
56
57     private WrappedTextView lastWrappedView = null;
58     public javax.swing.text.View JavaDoc create(Element JavaDoc element) {
59         javax.swing.text.View JavaDoc result =
60                 wrapped ? (javax.swing.text.View JavaDoc) new WrappedTextView(element, comp) :
61                 (javax.swing.text.View JavaDoc) new ExtPlainView (element);
62         lastWrappedView = wrapped ? (WrappedTextView) result : null;
63         if (wrapped) {
64             lastWrappedView.updateInfo(null);
65         }
66         return result;
67     }
68
69     public javax.swing.text.ViewFactory JavaDoc getViewFactory() {
70         return this;
71     }
72
73     public boolean isWrapped() {
74         return wrapped;
75     }
76     
77     public void install(JEditorPane JavaDoc c) {
78         super.install(c);
79         if (wrapped) {
80             c.getCaret().addChangeListener(this);
81         }
82     }
83     
84     public void deinstall(JEditorPane JavaDoc c) {
85         super.deinstall(c);
86         if (wrapped) {
87             c.getCaret().removeChangeListener(this);
88         }
89     }
90     
91     private int lastMark = -1;
92     private int lastDot = -1;
93     private static final Rectangle JavaDoc scratch = new Rectangle JavaDoc();
94     
95     /**
96      * Manages repainting when the selection changes
97      */

98     public void stateChanged (ChangeEvent JavaDoc ce) {
99         int mark = comp.getSelectionStart();
100         int dot = comp.getSelectionEnd();
101         boolean hasSelection = mark != dot;
102         boolean hadSelection = lastMark != lastDot;
103         
104 // System.err.println("Change: " + mark + " : " + dot + "/" + lastMark + ":" + lastDot + " hadSelection " + hadSelection + " hasSelection " + hasSelection);
105

106         if (lastMark != mark || lastDot != dot) {
107             int begin = Math.min (mark, dot);
108             int end = Math.max (mark, dot);
109             int oldBegin = Math.min (lastMark, lastDot);
110             int oldEnd = Math.max (lastMark, lastDot);
111             
112             if (hadSelection && hasSelection) {
113                 if (begin != oldBegin) {
114                     int startChar = Math.min (begin, oldBegin);
115                     int endChar = Math.max (begin, oldBegin);
116                     repaintRange (startChar, endChar);
117                 } else {
118                     int startChar = Math.min (end, oldEnd);
119                     int endChar = Math.max (end, oldEnd);
120                     repaintRange (startChar, endChar);
121                 }
122             } else if (hadSelection && !hasSelection) {
123                 repaintRange (oldBegin, oldEnd);
124             }
125             
126         }
127         lastMark = mark;
128         lastDot = dot;
129     }
130     
131     private void repaintRange (int start, int end) {
132         try {
133             Rectangle JavaDoc r = (Rectangle JavaDoc) view().modelToView(end, scratch, Position.Bias.Forward);
134             int y1 = r.y + r.height;
135             r = (Rectangle JavaDoc) view().modelToView(start, scratch, Position.Bias.Forward);
136             r.x = 0;
137             r.width = comp.getWidth();
138             r.height = y1 - r.y;
139 // System.err.println("RepaintRange " + start + " to " + end + ": " + r);
140
comp.repaint (r);
141         } catch (BadLocationException JavaDoc e) {
142             comp.repaint();
143             Exceptions.printStackTrace(e);
144         }
145     }
146 }
147
Popular Tags