Re: Block Selection



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


.



Relevant Pages

  • Re: JTexPane: Cross-platform chaos
    ... > which caret and selection positions refer to). ... private AbstractDocument doc; ... private int start; ... public void insertNewText{ ...
    (comp.lang.java.gui)
  • Order of Events with JTable
    ... selection box based on what record number is selected (in order to test ... public void valueChanged{ ... int selectedRow = lsm.getMinSelectionIndex ... If all column heads are wider than the column's cells' ...
    (comp.lang.java.programmer)
  • Re: Block Selection
    ... 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.. ... public void copy{ ... int cnt = selections.length; ... catch (Exception ex) { ...
    (comp.lang.java.gui)
  • Re: How to move focus from one JTree node to another one and then back again?
    ... finally move it back to return selection to the previously selected ... public void valueChanged{ ... TreeNode currentNode = ... TreePath treePath = new TreePath); ...
    (comp.lang.java.gui)
  • Item-specific context menu on a JList?
    ... right-clicking on an unselected item doesn't change the selection prior to ... final JList listView = new JList; ... public void mouseReleased (MouseEvent e) { ...
    (comp.lang.java.gui)