private void updateTemplateFromEditor(PrintfTemplate template) { ArrayList params = new ArrayList(); String format = null; int text_length = editorPane.getDocument().getLength(); try { format = editorPane.getDocument().getText(0, text_length); } catch (BadLocationException ex1) { } Element section_el = editorPane.getDocument().getDefaultRootElement(); // Get number of paragraphs. int num_para = section_el.getElementCount(); for (int p_count = 0; p_count < num_para; p_count++) { Element para_el = section_el.getElement(p_count); // Enumerate the content elements int num_cont = para_el.getElementCount(); for (int c_count = 0; c_count < num_cont; c_count++) { Element content_el = para_el.getElement(c_count); AttributeSet attr = content_el.getAttributes(); // Get the name of the style applied to this content element; may be null String sn = (String) attr.getAttribute(StyleConstants.NameAttribute); // Check if style name match if (sn != null && sn.startsWith("Parameter")) { // we extract the label. JLabel l = (JLabel) StyleConstants.getComponent(attr); if (l != null) { params.add(l.getName()); } } } } template.setFormat(format); template.setTokens(params); }
/** * Performs layout for the minor axis of the box (i.e. the axis orthogonal to the axis that it * represents). The results of the layout (the offset and span for each children) are placed in * the given arrays which represent the allocations to the children along the minor axis. * * @param targetSpan the total span given to the view, which would be used to layout the children. * @param axis the axis being layed out * @param offsets the offsets from the origin of the view for each of the child views; this is a * return value and is filled in by the implementation of this method * @param spans the span of each child view; this is a return value and is filled in by the * implementation of this method */ protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) { int n = getViewCount(); Object key = (axis == X_AXIS) ? CSS.Attribute.WIDTH : CSS.Attribute.HEIGHT; for (int i = 0; i < n; i++) { View v = getView(i); int min = (int) v.getMinimumSpan(axis); int max; // check for percentage span AttributeSet a = v.getAttributes(); CSS.LengthValue lv = (CSS.LengthValue) a.getAttribute(key); if ((lv != null) && lv.isPercentage()) { // bound the span to the percentage specified min = Math.max((int) lv.getValue(targetSpan), min); max = min; } else { max = (int) v.getMaximumSpan(axis); } // assign the offset and span for the child if (max < targetSpan) { // can't make the child this wide, align it float align = v.getAlignment(axis); offsets[i] = (int) ((targetSpan - max) * align); spans[i] = max; } else { // make it the target width, or as small as it can get. offsets[i] = 0; spans[i] = Math.max(min, targetSpan); } } }
/** * Highlight lines to start or end delimiter. * * @param content the content to parse * @param line the line number * @throws BadLocationException if offsets are wrong */ protected void highlightLinesAfter(String content, int line) throws BadLocationException { int offset = m_RootElement.getElement(line).getEndOffset(); // Start/End delimiter not found, nothing to do int startDelimiter = -1; int endDelimiter = -1; if (getMultiLineComment()) { startDelimiter = indexOf(content, getMultiLineCommentStart(), offset); endDelimiter = indexOf(content, getMultiLineCommentEnd(), offset); } if (startDelimiter < 0) startDelimiter = content.length(); if (endDelimiter < 0) endDelimiter = content.length(); int delimiter = Math.min(startDelimiter, endDelimiter); if (delimiter < offset) return; // Start/End delimiter found, reapply highlighting int endLine = m_RootElement.getElementIndex(delimiter); for (int i = line + 1; i < endLine; i++) { Element branch = m_RootElement.getElement(i); Element leaf = m_Self.getCharacterElement(branch.getStartOffset()); AttributeSet as = leaf.getAttributes(); if (as.isEqual(DEFAULT_COMMENT)) applyHighlighting(content, i); } }
@Override public void mouseClicked(MouseEvent me) { Element el = doc.getCharacterElement(viewToModel(me.getPoint())); if (el == null) return; AttributeSet as = el.getAttributes(); if (as.isDefined("ip")) { String ip = (String) as.getAttribute("ip"); ScriptException se = (ScriptException) as.getAttribute("exception"); Node node = net.getAtIP(ip); if (node == null) { Utility.displayError("Error", "Computer does not exist"); return; } String errorString = "--ERROR--\n" + "Error at line number " + se.getLineNumber() + " and column number " + se.getColumnNumber() + "\n" + se.getMessage(); new ScriptDialog( parentFrame, str -> { if (str != null) { node.setScript(str); } }, node.getScript(), errorString) .setVisible(true); } }
/** Is this image within a link? */ boolean isLink() { // ! It would be nice to cache this but in an editor it can change // See if I have an HREF attribute courtesy of the enclosing A tag: AttributeSet anchorAttr = (AttributeSet) fElement.getAttributes().getAttribute(HTML.Tag.A); if (anchorAttr != null) { return anchorAttr.isDefined(HTML.Attribute.HREF); } return false; }
@Override public void mouseMoved(MouseEvent me) { Element el = doc.getCharacterElement(viewToModel(me.getPoint())); if (el == null) return; AttributeSet as = el.getAttributes(); if (as.isDefined("ip")) { setCursor(handCursor); } else { setCursor(defaultCursor); } }
/** Look up an integer-valued attribute. <b>Not</b> recursive. */ private int getIntAttr(HTML.Attribute name, int deflt) { AttributeSet attr = fElement.getAttributes(); if (attr.isDefined(name)) { // does not check parents! int i; String val = (String) attr.getAttribute(name); if (val == null) i = deflt; else try { i = Math.max(0, Integer.parseInt(val)); } catch (NumberFormatException x) { i = deflt; } return i; } else return deflt; }
void editorPane_keyPressed(KeyEvent e) { StyledDocument doc = editorPane.getStyledDocument(); int pos = editorPane.getCaretPosition(); int code = e.getKeyCode(); Element el; switch (code) { case KeyEvent.VK_BACK_SPACE: case KeyEvent.VK_DELETE: case KeyEvent.VK_LEFT: case KeyEvent.VK_KP_LEFT: if (pos == 0) return; // we want to get the element to the left of position. el = doc.getCharacterElement(pos - 1); break; case KeyEvent.VK_RIGHT: case KeyEvent.VK_KP_RIGHT: // we want to get the element to the right of position. el = doc.getCharacterElement(pos + 1); break; default: return; // bail we don't handle it. } AttributeSet attr = el.getAttributes(); String el_name = (String) attr.getAttribute(StyleConstants.NameAttribute); int el_range = el.getEndOffset() - el.getStartOffset() - 1; if (el_name.startsWith("Parameter") && StyleConstants.getComponent(attr) != null) { try { switch (code) { case KeyEvent.VK_BACK_SPACE: case KeyEvent.VK_DELETE: doc.remove(el.getStartOffset(), el_range); break; case KeyEvent.VK_LEFT: case KeyEvent.VK_KP_LEFT: editorPane.setCaretPosition(pos - el_range); break; case KeyEvent.VK_RIGHT: case KeyEvent.VK_KP_RIGHT: editorPane.setCaretPosition(pos + (el_range)); break; } } catch (BadLocationException ex) { } } }
/* * Determine the Y offset for the current row */ private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException { // Get the bounding rectangle of the row Rectangle r = component.modelToView(rowStartOffset); int lineHeight = fontMetrics.getHeight(); int y = r.y + r.height; int descent = 0; // The text needs to be positioned above the bottom of the bounding // rectangle based on the descent of the font(s) contained on the row. if (r.height == lineHeight) { // default font is being used descent = fontMetrics.getDescent(); } else { // We need to check all the attributes for font changes if (fonts == null) { fonts = new HashMap<String, FontMetrics>(); } Element root = component.getDocument().getDefaultRootElement(); int index = root.getElementIndex(rowStartOffset); Element line = root.getElement(index); for (int i = 0; i < line.getElementCount(); i++) { Element child = line.getElement(i); AttributeSet as = child.getAttributes(); String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily); Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize); String key = fontFamily + fontSize; FontMetrics fm = fonts.get(key); if (fm == null) { Font font = new Font(fontFamily, Font.PLAIN, fontSize); fm = component.getFontMetrics(font); fonts.put(key, fm); } descent = Math.max(descent, fm.getDescent()); } } return y - descent; }
@Override protected int drawUnselectedText(java.awt.Graphics g, int x, int y, int p0, int p1) throws BadLocationException { Document doc = getDocument(); Segment segment = new Segment(); Segment token = getLineBuffer(); doc.getText(p0, p1 - p0, segment); int count = p1 - p0; int left = 0; int state = TEXT; int elementIndex = doc.getDefaultRootElement().getElementIndex(p0); AttributeSet lineAttributes = doc.getDefaultRootElement().getElement(elementIndex).getAttributes(); if (lineAttributes.isDefined("inComment")) { state = MULTILINECOMMENT; } for (int i = 0; i < count; i++) { // Starting in default text state. if (state == TEXT) { if (Character.isLetter(segment.array[i + segment.offset]) && Character.isLowerCase(segment.array[i + segment.offset])) { // flush now g.setColor(textColor); doc.getText(p0 + left, i - left, token); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i; state = KEYWORD; } // Do nothing else { if (segment.array[i + segment.offset] == '/') { // flush now g.setColor(textColor); doc.getText(p0 + left, i - left, token); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i; state = COMMENT; } else if (segment.array[i + segment.offset] == '"') { // flush now g.setColor(textColor); doc.getText(p0 + left, i - left, token); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i; state = STRING; } } } else if (state == KEYWORD) { // Still if (Character.isLetter( segment .array[ i + segment .offset])) { // && Character.isLowerCase(segment.array[i+segment.offset])) } else { // flush now doc.getText(p0 + left, i - left, token); if (Keywords.isKeyword(token)) { g.setColor(keywordColor); } else { g.setColor(textColor); } x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i; state = TEXT; if (segment.array[i + segment.offset] == '/') { state = COMMENT; } else if (segment.array[i + segment.offset] == '"') { state = STRING; } } } else if (state == COMMENT) { if (segment.array[i + segment.offset] == '/') { break; } else if (segment.array[i + segment.offset] == '*') { state = MULTILINECOMMENT; } else { state = TEXT; } } else if (state == MULTILINECOMMENT) { if (i > 0 && segment.array[i + segment.offset] == '/' && segment.array[i + segment.offset - 1] == '*') { // flush now doc.getText(p0 + left, i + 1 - left, token); g.setColor(commentColor); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i + 1; state = TEXT; } } else if (state == STRING) { if (segment.array[i + segment.offset] == '"') { // flush now doc.getText(p0 + left, i + 1 - left, token); g.setColor(stringColor); x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); left = i + 1; state = TEXT; } } // Starting not in token } // end loop // Flush last doc.getText(p0 + left, p1 - p0 - left, token); if (state == KEYWORD) { if (Keywords.isKeyword(token)) { g.setColor(keywordColor); } else { g.setColor(textColor); } } else if (state == STRING) { g.setColor(stringColor); } else if (state == COMMENT && ((p1 - p0 - left) > 1)) { g.setColor(commentColor); } else if (state == MULTILINECOMMENT) { g.setColor(commentColor); } else { g.setColor(textColor); } x = Utilities.drawTabbedText(token, x, y, g, this, p0 + left); return x; }