Exemplo n.º 1
0
 @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);
   }
 }
Exemplo n.º 2
0
 /** 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;
 }
Exemplo n.º 3
0
 @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);
   }
 }
Exemplo n.º 4
0
  /**
   * Searches the attribute set for a tag, both of which are passed in as a parameter. Returns true
   * if no match is found and false otherwise.
   */
  private boolean noMatchForTagInAttributes(AttributeSet attr, HTML.Tag t, Object tagValue) {
    if (attr != null && attr.isDefined(t)) {
      Object newValue = attr.getAttribute(t);

      if ((tagValue == null)
          ? (newValue == null)
          : (newValue != null && tagValue.equals(newValue))) {
        return false;
      }
    }
    return true;
  }
 /** 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;
 }
  /** Update any cached values that come from attributes. */
  protected void setPropertiesFromAttributes() {
    StyleSheet sheet = getStyleSheet();
    this.attr = sheet.getViewAttributes(this);

    // Gutters
    borderSize = (short) getIntAttr(HTML.Attribute.BORDER, isLink() ? DEFAULT_BORDER : 0);

    leftInset = rightInset = (short) (getIntAttr(HTML.Attribute.HSPACE, 0) + borderSize);
    topInset = bottomInset = (short) (getIntAttr(HTML.Attribute.VSPACE, 0) + borderSize);

    borderColor = ((StyledDocument) getDocument()).getForeground(getAttributes());

    AttributeSet attr = getElement().getAttributes();

    // Alignment.
    // PENDING: This needs to be changed to support the CSS versions
    // when conversion from ALIGN to VERTICAL_ALIGN is complete.
    Object alignment = attr.getAttribute(HTML.Attribute.ALIGN);

    vAlign = 1.0f;
    if (alignment != null) {
      alignment = alignment.toString();
      if ("top".equals(alignment)) {
        vAlign = 0f;
      } else if ("middle".equals(alignment)) {
        vAlign = .5f;
      }
    }

    AttributeSet anchorAttr = (AttributeSet) attr.getAttribute(HTML.Tag.A);
    if (anchorAttr != null && anchorAttr.isDefined(HTML.Attribute.HREF)) {
      synchronized (this) {
        state |= LINK_FLAG;
      }
    } else {
      synchronized (this) {
        state = (state | LINK_FLAG) ^ LINK_FLAG;
      }
    }
  }
  @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;
  }