Beispiel #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();
  }
Beispiel #2
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);
  }
Beispiel #3
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();
    }
  }
Beispiel #4
0
  @Override
  public void keyTyped(final KeyEvent e) {
    if (!hist.active()
        || control(e)
        || DELNEXT.is(e)
        || DELPREV.is(e)
        || ESCAPE.is(e)
        || CUT2.is(e)) return;

    final int caret = editor.pos();

    // remember if marked text is to be deleted
    final StringBuilder sb = new StringBuilder(1).append(e.getKeyChar());
    final boolean indent = TAB.is(e) && editor.indent(sb, e.isShiftDown());

    // delete marked text
    final boolean selected = editor.selected() && !indent;
    if (selected) editor.delete();

    final int move = ENTER.is(e) ? editor.enter(sb) : editor.add(sb, selected);

    // refresh history and adjust cursor position
    hist.store(editor.text(), caret, editor.pos());
    if (move != 0) editor.pos(Math.min(editor.size(), caret + move));

    // adjust text height
    scrollCode.invokeLater(true);
    e.consume();
  }
Beispiel #5
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);
  }
Beispiel #6
0
 /**
  * Draws a visualization tooltip.
  *
  * @param g graphics reference
  * @param tt tooltip label
  * @param x horizontal position
  * @param y vertical position
  * @param w width
  * @param c color color depth
  */
 public static void drawTooltip(
     final Graphics g, final String tt, final int x, final int y, final int w, final int c) {
   final int tw = width(g, tt);
   final int th = g.getFontMetrics().getHeight();
   final int xx = Math.min(w - tw - 8, x);
   g.setColor(color(c));
   g.fillRect(xx - 1, y - th, tw + 4, th);
   g.setColor(BACK);
   g.drawString(tt, xx, y - 4);
 }
Beispiel #7
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);
      }
    }
  }
  /**
   * Creates and shows the combo box.
   *
   * @param sl strings to be added
   */
  private void createCombo(final StringList sl) {
    if (sl == null || sl.size() == 0) {
      pop.setVisible(false);
      return;
    }
    if (comboChanged(sl)) {
      box.setModel(new DefaultComboBoxModel(sl.toArray()));
      box.setSelectedIndex(-1);
      pop = new ComboPopup(box);
    }

    final int w = getFontMetrics(getFont()).stringWidth(pre);
    pop.show(this, Math.min(getWidth(), w), getHeight());
  }
Beispiel #9
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();
   }
 }