本文整理匯總了Java中javax.swing.text.Element類的典型用法代碼示例。如果您正苦於以下問題:Java Element類的具體用法?Java Element怎麽用?Java Element使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Element類屬於javax.swing.text包,在下文中一共展示了Element類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getPreviousImportantTokenFromOffs
import javax.swing.text.Element; //導入依賴的package包/類
/**
* Returns the last non-whitespace, non-comment token, before the
* specified offset.
*
* @param doc The document.
* @param offs The ending offset for the search.
* @return The last non-whitespace, non-comment token, or <code>null</code>
* if there isn't one.
* @see #getPreviousImportantToken(RSyntaxDocument, int)
* @see #getNextImportantToken(Token, RSyntaxTextArea, int)
*/
public static Token getPreviousImportantTokenFromOffs(
RSyntaxDocument doc, int offs) {
Element root = doc.getDefaultRootElement();
int line = root.getElementIndex(offs);
Token t = doc.getTokenListForLine(line);
// Check line containing offs
Token target = null;
while (t!=null && t.isPaintable() && !t.containsPosition(offs)) {
if (!t.isCommentOrWhitespace()) {
target = t;
}
t = t.getNextToken();
}
// Check previous line(s)
if (target==null) {
target = RSyntaxUtilities.getPreviousImportantToken(doc, line-1);
}
return target;
}
示例2: checkImages
import javax.swing.text.Element; //導入依賴的package包/類
private static void checkImages() throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
HTMLEditorKit c = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();
try {
c.read(new StringReader("<HTML><TITLE>Test</TITLE><BODY><IMG id=test></BODY></HTML>"), doc, 0);
} catch (Exception e) {
throw new RuntimeException("The test failed", e);
}
Element elem = doc.getElement("test");
ImageView iv = new ImageView(elem);
if (iv.getLoadingImageIcon() == null) {
throw new RuntimeException("getLoadingImageIcon returns null");
}
if (iv.getNoImageIcon() == null) {
throw new RuntimeException("getNoImageIcon returns null");
}
}
});
}
示例3: getRowFirstNonWhite
import javax.swing.text.Element; //導入依賴的package包/類
/**
* Gets index of first not space/tab element in line where caret is or caret
* position if non found before its location
*
* @param doc edited document
* @param caretOffset current caret position
* @return Integer index of first space or offset passed in if none before
* it
* @throws BadLocationException
*/
static int getRowFirstNonWhite(StyledDocument doc, int caretOffset)
throws BadLocationException {
Element lineElement = doc.getParagraphElement(caretOffset);//line start&stop offsets
int start = lineElement.getStartOffset();
int failsafe = start;
while (start + 1 < lineElement.getEndOffset()) {
try {
if (doc.getText(start, 1).charAt(0) != ' ') {
break;
}
} catch (BadLocationException ex) {
throw (BadLocationException) new BadLocationException(
"calling getText(" + start + ", " + (start + 1)
+ ") on doc of length: " + doc.getLength(), start
).initCause(ex);
}
start++;
}
return start > caretOffset ? failsafe : start;
}
示例4: endTag
import javax.swing.text.Element; //導入依賴的package包/類
/**
* Writes out an end tag for the element.
*
* @param elem an Element
* @exception IOException on any I/O error
*/
protected void endTag(Element elem) throws IOException {
if (synthesizedElement(elem)) {
return;
}
if (matchNameAttribute(elem.getAttributes(), HTML.Tag.PRE)) {
inPre = false;
}
// write out end tags for item on stack
closeOutUnwantedEmbeddedTags(elem.getAttributes());
if (inContent) {
if (!newlineOutputed) {
writeLineSeparator();
}
newlineOutputed = false;
inContent = false;
}
indent();
write('<');
write('/');
write(elem.getName());
write('>');
writeLineSeparator();
}
示例5: appendOffset
import javax.swing.text.Element; //導入依賴的package包/類
/**
* Get string representation of an offset for debugging purposes
* in form "offset[line:column]". Both lines and columns start counting from 1
* like in the editor's status bar. Tabs are expanded when counting the column.
*
* @param sb valid string builder to which text will be appended or null in which case
* the method itself will create a string builder and it will return it.
* @param doc non-null document in which the offset is located.
* @param offset offset in the document.
* @return non-null string builder to which the description was added.
* @since 1.27
*/
public static StringBuilder appendOffset(StringBuilder sb, Document doc, int offset) {
if (sb == null) {
sb = new StringBuilder(50);
}
sb.append(offset).append('[');
if (offset < 0) { // Offset too low
sb.append("<0");
} else if (offset > doc.getLength() + 1) { // +1 for AbstractDocument-based docs
sb.append(">").append(doc.getLength());
} else { // Valid offset
Element paragraphRoot = getParagraphRootElement(doc);
int lineIndex = paragraphRoot.getElementIndex(offset);
Element lineElem = paragraphRoot.getElement(lineIndex);
sb.append(lineIndex + 1).append(':'); // Line
sb.append(visualColumn(doc, lineElem.getStartOffset(), offset) + 1); // Column
}
sb.append(']');
return sb;
}
示例6: actionPerformed
import javax.swing.text.Element; //導入依賴的package包/類
public void actionPerformed(ActionEvent e) {
String trTag = "<tr>";
Element tr =
document
.getParagraphElement(editor.getCaretPosition())
.getParentElement()
.getParentElement();
for (int i = 0; i < tr.getElementCount(); i++)
if (tr.getElement(i).getName().toUpperCase().equals("TD"))
trTag += "<td><p></p></td>";
trTag += "</tr>";
/*
* HTMLEditorKit.InsertHTMLTextAction hta = new
* HTMLEditorKit.InsertHTMLTextAction("insertTR",trTag,
* HTML.Tag.TABLE, HTML.Tag.TR);
*/
try {
document.insertAfterEnd(tr, trTag);
//editorKit.insertHTML(document, editor.getCaretPosition(),
// trTag, 3, 0, HTML.Tag.TR);
} catch (Exception ex) {
ex.printStackTrace();
}
}
示例7: getElement
import javax.swing.text.Element; //導入依賴的package包/類
public @Override Element getElement(int index) {
if (index < 0) {
throw new IndexOutOfBoundsException("Invalid line index=" + index + " < 0"); // NOI18N
}
int elementCount = getElementCount();
if (index >= elementCount) {
throw new IndexOutOfBoundsException("Invalid line index=" + index // NOI18N
+ " >= lineCount=" + elementCount); // NOI18N
}
LineElement elem = (LineElement)super.getElement(index);
if (elem == null) {
// if the document is not locked elem may be null even after the initial checks (#159491)
throw new IndexOutOfBoundsException("Can't find element, index=" + index //NOI18N
+ ", count=" + getElementCount() //NOI18N
+ ", documentLocked=" + (DocumentUtilities.isReadLocked(doc) || DocumentUtilities.isWriteLocked(doc))); //NOI18N
}
return elem;
}
示例8: getChange
import javax.swing.text.Element; //導入依賴的package包/類
public @Override DocumentEvent.ElementChange getChange(Element elem) {
// Super of getChange()
if (changeLookup2 != null) {
return (DocumentEvent.ElementChange) changeLookup2.get(elem);
}
int n = edits.size();
for (int i = 0; i < n; i++) {
Object o = edits.elementAt(i);
if (o instanceof DocumentEvent.ElementChange) {
DocumentEvent.ElementChange c = (DocumentEvent.ElementChange) o;
if (c.getElement() == elem) {
return c;
}
}
}
return null;
// End super of getChange()
}
示例9: actionPerformed
import javax.swing.text.Element; //導入依賴的package包/類
/** The operation to perform when this action is triggered. */
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
if (target != null) {
Document doc = target.getDocument();
Element map = doc.getDefaultRootElement();
int offs = target.getCaretPosition();
int lineIndex = map.getElementIndex(offs);
int lineEnd = map.getElement(lineIndex).getEndOffset() - 1;
if (select) {
target.moveCaretPosition(lineEnd);
} else {
target.setCaretPosition(lineEnd);
}
}
}
示例10: getRowStart
import javax.swing.text.Element; //導入依賴的package包/類
public static int getRowStart(Document doc, int offset, int lineShift)
throws BadLocationException {
checkOffsetValid(doc, offset);
if (lineShift != 0) {
Element lineRoot = doc.getDefaultRootElement();
int line = lineRoot.getElementIndex(offset);
line += lineShift;
if (line < 0 || line >= lineRoot.getElementCount()) {
return -1; // invalid line shift
}
return lineRoot.getElement(line).getStartOffset();
} else { // no shift
return doc.getDefaultRootElement().getElement(
doc.getDefaultRootElement().getElementIndex(offset)).getStartOffset();
}
}
示例11: testCuriosities
import javax.swing.text.Element; //導入依賴的package包/類
public void testCuriosities() throws Exception {
// Test position at offset 0 does not move after insert
Document doc = new PlainDocument();
doc.insertString(0, "test", null);
Position pos = doc.createPosition(0);
assertEquals(0, pos.getOffset());
doc.insertString(0, "a", null);
assertEquals(0, pos.getOffset());
// Test there is an extra newline above doc.getLength()
assertEquals("\n", doc.getText(doc.getLength(), 1));
assertEquals("atest\n", doc.getText(0, doc.getLength() + 1));
// Test the last line element contains the extra newline
Element lineElem = doc.getDefaultRootElement().getElement(0);
assertEquals(0, lineElem.getStartOffset());
assertEquals(doc.getLength() + 1, lineElem.getEndOffset());
// Test that once position gets to zero it won't go anywhere else (unless undo performed)
pos = doc.createPosition(1);
doc.remove(0, 1);
assertEquals(0, pos.getOffset());
doc.insertString(0, "b", null);
assertEquals(0, pos.getOffset());
}
示例12: ImageComponentView
import javax.swing.text.Element; //導入依賴的package包/類
/**
* Very basic Attribute handling only. Expand as needed.
*/
public ImageComponentView(Element e) {
super(e);
imageName = (String) e.getAttributes()
.getAttribute(HTML.Attribute.SRC);
srcOp = imageName == null || imageName.trim().length() == 0
? null : Op.load(imageName);
}
示例13: create
import javax.swing.text.Element; //導入依賴的package包/類
public View create(javax.swing.text.Element element) {
final HTML.Tag kind = (HTML.Tag) (element.getAttributes().getAttribute(javax.swing.text.StyleConstants.NameAttribute));
if (kind instanceof HTML.Tag && element.getName().equals("img")) {
final String imageName = (String) element.getAttributes().getAttribute(HTML.Attribute.SRC);
if (imageName.indexOf("/") < 0) {
return new ImageComponentView(element);
}
}
return super.create(element);
}
示例14: setElementProperties
import javax.swing.text.Element; //導入依賴的package包/類
void setElementProperties(Element el, String id, String cls, String sty) {
ElementDialog dlg = new ElementDialog(null);
//dlg.setLocation(linkActionB.getLocationOnScreen());
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = this.getSize();
Point loc = this.getLocationOnScreen();
dlg.setLocation(
(frmSize.width - dlgSize.width) / 2 + loc.x,
(frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.setTitle(Local.getString("Object properties"));
dlg.idField.setText(id);
dlg.classField.setText(cls);
dlg.styleField.setText(sty);
// Uncommented, returns a simple p into the header... fix needed ?
//dlg.header.setText(el.getName());
dlg.setVisible(true);
if (dlg.CANCELLED)
return;
SimpleAttributeSet attrs = new SimpleAttributeSet(el.getAttributes());
if (dlg.idField.getText().length() > 0)
attrs.addAttribute(HTML.Attribute.ID, dlg.idField.getText());
if (dlg.classField.getText().length() > 0)
attrs.addAttribute(HTML.Attribute.CLASS, dlg.classField.getText());
if (dlg.styleField.getText().length() > 0)
attrs.addAttribute(HTML.Attribute.STYLE, dlg.styleField.getText());
document.setParagraphAttributes(el.getStartOffset(), 0, attrs, true);
}
示例15: synthesizedElement
import javax.swing.text.Element; //導入依賴的package包/類
/**
* Returns true if the element is a
* synthesized element. Currently we are only testing
* for the p-implied tag.
*/
protected boolean synthesizedElement(Element elem) {
if (matchNameAttribute(elem.getAttributes(), HTML.Tag.IMPLIED)) {
return true;
}
return false;
}