[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

What this mean? embedded function in a constructor

According to the code below:

//==========================================
public class BrowserPanel extends foo {

  private WebBuilder builder;
  private IlrBuilder.Ruleset ruleset = null;
  private IlrBuilder.Rule rule = null;
  private String packetName = null;

  public BrowserPanel(WebBuilder builder) {
    this.builder = builder;
    IlrWJavaAction ruleAction = new IlrWJavaAction()
    {
      public void perform(IlrWPort port, IlrWComponent source, Object [] args) {
        project = (IlrBuilder.Project)args[0];
        IlrBuilder.Rule rule = (IlrBuilder.Rule)args[2];
        getBuilder().setElement(rule);
        getBuilder().updateFrames(port);
      };
    }
    setSelectPacketAction(packetAction);

    ...
}
//======================

After the new IlrWJavaAction(), what mean the declaration of the embedded function perform()?

When this function can be called and what she's doing in the constructor?


Mario
0
mhebert
Asked:
mhebert
  • 2
1 Solution
 
userquinCommented:
Hi:

I do not know anything about IlrWJavaAction class or interface but this example may be obvious:

This example illustrates an example about java.awt.event.ActionListener. As you know (or you must know) ActionListener is an interface to listen button actions. There is a method in button class called addActionListener(ActionListener e) that adds to this button a object that listen to actions performed in this object (button). When a button is clicked (for example), the button (itself) generates an event (ActionEvent) and it calls actionPerformed(ActionEvent e) method of all action listeners added to it self via addActionListener method. So you have a VIEW/CONTROLLER MODEL. Sometimes (when listener has only a method) this listeners are added to the view with anonymous classes so you do not have to create a class to this object. In this example you will have code that look like this:

JButton x = new JButton("...);
x.addActionListener(new ActionListerner() {
  public void actionPerformed(ActionEvent e) {
    ....
    ....
  }
});

When you compile this class, compiler creates an anonynous class called Myclass$1.class. In this case, actionPerformed method will be called when someone clicks the button.

Hope this help.

Bye.

J.
0
 
0xDEADBEEFCommented:
The IlrWJavaAction object ruleAction in fact is an anonymous class derived from IlrWJavaAction. The declaration of the 'void perform' method specifies what to do if ruleAction.perform(bla) is called.

Anonymous classes are used for classes that are only used once in a very special context (such as event handling).
0
 
objectsCommented:
> After the new IlrWJavaAction(), what mean the
> declaration of the embedded function perform()?

It's just defining an (anonymous) inner class.

> When this function can be called and what she's doing in
> the constructor?

The function can be called using:

ruleAction.perform(a, b, c);

In the BrowserPanel ctor she defines an inner class which is a subclass of IlrWJavaAction.
It is identical to the following:

public class BrowserPanel extends foo {

 private WebBuilder builder;
 private IlrBuilder.Ruleset ruleset = null;
 private IlrBuilder.Rule rule = null;
 private String packetName = null;

 public BrowserPanel(WebBuilder builder) {
   this.builder = builder;
   IlrWJavaAction ruleAction = new MyIlrWJavaAction()
   setSelectPacketAction(packetAction);

   ...
  }

  class MyIlrWJavaAction extends IlrWJavaAction
  {
   {
     public void perform(IlrWPort port, IlrWComponent source, Object [] args) {
       project = (IlrBuilder.Project)args[0];
       IlrBuilder.Rule rule = (IlrBuilder.Rule)args[2];
       getBuilder().setElement(rule);
       getBuilder().updateFrames(port);
     };
   }
  }
...

0
 
objectsCommented:
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now