Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleHelloWorldPlugin.java
borderStylesolid
package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import org.freehep.application.studio.Plugin;

/**
 * An example of how to write a JAS3 extension. Phase 1.
 * @author tonyj
 */

public class HelloWorldPlugin extends Plugin implements ActionListener
{ 
   protected void init() throws Throwable
   {
      // Create the menu item
      JMenuItem item = new JMenuItem("Hello World");
      item.addActionListener(this);
      // Add an item to the help menu
      addMenu(item,900900900901);
   }

   public void actionPerformed(ActionEvent e)
   {
      // Popup a message dialog. 
      // The dialog is parented to the main application (JAS3).
      JOptionPane.showMessageDialog(getApplication(),"Hello World!");
   }
}

To use this class as an extension you will also need to create a plugins.xml file which describes your extension to JAS3. Here is an example plugins.xml file for the HelloWorld extension. This file must be saved in a directory called PLUGIN-inf (please create it).

Code Block
xml
xml
borderStylesolid
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plugins SYSTEM "http://java.freehep.org/schemas/plugin/1.0/plugin.dtd">

<plugins>
    <plugin>
      <information>
          <name>HelloWorld</name>
          <author>Tutorial User</author>
          <version>1.0</version>
          <description kind="short">HelloWorld extension</description>
          <description>Illustrates how to write a plugin.</description>
          <load-at-start/> 
      </information>
      <plugin-desc class="test.HelloWorldPlugin"/>
    </plugin>
</plugins>

...

Code Block
titleHelloWorldPlugin.java
borderStylesolid
package test;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import org.freehep.application.mdi.PageManager;
import org.freehep.application.studio.Plugin;

/**
 * An example of how to write a JAS3 extension. Phase 2.
 * @author tonyj
 */
public class HelloWorldPlugin extends Plugin implements ActionListener
{
   protected void init() throws Throwable
   {
      // Create the menu item
      JMenuItem item = new JMenuItem("Hello World");
      item.addActionListener(this);
      // Add an item to the help menu
      addMenu(item,900900900901);
   }
   
   public void actionPerformed(ActionEvent e)
   {
      // Get the applications PageManager
      PageManager manager = getApplication().getPageManager();
      // Ask it to open a new page and display our message
      JLabel label = new JLabel("Hello World!");
      label.setFont(new Font("Serif",Font.BOLD,36));
      manager.openPage(label,"Hello World", null);
   }
}

...

Code Block
titleHelloWorldPlugin.java
borderStylesolid
package test;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import org.freehep.application.mdi.PageContext;
import org.freehep.application.mdi.PageEvent;
import org.freehep.application.mdi.PageListener;
import org.freehep.application.mdi.PageManager;
import org.freehep.application.studio.Plugin;
import org.freehep.jas.plugin.console.ConsoleService;

/**
 * An example of how to write a JAS3 extension. Phase 3.
 * @author tonyj
 */
public class HelloWorldPlugin extends Plugin implements ActionListener, PageListener
{
   private PrintWriter log;
   protected void init() throws Throwable
   {
      // Create the menu item
      JMenuItem item = new JMenuItem("Hello World");
      item.addActionListener(this);
      // Add an item to the help menu
      addMenu(item,900900900901);
      
      // Use the console service to open a console
      ConsoleService cs = (ConsoleService) getApplication().getLookup().lookup(ConsoleService.class);
      if (cs != null)
      {
         log = new PrintWriter(cs.getConsoleOutputStream("Log",null),true);
      }
   }
   
   public void actionPerformed(ActionEvent e)
   {
      // Get the applications PageManager
      PageManager manager = getApplication().getPageManager();
      // Ask it to open a new page and display our message
      JLabel label = new JLabel("Hello World!");
      label.setFont(new Font("Serif",Font.BOLD,36));
      PageContext context = manager.openPage(label,"Hello World", null);
      context.addPageListener(this);
      if (log != null) log.println("Opened Page: "+context.getTitle());
   }
   
   public void pageChanged(PageEvent e)
   {
      if (log != null) log.println("PageEvent received "+e);
   } 
}

...

Code Block
titleHelloWorldPlugin.java
borderStylesolid
package test;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintWriter;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import org.freehep.application.mdi.PageContext;
import org.freehep.application.mdi.PageEvent;
import org.freehep.application.mdi.PageListener;
import org.freehep.application.mdi.PageManager;
import org.freehep.application.studio.Plugin;
import org.freehep.application.studio.Studio;
import org.freehep.jas.plugin.console.ConsoleService;

/**
 * An example of how to write a JAS3 extension. Phase 4.
 * @author tonyj
 */
public class HelloWorldPlugin extends Plugin implements ActionListener, PageListener, HelloWorldService
{
   private PrintWriter log;
   private Studio app;
   protected void init() throws Throwable
   {
      app = getApplication();
      // Create the menu item
      JMenuItem item = new JMenuItem("Hello World");
      item.addActionListener(this);
      // Add an item to the help menu
      addMenu(item,900900900901);
      
      // Register the HelloWorldService
      app.getLookup().add(this);
      
      // Use the console service to open a console
      ConsoleService cs = (ConsoleService) app.getLookup().lookup(ConsoleService.class);
      if (cs != null)
      {
         log = new PrintWriter(cs.getConsoleOutputStream("Log",null),true);
      }
   }
   
   public void actionPerformed(ActionEvent e)
   {
      // Look for the HelloWorldService
      HelloWorldService hws = (HelloWorldService) app.getLookup().lookup(HelloWorldService.class);
      if (hws != null) hws.say("Hello World");
      else app.error("HelloWorldService not available");
   }
   
   public void pageChanged(PageEvent e)
   {
      if (log != null) log.println("PageEvent received "+e);
   }
   
   public void say(String message)
   {
      // Get the applications PageManager
      PageManager manager = getApplication().getPageManager();
      // Ask it to open a new page and display our message
      JLabel label = new JLabel(message);
      label.setFont(new Font("Serif",Font.BOLD,36));
      PageContext context = manager.openPage(label,"Hello World", null);
      context.addPageListener(this);
      if (log != null) log.println("Opened Page: "+context.getTitle());
   }
}
interface HelloWorldService
{
   void say(String message);
}