Scripting: Example scripts using external libraries

From Freeplane - free mind mapping and knowledge management software
Revision as of 00:43, 12 March 2013 by Boercher (talk | contribs)

With external Java libraries you can explore completely new application fields with Freeplane and accomplish amazing things. Remember: Groovy is fully Java compatible, i.e. you can use any available Java library.

General installation instructions for external Java libraries

Java libraries normally come in form of .jar files which you have to make known to the application that wants to use them. Here's the way to do that in Freeplane:

1. Create a folder lib in your Freeplane user directory

2. Copy the .jar file to that directory.

3. Start Freeplane and add lib to Preferences -> Plugins -> Scripting -> Script -> Script classpath

4. Save and restart Freeplane

To install further .jar files in the future you just have to copy the .jar files to that lib directory and restart Freeplane.

Database access: Oracle

Test script that shows how to connect to an Oracle database. Thanks to Pascal for asking.

Installation instructions:

1. Save the script to the scripts folder in your Freeplane user directory and edit USERNAME/PASSWORD and the rest of the database url appropriately.

2. Follow the instructions at the beginning of this chapter to install ojdbc14-10.1.0.4.0.jar (or later) to the lib directory in your Freeplane user directory

Required permissions: Execute script, read file, network access. Requires Freeplane 1.2.

<groovy name="databaseTestOracle"> // @ExecutionModes({ON_SINGLE_NODE}) import java.sql.Connection import groovy.sql.Sql import oracle.jdbc.pool.OracleDataSource

// def driver = Class.forName("oracle.jdbc.driver.OracleDriver") // println driver.name

OracleDataSource ds = new OracleDataSource(); ds.setURL("jdbc:oracle:thin:USERNAME/PASSWORD@//localhost:1521/xe") def sql = new Sql(ds.getConnection())

def mviews = node.map.root.createChild("materialized views") sql.eachRow("select * from user_tables where table_name like 'MVIEW%'", { mviews.createChild(it.table_name)} ); </groovy>

Author: User:boercher

Database access: MySQL

Test script that shows how to connect to a MySQL database. Thanks to Pascal and Michel.

Installation instructions:

1. Save the script to the scripts folder in your Freeplane user directory and edit database access properties appropriately.

2. Follow the instructions at the beginning of this chapter to install mysql-connector-java-5.1.17-bin.jar (or similar) to the lib directory in your Freeplane user directory

Required permissions: Execute script, read file, network access. Requires Freeplane 1.2.

<groovy name="databaseTestMysql"> // @ExecutionModes({ON_SINGLE_NODE}) import java.sql.Connection; import groovy.sql.Sql import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

MysqlDataSource ds = new MysqlDataSource();

ds.setServerName("localhost"); ds.setUser("root"); ds.setPassword(""); ds.setDatabaseName("cdcol");

def sql = new Sql(ds.getConnection())

def titles = node.map.root.createChild("Titles") sql.eachRow("select * from cds"){

   titles.createChild(it.title)

} </groovy>

Author: User:megatop, User:boercher

JFreeChart: Diagrams and Charts

Example how to use JFreeChart in Freeplane. Thanks to Michel for asking. See also Gantt chart example.

Installation instructions:

Follow the instructions at the beginning of this chapter to install jfreechart-<version>.jar and jcommon-<version>.jar (download) to the lib directory in your Freeplane user directory

Required permissions: Execute script, read file. Requires Freeplane 1.2.

<groovy name="jfreechartTest"> // @ExecutionModes({ON_SINGLE_NODE}) import org.jfree.chart.ChartFactory import org.jfree.chart.ChartPanel import org.jfree.data.general.DefaultPieDataset import groovy.swing.SwingBuilder import java.awt.* import javax.swing.WindowConstants as WC

def piedataset = new DefaultPieDataset(); piedataset.with {

    setValue "Apr", 10
    setValue "May", 30
    setValue "June", 40

}

def options = [true, true, true] def chart = ChartFactory.createPieChart("Pie Chart Sample",

   piedataset, *options)

chart.backgroundPaint = Color.white def swing = new SwingBuilder() def frame = swing.frame(title:'Groovy PieChart',

       defaultCloseOperation:WC.EXIT_ON_CLOSE) {
   panel(id:'canvas') { widget(new ChartPanel(chart)) }

} frame.pack() frame.show() </groovy>

Author: User:boercher