/**
  * Creates a new instance of this class. Automatically starts listening to corresponding key and
  * modify events coming from the given <var>editor</var>.
  *
  * @param editor the text field to which the Undo-Redo functionality should be added
  */
 public UndoRedoStyledText(StyledText editor) {
   editor.addExtendedModifyListener(this);
   editor.addKeyListener(this);
   onMac = System.getProperty("os.name").startsWith("Mac OS X");
   this.editor = editor;
   stack = new UndoRedoStack<ExtendedModifyEvent>();
 }
示例#2
0
  /**
   * Installs this highlighter into the specified StyledText object. Client can manually call
   * detach() method, then wants to destroy this object.
   */
  public void attach(StyledText parent) {
    detach();

    text = parent;
    text.addDisposeListener(ml);
    text.addLineStyleListener(ml);
    text.addLineBackgroundListener(ml);
    text.addPaintListener(ml);
    text.addVerifyListener(ml);
    text.addExtendedModifyListener(ml);
    text.addControlListener(ml);
    text.addKeyListener(ml);
    text.addTraverseListener(ml);
    text.addMouseListener(ml);
    text.addSelectionListener(ml);
    text.getContent().addTextChangeListener(ml);
    ScrollBar sb = text.getVerticalBar();
    if (sb != null) sb.addSelectionListener(ml);
    updateViewport();

    new Thread() {
      public void run() {
        // setPriority(Thread.NORM_PRIORITY-1);
        while (true) {
          try {
            sleep(300);
          } catch (InterruptedException e) {
          }
          if (baseEditor == null || text == null) break;
          if (backParserDelay) {
            backParserDelay = false;
            try {
              sleep(1500);
            } catch (InterruptedException e) {
            }
            continue;
          }
          ;
          Display.getDefault()
              .syncExec(
                  new Runnable() {
                    public void run() {
                      if (baseEditor == null || text == null) return;
                      if (text.isDisposed()) return;
                      // System.out.println(System.currentTimeMillis());
                      baseEditor.idleJob(80);
                      // redrawFrom(text.getLineAtOffset(text.getCaretOffset()));
                    }
                  });
        }
        ;
      };
    }.start();
  }
  /** DOC amaumont Comment method "addListeners". */
  private void addListeners() {
    styledText.addFocusListener(
        new FocusListener() {

          public void focusGained(FocusEvent e) {
            refreshProposalSize();
          }

          public void focusLost(FocusEvent e) {}
        });

    styledText.addControlListener(
        new ControlListener() {

          public void controlMoved(ControlEvent e) {}

          public void controlResized(ControlEvent e) {
            refreshProposalSize();
          }
        });

    styledText.addExtendedModifyListener(
        new ExtendedModifyListener() {

          public void modifyText(ExtendedModifyEvent event) {
            updateCellExpression();
            // System.out.println("ExtendedModifyListener modify text");

          }
        });
    styledText
        .getContent()
        .addTextChangeListener(
            new TextChangeListener() {

              public void textChanged(TextChangedEvent event) {
                highlightLineOfCursorPosition(styledText.getSelection());
              }

              public void textChanging(TextChangingEvent event) {
                // System.out.println("textChanging");
              }

              public void textSet(TextChangedEvent event) {
                // System.out.println("textSet");
              }
            });
    styledText.addModifyListener(
        new ModifyListener() {

          public void modifyText(ModifyEvent e) {
            highlightLineOfCursorPosition(styledText.getSelection());
          }
        });

    styledText.addKeyListener(
        new KeyListener() {

          public void keyPressed(KeyEvent e) {
            highlightLineOfCursorPosition(styledText.getSelection());
          }

          public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

          }
        });

    styledText.addVerifyKeyListener(
        new VerifyKeyListener() {

          public void verifyKey(VerifyEvent verifyEvent) {
            if (verifyEvent.character == '\r'
                && contentProposalAdapter != null
                && contentProposalAdapter.isProposalOpened()) {
              verifyEvent.doit = false;
            } else {
              verifyEvent.doit = true;
            }
          }
        });

    styledText.addMouseListener(
        new MouseListener() {

          public void mouseDoubleClick(MouseEvent e) {
            highlightLineOfCursorPosition(styledText.getSelection());
          }

          public void mouseDown(MouseEvent e) {
            highlightLineOfCursorPosition(styledText.getSelection());
          }

          public void mouseUp(MouseEvent e) {}
        });
  }
  private void initialize(final Composite inContainer) {
    textWidget =
        new StyledText(
            inContainer,
            SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);

    content = new TextContent();
    textWidget.setContent(content);
    textWidget.setLayout(new GridLayout(1, true));
    textWidget.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    textWidget.addExtendedModifyListener(
        new ExtendedModifyListener() {
          @Override
          public void modifyText(final ExtendedModifyEvent inEvent) {
            if (textWidget.getCharCount() == 0) return;
            final StyleRange lRange =
                textWidget.getStyleRangeAtOffset(Math.max(inEvent.start - 1, 0));
            if (lRange != null) {
              final StyleRange lNew =
                  new StyleRange(
                      lRange.start,
                      lRange.length + inEvent.length,
                      lRange.foreground,
                      lRange.background,
                      lRange.fontStyle);
              textWidget.replaceStyleRanges(lNew.start, lNew.length, new StyleRange[] {lNew});
            }
            final int lEndPos = inEvent.start + inEvent.length;
            if (lEndPos - textWidget.getOffsetAtLine(textWidget.getLineAtOffset(lEndPos)) == 0) {
              final int lLineIndex = textWidget.getLineAtOffset(inEvent.start);
              if (lLineIndex + 2 > textWidget.getLineCount()) return;
              // a new line has been entered, therefore, check whether we
              // have to continue a list
              final Bullet lBullet = textWidget.getLineBullet(lLineIndex);
              textWidget.setLineBullet(lLineIndex + 1, 1, lBullet);
            }
          }
        });
    textWidget.addPaintObjectListener(Styles.getPaintObjectListener(textWidget));
    textWidget.addVerifyKeyListener(
        new VerifyKeyListener() {
          @Override
          public void verifyKey(final VerifyEvent inEvent) {
            if (inEvent.keyCode == 9) { // TAB keyCode
              final StyledText lWidget = (StyledText) inEvent.getSource();
              if (consumeTabKey(lWidget)) {
                inEvent.doit = false;
                final TextStyler lStyler = new TextStyler(lWidget);
                if ((inEvent.stateMask & SWT.SHIFT) != 0) {
                  lStyler.dedentLines();
                  provider.notifyPositionChange(lWidget.getCaretOffset());
                } else {
                  lStyler.indentLines();
                }
              }
            }
          }
        });
    textWidget.addTraverseListener(
        new TraverseListener() {
          @Override
          public void keyTraversed(final TraverseEvent inEvent) {
            if (inEvent.detail == SWT.TRAVERSE_TAB_PREVIOUS
                && consumeTabKey((StyledText) inEvent.getSource())) {
              inEvent.doit = false;
            }
          }
        });
    textWidget.addMouseListener(
        new MouseAdapter() {
          @Override
          public void mouseDown(final MouseEvent inEvent) {
            provider.notifyPositionChange(textWidget.getCaretOffset());
          }
        });
    textWidget.addKeyListener(
        new KeyAdapter() {
          @Override
          public void keyReleased(final KeyEvent inEvent) {
            provider.notifyPositionChange(textWidget.getCaretOffset());
          }
        });
    provider = ContextInjectionFactory.make(TextStyleProvider.class, context);
    provider.setWidget(textWidget);

    // font handling
    if (fontSizeToUse != 0) {
      trackFontSize(fontSizeToUse);
    }

    disposed = false;
  }
  /**
   * Creates a new <code>BZStyledText</code> for parentShell <code>parentShell</code>.
   *
   * @param parentShell parentShell of the new instance (cannot be null)
   */
  public BZStyledText(Shell parentShell) {
    this.parentShell = parentShell;

    //   version from jar manifest
    String version = getClass().getPackage().getImplementationVersion();

    //   version from build file
    if (version == null) version = System.getProperty("braillezephyr.version");

    //   no version
    if (version == null) {
      logWriter.println("WARNING:  unable to determine version, using 0.0");
      version = "0.0";
    }

    versionString = version;
    String[] versionStrings = versionString.split("\\.");
    versionMajor = Integer.parseInt(versionStrings[0]);
    if (versionStrings.length > 1) versionMinor = Integer.parseInt(versionStrings[1]);
    else versionMinor = 0;
    if (versionStrings.length > 2) versionPatch = Integer.parseInt(versionStrings[2]);
    else versionPatch = 0;

    color = parentShell.getDisplay().getSystemColor(SWT.COLOR_BLACK);

    composite = new Composite(parentShell, 0);
    composite.setLayout(new GridLayout(2, true));

    //   load fonts
    loadFont("BrailleZephyr_6.otf");
    loadFont("BrailleZephyr_6b.otf");
    loadFont("BrailleZephyr_6s.otf");
    loadFont("BrailleZephyr_6sb.otf");
    loadFont("BrailleZephyr_8.otf");
    loadFont("BrailleZephyr_8b.otf");
    loadFont("BrailleZephyr_8s.otf");
    loadFont("BrailleZephyr_8sb.otf");
    loadFont("BrailleZephyr_8w.otf");
    loadFont("BrailleZephyr_8wb.otf");
    loadFont("BrailleZephyr_8ws.otf");
    loadFont("BrailleZephyr_8wsb.otf");

    //   load line margin bell
    try {
      InputStream inputStreamBellMargin =
          new BufferedInputStream(getClass().getResourceAsStream("/sounds/line_margin_bell.wav"));
      AudioInputStream audioInputStreamMargin =
          AudioSystem.getAudioInputStream(inputStreamBellMargin);
      DataLine.Info dataLineInfoMargin =
          new DataLine.Info(Clip.class, audioInputStreamMargin.getFormat());
      lineMarginClip = (Clip) AudioSystem.getLine(dataLineInfoMargin);
      lineMarginClip.open(audioInputStreamMargin);
    } catch (IOException exception) {
      logWriter.println(
          "ERROR:  Unable to read default line margin bell file:  " + exception.getMessage());
      lineMarginClip = null;
    } catch (UnsupportedAudioFileException exception) {
      logWriter.println(
          "ERROR:  Sound file unsupported for default line margin bell:  "
              + exception.getMessage());
      lineMarginClip = null;
    } catch (LineUnavailableException exception) {
      logWriter.println(
          "ERROR:  Line unavailable for default line margin bell:  " + exception.getMessage());
      lineMarginClip = null;
    }

    //   load page margin bell
    try {
      InputStream inputStreamBellPage =
          new BufferedInputStream(getClass().getResourceAsStream("/sounds/page_margin_bell.wav"));
      AudioInputStream audioInputStreamPage = AudioSystem.getAudioInputStream(inputStreamBellPage);
      DataLine.Info dataLineInfoPage =
          new DataLine.Info(Clip.class, audioInputStreamPage.getFormat());
      pageMarginClip = (Clip) AudioSystem.getLine(dataLineInfoPage);
      pageMarginClip.open(audioInputStreamPage);
    } catch (IOException exception) {
      logWriter.println(
          "ERROR:  Unable to read default page margin bell file:  " + exception.getMessage());
      pageMarginClip = null;
    } catch (UnsupportedAudioFileException exception) {
      logWriter.println(
          "ERROR:  Sound file unsupported for default page margin bell:  "
              + exception.getMessage());
      pageMarginClip = null;
    } catch (LineUnavailableException exception) {
      logWriter.println(
          "ERROR:  Line unavailable for default page margin bell:  " + exception.getMessage());
      pageMarginClip = null;
    }

    //   load line end bell
    try {
      InputStream inputStreamBellPage =
          new BufferedInputStream(getClass().getResourceAsStream("/sounds/line_end_bell.wav"));
      AudioInputStream audioInputStreamPage = AudioSystem.getAudioInputStream(inputStreamBellPage);
      DataLine.Info dataLineInfoPage =
          new DataLine.Info(Clip.class, audioInputStreamPage.getFormat());
      lineEndClip = (Clip) AudioSystem.getLine(dataLineInfoPage);
      lineEndClip.open(audioInputStreamPage);
    } catch (IOException exception) {
      logWriter.println(
          "ERROR:  Unable to read default line end bell file:  " + exception.getMessage());
      lineEndClip = null;
    } catch (UnsupportedAudioFileException exception) {
      logWriter.println(
          "ERROR:  Sound file unsupported for default line end bell:  " + exception.getMessage());
      lineEndClip = null;
    } catch (LineUnavailableException exception) {
      logWriter.println(
          "ERROR:  Line unavailable for default line end bell:  " + exception.getMessage());
      lineEndClip = null;
    }

    brailleText = new StyledText(composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    brailleText.setLayoutData(new GridData(GridData.FILL_BOTH));
    brailleText.setFont(new Font(parentShell.getDisplay(), "BrailleZephyr_6s", 18, SWT.NORMAL));
    brailleText.addFocusListener(new FocusHandler(brailleText));
    brailleText.addPaintListener(new PaintHandler(brailleText));
    BrailleKeyHandler brailleKeyHandler = new BrailleKeyHandler(true);
    brailleText.addKeyListener(brailleKeyHandler);
    brailleText.addVerifyKeyListener(brailleKeyHandler);
    brailleText.addExtendedModifyListener(new ExtendedModifyHandler(brailleText));

    content = brailleText.getContent();

    asciiText = new StyledText(composite, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    asciiText.setContent(content);
    asciiText.setLayoutData(new GridData(GridData.FILL_BOTH));
    asciiText.setFont(new Font(parentShell.getDisplay(), "Monospace", 18, SWT.NORMAL));
    asciiText.addFocusListener(new FocusHandler(asciiText));
    asciiText.addPaintListener(new PaintHandler(asciiText));
    asciiText.addVerifyKeyListener(new BrailleKeyHandler(false));
    asciiText.addExtendedModifyListener(new ExtendedModifyHandler(asciiText));

    brailleText.addCaretListener(new CaretHandler(brailleText, asciiText));
    asciiText.addCaretListener(new CaretHandler(asciiText, brailleText));

    currentText = brailleText;
  }