/**
   * Provides a mapping from the view coordinate space to the logical coordinate space of the model.
   *
   * @param fx the X coordinate >= 0
   * @param fy the Y coordinate >= 0
   * @param a the allocated region to render into
   * @return the location within the model that best represents the given point in the view >= 0
   */
  @Override
  public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {

    bias[0] = Position.Bias.Forward;

    Rectangle alloc = a.getBounds();
    RSyntaxDocument doc = (RSyntaxDocument) getDocument();
    int x = (int) fx;
    int y = (int) fy;

    // If they're asking about a view position above the area covered by
    // this view, then the position is assumed to be the starting position
    // of this view.
    if (y < alloc.y) {
      return getStartOffset();
    }

    // If they're asking about a position below this view, the position
    // is assumed to be the ending position of this view.
    else if (y > alloc.y + alloc.height) {
      return host.getLastVisibleOffset();
    }

    // They're asking about a position within the coverage of this view
    // vertically.  So, we figure out which line the point corresponds to.
    // If the line is greater than the number of lines contained, then
    // simply use the last line as it represents the last possible place
    // we can position to.
    else {

      Element map = doc.getDefaultRootElement();
      int lineIndex = Math.abs((y - alloc.y) / lineHeight); // metrics.getHeight() );
      FoldManager fm = host.getFoldManager();
      // System.out.print("--- " + lineIndex);
      lineIndex += fm.getHiddenLineCountAbove(lineIndex, true);
      // System.out.println(" => " + lineIndex);
      if (lineIndex >= map.getElementCount()) {
        return host.getLastVisibleOffset();
      }

      Element line = map.getElement(lineIndex);

      // If the point is to the left of the line...
      if (x < alloc.x) {
        return line.getStartOffset();
      } else if (x > alloc.x + alloc.width) {
        return line.getEndOffset() - 1;
      } else {
        // Determine the offset into the text
        int p0 = line.getStartOffset();
        Token tokenList = doc.getTokenListForLine(lineIndex);
        tabBase = alloc.x;
        int offs = tokenList.getListOffset((RSyntaxTextArea) getContainer(), this, tabBase, x);
        return offs != -1 ? offs : p0;
      }
    } // End of else.
  }
Exemplo n.º 2
0
  void processMacro(JSONObject data) {
    // FIXME: need to check parameters.
    // FIXME: need to check permissions.
    if ("callMacro".equalsIgnoreCase(data.getString("command"))) {
      Token token = findTokenFromId(data.getString("tokenId"));

      MacroButtonProperties macro = token.getMacro(data.getInt("macroIndex"), false);
      macro.executeMacro(token.getId());
    }
  }
Exemplo n.º 3
0
  String getTokenValue(Token token, String name) {
    if (":name".equalsIgnoreCase(name)) {
      return token.getName();
    } else if (":notes".equalsIgnoreCase(name)) {
      return token.getNotes();
    } else if (":label".equalsIgnoreCase(name)) {
      return token.getLabel();
    }

    return "";
  }
Exemplo n.º 4
0
    @Override
    public void execute(final GUI gui) {
      final DialogExport dialog = new DialogExport(gui);
      if (!dialog.ok()) return;

      final IOFile root = new IOFile(dialog.path());

      // check if existing files will be overwritten
      if (root.exists()) {
        IO file = null;
        boolean overwrite = false;
        final Data d = gui.context.data();
        final IntList il = d.resources.docs();
        final int is = il.size();
        for (int i = 0; i < is; i++) {
          file = root.merge(Token.string(d.text(il.get(i), true)));
          if (file.exists()) {
            if (overwrite) {
              // more than one file will be overwritten; check remaining tests
              file = null;
              break;
            }
            overwrite = true;
          }
        }
        if (overwrite) {
          // show message for overwriting files or directories
          final String msg = file == null ? FILES_REPLACE_X : FILE_EXISTS_X;
          if (file == null) file = root;
          if (!BaseXDialog.confirm(gui, Util.info(msg, file))) return;
        }
      }
      DialogProgress.execute(gui, new Export(root.path()));
    }
Exemplo n.º 5
0
 @Override
 public void execute(final GUI gui) {
   final int pre = gui.context.marked.pres[0];
   final byte[] txt = ViewData.path(gui.context.data(), pre);
   // copy path to clipboard
   final Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
   clip.setContents(new StringSelection(Token.string(txt)), null);
 }
Exemplo n.º 6
0
  private void tokenRemoved(Token token) {
    JSONObject jobj = new JSONObject();
    JSONArray tokenArray = new JSONArray();
    tokenArray.add(token.getId().toString());
    jobj.put("tokensRemoved", tokenArray);

    MTWebClientManager.getInstance().sendToAllSessions("token-update", jobj);
  }
Exemplo n.º 7
0
 /**
  * Notifies all views of a focus change.
  *
  * @param pre focused pre value
  * @param vw the calling view
  */
 public void focus(final int pre, final View vw) {
   if (gui.context.focused == pre) return;
   gui.context.focused = pre;
   for (final View v : view) if (v != vw && v.visible()) v.refreshFocus();
   if (pre != -1) {
     gui.status.setText(Token.string(ViewData.path(gui.context.data(), pre)));
   }
 }
Exemplo n.º 8
0
  void sendTokenRegisterdProperties(MTWebSocket mtws, String inResponseTo, JSONObject data) {
    String tokenId = data.getString("tokenId");
    Token token = findTokenFromId(tokenId);

    if (token == null) {
      System.out.println("DEBUG: sendTokenInfo(): Unable to find token " + tokenId);
      return;
      // FIXME: log this error
    }

    JSONObject jobj = new JSONObject();
    jobj.put("tokenId", tokenId);
    jobj.put("name", token.getName());
    jobj.put("label", token.getLabel());
    jobj.put("notes", token.getNotes());

    JSONObject jprop = new JSONObject();

    for (TokenProperty tp : MapTool.getCampaign().getTokenPropertyList(token.getPropertyType())) {
      JSONObject jp = new JSONObject();
      jp.put("name", tp.getName());
      if (tp.getShortName() != null) {
        jp.put("shortName", tp.getShortName());
      }
      if (tp.getDefaultValue() != null) {
        jp.put("defaultValue", tp.getDefaultValue());
      }
      jp.put("value", token.getProperty(tp.getName()));
      jp.put("showOnStatSheet", tp.isShowOnStatSheet());

      jprop.put(tp.getName(), jp);
    }

    jobj.put("properties", jprop);

    JSONArray jmacros = new JSONArray();

    for (MacroButtonProperties macro : token.getMacroList(false)) {
      JSONObject jmb = new JSONObject();
      jmb.put("label", macro.getLabel());
      jmb.put("tooltip", macro.getEvaluatedToolTip());
      jmb.put("index", macro.getIndex());
      jmb.put("fontColor", macro.getFontColorAsHtml());
      jmb.put("displayGroup", macro.getGroupForDisplay());
      jmb.put("group", macro.getGroup());
      jmb.put("index", macro.getIndex());
      jmb.put("autoExecute", macro.getAutoExecute());
      jmb.put("maxWidth", macro.getMaxWidth());
      jmb.put("minWidth", macro.getMinWidth());
      jmb.put("applyToTokens", macro.getApplyToTokens());

      jmacros.add(jmb);
    }

    jobj.put("macros", jmacros);

    mtws.sendMessage("tokenInfo", inResponseTo, jobj);
  }
  /**
   * Provides a mapping from the document model coordinate space to the coordinate space of the view
   * mapped to it.
   *
   * @param pos the position to convert &gt;= 0
   * @param a the allocated region to render into
   * @return the bounding box of the given position
   * @exception BadLocationException if the given position does not represent a valid location in
   *     the associated document
   * @see View#modelToView
   */
  @Override
  public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {

    // line coordinates
    Element map = getElement();
    RSyntaxDocument doc = (RSyntaxDocument) getDocument();
    int lineIndex = map.getElementIndex(pos);
    Token tokenList = doc.getTokenListForLine(lineIndex);
    Rectangle lineArea = lineToRect(a, lineIndex);
    tabBase = lineArea.x; // Used by listOffsetToView().

    // int x = (int)RSyntaxUtilities.getTokenListWidthUpTo(tokenList,
    //							(RSyntaxTextArea)getContainer(),
    //							this, 0, pos);
    // We use this method instead as it returns the actual bounding box,
    // not just the x-coordinate.
    lineArea =
        tokenList.listOffsetToView((RSyntaxTextArea) getContainer(), this, pos, tabBase, lineArea);

    return lineArea;
  }
Exemplo n.º 10
0
  /**
   * Draws the passed-in text using syntax highlighting for the current language. It is assumed that
   * the entire line is either not in a selected region, or painting with a selection-foreground
   * color is turned off.
   *
   * @param painter The painter to render the tokens.
   * @param token The list of tokens to draw.
   * @param g The graphics context in which to draw.
   * @param x The x-coordinate at which to draw.
   * @param y The y-coordinate at which to draw.
   * @return The x-coordinate representing the end of the painted text.
   */
  private float drawLine(
      TokenPainter painter, Token token, Graphics2D g, float x, float y, int line) {

    float nextX = x; // The x-value at the end of our text.
    boolean paintBG = host.getPaintTokenBackgrounds(line, y);

    while (token != null && token.isPaintable() && nextX < clipEnd) {
      nextX = painter.paint(token, g, nextX, y, host, this, clipStart, paintBG);
      token = token.getNextToken();
    }

    // NOTE: We should re-use code from Token (paintBackground()) here,
    // but don't because I'm just too lazy.
    if (host.getEOLMarkersVisible()) {
      g.setColor(host.getForegroundForTokenType(Token.WHITESPACE));
      g.setFont(host.getFontForTokenType(Token.WHITESPACE));
      g.drawString("\u00B6", nextX, y);
    }

    // Return the x-coordinate at the end of the painted text.
    return nextX;
  }
Exemplo n.º 11
0
  void sendTokenProperties(MTWebSocket mtws, String inResponseTo, JSONObject data) {
    String tokenId = data.getString("tokenId");
    Token token = findTokenFromId(tokenId);

    if (token == null) {
      System.out.println("DEBUG: sendTokenInfo(): Unable to find token " + tokenId);
      return;
      // FIXME: log this error
    }

    JSONObject jobj = new JSONObject();
    jobj.put("tokenId", tokenId);

    JSONArray properties = new JSONArray();
    JSONObject propertiesMap = new JSONObject();

    JSONArray propToFetch = data.getJSONArray("propertyNames");
    for (int i = 0; i < propToFetch.size(); i++) {
      String pname = propToFetch.getString(i);
      String val;
      if (pname.startsWith(":")) {
        val = getTokenValue(token, pname);
      } else {

        val = token.getProperty(pname) == null ? null : token.getProperty(pname).toString();
      }
      JSONObject jprop = new JSONObject();
      jprop.put("name", pname);
      jprop.put("value", val);
      properties.add(jprop);
      propertiesMap.put(pname, val);
    }

    jobj.put("properties", properties);
    jobj.put("propertiesMap", propertiesMap);

    mtws.sendMessage("tokenProperties", inResponseTo, jobj);
  }
Exemplo n.º 12
0
  /**
   * This is called by the nested wrapped line views to determine the break location. This can be
   * reimplemented to alter the breaking behavior. It will either break at word or character
   * boundaries depending upon the break argument given at construction.
   */
  protected int calculateBreakPosition(int p0, Token tokenList, float x0) {
    // System.err.println("------ beginning calculateBreakPosition() --------");
    int p = p0;
    RSyntaxTextArea textArea = (RSyntaxTextArea) getContainer();
    float currentWidth = getWidth();
    if (currentWidth == Integer.MAX_VALUE) currentWidth = getPreferredSpan(X_AXIS);
    // Make sure width>0; this is a huge hack to fix a bug where
    // loading text into an RTextArea before it is visible if word wrap
    // is enabled causes an infinite loop in calculateBreakPosition()
    // because of the 0-width!  We cannot simply check in setSize()
    // because the width is set to 0 somewhere else too somehow...
    currentWidth = Math.max(currentWidth, MIN_WIDTH);
    Token t = tokenList;
    while (t != null && t.isPaintable()) {
      // FIXME:  Replace the code below with the commented-out line below.  This will
      // allow long tokens to be broken at embedded spaces (such as MLC's).  But it
      // currently throws BadLocationExceptions sometimes...
      float tokenWidth = t.getWidth(textArea, this, x0);
      if (tokenWidth > currentWidth) {
        // If the current token alone is too long for this line,
        // break at a character boundary.
        if (p == p0) {
          return t.getOffsetBeforeX(textArea, this, 0, currentWidth);
        }
        // Return the first non-whitespace char (i.e., don't start
        // off the continuation of a wrapped line with whitespace).
        return t.isWhitespace() ? p + t.length() : p;
        // return getBreakLocation(t, fm, x0, currentWidth, this);
      }
      currentWidth -= tokenWidth;
      x0 += tokenWidth;
      p += t.length();
      // System.err.println("*** *** *** token fit entirely (width==" + tokenWidth + "), adding " +
      // t.textCount + " to p, now p==" + p);
      t = t.getNextToken();
    }
    // System.err.println("... ... whole line fits; returning p==" + p);
    // System.err.println("------ ending calculateBreakPosition() --------");

    //		return p;
    return p + 1;
  }
Exemplo n.º 13
0
    @Override
    public void execute(final GUI gui) {
      final Nodes n = gui.context.marked;
      final DialogInsert insert = new DialogInsert(gui);
      if (!insert.ok()) return;

      final StringList sl = insert.result;
      final NodeType type = ANode.type(insert.kind);
      String item = Token.string(type.string()) + " { " + quote(sl.get(0)) + " }";

      if (type == NodeType.ATT || type == NodeType.PI) {
        item += " { " + quote(sl.get(1)) + " }";
      } else if (type == NodeType.ELM) {
        item += " { () }";
      }

      gui.context.copied = null;
      gui.execute(new XQuery("insert node " + item + " into " + openPre(n, 0)));
    }
Exemplo n.º 14
0
 /**
  * Sets the output text.
  *
  * @param text output text
  * @param size text size
  */
 public final void setText(final byte[] text, final int size) {
   byte[] txt = text;
   if (Token.contains(text, '\r')) {
     // remove carriage returns
     int ns = 0;
     for (int r = 0; r < size; ++r) {
       final byte b = text[r];
       if (b != '\r') text[ns++] = b;
     }
     // new text is different...
     txt = Arrays.copyOf(text, ns);
   } else if (text.length != size) {
     txt = Arrays.copyOf(text, size);
   }
   if (editor.text(txt)) {
     if (hist != null) hist.store(txt, editor.pos(), 0);
   }
   if (isShowing()) resizeCode.invokeLater();
 }
Exemplo n.º 15
0
Arquivo: AI.java Projeto: giurra8/Hex
  public void proveraZaToken(Cell c, Token t) {
    int i0 = 0, i1 = 0, i2 = 0;
    Map numberToOwn = t.getNumberToOwn();
    ArrayList<Cell> possibleCellsforToken = t.getPossibleCells();
    possibleCellsforToken.add(c);
    Frame frejm = Frame.getInstance();
    int val = 0;

    for (TokenField field : t.getTcells()) {
      if (field.getId() == 1) {
        if (c.getIdx() % 2 == 0) {
          i0 = t.getNiz()[0];

          if (frejm.getCellById(c.getIdx() - 1, c.getIdy()) != null
              && frejm.getCellById(c.getIdx() - 1, c.getIdy()).getTok() != null) {
            i1 = frejm.getCellById(c.getIdx() - 1, c.getIdy()).getTok().getNiz()[1];
            if (i0 > i1 && i1 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx() - 1, c.getIdy() + 1) != null
              && frejm.getCellById(c.getIdx() - 1, c.getIdy() + 1).getTok() != null) {
            i2 = frejm.getCellById(c.getIdx() - 1, c.getIdy() + 1).getTok().getNiz()[2];
            if (i0 > i2 && i2 != 0) val++;
          }

        } else {
          i0 = t.getNiz()[0];
          if (frejm.getCellById(c.getIdx() - 1, c.getIdy() - 1) != null
              && frejm.getCellById(c.getIdx() - 1, c.getIdy() - 1).getTok() != null) {
            i1 = frejm.getCellById(c.getIdx() - 1, c.getIdy() - 1).getTok().getNiz()[1];
            if (i0 > i1 && i1 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx() - 1, c.getIdy()) != null
              && frejm.getCellById(c.getIdx() - 1, c.getIdy()).getTok() != null) {
            i2 = frejm.getCellById(c.getIdx() - 1, c.getIdy()).getTok().getNiz()[2];

            if (i0 > i2 && i2 != 0) val++;
          }
        }
      } else if (field.getId() == 2) {
        if (c.getIdx() % 2 == 0) {
          i1 = t.getNiz()[1];
          if (frejm.getCellById(c.getIdx() + 1, c.getIdy() + 1) != null
              && frejm.getCellById(c.getIdx() + 1, c.getIdy() + 1).getTok() != null) {
            i0 = frejm.getCellById(c.getIdx() + 1, c.getIdy() + 1).getTok().getNiz()[0];
            if (i1 > i0 && i0 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx(), c.getIdy() + 1) != null
              && frejm.getCellById(c.getIdx(), c.getIdy() + 1).getTok() != null) {
            i2 = frejm.getCellById(c.getIdx(), c.getIdy() + 1).getTok().getNiz()[2];
            if (i1 > i2 && i2 != 0) val++;
          }

        } else {
          i1 = t.getNiz()[1];
          if (frejm.getCellById(c.getIdx() + 1, c.getIdy()) != null
              && frejm.getCellById(c.getIdx() + 1, c.getIdy()).getTok() != null) {
            i0 = frejm.getCellById(c.getIdx() + 1, c.getIdy()).getTok().getNiz()[0];

            if (i1 > i0 && i0 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx(), c.getIdy() + 1) != null
              && frejm.getCellById(c.getIdx(), c.getIdy() + 1).getTok() != null) {
            i2 = frejm.getCellById(c.getIdx(), c.getIdy() + 1).getTok().getNiz()[2];
            if (i1 > i2 && i2 != 0) val++;
          }
        }
      } else if (field.getId() == 3) {
        if (c.getIdx() % 2 == 0) {

          i2 = t.getNiz()[2];
          if (frejm.getCellById(c.getIdx() + 1, c.getIdy()) != null
              && frejm.getCellById(c.getIdx() + 1, c.getIdy()).getTok() != null) {
            i0 = frejm.getCellById(c.getIdx() + 1, c.getIdy()).getTok().getNiz()[0];
            if (i2 > i0 && i0 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx(), c.getIdy() - 1) != null
              && frejm.getCellById(c.getIdx(), c.getIdy() - 1).getTok() != null) {
            i1 = frejm.getCellById(c.getIdx(), c.getIdy() - 1).getTok().getNiz()[1];
            if (i2 > i1 && i1 != 0) val++;
          }

        } else {
          i2 = t.getNiz()[2];
          if (frejm.getCellById(c.getIdx() + 1, c.getIdy() - 1) != null
              && frejm.getCellById(c.getIdx() + 1, c.getIdy() - 1).getTok() != null) {
            i0 = frejm.getCellById(c.getIdx() + 1, c.getIdy() - 1).getTok().getNiz()[0];
            if (i2 > i0 && i0 != 0) val++;
          }
          if (frejm.getCellById(c.getIdx(), c.getIdy() - 1) != null
              && frejm.getCellById(c.getIdx(), c.getIdy() - 1).getTok() != null) {
            i1 = frejm.getCellById(c.getIdx(), c.getIdy() - 1).getTok().getNiz()[1];
            if (i2 > i1 && i1 != 0) val++;
          }
        }
      }
    }
    Integer number = val;
    t.getBrojpromena().add(val);
    numberToOwn.put(number, c);
  }
Exemplo n.º 16
0
  /**
   * Draws a single view (i.e., a line of text for a wrapped view), wrapping the text onto multiple
   * lines if necessary. Any selected text is rendered with the editor's "selected text" color.
   *
   * @param painter The painter to use to render tokens.
   * @param g The graphics context in which to paint.
   * @param r The rectangle in which to paint.
   * @param view The <code>View</code> to paint.
   * @param fontHeight The height of the font being used.
   * @param y The y-coordinate at which to begin painting.
   * @param selStart The start of the selection.
   * @param selEnd The end of the selection.
   */
  protected void drawViewWithSelection(
      TokenPainter painter,
      Graphics2D g,
      Rectangle r,
      View view,
      int fontHeight,
      int y,
      int selStart,
      int selEnd) {

    float x = r.x;

    LayeredHighlighter h = (LayeredHighlighter) host.getHighlighter();

    RSyntaxDocument document = (RSyntaxDocument) getDocument();
    Element map = getElement();

    int p0 = view.getStartOffset();
    int lineNumber = map.getElementIndex(p0);
    int p1 = view.getEndOffset(); // - 1;

    setSegment(p0, p1 - 1, document, drawSeg);
    // System.err.println("drawSeg=='" + drawSeg + "' (p0/p1==" + p0 + "/" + p1 + ")");
    int start = p0 - drawSeg.offset;
    Token token = document.getTokenListForLine(lineNumber);

    // If this line is an empty line, then the token list is simply a
    // null token.  In this case, the line highlight will be skipped in
    // the loop below, so unfortunately we must manually do it here.
    if (token != null && token.getType() == Token.NULL) {
      h.paintLayeredHighlights(g, p0, p1, r, host, this);
      return;
    }

    // Loop through all tokens in this view and paint them!
    while (token != null && token.isPaintable()) {

      int p = calculateBreakPosition(p0, token, x);
      x = r.x;

      h.paintLayeredHighlights(g, p0, p, r, host, this);

      while (token != null && token.isPaintable() && token.getEndOffset() - 1 < p) { // <=p) {

        // Selection starts in this token
        if (token.containsPosition(selStart)) {

          if (selStart > token.getOffset()) {
            tempToken.copyFrom(token);
            tempToken.textCount = selStart - tempToken.getOffset();
            x = painter.paint(tempToken, g, x, y, host, this);
            tempToken.textCount = token.length();
            tempToken.makeStartAt(selStart);
            // Clone required since token and tempToken must be
            // different tokens for else statement below
            token = new TokenImpl(tempToken);
          }

          int selCount = Math.min(token.length(), selEnd - token.getOffset());
          if (selCount == token.length()) {
            x = painter.paintSelected(token, g, x, y, host, this);
          } else {
            tempToken.copyFrom(token);
            tempToken.textCount = selCount;
            x = painter.paintSelected(tempToken, g, x, y, host, this);
            tempToken.textCount = token.length();
            tempToken.makeStartAt(token.getOffset() + selCount);
            token = tempToken;
            x = painter.paint(token, g, x, y, host, this);
          }

        }

        // Selection ends in this token
        else if (token.containsPosition(selEnd)) {
          tempToken.copyFrom(token);
          tempToken.textCount = selEnd - tempToken.getOffset();
          x = painter.paintSelected(tempToken, g, x, y, host, this);
          tempToken.textCount = token.length();
          tempToken.makeStartAt(selEnd);
          token = tempToken;
          x = painter.paint(token, g, x, y, host, this);
        }

        // This token is entirely selected
        else if (token.getOffset() >= selStart && token.getEndOffset() <= selEnd) {
          x = painter.paintSelected(token, g, x, y, host, this);
        }

        // This token is entirely unselected
        else {
          x = painter.paint(token, g, x, y, host, this);
        }

        token = token.getNextToken();
      }

      // If there's a token that's going to be split onto the next line
      if (token != null && token.isPaintable() && token.getOffset() < p) {

        int tokenOffset = token.getOffset();
        Token orig = token;
        token =
            new TokenImpl(
                drawSeg, tokenOffset - start, p - 1 - start, tokenOffset, token.getType());

        // Selection starts in this token
        if (token.containsPosition(selStart)) {

          if (selStart > token.getOffset()) {
            tempToken.copyFrom(token);
            tempToken.textCount = selStart - tempToken.getOffset();
            x = painter.paint(tempToken, g, x, y, host, this);
            tempToken.textCount = token.length();
            tempToken.makeStartAt(selStart);
            // Clone required since token and tempToken must be
            // different tokens for else statement below
            token = new TokenImpl(tempToken);
          }

          int selCount = Math.min(token.length(), selEnd - token.getOffset());
          if (selCount == token.length()) {
            x = painter.paintSelected(token, g, x, y, host, this);
          } else {
            tempToken.copyFrom(token);
            tempToken.textCount = selCount;
            x = painter.paintSelected(tempToken, g, x, y, host, this);
            tempToken.textCount = token.length();
            tempToken.makeStartAt(token.getOffset() + selCount);
            token = tempToken;
            x = painter.paint(token, g, x, y, host, this);
          }

        }

        // Selection ends in this token
        else if (token.containsPosition(selEnd)) {
          tempToken.copyFrom(token);
          tempToken.textCount = selEnd - tempToken.getOffset();
          x = painter.paintSelected(tempToken, g, x, y, host, this);
          tempToken.textCount = token.length();
          tempToken.makeStartAt(selEnd);
          token = tempToken;
          x = painter.paint(token, g, x, y, host, this);
        }

        // This token is entirely selected
        else if (token.getOffset() >= selStart && token.getEndOffset() <= selEnd) {
          x = painter.paintSelected(token, g, x, y, host, this);
        }

        // This token is entirely unselected
        else {
          x = painter.paint(token, g, x, y, host, this);
        }

        token = new TokenImpl(orig);
        ((TokenImpl) token).makeStartAt(p);
      }

      p0 = (p == p0) ? p1 : p;
      y += fontHeight;
    } // End of while (token!=null && token.isPaintable()).

    // NOTE: We should re-use code from Token (paintBackground()) here,
    // but don't because I'm just too lazy.
    if (host.getEOLMarkersVisible()) {
      g.setColor(host.getForegroundForTokenType(Token.WHITESPACE));
      g.setFont(host.getFontForTokenType(Token.WHITESPACE));
      g.drawString("\u00B6", x, y - fontHeight);
    }
  }
Exemplo n.º 17
0
  /**
   * Draws a single view (i.e., a line of text for a wrapped view), wrapping the text onto multiple
   * lines if necessary.
   *
   * @param painter The painter to use to render tokens.
   * @param g The graphics context in which to paint.
   * @param r The rectangle in which to paint.
   * @param view The <code>View</code> to paint.
   * @param fontHeight The height of the font being used.
   * @param y The y-coordinate at which to begin painting.
   */
  protected void drawView(
      TokenPainter painter, Graphics2D g, Rectangle r, View view, int fontHeight, int y) {

    float x = r.x;

    LayeredHighlighter h = (LayeredHighlighter) host.getHighlighter();

    RSyntaxDocument document = (RSyntaxDocument) getDocument();
    Element map = getElement();

    int p0 = view.getStartOffset();
    int lineNumber = map.getElementIndex(p0);
    int p1 = view.getEndOffset(); // - 1;

    setSegment(p0, p1 - 1, document, drawSeg);
    // System.err.println("drawSeg=='" + drawSeg + "' (p0/p1==" + p0 + "/" + p1 + ")");
    int start = p0 - drawSeg.offset;
    Token token = document.getTokenListForLine(lineNumber);

    // If this line is an empty line, then the token list is simply a
    // null token.  In this case, the line highlight will be skipped in
    // the loop below, so unfortunately we must manually do it here.
    if (token != null && token.getType() == Token.NULL) {
      h.paintLayeredHighlights(g, p0, p1, r, host, this);
      return;
    }

    // Loop through all tokens in this view and paint them!
    while (token != null && token.isPaintable()) {

      int p = calculateBreakPosition(p0, token, x);
      x = r.x;

      h.paintLayeredHighlights(g, p0, p, r, host, this);

      while (token != null && token.isPaintable() && token.getEndOffset() - 1 < p) { // <=p) {
        x = painter.paint(token, g, x, y, host, this);
        token = token.getNextToken();
      }

      if (token != null && token.isPaintable() && token.getOffset() < p) {
        int tokenOffset = token.getOffset();
        tempToken.set(
            drawSeg.array, tokenOffset - start, p - 1 - start, tokenOffset, token.getType());
        painter.paint(tempToken, g, x, y, host, this);
        tempToken.copyFrom(token);
        tempToken.makeStartAt(p);
        token = new TokenImpl(tempToken);
      }

      p0 = (p == p0) ? p1 : p;
      y += fontHeight;
    } // End of while (token!=null && token.isPaintable()).

    // NOTE: We should re-use code from Token (paintBackground()) here,
    // but don't because I'm just too lazy.
    if (host.getEOLMarkersVisible()) {
      g.setColor(host.getForegroundForTokenType(Token.WHITESPACE));
      g.setFont(host.getFontForTokenType(Token.WHITESPACE));
      g.drawString("\u00B6", x, y - fontHeight);
    }
  }
Exemplo n.º 18
0
    /**
     * Provides a mapping from the view coordinate space to the logical coordinate space of the
     * model.
     *
     * @param fx the X coordinate
     * @param fy the Y coordinate
     * @param a the allocated region to render into
     * @return the location within the model that best represents the given point in the view
     * @see View#viewToModel
     */
    @Override
    public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {

      // PENDING(prinz) implement bias properly
      bias[0] = Position.Bias.Forward;

      Rectangle alloc = (Rectangle) a;
      RSyntaxDocument doc = (RSyntaxDocument) getDocument();
      int x = (int) fx;
      int y = (int) fy;
      if (y < alloc.y) {
        // above the area covered by this icon, so the the position
        // is assumed to be the start of the coverage for this view.
        return getStartOffset();
      } else if (y > alloc.y + alloc.height) {
        // below the area covered by this icon, so the the position
        // is assumed to be the end of the coverage for this view.
        return getEndOffset() - 1;
      } else {

        // positioned within the coverage of this view vertically,
        // so we figure out which line the point corresponds to.
        // if the line is greater than the number of lines
        // contained, then simply use the last line as it represents
        // the last possible place we can position to.

        RSyntaxTextArea textArea = (RSyntaxTextArea) getContainer();
        alloc.height = textArea.getLineHeight();
        int p1 = getEndOffset();

        // Get the token list for this line so we don't have to keep
        // recomputing it if this logical line spans multiple
        // physical lines.
        Element map = doc.getDefaultRootElement();
        int p0 = getStartOffset();
        int line = map.getElementIndex(p0);
        Token tlist = doc.getTokenListForLine(line);

        // Look at each physical line-chunk of this logical line.
        while (p0 < p1) {

          // We can always use alloc.x since we always break
          // lines so they start at the beginning of a physical
          // line.
          TokenSubList subList =
              TokenUtils.getSubTokenList(
                  tlist, p0, WrappedSyntaxView.this, textArea, alloc.x, lineCountTempToken);
          tlist = subList != null ? subList.tokenList : null;
          int p = calculateBreakPosition(p0, tlist, alloc.x);

          // If desired view position is in this physical chunk.
          if ((y >= alloc.y) && (y < (alloc.y + alloc.height))) {

            // Point is to the left of the line
            if (x < alloc.x) {
              return p0;
            }

            // Point is to the right of the line
            else if (x > alloc.x + alloc.width) {
              return p - 1;
            }

            // Point is in this physical line!
            else {

              // Start at alloc.x since this chunk starts
              // at the beginning of a physical line.
              int n = tlist.getListOffset(textArea, WrappedSyntaxView.this, alloc.x, x);

              // NOTE:  We needed to add the max() with
              // p0 as getTokenListForLine returns -1
              // for empty lines (just a null token).
              // How did this work before?
              // FIXME:  Have null tokens have their
              // offset but a -1 length.
              return Math.max(Math.min(n, p1 - 1), p0);
            } // End of else.
          } // End of if ((y>=alloc.y) && ...

          p0 = (p == p0) ? p1 : p;
          alloc.y += alloc.height;
        } // End of while (p0<p1).

        return getEndOffset() - 1;
      } // End of else.
    }
Exemplo n.º 19
0
  /**
   * Draws the passed-in text using syntax highlighting for the current language. Tokens are checked
   * for being in a selected region, and are rendered appropriately if they are.
   *
   * @param painter The painter to render the tokens.
   * @param token The list of tokens to draw.
   * @param g The graphics context in which to draw.
   * @param x The x-coordinate at which to draw.
   * @param y The y-coordinate at which to draw.
   * @param selStart The start of the selection.
   * @param selEnd The end of the selection.
   * @return The x-coordinate representing the end of the painted text.
   */
  private float drawLineWithSelection(
      TokenPainter painter, Token token, Graphics2D g, float x, float y, int selStart, int selEnd) {

    float nextX = x; // The x-value at the end of our text.
    boolean useSTC = host.getUseSelectedTextColor();

    while (token != null && token.isPaintable() && nextX < clipEnd) {

      // Selection starts in this token
      if (token.containsPosition(selStart)) {

        if (selStart > token.getOffset()) {
          tempToken.copyFrom(token);
          tempToken.textCount = selStart - tempToken.getOffset();
          nextX = painter.paint(tempToken, g, nextX, y, host, this, clipStart);
          tempToken.textCount = token.length();
          tempToken.makeStartAt(selStart);
          // Clone required since token and tempToken must be
          // different tokens for else statement below
          token = new TokenImpl(tempToken);
        }

        int tokenLen = token.length();
        int selCount = Math.min(tokenLen, selEnd - token.getOffset());
        if (selCount == tokenLen) {
          nextX = painter.paintSelected(token, g, nextX, y, host, this, clipStart, useSTC);
        } else {
          tempToken.copyFrom(token);
          tempToken.textCount = selCount;
          nextX = painter.paintSelected(tempToken, g, nextX, y, host, this, clipStart, useSTC);
          tempToken.textCount = token.length();
          tempToken.makeStartAt(token.getOffset() + selCount);
          token = tempToken;
          nextX = painter.paint(token, g, nextX, y, host, this, clipStart);
        }

      }

      // Selection ends in this token
      else if (token.containsPosition(selEnd)) {
        tempToken.copyFrom(token);
        tempToken.textCount = selEnd - tempToken.getOffset();
        nextX = painter.paintSelected(tempToken, g, nextX, y, host, this, clipStart, useSTC);
        tempToken.textCount = token.length();
        tempToken.makeStartAt(selEnd);
        token = tempToken;
        nextX = painter.paint(token, g, nextX, y, host, this, clipStart);
      }

      // This token is entirely selected
      else if (token.getOffset() >= selStart && token.getEndOffset() <= selEnd) {
        nextX = painter.paintSelected(token, g, nextX, y, host, this, clipStart, useSTC);
      }

      // This token is entirely unselected
      else {
        nextX = painter.paint(token, g, nextX, y, host, this, clipStart);
      }

      token = token.getNextToken();
    }

    // NOTE: We should re-use code from Token (paintBackground()) here,
    // but don't because I'm just too lazy.
    if (host.getEOLMarkersVisible()) {
      g.setColor(host.getForegroundForTokenType(Token.WHITESPACE));
      g.setFont(host.getFontForTokenType(Token.WHITESPACE));
      g.drawString("\u00B6", nextX, y);
    }

    // Return the x-coordinate at the end of the painted text.
    return nextX;
  }
Exemplo n.º 20
0
 /**
  * Use this when the user has a code from the browser. This browser-code can be exchanged for a
  * accessToken. The accessToken is used to use further API-calls.
  *
  * @param code the code from the browser
  * @return accessToken
  */
 public String loginWithBrowserCode(String code) {
   Verifier v = new Verifier(code);
   Token accessToken = service.getAccessToken(null, v);
   accessTokenString = accessToken.getToken();
   return accessTokenString;
 }
Exemplo n.º 21
0
Arquivo: AI.java Projeto: giurra8/Hex
  public CellToken doAction() {
    Cell randomCell = null;
    Token randomToken = null;
    CellToken randomCellToken = null;
    ArrayList<Cell> cells = Frame.getInstance().getBoard().getCells();
    Random r = new Random();
    switch (dif) {
      case EASY:
        {
          int index = r.nextInt(cells.size());
          while (cells.get(index).hasToken()) index = r.nextInt(cells.size());
          randomCell = cells.get(index);
          index = r.nextInt(tokens.size());
          randomToken = tokens.get(index);
          break;
        }

      case HARD:
        {
          int max = 0;
          ArrayList<CellToken> cellToken = new ArrayList<>();
          ArrayList<Cell> enemyCells = enemyCells();
          Map<Cell, ArrayList<Cell>> neighborCells = Frame.getInstance().getNeighborCells();
          for (Cell c : enemyCells) {
            for (Cell nc : neighborCells.get(c)) {
              if (nc != null)
                if (!nc.hasToken()) {
                  for (Token t : tokens) {
                    proveraZaToken(nc, t);
                    for (Integer i : t.getBrojpromena()) {
                      if (max <= i) {
                        max = i;
                        cellToken.add(new CellToken(nc, t, max));
                      }
                    }
                    t.setBrojpromena(new ArrayList<Integer>());
                    Map<Integer, Cell> map = new HashMap<>();
                    t.setNumberToOwn(map);
                  }
                }
            }
            max = 0;
          }

          for (CellToken ct : cellToken) {
            if (max <= ct.brojOkrenutih) max = ct.brojOkrenutih;
          }
          int zbir = 29;
          for (CellToken ct : cellToken) {
            if (ct.brojOkrenutih == max)
              if (ct.token.getZbir() < zbir) {
                zbir = ct.token.getZbir();
                randomCellToken = ct;
              }
          }

          randomCell = randomCellToken.cell;
          randomToken = randomCellToken.token;

          break;
        }
      case MEDIUM:
        {
          int max = 0;

          ArrayList<Cell> enemyCells = enemyCells();
          Map<Cell, ArrayList<Cell>> neighborCells = Frame.getInstance().getNeighborCells();
          for (Cell c : enemyCells) {
            for (Cell nc : neighborCells.get(c)) {
              if (nc != null)
                if (!nc.hasToken()) {
                  for (Token t : tokens) {
                    Map numberToOwn = t.getNumberToOwn();
                    proveraZaToken(nc, t);
                    for (Integer i : t.getBrojpromena()) {
                      if (max <= i) {
                        max = i;
                        randomToken = t;
                        randomCell = (Cell) numberToOwn.get(max);
                      }
                    }
                    t.setBrojpromena(new ArrayList<>());
                    Map<Integer, Cell> map = new HashMap<>();
                    t.setNumberToOwn(map);
                  }
                  // System.out.println(""+max);
                }
            }
            max = 0;
          }

          break;
        }
    }
    return new CellToken(randomCell, randomToken);
  }