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); }
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; }
private void initialize(Element elem) { synchronized (this) { loading = true; fWidth = fHeight = 0; } int width = 0; int height = 0; boolean customWidth = false; boolean customHeight = false; try { fElement = elem; // Request image from document's cache: AttributeSet attr = elem.getAttributes(); if (isURL()) { URL src = getSourceURL(); if (src != null) { Dictionary cache = (Dictionary) getDocument().getProperty(IMAGE_CACHE_PROPERTY); if (cache != null) fImage = (Image) cache.get(src); else fImage = Toolkit.getDefaultToolkit().getImage(src); } } else { /** ****** Code to load from relative path ************ */ String src = (String) fElement.getAttributes().getAttribute(HTML.Attribute.SRC); System.out.println("before src: " + src); src = processSrcPath(src); System.out.println("after src: " + src); fImage = Toolkit.getDefaultToolkit().createImage(src); try { waitForImage(); } catch (InterruptedException e) { fImage = null; } /** *************************************************** */ } // Get height/width from params or image or defaults: height = getIntAttr(HTML.Attribute.HEIGHT, -1); customHeight = (height > 0); if (!customHeight && fImage != null) height = fImage.getHeight(this); if (height <= 0) height = DEFAULT_HEIGHT; width = getIntAttr(HTML.Attribute.WIDTH, -1); customWidth = (width > 0); if (!customWidth && fImage != null) width = fImage.getWidth(this); if (width <= 0) width = DEFAULT_WIDTH; // Make sure the image starts loading: if (fImage != null) if (customWidth && customHeight) Toolkit.getDefaultToolkit().prepareImage(fImage, height, width, this); else Toolkit.getDefaultToolkit().prepareImage(fImage, -1, -1, this); /** * ****************************************************** // Rob took this out. Changed scope * of src. if( DEBUG ) { if( fImage != null ) System.out.println("ImageInfo: new on "+src+ " * ("+fWidth+"x"+fHeight+")"); else System.out.println("ImageInfo: couldn't get image at "+ * src); if(isLink()) System.out.println(" It's a link! Border = "+ getBorder()); * //((AbstractDocument.AbstractElement)elem).dump(System.out,4); } * ****************************************************** */ } finally { synchronized (this) { loading = false; if (customWidth || fWidth == 0) { fWidth = width; } if (customHeight || fHeight == 0) { fHeight = height; } } } }