Re: Block Selection
- From: "ashuvashi" <asheshvashi@xxxxxxxxx>
- Date: Thu, 28 Jul 2005 00:59:46 -0400
Thanks for reply...
i got something for it.. that i would like to share with you all...
it could be useful for someone...
and one more thing text components support block selection by default that
is true but when you want to make editor kind of thing you need to have
something that helps you to rectangle selection which is not selection..
This is the code
you can use it but may be need to refine it a bit...
private class NewEditorPane
extends javax.swing.JEditorPane {
public NewEditorPane() {
super();
setCaret(new Caret_());
}
public void copy() {
javax.swing.text.Highlighter.Highlight[] selections =
jTemplateEditorPane.getHighlighter().getHighlights();
String text = "";
int cnt = selections.length;
if (cnt != 0) {
for (int i = 0; i < cnt; i++) {
int start = selections[i].getStartOffset();
int end = selections[i].getEndOffset();
try {
String selectedText = jTemplateEditorPane.getDocument().
getText(start,
end - start);
text += selectedText + '\n';
}
catch (javax.swing.text.BadLocationException bex) {
}
}
java.awt.datatransfer.StringSelection ss = new java.awt.
datatransfer.
StringSelection(text);
java.awt.Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
ss, ss);
}
else {
super.copy();
}
}
}
private class Caret_
extends javax.swing.text.DefaultCaret {
public Caret_() {
setBlinkRate(500);
this.addChangeListener(new StateChangeAdapter());
}
java.awt.Point lastPoint = new java.awt.Point(0, 0);
public void mouseMoved(java.awt.event.MouseEvent e) {
super.mouseMoved(e);
lastPoint = new java.awt.Point(e.getX(), e.getY());
}
public void mouseClicked(java.awt.event.MouseEvent e) {
super.mouseClicked(e);
getComponent().getHighlighter().removeAllHighlights();
}
public void mouseReleased(java.awt.event.MouseEvent me) {
javax.swing.text.Highlighter.Highlight[] selections =
jTemplateEditorPane.getHighlighter().getHighlights();
if (selections.length > 0) {
generateDataGathereEvent();
}
}
protected void moveCaret(java.awt.event.MouseEvent e) {
java.awt.Point pt = new java.awt.Point(e.getX(), e.getY());
javax.swing.text.Position.Bias[] biasRet = new javax.swing.text.
Position.Bias[1];
int pos = getComponent().getUI().viewToModel(getComponent(), pt,
biasRet);
if (biasRet[0] == null) {
biasRet[0] = javax.swing.text.Position.Bias.Forward;
}
if (pos >= 0) {
setDot(pos);
java.awt.Point start = new java.awt.Point(Math.min(lastPoint.
x, pt.x),
Math.min(lastPoint.y,
pt.y));
java.awt.Point end = new java.awt.Point(Math.max(lastPoint.x,
pt.x),
Math.max(lastPoint.y,
pt.y));
customHighlight(start, end);
}
}
protected void customHighlight(java.awt.Point start,
java.awt.Point end) {
getComponent().getHighlighter().removeAllHighlights();
int y = start.y;
int firstX = start.x;
int lastX = end.x;
int pos1 = getComponent().getUI().viewToModel(getComponent(),
new java.awt.Point(firstX, y));
int pos2 = getComponent().getUI().viewToModel(getComponent(),
new java.awt.Point(lastX, y));
try {
getComponent().getHighlighter().addHighlight(pos1, pos2,
( (javax.swing.text.DefaultHighlighter) getComponent().
getHighlighter()).
DefaultPainter);
}
catch (Exception ex) {
ex.printStackTrace();
}
y++;
while (y < end.y) {
int pos1new = getComponent().getUI().viewToModel(getComponent(),
new java.awt.Point(firstX, y));
int pos2new = getComponent().getUI().viewToModel(getComponent(),
new java.awt.Point(lastX, y));
if (pos1 != pos1new) {
pos1 = pos1new;
pos2 = pos2new;
try {
getComponent().getHighlighter().addHighlight(pos1,
pos2,
( (javax.swing.text.DefaultHighlighter)
getComponent().getHighlighter()).
DefaultPainter);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
y++;
}
}
}
Thanks,
With Regards,
Ashesh Vashi
.
- Follow-Ups:
- Re: Block Selection
- From: Knute Johnson
- Re: Block Selection
- Prev by Date: Re: rectangular selections in text componets.
- Next by Date: checkbox in JTable header?
- Previous by thread: Re: rectangular selections in text componets.
- Next by thread: Re: Block Selection
- Index(es):
Relevant Pages
|