Jan 10

By default, the source level is 1.3 in maven 2. We need to config maven compiler to use Java 1.6. Add the following snippet to your pom:

  1.  
  2. <build>
  3.         <plugins>
  4.                 <plugin>
  5.                         <groupId>org.apache.maven.plugins</groupId>
  6.                         <artifactId>maven-compiler-plugin</artifactId>
  7.                         <configuration>
  8.                                 <source>1.6</source>
  9.                                 <target>1.6</target>
  10.                         </configuration>
  11.                 </plugin>
  12.         </plugins>
  13. </build>
  14.  
  • Share/Bookmark
Jan 08

If you are doing antlr v3 for Java, you can be very interested in this series of video tutorials. It demos how to develop with antlr3 IDE, and also the process of a small language. It credits to Marcel for the recommendation. Check it out at:

ANTLR 3.x Tutorials:
http://vimeo.com/groups/29150/videos

  • Share/Bookmark
Sep 28

Have you heard that Java support scripting engine? Try the source code below:

  1.  
  2. import javax.script.ScriptEngine;
  3. import javax.script.ScriptEngineManager;
  4. import javax.script.ScriptException;
  5.  
  6.  
  7. public class TestScript {
  8.         public static void main(String[] args) {
  9.                 String script = "println(’Hello, world’);";
  10.                 script += "s=0;";
  11.                 script += "for(var i=1; i<=100; i++)";
  12.                 script += "s += i;";
  13.                 script += "println(’s=’+s);";   
  14.                
  15.                 ScriptEngineManager factory = new ScriptEngineManager();
  16.                 ScriptEngine engine = factory.getEngineByName("JavaScript");
  17.                 try {
  18.                         engine.eval(script);
  19.                 } catch (ScriptException e) {
  20.                         e.printStackTrace();
  21.                 }
  22.         }
  23. }
  24.  
  • Share/Bookmark
Sep 24

Below is very straightforward code snippet to use PageBook. It is used together with eclipse form. Therefore you will see widgets are created via the FormToolkit class.

  1.  
  2. toolkit = new FormToolkit(container.getDisplay());
  3. form = toolkit.createScrolledForm(container);
  4.  
  5. Composite parent = form.getBody();
  6. GridLayoutFactory.fillDefaults().numColumns(1).applyTo(parent);
  7.  
  8. // create a table as selection provider
  9. final Table table = toolkit.createTable(parent, SWT.BORDER | SWT.VIRTUAL);
  10. table.setHeaderVisible(true);
  11. table.setLinesVisible(true);
  12. for(int i=1; i<=12; i++){
  13.         TableItem item = new TableItem(table, 0);
  14.         item.setText("Item " + i);
  15. }                              
  16.  
  17. toolkit.createLabel(parent, "Selected pagebook:");
  18.  
  19. // create a PageBook
  20. final ScrolledPageBook pageBook = toolkit.createPageBook(parent, SWT.NONE);
  21. GridDataFactory.defaultsFor(pageBook).grab(true, true).applyTo(pageBook);
  22.  
  23. // create PageBook controls
  24. for(int i=0; i<table.getItemCount(); i++){
  25.         TableItem item = table.getItem(i);
  26.         Composite pageContainer = pageBook.createPage(item.getText());
  27.         GridLayoutFactory.fillDefaults().numColumns(1).applyTo(pageContainer);
  28.        
  29.         toolkit.createLabel(pageContainer, item.getText());
  30.         toolkit.createText(pageContainer, "to be edited");
  31.        
  32.         pageBook.showPage(item.getText());
  33. }
  34.  
  35. pageBook.showEmptyPage();
  36.  
  37. // add selection listener, and show relative PageBook on selection
  38. table.addSelectionListener(new SelectionAdapter(){
  39.         public void widgetSelected(SelectionEvent e) {
  40.                 TableItem item = (TableItem) e.item;
  41.                 pageBook.showPage(item.getText());
  42.         }                      
  43. });
  44.  
  • Share/Bookmark
Jul 05

In some cases, we’d like to get all the dependency jar files for a project. There is a maven plugin to do it simply, via a simple way. Just add the build sections to your pom file, as shown below:

  1.  
  2. <project>
  3. <!– ……. –>
  4. <build>
  5.         <plugins>
  6.                 <plugin>
  7.                                 <groupId>org.apache.maven.plugins</groupId>
  8.                                 <artifactId>maven-dependency-plugin</artifactId>
  9.                                 <executions>
  10.                                         <execution>
  11.                                                 <id>copy-dependencies</id>
  12.                                                 <phase>process-sources</phase>
  13.                                                 <goals>
  14.                                                         <goal>copy-dependencies</goal>
  15.                                                 </goals>
  16.                                                 <configuration>
  17.                                                         <outputDirectory>c:\output</outputDirectory>
  18.                                                         <overWriteReleases>false</overWriteReleases>
  19.                                                         <overWriteSnapshots>false</overWriteSnapshots>
  20.                                                         <overWriteIfNewer>true</overWriteIfNewer>
  21.                                                 </configuration>
  22.                                         </execution>
  23.                                 </executions>
  24.                         </plugin>
  25.                         </plugins>
  26.         </build>
  27.  
  28. <!– ……. –>
  29.  
  30. </project>
  31.  
  • Share/Bookmark
Jun 05

Recently I am interested in Yahoo API and web services, especially inquirying finance data via the APIs.

Yahoo provides nice language support. For example, python is supported at

http://developer.yahoo.com/python/

At the bottom of the page, you can find a list of popular programming language, such as Java, .NET, Ruby, Javascript,  and so on.

Also at the bottom, there is a list of yahoo services, such as search, finance, weather, flicker….seems a lot of stuff to play with. :)

  • Share/Bookmark
Mar 10

By default, the MultiPageEditorPage doesn’t allow to create closable editor pages. What is closable editor page? It means you can dynamically load and close editor pages, like what you have in EditorPlus, UltraEdit, and Notepad++.

To enable closable editor page, there are a few works to do:

1. Create your subclass of MultiPageEditorPart

2. Add your own addPage method. It will call the existing addPage(…) method, and get a pageIndex of newly added editor page:

  1.  
  2. //……
  3. int pageIndex = addPage(editor, new MyEditorInput(stepInfo));
  4. setPageText(pageIndex, stepInfo.getName());
  5. setActivePage(pageIndex);
  6.  
  7. // start to set closable property
  8. Composite container = super.getContainer();
  9. if(container instanceof CTabFolder){
  10.         CTabItem tabItem = ((CTabFolder)container).getItem(pageIndex);
  11.         tabItem.setShowClose(true);
  12. }
  13. // voila! done!
  14.  
  • Share/Bookmark
Feb 27

Part 1 of the notes is located at http://www.frankdu.com/weblog/archives/46

The relative presentation slide is located at openArchitectureWare.org.

23. In XText, you start to work with defining concret syntax.

24. For existing meta model, use importMetamodel directive. Use preventMMGeneration to prvent any meta model generation.

25. Simple Editor Customization in Xtext
- Xtend, expression language used throughout oAW
- Constraint checks: oAW check language, based on Xtend
- OutlineView customization: override label(…) and image(…) for meta types
- Content Assist
- customize the font style for keyword (keyword only)

26. Xtext instantiates Ecore metamodels, which means that it can be processed with any EMF tool.
- Within oAW workflow: the only Xtext-specific aspect is using the generated parser. Xpand template language is powerful code generation tool. Easily traverse the model/meta model using Xtend language
- EMF way: EMF’s native resource mechanism (what are the details?)
- Your own code: use the generated parser.

27. NodeUtil with generated parser
- Typically you only work with AST (ecore file)
- Help to obtain info from the parser tree: element location, element text, parser tree node at certain offset.

28. Two phases for doing your DSL:
- designing your language
- building language tools
Xtext focuses on the second phase. Except from the phases, it is also important to provide framework that run the tasks defined your DSL.

29. oAW Xtext become a part of TMF project. The first release is expected in later half 2009.

30. A Xtext parser limitation. It’s impossible to add custom action code in the parser. Sometimes it results in ugly meta models, especially with building expression languages.

  • Share/Bookmark
Feb 26

The relative presentation slide is located at openArchitectureWare.org.

Part 2 of the notes is located at http://www.frankdu.com/weblog/archives/52

Below are my reading notes for Textual DSLs and text modeling in eclipse. I haven’t finished the ppt slides. Therefore, this is only part one.

1. EMF servers as the foundation. It provides Ecore Metamodel and framework tools like:
- editing
- transactions
- validation
- query
- distribution/persistence

2. GMF is used for building custom graphical editors based on EMF meta models. It is industry-proven technology. Based on GEF.

3. TMF is used for building custom textual editors. It is in incubation phase. There are two implementations: Xtext and TCS.

4. M2M (Model-to-Model) delivers an extensible framework for m2m transformation languages. ATL is M2M language from INRIA. QVT is an implementation.

5. M2T (Model-to-Text) focuses on transforming models into text (code generation, model serialization). For example, you may want to convert in-memory models into xml files for persistence/transportation purposes. You may want to use a parser to convert xml files back to models. There are 2 so-called frameworks:

- JET is code generation tools that are used by EMF
- Xpand is code generation tools that are part of M2T releases.

6. Xtext is originally from openArchitectureWare.com. It’s a good integration with eclipse. The oAW uses EMF as a basis, bases graphical editors on GMF, and all tooling are based on eclipse. Since Xtext has become part of eclipse TMF, there are two versions of Xtext: oAW Xtext, and TMF Xtext. The former is relatively mature. The latter is under active development, and expected to be first released sometime this year, namely in 2009.

7. DSL is a focused, processable language for addressing specific concerns in a specific domain. It is targeted to be a simple tool for a relatively complex domain. Therefore, in most cases, DSLs are human-readable to domain experts without any training. The popular DSL examples are SQL and Excel.

8. DSLs can be classified in many ways:
- configuration vs. customization
- internal vs. external
- graphical vs. textual

9. Xtext is a so-called framework tool for building external textual customization DSLs.

10. Is it possible to edit same model with both textual and graphical editing interfaces?
It might be possible. Consider one of the following: a. Visualize a subset of the model, using graphvis or prefuse. But it is typically read-only. b. Use different perspectives. Some of them use graphcial editor. It requires cross references between textual and graphical models). c. Edit the same model textually and graphically. Textual format is used as the serialization format from the graphical model. It requires writability and sync of both models!

11. Typically textual DSLs leverage one of many parser generators (ANTLR, Java CC, Lex/yacc). They help to generate a parser based on grammar definition. Consequencely, a parser tries to match text, and try to create a parse tree.

12. Typically, textual DSLs are transformed into an Abstract Syntax Tree (AST). It is ofen a binary tree. For exampe, the AST for 1 + 2*3:

Literal[1] AddExpression ( Literal[2] MultiplyExpression Literal[3])

Literal, AddExpression, and MultiplyExpression are binary nodes.

13. The AST can be taken as a model. But textual DSLs are written without careness of the AST. They can even be against AST.

14. Challenges in Xtext DSL implementation:
- Writting a parser is non-trivial.
- A parser generator makes life easier, but still not one for all.
- A parser generator only creates a matcher and/or a simplistic AST. You still need to further transform the model to easily processable form, and create an editor with syntax highlighting, code completion, etc.

Xtext is designed to ease unbearable burden of life like that.

15. Xtext is based on an EBNF grammar (what’s that? Why will it make a difference?). Xtext will generate:
- ANTLR-based parser
- EMF-based metamodel
- Eclipse editor with or extensible for: syntax highlighting, code completion, code folding, constraint checking, and so on.

16. Different Kinds of Xtext Rules:
- Type Rule
- String Rule
- Enum Rule
- Native Rule

17. Built-in Lexer Types in Xtext:
- ID
- STRING
- INT
- Comments ( Single line and multiple lines)
- Whitespace
The content of those rules is not transformed into the meta model. (How it matters?)

18. Built-in Reference Types in Xtext:
- Reference
- File Reference/Import

19. Abstract Type Rules are implicitly declared with a collection of OR-ed alternatives: R1 | R2 | R3. They will be mapped to abstract metaclass. The alternatives will become subclass. The common properties will be lifted into abstract superclass.

20. String Rules are declared in the format: String [rule_name]: [rule_definition];

21. Enum Rule is mapped to Enum in metamodel. Its format: Enum [rule_name]: [token_name="string"]+;

22. Native Rule. Example:
Native SL_COMMENT:
“‘#’ ~(’\n’|'\r’)* ‘\r’? ‘\n’”;

Term:
1. EMF - Eclipse Modeling Framework
2. GMF - Graphical Modeling Framework
3. GEF - Graphical Editing Framework
4. TMF - Textual Modeling Framework
5. DSL - Domain Specific Langauge
6. JET - Jave Emitter Templates
7. AST - Abstract Syntax Tree

Continue to read: Part 2 of the notes is located at http://www.frankdu.com/weblog/archives/52

  • Share/Bookmark
Feb 26

In the text editor, the markers are displayed on the vertical ruler. In Java source editor (CompilationUnitEditor class), you can click a problem marker, then a suggestion list will pop up. It’s pretty user friendly, isn’t it?

Then, how to implement our own clickable annotation marker? Generally the following steps address the issue:

  1. Add the extension org.eclipse.ui.editorActions.
  2. Add an editorContribution sub node. Config the contribution.
  3. Implement an AbstractRulerActionDelegate for the contribution. In its createAction method, return your customized action.
  4. Implement your customized action by extending SelectMarkerRulerAction. In the action, you may be interested to traverse through annotation model, to invoke the appropriate commands.

Examples are best tutorials.  Therefore, let’s take a look at Java source editor in org.eclipse.jdt.ui, step bey step:

1. The editorActions contribution in JDT UI plug-in:

  1.  
  2. <extension point="org.eclipse.ui.editorActions">
  3.         <editorContribution
  4.             targetID="org.eclipse.jdt.ui.CompilationUnitEditor"
  5.             id="org.eclipse.jdt.internal.ui.CompilationUnitEditor.ruler.actions">
  6.                 <action
  7.                label="%JavaSelectRulerAction.label"
  8.                class="org.eclipse.jdt.internal.ui.javaeditor.JavaSelectRulerAction"
  9.                actionID="RulerClick"
  10.                id="org.eclipse.jdt.internal.ui.javaeditor.JavaSelectRulerAction">
  11.          </action>
  12.         </editorContribution>
  13. </extension>
  14.  

Pay attention to actionID and targetID.

2. The AbstractRulerActionDelegate. It uses template pattern to let you create your own action:

  1.  
  2. public class JavaSelectRulerAction extends AbstractRulerActionDelegate {
  3.         protected IAction createAction(ITextEditor editor, IVerticalRulerInfo rulerInfo) {
  4.                 return new JavaSelectAnnotationRulerAction(JavaEditorMessages.getBundleForConstructedKeys(), "JavaSelectAnnotationRulerAction.", editor, rulerInfo); //$NON-NLS-1$
  5.         }
  6. }
  7.  

3. The SelectMarkerRulerAction. It is needed to access the annotation model. If you don’t need to access annotation model, of course you can use other actions. The Java editor version is JavaSelectAnnotationRulerAction. It contains complicated relations. Therefore, to make it simple, here I paste my simple version:

  1.  
  2. public class DataRequestSelectAnnotationRulerAction extends SelectMarkerRulerAction {
  3.  
  4.         private ITextEditor fTextEditor;
  5.         private boolean fHasCorrection;
  6.         private Position fPosition;
  7.         private Annotation fAnnotation;
  8.  
  9.         public DataRequestSelectAnnotationRulerAction(ResourceBundle bundle, String prefix,
  10.                         ITextEditor editor, IVerticalRulerInfo ruler) {
  11.                 super(bundle, prefix, editor, ruler);
  12.                 fTextEditor = editor;
  13.         }
  14.  
  15.         @Override
  16.         public void run() {
  17.                 runWithEvent(null);
  18.         }
  19.  
  20.         @Override
  21.         public void runWithEvent(Event event) {
  22.                 if(fHasCorrection){
  23.                         ITextOperationTarget operation= (ITextOperationTarget) fTextEditor.getAdapter(ITextOperationTarget.class);
  24.                         final int opCode= ISourceViewer.QUICK_ASSIST;
  25.                         if (operation != null && operation.canDoOperation(opCode)) {
  26.                                 fTextEditor.selectAndReveal(fPosition.getOffset(), fPosition.getLength());
  27.                                 operation.doOperation(opCode);
  28.                         }
  29.                 }
  30.         }
  31.  
  32.         @Override
  33.         public void update() {
  34.                 findDataRequestAnnotation();
  35.                 setEnabled(true);
  36.  
  37.                 super.update();
  38.         }
  39.  
  40.         private void findDataRequestAnnotation(){
  41.                 fPosition = null;
  42.                 fAnnotation = null;
  43.                 fHasCorrection = false;
  44.  
  45.                 AbstractMarkerAnnotationModel model = this.getAnnotationModel();
  46.                 IAnnotationAccessExtension annotationAccess = this.getAnnotationAccessExtension();
  47.  
  48.                 IDocument document = getDocument();
  49.                 if(model == null)
  50.                         return;
  51.  
  52.                 boolean hasAssistLightbulb = true;
  53.  
  54.                 Iterator iter = model.getAnnotationIterator();
  55.                 int layer = Integer.MIN_VALUE;
  56.  
  57.                 while(iter.hasNext()){
  58.                         Annotation annotation = (Annotation)iter.next();
  59.  
  60.                         if(annotation.isMarkedDeleted())
  61.                                 continue;
  62.  
  63.                         int annotationLayer = layer;
  64.                         if(annotationAccess != null){
  65.                                 annotationLayer = annotationAccess.getLayer(annotation);
  66.                                 if(annotationLayer < layer)
  67.                                         continue;
  68.                         }
  69.  
  70.                         Position position = model.getPosition(annotation);
  71.                         if(!includesRulerLine(position, document))
  72.                                 continue;
  73.  
  74.                         boolean isReadOnly = fTextEditor instanceof ITextEditorExtension && ((ITextEditorExtension)fTextEditor).isEditorInputReadOnly();
  75.  
  76.                         if(!isReadOnly && (
  77.                                         ((hasAssistLightbulb && annotation instanceof TemporaryAnnotation)))
  78.                         ){
  79.                                 fPosition = position;
  80.                                 fAnnotation = annotation;
  81.                                 fHasCorrection = true;
  82.                                 layer = annotationLayer;
  83.                                 return;
  84.                         }
  85.  
  86.                 }
  87.         }
  88.  
  89. }
  90.  

Hope the example above helps.

  • Share/Bookmark