Example #1
1
  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);
  }
Example #2
0
  /*
   *  We need to know if the caret is currently positioned on the line we
   *  are about to paint so the line number can be highlighted.
   */
  private boolean isCurrentLine(int rowStartOffset) {
    int caretPosition = component.getCaretPosition();
    Element root = component.getDocument().getDefaultRootElement();

    if (root.getElementIndex(rowStartOffset) == root.getElementIndex(caretPosition)) return true;
    else return false;
  }
Example #3
0
 public void parseChildren(Element e) {
   try {
     NodeList nl = e.getChildNodes();
     for (int i = 0; i < nl.getLength(); i++) {
       // parse Elements only
       if ((nl.item(i).getNodeType() == Node.ELEMENT_NODE)) {
         Element elm = (Element) nl.item(i);
         String tag = elm.getTagName();
         if (tag.equals("canvas")) {
           parseChildren(elm);
         } else if (tag.equals("label")) {
           parseLabel(elm);
         } else if (tag.equals("textfield")) {
           parseTextField(elm);
         } else if (tag.equals("button")) {
           parseButton(elm);
         } else if (tag.equals("textarea")) {
           parseTextArea(elm);
         } else if (tag.equals("progressbar")) {
           parseProgressBar(elm);
         }
       }
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
Example #4
0
  /*
   *	Get the line number to be drawn. The empty string will be returned
   *  when a line of text has wrapped.
   */
  protected String getTextLineNumber(int rowStartOffset) {
    Element root = component.getDocument().getDefaultRootElement();
    int index = root.getElementIndex(rowStartOffset);
    Element line = root.getElement(index);

    if (line.getStartOffset() == rowStartOffset) return String.valueOf(index + 1);
    else return "";
  }
Example #5
0
  //
  //  Implement CaretListener interface
  //
  @Override
  public void caretUpdate(CaretEvent e) {
    //  Get the line the caret is positioned on

    int caretPosition = component.getCaretPosition();
    Element root = component.getDocument().getDefaultRootElement();
    int currentLine = root.getElementIndex(caretPosition);

    //  Need to repaint so the correct line number can be highlighted
    if (lastLine != currentLine) {
      repaint();
      lastLine = currentLine;
    }
  }
Example #6
0
 public void caretUpdate(CaretEvent e) {
   // when the cursor moves on _textView
   // this method will be called. Then, we
   // must determine what the line number is
   // and update the line number view
   Element root = textView.getDocument().getDefaultRootElement();
   int line = root.getElementIndex(e.getDot());
   root = root.getElement(line);
   int col = root.getElementIndex(e.getDot());
   lineNumberView.setText(line + ":" + col);
   // if text is selected then enable copy and cut
   boolean isSelection = e.getDot() != e.getMark();
   copyAction.setEnabled(isSelection);
   cutAction.setEnabled(isSelection);
 }
Example #7
0
 public void parseAttributes(Element elm, JComponent comp) {
   String enabled = elm.getAttribute("enabled");
   if (enabled != null) {
     comp.setEnabled(!enabled.equals("false"));
   }
   String id = elm.getAttribute("id");
   if (id.length() > 0) {
     components.put(id, comp);
     comp.setName(id);
   }
   String tooltip = elm.getAttribute("tooltip");
   if (tooltip.length() > 0) {
     comp.setToolTipText(tooltip);
   }
 }
Example #8
0
 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) {
     }
   }
 }
Example #9
0
  /** Calculate the width needed to display the maximum line number */
  private void setPreferredWidth() {
    Element root = component.getDocument().getDefaultRootElement();
    int lines = root.getElementCount();
    int digits = Math.max(String.valueOf(lines).length(), minimumDisplayDigits);

    //  Update sizes when number of digits in the line number changes
    if (lastDigits != digits) {
      lastDigits = digits;
      FontMetrics fontMetrics = getFontMetrics(getFont());
      int width = fontMetrics.charWidth('0') * digits;
      Insets insets = getInsets();
      int preferredWidth = insets.left + insets.right + width;

      Dimension d = getPreferredSize();
      d.setSize(preferredWidth, HEIGHT);
      setPreferredSize(d);
      setSize(d);
    }
  }
Example #10
0
 public void parseBorder(JComponent c, Element elm) {
   if (elm.getTagName().equals("border")) {
     String type = elm.getAttribute("type");
     if (type.equals("empty")) {
       c.setBorder(new EmptyBorder(2, 2, 2, 2));
     } else if (type.equals("titled")) {
       String title = elm.getAttribute("title");
       c.setBorder(new TitledBorder(title));
     } else if (type.equals("beveled")) {
       int btype = Integer.parseInt(elm.getAttribute("bevel"));
       c.setBorder(new BevelBorder(btype));
     }
   } else {
     NodeList nl = elm.getElementsByTagName("border");
     for (int n = 0; n < nl.getLength(); n++) {
       parseBorder(c, (Element) nl.item(n));
     }
   }
 }
Example #11
0
 public void parseButton(Element elm) {
   JButton btn = new JButton();
   String title = elm.getAttribute("title");
   if (title != null) {
     btn.setText(title);
   }
   SpazPosition lp = parseSpazPosition(elm);
   if (lp == null) {
     return;
   }
   this.add(btn, lp);
   parseBorder(btn, elm);
   parseAttributes(elm, btn);
   String action = elm.getAttribute("action");
   if (action.length() > 0) {
     actions.put(btn, action);
     btn.addActionListener(this);
   }
 }
Example #12
0
 public SpazPosition parseSpazPosition(Element elm) {
   if (elm.getTagName().equals("position")) {
     SpazPosition lp = new SpazPosition();
     double left_rel = Double.parseDouble(elm.getAttribute("left_rel"));
     double right_rel = Double.parseDouble(elm.getAttribute("right_rel"));
     double top_rel = Double.parseDouble(elm.getAttribute("top_rel"));
     double bottom_rel = Double.parseDouble(elm.getAttribute("bottom_rel"));
     int left_abs = Integer.parseInt(elm.getAttribute("left_abs"));
     int right_abs = Integer.parseInt(elm.getAttribute("right_abs"));
     int top_abs = Integer.parseInt(elm.getAttribute("top_abs"));
     int bottom_abs = Integer.parseInt(elm.getAttribute("bottom_abs"));
     lp.setHPos(left_rel, left_abs, right_rel, right_abs);
     lp.setVPos(top_rel, top_abs, bottom_rel, bottom_abs);
     return lp;
   } else {
     NodeList nl = elm.getElementsByTagName("position");
     for (int n = 0; n < nl.getLength(); n++) {
       SpazPosition lp = parseSpazPosition((Element) (nl.item(n)));
       return lp;
     }
   }
   return null;
 }
Example #13
0
 public void parseLabel(Element elm) {
   JLabel label = new JLabel();
   String title = elm.getAttribute("title");
   if (title != null) {
     label.setText(title);
   }
   SpazPosition lp = parseSpazPosition(elm);
   if (lp == null) {
     return;
   }
   this.add(label, lp);
   parseBorder(label, elm);
   parseAttributes(elm, label);
 }
Example #14
0
  /*
   *  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;
  }