Demonstrates multiline comments : Text « SWT JFace Eclipse « Java






Demonstrates multiline comments

//Send questions, comments, bug reports, etc. to the authors:

//Rob Warner (rwarner@interspatial.com)
//Robert Harris (rbrt_harris@yahoo.com)

import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import java.util.LinkedList;
import java.util.ArrayList;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;

/**
 * This program demonstrates multiline comments. It uses MultiLineCommentListener
 * to do the syntax coloring
 */
public class MultiLineComment {
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Multiline Comments");
    createContents(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }

  /**
   * Creates the main window contents
   * 
   * @param shell the main window
   */
  private void createContents(Shell shell) {
    shell.setLayout(new FillLayout());
    final StyledText styledText = new StyledText(shell, SWT.BORDER | SWT.H_SCROLL
        | SWT.V_SCROLL);

    // Add the line style listener
    final MultiLineCommentListener lineStyleListener = new MultiLineCommentListener();
    styledText.addLineStyleListener(lineStyleListener);

    // Add the modification listener
    styledText.addExtendedModifyListener(new ExtendedModifyListener() {
      public void modifyText(ExtendedModifyEvent event) {
        // Recalculate the comments
        lineStyleListener.refreshMultilineComments(styledText.getText());

        // Redraw the text
        styledText.redraw();
      }
    });
  }

  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new MultiLineComment().run();
  }
}
//Send questions, comments, bug reports, etc. to the authors:

//Rob Warner (rwarner@interspatial.com)
//Robert Harris (rbrt_harris@yahoo.com)
/**
 * This class supports multiline comments. It turns comments green.
 */
class MultiLineCommentListener implements LineStyleListener {
  // Markers for multiline comments
  private static final String COMMENT_START = "/*";
  private static final String COMMENT_END = "*/";

  // Color for comments
  private static final Color COMMENT_COLOR = Display.getCurrent().getSystemColor(
      SWT.COLOR_DARK_GREEN);

  // Offsets for all multiline comments
  List commentOffsets;

  /**
   * MultilineCommentListener constructor
   */
  public MultiLineCommentListener() {
    commentOffsets = new LinkedList();
  }

  /**
   * Refreshes the offsets for all multiline comments in the parent StyledText.
   * The parent StyledText should call this whenever its text is modified. Note
   * that this code doesn't ignore comment markers inside strings.
   * 
   * @param text the text from the StyledText
   */
  public void refreshMultilineComments(String text) {
    // Clear any stored offsets
    commentOffsets.clear();

    // Go through all the instances of COMMENT_START
    for (int pos = text.indexOf(COMMENT_START); pos > -1; pos = text.indexOf(
        COMMENT_START, pos)) {
      // offsets[0] holds the COMMENT_START offset
      // and COMMENT_END holds the ending offset
      int[] offsets = new int[2];
      offsets[0] = pos;

      // Find the corresponding end comment.
      pos = text.indexOf(COMMENT_END, pos);

      // If no corresponding end comment, use the end of the text
      offsets[1] = pos == -1 ? text.length() - 1 : pos + COMMENT_END.length() - 1;
      pos = offsets[1];

      // Add the offsets to the collection
      commentOffsets.add(offsets);
    }
  }

  /**
   * Called by StyledText to get the styles for a line
   * 
   * @param event the event
   */
  public void lineGetStyle(LineStyleEvent event) {
    // Create a collection to hold the StyleRanges
    List styles = new ArrayList();

    // Store the length for convenience
    int length = event.lineText.length();

    for (int i = 0, n = commentOffsets.size(); i < n; i++) {
      int[] offsets = (int[]) commentOffsets.get(i);

      // If starting offset is past current line--quit
      if (offsets[0] > event.lineOffset + length) break;

      // Check if we're inside a multiline comment
      if (offsets[0] <= event.lineOffset + length
          && offsets[1] >= event.lineOffset) {
        // Calculate starting offset for StyleRange
        int start = Math.max(offsets[0], event.lineOffset);

        // Calculate length for style range
        int len = Math.min(offsets[1], event.lineOffset + length) - start + 1;

        // Add the style range
        styles.add(new StyleRange(start, len, COMMENT_COLOR, null));
      }
    }

    // Copy all the ranges into the event
    event.styles = (StyleRange[]) styles.toArray(new StyleRange[0]);
  }
}



           
       








Related examples in the same category

1.Text to uppercase
2.Text EventText Event
3.Text and Label demoText and Label demo
4.Wrap LinesWrap Lines
5.Remarks TextRemarks Text
6.Demonstrates text fieldsDemonstrates text fields
7.Turns e characters red using a LineStyleListenerTurns e characters red using a LineStyleListener
8.TextField Example 5TextField Example 5
9.TextField Example 4
10.TextField Example 3TextField Example 3
11.TextField Example 2TextField Example 2
12.TextField ExampleTextField Example
13.SWT XML Editor: Modify DOMSWT XML Editor: Modify DOM
14.Draw internationalized styled text on a shellDraw internationalized styled text on a shell
15.Detect when the user scrolls a text controlDetect when the user scrolls a text control
16.Verify input (format for date)Verify input (format for date)
17.Verify input (only allow digits)Verify input (only allow digits)
18.Set the selection (start, end)Set the selection (start, end)
19.Text example snippet: set the selection (i-beam)Text example snippet: set the selection (i-beam)
20.Select all the text in the controlSelect all the text in the control
21.Resize a text control (show about 10 characters)Resize a text control (show about 10 characters)
22.Prompt for a password (set the echo character)Prompt for a password (set the echo character)
23.Stop CR from going to the default buttonStop CR from going to the default button
24.Add a select all menu item to the controlAdd a select all menu item to the control
25.Detect CR in a text or combo control (default selection)Detect CR in a text or combo control (default selection)