/**
   * Returns the bounding box (in the current view) of a specified position in the model. This
   * method is designed for line-wrapped views to use, as it allows you to specify a "starting
   * position" in the line, from which the x-value is assumed to be zero. The idea is that you
   * specify the first character in a physical line as <code>p0</code>, as this is the character
   * where the x-pixel value is 0.
   *
   * @param textArea The text area containing the text.
   * @param s A segment in which to load the line. This is passed in so we don't have to reallocate
   *     a new <code>Segment</code> for each call.
   * @param p0 The starting position in the physical line in the document.
   * @param p1 The position for which to get the bounding box in the view.
   * @param e How to expand tabs.
   * @param rect The rectangle whose x- and width-values are changed to represent the bounding box
   *     of <code>p1</code>. This is reused to keep from needlessly reallocating Rectangles.
   * @param x0 The x-coordinate (pixel) marking the left-hand border of the text. This is useful if
   *     the text area has a border, for example.
   * @return The bounding box in the view of the character <code>p1</code>.
   * @throws BadLocationException If <code>p0</code> or <code>p1</code> is not a valid location in
   *     the specified text area's document.
   * @throws IllegalArgumentException If <code>p0</code> and <code>p1</code> are not on the same
   *     line.
   */
  public static Rectangle getLineWidthUpTo(
      RSyntaxTextArea textArea, Segment s, int p0, int p1, TabExpander e, Rectangle rect, int x0)
      throws BadLocationException {

    RSyntaxDocument doc = (RSyntaxDocument) textArea.getDocument();

    // Ensure p0 and p1 are valid document positions.
    if (p0 < 0) throw new BadLocationException("Invalid document position", p0);
    else if (p1 > doc.getLength()) throw new BadLocationException("Invalid document position", p1);

    // Ensure p0 and p1 are in the same line, and get the start/end
    // offsets for that line.
    Element map = doc.getDefaultRootElement();
    int lineNum = map.getElementIndex(p0);
    // We do ">1" because p1 might be the first position on the next line
    // or the last position on the previous one.
    // if (lineNum!=map.getElementIndex(p1))
    if (Math.abs(lineNum - map.getElementIndex(p1)) > 1)
      throw new IllegalArgumentException(
          "p0 and p1 are not on the " + "same line (" + p0 + ", " + p1 + ").");

    // Get the token list.
    Token t = doc.getTokenListForLine(lineNum);

    // Modify the token list 't' to begin at p0 (but still have correct
    // token types, etc.), and get the x-location (in pixels) of the
    // beginning of this new token list.
    makeTokenListStartAt(t, p0, e, textArea, 0);

    rect = t.listOffsetToView(textArea, e, p1, x0, rect);
    return rect;
  }
  /**
   * Modifies the passed-in token list to start at the specified offset. For example, if the token
   * list covered positions 20-60 in the document (inclusive) like so:
   *
   * <pre>
   *   [token1] -> [token2] -> [token3] -> [token4]
   *   20     30   31     40   41     50   51     60
   * </pre>
   *
   * and you used this method to make the token list start at position 44, then the token list would
   * be modified to be the following:
   *
   * <pre>
   *   [part-of-old-token3] -> [token4]
   *   44                 50   51     60
   * </pre>
   *
   * Tokens that come before the specified position are forever lost, and the token containing that
   * position is made to begin at that position if necessary. All token types remain the same as
   * they were originally.
   *
   * <p>This method can be useful if you are only interested in part of a token list (i.e., the line
   * it represents), but you don't want to modify the token list yourself.
   *
   * @param tokenList The list to make start at the specified position. This parameter is modified.
   * @param pos The position at which the new token list is to start. If this position is not in the
   *     passed-in token list, returned token list will either be <code>null</code> or the
   *     unpaintable token(s) at the end of the passed-in token list.
   * @param e How to expand tabs.
   * @param textArea The text area from which the token list came.
   * @param x0 The initial x-pixel position of the old token list.
   * @return The width, in pixels, of the part of the token list "removed from the front." This way,
   *     you know the x-offset of the "new" token list.
   */
  public static float makeTokenListStartAt(
      Token tokenList, int pos, TabExpander e, final RSyntaxTextArea textArea, float x0) {

    Token t = tokenList;

    // Loop through the token list until you find the one that contains
    // pos.  Remember the cumulative width of all of these tokens.
    while (t != null && t.isPaintable() && !t.containsPosition(pos)) {
      x0 += t.getWidth(textArea, e, x0);
      t = t.getNextToken();
    }

    // Make the token that contains pos start at pos.
    if (t != null && t.isPaintable() && t.offset != pos) {
      // Number of chars between p0 and token start.
      int difference = pos - t.offset;
      x0 += t.getWidthUpTo(t.textCount - difference + 1, textArea, e, x0);
      t.makeStartAt(pos);
    }

    // Make the passed-in token list point to the proper place.
    // t can be null, for example, if line ends with unended MLC.
    if (t != null && t.isPaintable()) tokenList.copyFrom(t);
    else tokenList = null;
    t = null;

    // Return the x-offset (in pixels) of the newly-modified t.
    return x0;
  }
 /**
  * Determines the width of the given token list taking tabs into consideration. This is
  * implemented in a 1.1 style coordinate system where ints are used and 72dpi is assumed.
  *
  * <p>
  *
  * @param tokenList The token list list representing the text.
  * @param textArea The text area in which this token list resides.
  * @param e The tab expander. This value cannot be <code>null</code>.
  * @param x0 The x-pixel coordinate of the start of the token list.
  * @return The width of the token list, in pixels.
  * @see #getTokenListWidthUpTo
  */
 public static final float getTokenListWidth(
     final Token tokenList, RSyntaxTextArea textArea, TabExpander e, float x0) {
   float width = x0;
   for (Token t = tokenList; t != null && t.isPaintable(); t = t.getNextToken()) {
     width += t.getWidth(textArea, e, width);
   }
   return width - x0;
 }
  /**
   * Determines the position in the model that is closest to the given view location in the row
   * below. The component given must have a size to compute the result. If the component doesn't
   * have a size a value of -1 will be returned.
   *
   * @param c the editor
   * @param offs the offset in the document >= 0
   * @param x the X coordinate >= 0
   * @return the position >= 0 if the request can be computed, otherwise a value of -1 will be
   *     returned.
   * @exception BadLocationException if the offset is out of range
   */
  public static final int getPositionBelow(RSyntaxTextArea c, int offs, float x, TabExpander e)
      throws BadLocationException {

    TokenOrientedView tov = (TokenOrientedView) e;
    Token token = tov.getTokenListForPhysicalLineBelow(offs);
    if (token == null) return -1;

    // A line containing only Token.NULL is an empty line.
    else if (token.type == Token.NULL) {
      int line = c.getLineOfOffset(offs); // Sure to be > c.getLineCount()-1 ??
      return c.getLineStartOffset(line + 1);
    } else {
      return token.getListOffset(c, e, 0, x);
    }
  }
Example #5
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()));
    }
Example #6
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);
 }
 /**
  * Determines the width of the given token list taking tabs into consideration and only up to the
  * given index in the document (exclusive).
  *
  * @param tokenList The token list representing the text.
  * @param textArea The text area in which this token list resides.
  * @param e The tab expander. This value cannot be <code>null</code>.
  * @param x0 The x-pixel coordinate of the start of the token list.
  * @param upTo The document position at which you want to stop, exclusive. If this position is
  *     before the starting position of the token list, a width of <code>0</code> will be returned;
  *     similarly, if this position comes after the entire token list, the width of the entire
  *     token list is returned.
  * @return The width of the token list, in pixels, up to, but not including, the character at
  *     position <code>upTo</code>.
  * @see #getTokenListWidth
  */
 public static final float getTokenListWidthUpTo(
     final Token tokenList, RSyntaxTextArea textArea, TabExpander e, float x0, int upTo) {
   float width = 0;
   for (Token t = tokenList; t != null && t.isPaintable(); t = t.getNextToken()) {
     if (t.containsPosition(upTo)) {
       return width + t.getWidthUpTo(upTo - t.offset, textArea, e, x0 + width);
     }
     width += t.getWidth(textArea, e, x0 + width);
   }
   return width;
 }
Example #8
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();
 }
Example #9
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)));
    }
Example #10
0
File: AI.java Project: 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);
  }
Example #11
0
File: AI.java Project: 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);
  }
 /**
  * Returns the token at the specified index, or <code>null</code> if the given offset isn't in
  * this token list's range.<br>
  * Note that this method does NOT check to see if <code>tokenList</code> is null; callers should
  * check for themselves.
  *
  * @param tokenList The list of tokens in which to search.
  * @param offset The offset at which to get the token.
  * @return The token at <code>offset</code>, or <code>null</code> if none of the tokens are at
  *     that offset.
  */
 public static final Token getTokenAtOffset(Token tokenList, int offset) {
   for (Token t = tokenList; t != null; t = t.getNextToken()) {
     if (t.containsPosition(offset)) return t;
   }
   return null;
 }