Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2008, 04:31 PM
Moderator
 
Join Date: Nov 2007
Posts: 1,657
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Implementing syntax coloring using the StyledText API
Code:
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; } }
__________________
Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums! (closes on July 27, 2008)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Beginner needs help on Loop,Math-syntax on Graphics obdi New To Java 13 07-06-2008 11:11 AM
Java Syntax for making 2 divs same height dynamically. jatrant New To Java 4 06-25-2008 10:09 PM
Cannot get passed these syntax errors MrKP New To Java 1 05-12-2008 09:05 AM
syntax error gabriel New To Java 3 08-03-2007 05:26 PM
Syntax error on token "(", ; expected baltimore AWT / Swing 1 08-01-2007 01:34 AM


All times are GMT +3. The time now is 08:26 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org