ZoneView: does it work?
- From: "i30817@xxxxxxxxx" <i30817@xxxxxxxxx>
- Date: 10 Jul 2006 04:25:54 -0700
I'm going to repeat a question that i've asked in javalobby to no
avail.
Did anyone ever got zoneview working for a JEditorPane and if so how?
I'm using a custom stylededitorkit that implements view factory and my
factory code looks like this:
public View create(Element elem) {
String name = elem.getName();
if (name != null) {
if (name.equals(AbstractDocument.ContentElementName)) {
return new LabelView(elem);
} else if (name.equals(AbstractDocument.ParagraphElementName)) {
return new CountingParagraphView(elem,observer);
} else if (name.equals(AbstractDocument.SectionElementName)) {
return new ZoneView(elem, View.Y_AXIS);
} else if (name.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
}else if (name.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
return new LabelView(elem);
}
Ignore the contngparagraph view, its just something i did to be able to
tell the amount of text visible.
Using zoneview here gives an nullpointerexception in the guts of swing,
in AsyncBoxView i think.
I'm just looking for a way not to load to the views the entire text at
a time (That is sloooow). And getting an indicator of the index of
text. I'm going to append my editor kit and textpaintobserver if you
want to see how it works (to make it work, just replace zoneview with
boxview in the method above).
/**
*
*/
package ui;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import javax.swing.text.BoxView;
import javax.swing.text.ComponentView;
import javax.swing.text.Element;
import javax.swing.text.IconView;
import javax.swing.text.JTextComponent;
import javax.swing.text.LabelView;
import javax.swing.text.ParagraphView;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
class TextCountingEditorKit extends StyledEditorKit implements
ViewFactory{
/**
* Observer given in the constructor.
*/
private final TextPaintObserver observer;
private static final long serialVersionUID = -7828351555750309111L;
public TextCountingEditorKit(TextPaintObserver observer) {
super();
this.observer = observer;
}
public ViewFactory getViewFactory()
{
return this;
}
public View create(Element elem) {
String name = elem.getName();
if (name != null) {
if (name.equals(AbstractDocument.ContentElementName)) {
return new LabelView(elem);
} else if (name.equals(AbstractDocument.ParagraphElementName)) {
return new CountingParagraphView(elem,observer);
} else if (name.equals(AbstractDocument.SectionElementName)) {
return new ZoneView(elem, View.Y_AXIS);
} else if (name.equals(StyleConstants.ComponentElementName)) {
return new ComponentView(elem);
}else if (name.equals(StyleConstants.IconElementName)) {
return new IconView(elem);
}
}
return new LabelView(elem);
}
/**
* Counting paragraph view
*/
class CountingParagraphView extends ParagraphView {
private Rectangle line;
private Rectangle win;
private TextPaintObserver observer;
public CountingParagraphView(Element elem, TextPaintObserver
observer){
super(elem);
//strategy = new MyFlowStrategy(); TODO
line = new Rectangle();
win = new Rectangle();
this.observer = observer;
}
/**
* Overrides the normal paint method for eliminating
* cut lines at the top and bottom of the viewport
* and for counting the number of visible chars from
* the model (without added \n from line-wrap,or tabs)
* the visible chars are derivated by subtracting
* two variables of the class, first and last,
* who this method updates, and who is the responsability
* of the component that wants to know them to rest afterwards
* @param g
* @param a
*/
public void paint(Graphics g, Shape a) {
Rectangle box = (a instanceof Rectangle) ? (Rectangle)a :
a.getBounds();
SwingUtilities.calculateInnerArea((JTextComponent)getContainer(), win);
int n = getViewCount();
int x = box.x;
int y = box.y;
for (int i = 0; i < n; i++) {
line.x = x + getOffset(X_AXIS, i);
line.y = y + getOffset(Y_AXIS, i);
line.width = getSpan(X_AXIS, i);
line.height = getSpan(Y_AXIS, i);
View view = getView(i);
//g.draw3DRect(line.x,line.y,line.width,line.height,true);
if ( win.contains(line) ) {
int startOffSet = view.getStartOffset();
//Lame document default behavior workaround (see
AbstractDocument)
int endOffSet = (view.getEndOffset() >
getDocument().getLength() ) ? view.getEndOffset()-1:
view.getEndOffset();
if( startOffSet < observer.getFirst()){
observer.setFirst(startOffSet);
}
if( endOffSet > observer.getLast()){
observer.setLast(endOffSet);
}
paintChild(g, line, i);
}
}
}
}
}
package ui;
import java.lang.reflect.InvocationTargetException;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
public class TextPaintObserver {
private int first;
private int last;
private JTextComponent text;
public TextPaintObserver(JTextComponent text) {
reset();
this.text = text;
}
public void setFirst(int in) {
this.first = in;
}
public void setLast(int in) {
this.last = in;
}
public int getFirst() {
return (this.first == Integer.MAX_VALUE) ? 0 : first;
}
public int getLast() {
return (this.last == Integer.MIN_VALUE) ? 0 : last;
}
/**
* Resets the state of the observed values
*/
private void reset(){
first = Integer.MAX_VALUE;
last = Integer.MIN_VALUE;
}
/**
* Gets the visible length
*/
public int getInterval(){
resetState();
return getLast() - getFirst();
}
/**
* Causes the state of the visible length to
* be updated
*
*/
private void resetState(){
reset();
if(SwingUtilities.isEventDispatchThread()){
text.paintImmediately(text.getBounds());
}
else{
try {
SwingUtilities.invokeAndWait(
new Runnable(){
public void run(){
text.paintImmediately(text.getBounds());
}
}
);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
.
- Follow-Ups:
- Re: ZoneView: does it work?
- From: i30817@xxxxxxxxx
- Re: ZoneView: does it work?
- Prev by Date: Re: ServerSocket in GUI
- Next by Date: Re: ask user before closing application...
- Previous by thread: Excel like filter in JTable's header
- Next by thread: Re: ZoneView: does it work?
- Index(es):
Relevant Pages
|
|