本文整理汇总了Java中javax.swing.text.JTextComponent.viewToModel方法的典型用法代码示例。如果您正苦于以下问题:Java JTextComponent.viewToModel方法的具体用法?Java JTextComponent.viewToModel怎么用?Java JTextComponent.viewToModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.JTextComponent
的用法示例。
在下文中一共展示了JTextComponent.viewToModel方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkExpandFold
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public boolean checkExpandFold(JTextComponent c, Point p) {
FoldHierarchy foldHierarchy = FoldHierarchy.get(c);
foldHierarchy.lock();
try {
int offset = c.viewToModel(p);
Iterator collapsedFoldIterator = FoldUtilities.collapsedFoldIterator(foldHierarchy, offset, offset);
if (collapsedFoldIterator.hasNext()) {
Fold fold = (Fold) collapsedFoldIterator.next();
// Expand even if the offset is at fold's begining/end because that's what viewToModel() will return
if (offset >= fold.getStartOffset() && offset <= fold.getEndOffset()) {
foldHierarchy.expand(fold);
return true;
}
}
return false;
} finally {
foldHierarchy.unlock();
}
}
示例2: mouse2Offset
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Translates mouse event to text offset
*/
int mouse2Offset(MouseEvent evt) {
JTextComponent c = component;
int offset = 0;
if (c != null) {
int y = evt.getY();
if (y < 0) {
offset = 0;
} else if (y > c.getSize().getHeight()) {
offset = c.getDocument().getLength();
} else {
offset = c.viewToModel(new Point(evt.getX(), evt.getY()));
}
}
return offset;
}
示例3: isDragPossible
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Determines if the following are true:
* <ul>
* <li>the press event is located over a selection
* <li>the dragEnabled property is true
* <li>A TranferHandler is installed
* </ul>
* <p>
* This is implemented to check for a TransferHandler.
* Subclasses should perform the remaining conditions.
*/
protected boolean isDragPossible(MouseEvent e) {
JComponent comp = getEventComponent(e);
boolean possible = (comp == null) ? false : (comp.getTransferHandler() != null);
if (possible) {
JTextComponent c = (JTextComponent) getEventComponent(e);
if (c.getDragEnabled()) {
Caret caret = c.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
Point p = new Point(e.getX(), e.getY());
int pos = c.viewToModel(p);
int p0 = Math.min(dot, mark);
int p1 = Math.max(dot, mark);
if ((pos >= p0) && (pos < p1)) {
return true;
}
}
}
}
return false;
}
示例4: getOffsetForPoint
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private static int getOffsetForPoint(Point p, JTextComponent c, BaseDocument doc) throws BadLocationException {
if (p.x >= 0 && p.y >= 0) {
int offset = c.viewToModel(p);
Rectangle r = c.modelToView(offset);
EditorUI eui = Utilities.getEditorUI(c);
// Check that p is on a line with text and not completely below text,
// ie. behind EOF.
int relY = p.y - r.y;
if (eui != null && relY < eui.getLineHeight()) {
// Check that p is on a line with text before its EOL.
if (offset < Utilities.getRowEnd(doc, offset)) {
return offset;
}
}
}
return -1;
}
示例5: adjustRectangularSelectionMouseX
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private void adjustRectangularSelectionMouseX(int x, int y) {
if (!rectangularSelection) {
return;
}
JTextComponent c = component;
int offset = c.viewToModel(new Point(x, y));
Rectangle r = null;;
if (offset >= 0) {
try {
r = c.modelToView(offset);
} catch (BadLocationException ex) {
r = null;
}
}
if (r != null) {
float xDiff = x - r.x;
if (xDiff > 0) {
float charWidth;
LockedViewHierarchy lvh = ViewHierarchy.get(c).lock();
try {
charWidth = lvh.getDefaultCharWidth();
} finally {
lvh.unlock();
}
int n = (int) (xDiff / charWidth);
r.x += n * charWidth;
r.width = (int) charWidth;
}
rsDotRect.x = r.x;
rsDotRect.width = r.width;
updateRectangularSelectionPaintRect();
fireStateChanged();
}
}
示例6: adjustRectangularSelectionMouseX
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private void adjustRectangularSelectionMouseX(int x, int y) {
if (!rectangularSelection) {
return;
}
JTextComponent c = component;
int offset = c.viewToModel(new Point(x, y));
Rectangle r = null;;
if (offset >= 0) {
try {
r = c.modelToView(offset);
} catch (BadLocationException ex) {
r = null;
}
}
if (r != null) {
float xDiff = x - r.x;
if (xDiff > 0) {
float charWidth;
LockedViewHierarchy lvh = ViewHierarchy.get(c).lock();
try {
charWidth = lvh.getDefaultCharWidth();
} finally {
lvh.unlock();
}
int n = (int) (xDiff / charWidth);
r.x += n * charWidth;
r.width = (int) charWidth;
}
rsDotRect.x = r.x;
rsDotRect.width = r.width;
updateRectangularSelectionPaintRect();
fireStateChanged(null);
}
}
示例7: isDragPossible
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Determines if the following are true:
* <ul>
* <li>the press event is located over a selection
* <li>the dragEnabled property is true
* <li>A TranferHandler is installed
* </ul>
* <p>
* This is implemented to check for a TransferHandler.
* Subclasses should perform the remaining conditions.
*/
private boolean isDragPossible(MouseEvent e) {
Object src = e.getSource();
if (src instanceof JComponent) {
JComponent comp = (JComponent) src;
boolean possible = (comp == null) ? false : (comp.getTransferHandler() != null);
if (possible && comp instanceof JTextComponent) {
JTextComponent c = (JTextComponent) comp;
if (c.getDragEnabled()) {
Caret caret = c.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
Point p = new Point(e.getX(), e.getY());
int pos = c.viewToModel(p);
int p0 = Math.min(dot, mark);
int p1 = Math.max(dot, mark);
if ((pos >= p0) && (pos < p1)) {
return true;
}
}
}
}
}
return false;
}
示例8: getCompletionsAt
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public List<Completion> getCompletionsAt(JTextComponent tc, Point p) {
int offset = tc.viewToModel(p);
if (offset<0 || offset>=tc.getDocument().getLength()) {
lastCompletionsAtText = null;
return lastParameterizedCompletionsAt = null;
}
Segment s = new Segment();
Document doc = tc.getDocument();
Element root = doc.getDefaultRootElement();
int line = root.getElementIndex(offset);
Element elem = root.getElement(line);
int start = elem.getStartOffset();
int end = elem.getEndOffset() - 1;
try {
doc.getText(start, end-start, s);
// Get the valid chars before the specified offset.
int startOffs = s.offset + (offset-start) - 1;
while (startOffs>=s.offset && isValidChar(s.array[startOffs])) {
startOffs--;
}
// Get the valid chars at and after the specified offset.
int endOffs = s.offset + (offset-start);
while (endOffs<s.offset+s.count && isValidChar(s.array[endOffs])) {
endOffs++;
}
int len = endOffs - startOffs - 1;
if (len<=0) {
return lastParameterizedCompletionsAt = null;
}
String text = new String(s.array, startOffs+1, len);
if (text.equals(lastCompletionsAtText)) {
return lastParameterizedCompletionsAt;
}
// Get a list of all Completions matching the text.
List<Completion> list = getCompletionByInputText(text);
lastCompletionsAtText = text;
return lastParameterizedCompletionsAt = list;
} catch (BadLocationException ble) {
ble.printStackTrace(); // Never happens
}
lastCompletionsAtText = null;
return lastParameterizedCompletionsAt = null;
}