Example #1
0
  @Override
  public void keyTyped(final KeyEvent e) {
    if (undo == null || control(e) || DELNEXT.is(e) || DELPREV.is(e) || ESCAPE.is(e)) return;

    text.pos(text.cursor());
    // string to be added
    String ch = String.valueOf(e.getKeyChar());

    // remember if marked text is to be deleted
    boolean del = true;
    final byte[] txt = text.text();
    if (TAB.is(e)) {
      if (text.marked()) {
        // check if lines are to be indented
        final int s = Math.min(text.pos(), text.start());
        final int l = Math.max(text.pos(), text.start()) - 1;
        for (int p = s; p <= l && p < txt.length; p++) del &= txt[p] != '\n';
        if (!del) {
          text.indent(s, l, e.isShiftDown());
          ch = null;
        }
      } else {
        boolean c = true;
        for (int p = text.pos() - 1; p >= 0 && c; p--) {
          final byte b = txt[p];
          c = ws(b);
          if (b == '\n') break;
        }
        if (c) ch = "  ";
      }
    }

    // delete marked text
    if (text.marked() && del) text.delete();

    if (ENTER.is(e)) {
      // adopt indentation from previous line
      final StringBuilder sb = new StringBuilder(1).append(e.getKeyChar());
      int s = 0;
      for (int p = text.pos() - 1; p >= 0; p--) {
        final byte b = txt[p];
        if (b == '\n') break;
        if (b == '\t') {
          s += 2;
        } else if (b == ' ') {
          s++;
        } else {
          s = 0;
        }
      }
      for (int p = 0; p < s; p++) sb.append(' ');
      ch = sb.toString();
    }

    if (ch != null) text.add(ch);
    text.setCaret();
    rend.calc();
    showCursor(2);
    e.consume();
  }
Example #2
0
  /**
   * Draws the specified string.
   *
   * @param g graphics reference
   * @param s text
   * @param x x coordinate
   * @param y y coordinate
   * @param w width
   * @param fs font size
   */
  public static void chopString(
      final Graphics g, final byte[] s, final int x, final int y, final int w, final int fs) {

    if (w < 12) return;
    final int[] cw = fontWidths(g.getFont());

    int j = s.length;
    try {
      int l = 0;
      int fw = 0;
      for (int k = 0; k < j; k += l) {
        final int ww = width(g, cw, cp(s, k));
        if (fw + ww >= w - 4) {
          j = Math.max(1, k - l);
          if (k > 1) fw -= width(g, cw, cp(s, k - 1));
          g.drawString("..", x + fw, y + fs);
          break;
        }
        fw += ww;
        l = cl(s, k);
      }
    } catch (final Exception ex) {
      Util.debug(ex);
    }
    g.drawString(string(s, 0, j), x, y + fs);
  }
Example #3
0
  @Override
  public final void mouseDragged(final MouseEvent e) {
    if (!SwingUtilities.isLeftMouseButton(e)) return;

    // selection mode
    select(e.getPoint(), false);
    final int y = Math.max(20, Math.min(e.getY(), getHeight() - 20));
    if (y != e.getY()) scroll.pos(scroll.pos() + e.getY() - y);
  }
Example #4
0
 @Override
 public void keyPressed(final KeyEvent e) {
   final int old = value;
   if (PREVCHAR.is(e) || PREVLINE.is(e)) {
     value = Math.max(min, value - 1);
   } else if (NEXTCHAR.is(e) || NEXTLINE.is(e)) {
     value = Math.min(max, value + 1);
   } else if (NEXTPAGE.is(e)) {
     value = Math.max(min, value + 10);
   } else if (PREVPAGE.is(e)) {
     value = Math.min(max, value - 10);
   } else if (LINESTART.is(e)) {
     value = min;
   } else if (LINEEND.is(e)) {
     value = max;
   }
   if (value != old) {
     if (dialog != null) dialog.action(null);
     for (final ActionListener al : listenerList.getListeners(ActionListener.class)) {
       al.actionPerformed(null);
     }
     repaint();
   }
 }
Example #5
0
  @Override
  public void mouseDragged(final MouseEvent e) {
    final double prop = (max - min) * (mouseX - e.getX()) / (getWidth() - SLIDERW);

    final int old = value;
    value = Math.max(min, Math.min(max, (int) (oldValue - prop)));

    if (value != old) {
      if (dialog != null) dialog.action(null);
      for (final ActionListener al : listenerList.getListeners(ActionListener.class)) {
        al.actionPerformed(null);
      }
      repaint();
    }
  }
Example #6
0
  /**
   * Reacts on key codes.
   *
   * @param e key event
   */
  void code(final KeyEvent e) {
    if (ENTER.is(e) || text == null) stop();

    flashing = true;
    if (LINESTART.is(e)) {
      pos = 0;
    } else if (LINEEND.is(e)) {
      pos = text.length();
    } else if (PREV.is(e)) {
      pos = Math.max(0, pos - 1);
    } else if (NEXT.is(e)) {
      pos = Math.min(text.length(), pos + 1);
    } else if (DELPREV.is(e)) {
      if (pos > 0) text = text.substring(0, pos - 1) + text.substring(pos--);
    } else if (DELNEXT.is(e)) {
      if (pos < text.length()) {
        text = text.substring(0, pos) + text.substring(pos + 1);
      }
    }
  }