コード例 #1
0
 void pairDraw(GC gc, StyledRegion sr, int start, int end) {
   if (start > text.getCharCount() || end > text.getCharCount()) return;
   if (gc != null) {
     Point left = text.getLocationAtOffset(start);
     Point right = text.getLocationAtOffset(end);
     if (sr != null) {
       if (highlightStyle == HLS_XOR) {
         int resultColor = sr.fore ^ cm.getColor(text.getBackground());
         if (text.getLineAtOffset(text.getCaretOffset()) == text.getLineAtOffset(start)
             && horzCross
             && horzCrossColor != null
             && ((StyledRegion) horzCrossColor).bback)
           resultColor = sr.fore ^ ((StyledRegion) horzCrossColor).back;
         Color color = cm.getColor(sr.bfore, resultColor);
         gc.setBackground(color);
         gc.setXORMode(true);
         gc.fillRectangle(left.x, left.y, right.x - left.x, gc.getFontMetrics().getHeight());
       } else if (highlightStyle == HLS_OUTLINE) {
         Color color = cm.getColor(sr.bfore, sr.fore);
         gc.setForeground(color);
         gc.drawRectangle(
             left.x, left.y, right.x - left.x - 1, gc.getFontMetrics().getHeight() - 1);
       } else if (highlightStyle == HLS_OUTLINE2) {
         Color color = cm.getColor(sr.bfore, sr.fore);
         gc.setForeground(color);
         gc.setLineWidth(2);
         gc.drawRectangle(
             left.x + 1, left.y + 1, right.x - left.x - 2, gc.getFontMetrics().getHeight() - 2);
       }
     }
   } else {
     text.redrawRange(start, end - start, true);
   }
 }
コード例 #2
0
 @Override
 public void resetContents(String newContents) {
   commandLineText.replaceTextRange(
       contentsOffset, commandLineText.getCharCount() - contentsOffset, newContents);
   // Clear selection, set caret position to end of widget.
   commandLineText.setCaret(endCharCaret);
   commandLineText.setSelection(commandLineText.getCharCount());
 }
コード例 #3
0
ファイル: TerminalPart.java プロジェクト: glumanda/grblrunner
  private void appendText(
      String line, final Color foreground, final Color background, final int textStyle) {

    int start = terminalText.getCharCount();
    terminalText.append(line);
    int len = terminalText.getCharCount() - start;
    terminalText.setStyleRange(new StyleRange(start, len, foreground, background, textStyle));

    scrollToEnd();
  }
コード例 #4
0
 public void setPosition(int offset) {
   if (offset < 0) {
     offset = contentsOffset;
   } else if (offset + contentsOffset > commandLineText.getCharCount()) {
     offset = commandLineText.getCharCount();
   } else {
     offset += contentsOffset;
   }
   commandLineText.setCaretOffset(offset);
   // If the caret didn't move, no CaretEvent gets sent. Update manually.
   updateCaret();
 }
コード例 #5
0
 /*
  * @see org.eclipse.jface.text.IPainter#paint(int)
  */
 public void paint(int reason) {
   IDocument document = fTextViewer.getDocument();
   if (document == null) {
     deactivate(false);
     return;
   }
   if (!fIsActive) {
     fIsActive = true;
     fTextWidget.addPaintListener(this);
     redrawAll();
   } else if (reason == CONFIGURATION || reason == INTERNAL) {
     redrawAll();
   } else if (reason == TEXT_CHANGE) {
     // redraw current line only
     try {
       IRegion lineRegion =
           document.getLineInformationOfOffset(getDocumentOffset(fTextWidget.getCaretOffset()));
       int widgetOffset = getWidgetOffset(lineRegion.getOffset());
       int charCount = fTextWidget.getCharCount();
       int redrawLength = Math.min(lineRegion.getLength(), charCount - widgetOffset);
       if (widgetOffset >= 0 && redrawLength > 0) {
         fTextWidget.redrawRange(widgetOffset, redrawLength, true);
       }
     } catch (BadLocationException e) {
       // ignore
     }
   }
 }
コード例 #6
0
 protected void updateCaret() {
   if (commandLineText.getCaretOffset() == commandLineText.getCharCount()) {
     commandLineText.setCaret(endCharCaret);
   } else {
     commandLineText.setCaret(defaultCaret);
   }
 }
コード例 #7
0
 /**
  * Draw the given line range.
  *
  * @param gc
  * @param startLine first line number
  * @param endLine last line number (inclusive)
  * @param x the X-coordinate of the drawing range
  * @param w the width of the drawing range
  */
 private void drawLineRange(GC gc, int startLine, int endLine, int x, int w) {
   final int viewPortWidth = fTextWidget.getClientArea().width;
   for (int line = startLine; line <= endLine; line++) {
     int lineOffset = fTextWidget.getOffsetAtLine(line);
     // line end offset including line delimiter
     int lineEndOffset;
     if (line < fTextWidget.getLineCount() - 1) {
       lineEndOffset = fTextWidget.getOffsetAtLine(line + 1);
     } else {
       lineEndOffset = fTextWidget.getCharCount();
     }
     // line length excluding line delimiter
     int lineLength = lineEndOffset - lineOffset;
     while (lineLength > 0) {
       char c = fTextWidget.getTextRange(lineOffset + lineLength - 1, 1).charAt(0);
       if (c != '\r' && c != '\n') {
         break;
       }
       --lineLength;
     }
     // compute coordinates of last character on line
     Point endOfLine = fTextWidget.getLocationAtOffset(lineOffset + lineLength);
     if (x - endOfLine.x > viewPortWidth) {
       // line is not visible
       continue;
     }
     // Y-coordinate of line
     int y = fTextWidget.getLinePixel(line);
     // compute first visible char offset
     int startOffset;
     try {
       startOffset = fTextWidget.getOffsetAtLocation(new Point(x, y)) - 1;
       if (startOffset - 2 <= lineOffset) {
         startOffset = lineOffset;
       }
     } catch (IllegalArgumentException iae) {
       startOffset = lineOffset;
     }
     // compute last visible char offset
     int endOffset;
     if (x + w >= endOfLine.x) {
       // line end is visible
       endOffset = lineEndOffset;
     } else {
       try {
         endOffset = fTextWidget.getOffsetAtLocation(new Point(x + w - 1, y)) + 1;
         if (endOffset + 2 >= lineEndOffset) {
           endOffset = lineEndOffset;
         }
       } catch (IllegalArgumentException iae) {
         endOffset = lineEndOffset;
       }
     }
     // draw character range
     if (endOffset > startOffset) {
       drawCharRange(gc, startOffset, endOffset);
     }
   }
 }
コード例 #8
0
 boolean getCharCountChangedAndStoreNew() {
   int charCount = styledText.getCharCount();
   if (currCharCount != charCount) {
     currCharCount = charCount;
     return true;
   }
   return false;
 }
コード例 #9
0
 @Override
 public void setPrompt(String newPrompt) {
   prompt = newPrompt;
   commandLineText.replaceTextRange(0, contentsOffset, newPrompt);
   contentsOffset = newPrompt.length();
   commandLineText.setCaretOffset(commandLineText.getCharCount());
   updateUISize();
 }
コード例 #10
0
 @Override
 public String getContents() {
   int charCount = commandLineText.getCharCount();
   if (charCount == contentsOffset) {
     // SWT doesn't like text ranges where start == end, handle empty contents explicitly
     return "";
   } else {
     return commandLineText.getText(contentsOffset, charCount - 1);
   }
 }
コード例 #11
0
 void drawLine(int lno) {
   if (lno < 0 || lno >= text.getLineCount()) return;
   int y = text.getLocationAtOffset(text.getOffsetAtLine(lno)).y;
   int height = 0;
   if (text.getLineCount() > lno + 1)
     height = text.getLocationAtOffset(text.getOffsetAtLine(lno + 1)).y - y;
   else height = text.getLocationAtOffset(text.getCharCount()).y + text.getLineHeight();
   int width = text.getClientArea().width + text.getHorizontalPixel();
   text.redraw(0, y, width, height, false);
 }
コード例 #12
0
 /**
  * Get style range at x/y coordinates
  *
  * @param x
  * @param y
  * @return style range, will be null when no style range exists at given coordinates
  */
 private StyleRange getStyleRange(final int x, final int y) {
   final StyledText t = getTextWidget();
   final int offset;
   try {
     offset = t.getOffsetAtLocation(new Point(x, y));
   } catch (IllegalArgumentException e) {
     return null;
   }
   if (offset < t.getCharCount()) return t.getStyleRangeAtOffset(offset);
   else return null;
 }
コード例 #13
0
  private void drawCurrentLine(LineBackgroundEvent event, final IRegion lineRegion) {
    final StyledText textWidget = fViewer.getTextWidget();
    final int offset = event.lineOffset;
    final RGBa lineHighlight = getCurrentTheme().getLineHighlight();
    event.lineBackground = getColorManager().getColor(lineHighlight.toRGB());

    // In this case, we should be overriding the bg of the style ranges for the line too!
    if (textWidget.isDisposed()) {
      return;
    }
    // FIXME Only change bg colors of visible ranges!
    int replaceLength = 160;
    if (lineRegion != null) {
      replaceLength = Math.min(replaceLength, lineRegion.getLength());
    }

    // be safe about offsets
    int charCount = textWidget.getCharCount();
    if (offset + replaceLength > charCount) {
      replaceLength = charCount - offset;
      if (replaceLength < 0) {
        // Just playing safe here
        replaceLength = 0;
      }
    }
    final StyleRange[] ranges = textWidget.getStyleRanges(offset, replaceLength, true);
    if (ranges == null || ranges.length == 0) {
      return;
    }
    Color background = textWidget.getBackground();
    final int[] positions = new int[ranges.length << 1];
    int x = 0;
    boolean apply = false;
    for (StyleRange range : ranges) {
      if (range.background != null) {
        if (!range.background.equals(background)) {
          positions[x] = range.start;
          positions[x + 1] = range.length;
          x += 2;
          continue;
        }
        apply = true;
      }
      range.background = null;
      positions[x] = range.start;
      positions[x + 1] = range.length;
      x += 2;
    }

    if (apply) {
      textWidget.setStyleRanges(offset, replaceLength, positions, ranges);
    }
  }
コード例 #14
0
ファイル: BentoHighlighting.java プロジェクト: jesusc/bento
 private org.eclipse.jface.text.Position convertToWidgetPosition(
     org.eclipse.jface.text.Position position) {
   if (position == null) {
     return null;
   }
   int startOffset = projectionViewer.modelOffset2WidgetOffset(position.offset);
   int endOffset = projectionViewer.modelOffset2WidgetOffset(position.offset + position.length);
   if (endOffset - startOffset != position.length
       || startOffset == -1
       || textWidget.getCharCount() < endOffset) {
     return null;
   }
   return new org.eclipse.jface.text.Position(startOffset, endOffset - startOffset);
 }
コード例 #15
0
    public void notifyPositionChange(int inOffset) {
      final int lLength = textWidget.getCharCount();

      if (lLength == 0) return;
      if (inOffset >= lLength) {
        inOffset = lLength - 1;
      }
      final StyleSnapshot lNew = new StyleSnapshot(textWidget, inOffset);
      if (styleSnapshot == null || !styleSnapshot.similarTo(lNew)) {
        // currentOffset = inOffset;
        fireStyleChange(lNew);
      }
      styleSnapshot = lNew;
    }
コード例 #16
0
 @Override
 public void addOffsetToPosition(int offset) {
   if (offset == 0) {
     return;
   }
   int newOffset = commandLineText.getCaretOffset() + offset;
   int endOffset = commandLineText.getCharCount();
   if (newOffset > endOffset) {
     newOffset = endOffset;
   } else if (newOffset < contentsOffset) {
     newOffset = contentsOffset;
   }
   commandLineText.setCaretOffset(newOffset);
 }
コード例 #17
0
 @Override
 public void delete() {
   clipSelection();
   if (commandLineText.getSelectionCount() > 0) {
     commandLineText.insert("");
   } else {
     int startOffset = commandLineText.getCaretOffset();
     if (startOffset < commandLineText.getCharCount()) {
       commandLineText.replaceTextRange(startOffset, 1, "");
     }
   }
   // caret doesn't move if character after caret is deleted and won't trigger caretMoved
   // update manually
   updateCaret();
 }
コード例 #18
0
  private Rectangle getLineRectangle(Position position) {
    if (position == null) {
      return null;
    }

    // if the position that is about to be drawn was deleted then we can't
    if (position.isDeleted()) {
      return null;
    }

    int widgetOffset = 0;
    if (fViewer instanceof ITextViewerExtension5) {

      ITextViewerExtension5 extension = (ITextViewerExtension5) fViewer;
      widgetOffset = extension.modelOffset2WidgetOffset(position.getOffset());
      if (widgetOffset == -1) {
        return null;
      }
    } else {

      IRegion visible = fViewer.getVisibleRegion();
      widgetOffset = position.getOffset() - visible.getOffset();
      if (widgetOffset < 0 || visible.getLength() < widgetOffset) {
        return null;
      }
    }

    StyledText textWidget = fViewer.getTextWidget();
    // check for https://bugs.eclipse.org/bugs/show_bug.cgi?id=64898
    // this is a guard against the symptoms but not the actual solution
    if (0 <= widgetOffset && widgetOffset <= textWidget.getCharCount()) {
      Point upperLeft = textWidget.getLocationAtOffset(widgetOffset);
      int width = textWidget.getClientArea().width + textWidget.getHorizontalPixel();
      int height = textWidget.getLineHeight(widgetOffset);
      return new Rectangle(0, upperLeft.y, width, height);
    }

    return null;
  }
コード例 #19
0
ファイル: WelcomeWindow.java プロジェクト: aprasa/project
  public void _setWhatsNew() {

    if (sWhatsNew.indexOf("<html") >= 0 || sWhatsNew.indexOf("<HTML") >= 0) {
      BrowserWrapper browser = Utils.createSafeBrowser(cWhatsNew, SWT.NONE);
      if (browser != null) {
        browser.setText(sWhatsNew);
      } else {
        try {
          File tempFile = File.createTempFile("AZU", ".html");
          tempFile.deleteOnExit();
          FileUtil.writeBytesAsFile(tempFile.getAbsolutePath(), sWhatsNew.getBytes("utf8"));
          Utils.launch(tempFile.getAbsolutePath());
          shell.dispose();
          return;
        } catch (IOException e) {
        }
      }
    } else {

      StyledText helpPanel = new StyledText(cWhatsNew, SWT.VERTICAL | SWT.HORIZONTAL);

      helpPanel.setEditable(false);
      try {
        helpPanel.setRedraw(false);
        helpPanel.setWordWrap(false);
        helpPanel.setFont(monospace);

        black = ColorCache.getColor(display, 0, 0, 0);
        white = ColorCache.getColor(display, 255, 255, 255);
        light = ColorCache.getColor(display, 200, 200, 200);
        grey = ColorCache.getColor(display, 50, 50, 50);
        green = ColorCache.getColor(display, 30, 80, 30);
        blue = ColorCache.getColor(display, 20, 20, 80);
        int style;
        boolean setStyle;

        helpPanel.setForeground(grey);

        String[] lines = sWhatsNew.split("\\r?\\n");
        for (int i = 0; i < lines.length; i++) {
          String line = lines[i];

          setStyle = false;
          fg = grey;
          bg = white;
          style = SWT.NORMAL;

          char styleChar;
          String text;

          if (line.length() < 2) {
            styleChar = ' ';
            text = " " + lineSeparator;
          } else {
            styleChar = line.charAt(0);
            text = line.substring(1) + lineSeparator;
          }

          switch (styleChar) {
            case '*':
              text = "  * " + text;
              fg = green;
              setStyle = true;
              break;
            case '+':
              text = "     " + text;
              fg = black;
              bg = light;
              style = SWT.BOLD;
              setStyle = true;
              break;
            case '!':
              style = SWT.BOLD;
              setStyle = true;
              break;
            case '@':
              fg = blue;
              setStyle = true;
              break;
            case '$':
              bg = blue;
              fg = white;
              style = SWT.BOLD;
              setStyle = true;
              break;
            case ' ':
              text = "  " + text;
              break;

            default:
              text = styleChar + text;
          }

          helpPanel.append(text);

          if (setStyle) {
            int lineCount = helpPanel.getLineCount() - 1;
            int charCount = helpPanel.getCharCount();
            //          System.out.println("Got Linecount " + lineCount + ", Charcount " +
            // charCount);

            int lineOfs = helpPanel.getOffsetAtLine(lineCount - 1);
            int lineLen = charCount - lineOfs;
            //          System.out.println("Setting Style : " + lineOfs + ", " + lineLen);
            helpPanel.setStyleRange(new StyleRange(lineOfs, lineLen, fg, bg, style));
            helpPanel.setLineBackground(lineCount - 1, 1, bg);
          }
        }

        helpPanel.setRedraw(true);
      } catch (Exception e) {
        System.out.println("Unable to load help contents because:" + e);
        // e.printStackTrace();
      }
    }

    if (labelLoading != null && !labelLoading.isDisposed()) {
      labelLoading.dispose();
    }
    shell.layout(true, true);
  }
コード例 #20
0
 @Override
 public int getEndPosition() {
   return commandLineText.getCharCount() - contentsOffset;
 }
コード例 #21
0
 /*
  * @see IInformationControlExtension#hasContents()
  */
 public boolean hasContents() {
   return fText.getCharCount() > 0;
 }