Sunday, November 30, 2008
Saturday, October 4, 2008
Groovy as extension for Netbeans editor
I played a little bit with Groovy and GroovyShell and noticed that you can easily pass your (java) variables to it. It means that you can pass your editor's document and process it with Groovy.
I created simple module for processing xml files. It is based on XPathEvaluator module. It adds a new window to Netbeans where you can write Groovy scripts.
Code for "Run Script" button looks like this:
Binding binding = new Binding(); binding.setVariable("doc", xmlDoc); binding.setVariable("output", jTextArea1); GroovyShell shell = new GroovyShell(binding); shell.evaluate(jEditorPane1.getText());
It binds xmlDoc, which is EditorCookie.getDocument(), to doc variable and evaluates the script which is in jEditorArea.
Maybe a Groovy could be a new way for extending Netbeans? Tell me what you think.
Saturday, August 23, 2008
SubnetCalculator JM2E
Download: jar and source code
I also found some interesting stuff: my antivirus (Avira AntiVir) software is reporting that Java Wireless Toolkit is infected by "BDS/Backdoor.Gen" back-door program when I try to run emulator.exe.
Monday, June 2, 2008
Why I haven't been blogging much lately
Well, I was occupied with school and work. Last two months I was programming my diploma work :) and next week I have final exam. I just wanted to say hello and show you what I did using Netbeans.
What you see is an interface of program for modeling the spatial and temporal distribution of concentration changes caused by the diffusion and chemical reactions. I'm specially proud on feature which shows the model in 3D while you type the code. Something like JavaFX editor.
Some technologies I struggled with:
- Web Service
- XML/XSD
- OpenGL
Now I have to learn for next week.
Bye :)
Saturday, March 29, 2008
Netbeans Platform based Web Service client
interface com.sun.xml.ws.developer.WSBindingProvider is not visible from class loader
Unable to create Provider: com.sun.xml.ws.spi.ProviderImpl cannot be cast to javax.xml.ws.spi.Provider
There is no exact tutorial (for Netbeans 6.x and JAX-WS 2.1) describing how to invoke web service from Netbeans Platform application.
So here are steps:
- Create web service and client using this tutorial
- Create library wrapper module for web service client (you don't need to include JAX-WS libs, only your client jar)
- In your wrapper module add following dependencies (important):
- JAX-WS 2.1 API
- JAX-WS 2.1 and JAXB 2.1 Library (for this you have to check Show Non-API Modules in "Add Module Dependency" window)
If you try to build module after these steps it will fail telling you that your module is not friend of "path-to-netbeans"/java2/modules/org-netbeans-modules-websvc-jaxws21.jar
- Right click on JAX-WS 2.1 and JAXB 2.1 Library and choose Edit. Select Implementation Version.
Tuesday, March 4, 2008
Sunday, February 24, 2008
File opening: EditorCookie or OpenCookie?
FileSystem fs = FileUtil.createMemoryFileSystem(); FileObject fob = fs.getRoot().createData(name, "txt"); DataObject data = DataObject.find(fob); EditorCookie cookie = (EditorCookie)data.getCookie(EditorCookie.class); cookie.open();Feel free to comment.
Tuesday, February 12, 2008
Monday, February 4, 2008
XPath evaluator module for Netbeans
I do a lot XML and Xslt processing at my workplace. Altova XmlSpy has XPath tool for evaluating queries on edited XML file. I'm missing that (useful) feature in Netbeans so I decided to build something similar. Of course it's far away from Altova's version in features but it can be useful (and you can always extend it).
- New project: Choose File > New Project (Ctrl-Shift-N). Under Categories, select NetBeans Modules. Under projects, select Module and click Next.
- In the Name and Location panel, type XPathEvaluator in Project Name. Change the Project Location to any directory on your computer, such as /home/damir/Projects/netbeans. Leave the Standalone Module radio button selected.
- In the Basic Module Configuration panel, replace yourorghere in Code Name Base with netbeans.modules, so that the whole code name base is org.netbeans.modules.xpathevaluator. Leave the location of the localizing bundle and XML layer, so that they will be stored in a package with the name org/netbeans/modules/xpathevaluator.
- Click finish.
In project tree right click on Libraries Node > Add module dependency...
-
Import Datasystems API by typing datasystems
It contains XmlDataObject which we will listen for.
- New file (Ctrl-N). Under Categories, select Module Development. Under file types, select Window Component and click Next.
- Window Position: output
- Leave Open on Application start unchecked
- Class Name Prefix: XPathEvaluator
- Package: org.netbeans.modules.xpathevaluator
- Click finish
- 2x JLabel
- 1x JTextField for XPath input
- 1x JTextArea for XPath results
- New file (Ctrl-N). Under Categories, select Module Development. Under file types, select Action and click Next.
- Check conditionally enabled and choose EditorCookie for cookie class
- GUI registration:
- Category: XML
- Uncheck Global Menu Item
- Check Editor Context Menu and choose text/xml for content type, change position to Tools - HERE
- Check Separator before and Separator after
- Name, Icon, and Location:
- Class Name: ShowXPathEvaluatorAction
- Display Name: XPath Evaluator
- Package Name: org.netbeans.modules.xpathevaluator
- Click finish
PerformAction method should look like this:
protected void performAction(Node[] activatedNodes) { EditorCookie editorCookie = activatedNodes[0].getLookup().lookup(EditorCookie.class); try { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(true); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse(((org.openide.text.CloneableEditorSupport) editorCookie).getInputStream()); XPathEvaluatorTopComponent win = XPathEvaluatorTopComponent.findInstance(); win.setDocument(doc); win.open(); win.requestActive(); } catch (SAXException ex) { //Exceptions.printStackTrace(ex); } catch (ParserConfigurationException ex) { //Exceptions.printStackTrace(ex); } catch (IOException ex) { //Exceptions.printStackTrace(ex); } }Press Ctrl+I to fix imports. Make sure you select org.w3c.dom.Document from combo box.
We have to:
- create private member which will hold org.w3c.dom.Document from selected editor and on which we will execute XPath.
- create setDocument(org.w3c.dom.Document doc) method for setting current xml document.
- create evaluate() method for executing and displaying XPath query.
- add DocumentListener to jTextField1 for executing XPath query as we type.
- listen for changes in TopComponent selection (when user opens/activates/closes other xml or non xml panel).
This method is simple. It evaluates text from jTextField1 control and fills jTextArea1 control with results after clearing the same. If there were errors it shows them at bottom of window (jLabel2). xmlDoc is current xml document.
private void evaluate() { if (xmlDoc != null) { try { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); XPathExpression expr = xpath.compile(jTextField1.getText()); NodeList nodes = (NodeList) expr.evaluate(xmlDoc, XPathConstants.NODESET); jTextArea1.setText(""); for (int i = 0; i < nodes.getLength(); i++) { jTextArea1.setText(jTextArea1.getText() + nodes.item(i).getNodeValue() + "\n"); } } catch (XPathExpressionException ex) { jLabel2.setText(ex.getMessage()); } } }
Append following code at the end of XmlPathEvaluatorTopComponent's constructor:
jTextField1.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { evaluate(); } public void removeUpdate(DocumentEvent e) { evaluate(); } public void changedUpdate(DocumentEvent e) { } });
Following code is based on Netbeans Selection Managment Tutorial I. We have to listen for global selection changes of "Object". In our case it is XmlDataObject. We have to extend our TopComponent to implement LookupListener.
final class XPathEvaluatorTopComponent extends TopComponent implements LookupListenerLookupListener provides a method for handling changes (resultChanged(LookupEvent ev)). We have to check if there are any instances of XmlDataObject selected and change xmlDoc value according to that.
- If there are any XmlDataObject instances (in this case only one) then we have set xmlDoc member's value to selected XmlDataObject's XmlDocument and we have to enable input in window.
- If there are not any instances then we have to set it to null and to disable input on window.
To avoid this we have to check if our window (XPathEvaluator) is selected.
public void resultChanged(LookupEvent arg0) { // if this window selected then do nothing if (TopComponent.getRegistry().getActivated().equals(this)) { // do nothing } else { Lookup.Result r = (Lookup.Result) arg0.getSource(); Collection c = r.allInstances(); // if there are instances if (!c.isEmpty()) { try { XMLDataObject o = (XMLDataObject) c.iterator().next(); setDocument(o.getDocument()); evaluate(); jLabel2.setText(o.getName()); jTextArea1.setEnabled(true); jTextField1.setEnabled(true); } catch (IOException ex) { jLabel2.setText(ex.getMessage()); } catch (SAXException ex) { jLabel2.setText(ex.getMessage()); } } else { xmlDoc = null; jTextArea1.setEnabled(false); jTextField1.setEnabled(false); jLabel2.setText("no selection"); } } }Now we have to register LookupListener. We will do that when window is opened:
@Override public void componentOpened() { Lookup.Template tpl = new Lookup.Template(org.openide.loaders.XMLDataObject.class); result = Utilities.actionsGlobalContext().lookup(tpl); result.addLookupListener(this); }When we close window we have to unregister the listener:
@Override public void componentClosed() { result.removeLookupListener(this); result = null; }We have to create a member for holding the result where lookup listener is registered. You can find explanation for this in Netbeans Selection Managment Tutorial I.