/** Select or grow image when clicked. */
 public void mousePressed(MouseEvent e) {
   Dimension size = fComponent.getSize();
   if (e.getX() >= size.width - 7 && e.getY() >= size.height - 7 && getSelectionState() == 2) {
     // Click in selected grow-box:
     if (DEBUG) System.out.println("ImageView: grow!!! Size=" + fWidth + "x" + fHeight);
     Point loc = fComponent.getLocationOnScreen();
     fGrowBase = new Point(loc.x + e.getX() - fWidth, loc.y + e.getY() - fHeight);
     fGrowProportionally = e.isShiftDown();
   } else {
     // Else select image:
     fGrowBase = null;
     JTextComponent comp = (JTextComponent) fContainer;
     int start = fElement.getStartOffset();
     int end = fElement.getEndOffset();
     int mark = comp.getCaret().getMark();
     int dot = comp.getCaret().getDot();
     if (e.isShiftDown()) {
       // extend selection if shift key down:
       if (mark <= start) comp.moveCaretPosition(end);
       else comp.moveCaretPosition(start);
     } else {
       // just select image, without shift:
       if (mark != start) comp.setCaretPosition(start);
       if (dot != end) comp.moveCaretPosition(end);
     }
   }
 }
  static void updateStyle(JTextComponent comp, SynthContext context, String prefix) {
    SynthStyle style = context.getStyle();

    Color color = comp.getCaretColor();
    if (color == null || color instanceof UIResource) {
      comp.setCaretColor((Color) style.get(context, prefix + ".caretForeground"));
    }

    Color fg = comp.getForeground();
    if (fg == null || fg instanceof UIResource) {
      fg = style.getColorForState(context, ColorType.TEXT_FOREGROUND);
      if (fg != null) {
        comp.setForeground(fg);
      }
    }

    Object ar = style.get(context, prefix + ".caretAspectRatio");
    if (ar instanceof Number) {
      comp.putClientProperty("caretAspectRatio", ar);
    }

    context.setComponentState(SELECTED | FOCUSED);

    Color s = comp.getSelectionColor();
    if (s == null || s instanceof UIResource) {
      comp.setSelectionColor(style.getColor(context, ColorType.TEXT_BACKGROUND));
    }

    Color sfg = comp.getSelectedTextColor();
    if (sfg == null || sfg instanceof UIResource) {
      comp.setSelectedTextColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
    }

    context.setComponentState(DISABLED);

    Color dfg = comp.getDisabledTextColor();
    if (dfg == null || dfg instanceof UIResource) {
      comp.setDisabledTextColor(style.getColor(context, ColorType.TEXT_FOREGROUND));
    }

    Insets margin = comp.getMargin();
    if (margin == null || margin instanceof UIResource) {
      margin = (Insets) style.get(context, prefix + ".margin");

      if (margin == null) {
        // Some places assume margins are non-null.
        margin = SynthLookAndFeel.EMPTY_UIRESOURCE_INSETS;
      }
      comp.setMargin(margin);
    }

    Caret caret = comp.getCaret();
    if (caret instanceof UIResource) {
      Object o = style.get(context, prefix + ".caretBlinkRate");
      if (o != null && o instanceof Integer) {
        Integer rate = (Integer) o;
        caret.setBlinkRate(rate.intValue());
      }
    }
  }
Exemple #3
0
  /**
   * Provides a way to determine the next visually represented model location at which one might
   * place a caret. Some views may not be visible, they might not be in the same order found in the
   * model, or they just might not allow access to some of the locations in the model.
   *
   * @param pos the position to convert >= 0
   * @param a the allocated region in which to render
   * @param direction the direction from the current position that can be thought of as the arrow
   *     keys typically found on a keyboard. This will be one of the following values:
   *     <ul>
   *       <li>SwingConstants.WEST
   *       <li>SwingConstants.EAST
   *       <li>SwingConstants.NORTH
   *       <li>SwingConstants.SOUTH
   *     </ul>
   *
   * @return the location within the model that best represents the next location visual position
   * @exception BadLocationException
   * @exception IllegalArgumentException if <code>direction</code> doesn't have one of the legal
   *     values above
   */
  public int getNextVisualPositionFrom(
      int pos, Position.Bias b, Shape a, int direction, Position.Bias[] biasRet)
      throws BadLocationException {

    biasRet[0] = Position.Bias.Forward;
    switch (direction) {
      case NORTH:
      case SOUTH:
        {
          if (pos == -1) {
            pos = (direction == NORTH) ? Math.max(0, getEndOffset() - 1) : getStartOffset();
            break;
          }
          JTextComponent target = (JTextComponent) getContainer();
          Caret c = (target != null) ? target.getCaret() : null;
          // YECK! Ideally, the x location from the magic caret position
          // would be passed in.
          Point mcp;
          if (c != null) {
            mcp = c.getMagicCaretPosition();
          } else {
            mcp = null;
          }
          int x;
          if (mcp == null) {
            Rectangle loc = target.modelToView(pos);
            x = (loc == null) ? 0 : loc.x;
          } else {
            x = mcp.x;
          }
          if (direction == NORTH) {
            pos = Utilities.getPositionAbove(target, pos, x);
          } else {
            pos = Utilities.getPositionBelow(target, pos, x);
          }
        }
        break;
      case WEST:
        if (pos == -1) {
          pos = Math.max(0, getEndOffset() - 1);
        } else {
          pos = Math.max(0, pos - 1);
        }
        break;
      case EAST:
        if (pos == -1) {
          pos = getStartOffset();
        } else {
          pos = Math.min(pos + 1, getDocument().getLength());
        }
        break;
      default:
        throw new IllegalArgumentException("Bad direction: " + direction);
    }
    return pos;
  }
 /*     */ public void paint(Graphics paramGraphics, Shape paramShape) /*     */ {
   /* 356 */ Rectangle localRectangle = (Rectangle) paramShape;
   /* 357 */ this.tabBase = localRectangle.x;
   /* 358 */ JTextComponent localJTextComponent = (JTextComponent) getContainer();
   /* 359 */ this.sel0 = localJTextComponent.getSelectionStart();
   /* 360 */ this.sel1 = localJTextComponent.getSelectionEnd();
   /* 361 */ this.unselected =
       (localJTextComponent.isEnabled()
           ? localJTextComponent.getForeground()
           : localJTextComponent.getDisabledTextColor());
   /*     */
   /* 363 */ Caret localCaret = localJTextComponent.getCaret();
   /* 364 */ this.selected =
       ((localCaret.isSelectionVisible()) && (localJTextComponent.getHighlighter() != null)
           ? localJTextComponent.getSelectedTextColor()
           : this.unselected);
   /*     */
   /* 366 */ paramGraphics.setFont(localJTextComponent.getFont());
   /*     */
   /* 369 */ super.paint(paramGraphics, paramShape);
   /*     */ }
Exemple #5
0
    public void actionPerformed(JTextComponent text) {
      indentationLogic = ((EditorPane) text).getIndentationLogic();
      StyledDocument doc = (StyledDocument) text.getDocument();
      Element map = doc.getDefaultRootElement();
      Caret c = text.getCaret();
      int dot = c.getDot();
      int mark = c.getMark();
      int line1 = map.getElementIndex(dot);

      if (dot != mark) {
        int line2 = map.getElementIndex(mark);
        int begin = Math.min(line1, line2);
        int end = Math.max(line1, line2);
        Element elem;
        try {
          for (line1 = begin; line1 < end; line1++) {
            elem = map.getElement(line1);
            handleDecreaseIndent(line1, elem, doc);
          }
          elem = map.getElement(end);
          int start = elem.getStartOffset();
          if (Math.max(c.getDot(), c.getMark()) != start) {
            handleDecreaseIndent(end, elem, doc);
          }
        } catch (BadLocationException ble) {
          Debug.error(me + "Problem while de-indenting line\n%s", ble.getMessage());
          UIManager.getLookAndFeel().provideErrorFeedback(text);
        }
      } else {
        Element elem = map.getElement(line1);
        try {
          handleDecreaseIndent(line1, elem, doc);
        } catch (BadLocationException ble) {
          Debug.error(me + "Problem while de-indenting line\n%s", ble.getMessage());
          UIManager.getLookAndFeel().provideErrorFeedback(text);
        }
      }
    }
Exemple #6
0
    public void actionPerformed(JTextComponent text) {
      indentationLogic = ((EditorPane) text).getIndentationLogic();
      boolean indentError = false;
      Document doc = text.getDocument();
      Element map = doc.getDefaultRootElement();
      String tabWhitespace = PreferencesUser.getInstance().getTabWhitespace();
      Caret c = text.getCaret();
      int dot = c.getDot();
      int mark = c.getMark();
      int dotLine = map.getElementIndex(dot);
      int markLine = map.getElementIndex(mark);

      if (dotLine != markLine) {
        int first = Math.min(dotLine, markLine);
        int last = Math.max(dotLine, markLine);
        Element elem;
        int start;
        try {
          for (int i = first; i < last; i++) {
            elem = map.getElement(i);
            start = elem.getStartOffset();
            doc.insertString(start, tabWhitespace, null);
          }
          elem = map.getElement(last);
          start = elem.getStartOffset();
          if (Math.max(c.getDot(), c.getMark()) != start) {
            doc.insertString(start, tabWhitespace, null);
          }
        } catch (BadLocationException ble) {
          Debug.error(me + "Problem while indenting line\n%s", ble.getMessage());
          UIManager.getLookAndFeel().provideErrorFeedback(text);
        }
      } else {
        text.replaceSelection(tabWhitespace);
      }
    }
Exemple #7
0
    @Override
    public void actionPerformed(ActionEvent e) {
      JTextComponent textArea = (JTextComponent) e.getSource();

      Caret caret = textArea.getCaret();
      int dot = caret.getDot();

      /*
       * Move to the beginning/end of selection on a "non-shifted"
       * left- or right-keypress.  We shouldn't have to worry about
       * navigation filters as, if one is being used, it let us get
       * to that position before.
       */
      if (!select) {
        switch (direction) {
          case SwingConstants.EAST:
            int mark = caret.getMark();
            if (dot != mark) {
              caret.setDot(Math.max(dot, mark));
              return;
            }
            break;
          case SwingConstants.WEST:
            mark = caret.getMark();
            if (dot != mark) {
              caret.setDot(Math.min(dot, mark));
              return;
            }
            break;
          default:
        }
      }

      Position.Bias[] bias = new Position.Bias[1];
      Point magicPosition = caret.getMagicCaretPosition();

      try {

        if (magicPosition == null
            && (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH)) {
          Rectangle r = textArea.modelToView(dot);
          magicPosition = new Point(r.x, r.y);
        }

        NavigationFilter filter = textArea.getNavigationFilter();

        if (filter != null) {
          dot =
              filter.getNextVisualPositionFrom(
                  textArea, dot, Position.Bias.Forward, direction, bias);
        } else {
          if (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH) {
            dot = getNSVisualPosition((EditorPane) textArea, dot, direction);
          } else {
            dot =
                textArea
                    .getUI()
                    .getNextVisualPositionFrom(
                        textArea, dot, Position.Bias.Forward, direction, bias);
          }
        }
        if (select) {
          caret.moveDot(dot);
        } else {
          caret.setDot(dot);
        }

        if (magicPosition != null
            && (direction == SwingConstants.NORTH || direction == SwingConstants.SOUTH)) {
          caret.setMagicCaretPosition(magicPosition);
        }

      } catch (BadLocationException ble) {
        Debug.error(me + "Problem while trying to move caret\n%s", ble.getMessage());
      }
    }