Mega Code Archive

 
Categories / Java / SWT Jface Eclipse
 

Implements syntax coloring using the StyledText API

//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.com) //Robert Harris (rbrt_harris@yahoo.com) import java.util.*; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.*; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; /**  * This class implements syntax coloring using the StyledText API  */ public class SyntaxTest {   // Punctuation   private static final String PUNCTUATION = "(){};!&|.+-*/";   // Color for the StyleRanges   private Color red;   /**    * Runs the application    */   public void run() {     Display display = new Display();     Shell shell = new Shell(display);     // Get color for style ranges     red = display.getSystemColor(SWT.COLOR_RED);     createContents(shell);     shell.open();     while (!shell.isDisposed()) {       if (!display.readAndDispatch()) {         display.sleep();       }     }     // No need to dispose red     display.dispose();   }   /**    * Creates the main window contents    *     * @param shell the main window    */   private void createContents(Shell shell) {     shell.setLayout(new FillLayout());     // Create the StyledText     final StyledText styledText = new StyledText(shell, SWT.BORDER);     // Add the syntax coloring handler     styledText.addExtendedModifyListener(new ExtendedModifyListener() {       public void modifyText(ExtendedModifyEvent event) {         // Determine the ending offset         int end = event.start + event.length - 1;         // If they typed something, get it         if (event.start <= end) {           // Get the text           String text = styledText.getText(event.start, end);           // Create a collection to hold the StyleRanges           java.util.List ranges = new java.util.ArrayList();           // Turn any punctuation red           for (int i = 0, n = text.length(); i < n; i++) {             if (PUNCTUATION.indexOf(text.charAt(i)) > -1) {               ranges.add(new StyleRange(event.start + i, 1, red, null, SWT.BOLD));             }           }           // If we have any ranges to set, set them           if (!ranges.isEmpty()) {             styledText.replaceStyleRanges(event.start, event.length,                 (StyleRange[]) ranges.toArray(new StyleRange[0]));           }         }       }     });   }   /**    * The application entry point    *     * @param args the command line arguments    */   public static void main(String[] args) {     new SyntaxTest().run();   } } //Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.com) //Robert Harris (rbrt_harris@yahoo.com) /**  * This class contains information for syntax coloring and styling for an  * extension  */ class SyntaxData {   private String extension;   private Collection keywords;   private String punctuation;   private String comment;   private String multiLineCommentStart;   private String multiLineCommentEnd;   /**    * Constructs a SyntaxData    *     * @param extension the extension    */   public SyntaxData(String extension) {     this.extension = extension;   }   /**    * Gets the extension    *     * @return String    */   public String getExtension() {     return extension;   }   /**    * Gets the comment    *     * @return String    */   public String getComment() {     return comment;   }   /**    * Sets the comment    *     * @param comment The comment to set.    */   public void setComment(String comment) {     this.comment = comment;   }   /**    * Gets the keywords    *     * @return Collection    */   public Collection getKeywords() {     return keywords;   }   /**    * Sets the keywords    *     * @param keywords The keywords to set.    */   public void setKeywords(Collection keywords) {     this.keywords = keywords;   }   /**    * Gets the multi-line comment end    *     * @return String    */   public String getMultiLineCommentEnd() {     return multiLineCommentEnd;   }   /**    * Sets the multi-line comment end    *     * @param multiLineCommentEnd The multiLineCommentEnd to set.    */   public void setMultiLineCommentEnd(String multiLineCommentEnd) {     this.multiLineCommentEnd = multiLineCommentEnd;   }   /**    * Gets the multi-line comment start    *     * @return String    */   public String getMultiLineCommentStart() {     return multiLineCommentStart;   }   /**    * Sets the multi-line comment start    *     * @param multiLineCommentStart The multiLineCommentStart to set.    */   public void setMultiLineCommentStart(String multiLineCommentStart) {     this.multiLineCommentStart = multiLineCommentStart;   }   /**    * Gets the punctuation    *     * @return String    */   public String getPunctuation() {     return punctuation;   }   /**    * Sets the punctuation    *     * @param punctuation The punctuation to set.    */   public void setPunctuation(String punctuation) {     this.punctuation = punctuation;   } } //Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.com) //Robert Harris (rbrt_harris@yahoo.com) /**  * This class manages the syntax coloring and styling data  */ class SyntaxManager {   // Lazy cache of SyntaxData objects   private static Map data = new Hashtable();   /**    * Gets the syntax data for an extension    */   public static synchronized SyntaxData getSyntaxData(String extension) {     // Check in cache     SyntaxData sd = (SyntaxData) data.get(extension);     if (sd == null) {       // Not in cache; load it and put in cache       sd = loadSyntaxData(extension);       if (sd != null) data.put(sd.getExtension(), sd);     }     return sd;   }   /**    * Loads the syntax data for an extension    *     * @param extension the extension to load    * @return SyntaxData    */   private static SyntaxData loadSyntaxData(String extension) {     SyntaxData sd = null;     try {       ResourceBundle rb = ResourceBundle.getBundle("examples.ch11." + extension);       sd = new SyntaxData(extension);       sd.setComment(rb.getString("comment"));       sd.setMultiLineCommentStart(rb.getString("multilinecommentstart"));       sd.setMultiLineCommentEnd(rb.getString("multilinecommentend"));       // Load the keywords       Collection keywords = new ArrayList();       for (StringTokenizer st = new StringTokenizer(rb.getString("keywords"), " "); st           .hasMoreTokens();) {         keywords.add(st.nextToken());       }       sd.setKeywords(keywords);       // Load the punctuation       sd.setPunctuation(rb.getString("punctuation"));     } catch (MissingResourceException e) {       // Ignore     }     return sd;   } }