Pages: 1
Print
Author Topic: use Java API to check running XMetaL  (Read 4055 times)
liujason
Member

Posts: 4


« on: October 08, 2010, 02:59:43 PM »

Hi,

Is it possible to use Java API to check if there is a running XMetaL? So if there is a running XMetaL, get the Application object from the running XMetaL.

Also, when XMetaL closes, is there a way the Application object will release itself?

Thanks,

Jason
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2580



WWW
« Reply #1 on: October 12, 2010, 06:09:04 PM »

I think the solution you are looking for is in the demo that ships with XMetaL Developer. The demo contains code to gracefully fail when XMetaL Author cannot be instantiated using COM.

The demo can be kicked off by selecting (from the Windows Start menu) Start > All Programs > XMetaL > Developer > Java API Toolkit Demo

With a standard installation of XMetaL Developer 6.0 on XP the file you need to have a look at is located here:

C:\Program Files\XMetaL 6.0\Developer\Jat\Demo\XMDemoFrame.java

The code that handles this is just a standard Java try...catch as follows:

Code:
  /**Construct the frame*/
  public XMDemoFrame() {
    // try to initialize the XMetaL application object; abandon program if there
    // is a problem instantiating XMetaL
    try {
      xmetal = new Application();
  xmetal.PreventExit("JavaAPI Demo", "XMetaL cannot be closed because the Java API Demo is running. Close the Java API Demo first, and then close XMetaL.");
    }
    catch (Exception ex) {
      System.err.println("Execution terminated due to the following error:");
      ex.printStackTrace();
      System.exit(0);
    }

    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

That works for total failure. If you want to check specifically for an existing running copy then you should be able to just check to see if an Application object returns anything (in another try...catch). Calling Application.Documents should give you that. If it fails then you would assume there is no running instance. You might also use the PreventExit() API and force your users to always launch and shut down XMetaL Author through your UI. That would eliminate the need to check for an existing running instance from Java (as in the demo).

You could also try to do something else if it fails (offer to launch it, display a dialog with a nice error perhaps, or in the case of the former offer to take the user to your company's software storage location so they can install XMetaL Author, give the phone number for your IT dept, etc) but the basic logic would be similar.
« Last Edit: October 12, 2010, 06:17:13 PM by Derek Read » Logged
liujason
Member

Posts: 4


« Reply #2 on: October 13, 2010, 07:39:38 PM »

Thanks for the reply Derek.

xmetal=new Application() actually launches an XMetaL Author instance in my test case.
As soon as the following object initiated, constructor creates an xmetal instance, XMetaL Author is launched.

Code:
public class XMetaLWorker {
    Application xmetal=null;
    public XMetaLWorker(){
        try{
            xmetal=new Application();
            xmetal.PreventExit("<Prolog> Editor", "XMetaL cannot be closed because the <Prolog> Editor is running. Close the <Prolog> Editor first, and then close XMetaL.");
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}

I didn't really want to launch the application, I wanted it to check if an XMetaL instance is running. If there is one running, it will proceed working with the running XMetaL, otherwise, proceed with other business logic.

Maybe there is another way of achieving this? (XMetaL(R) Author Enterprise 5.5)

Also, there is another problem. Even I have closed my Java application, XMetaL still displays a notice saying it is running. That is very odd.

Thanks,

Jason
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2580



WWW
« Reply #3 on: October 14, 2010, 05:57:02 PM »

Interesting, so it sounds like your requirement is really to ask XMetaL if it is running when it is not running (which is not going to be possible). Do I understand correctly?

I think you will need try to figure out a way to ask the operating system if it has a process called xmetal60.exe (or other version) running, or perhaps there is a 3rd party "process spying" tool or Java JAR that will let you do that. Such as solution would be the same as for any other software I think, so if you could get it working for something simple like notepad.exe then it should work for XMetaL as well.

I think for your other issue (when you close your Java application) if you want XMetaL Author to be shut down at the same time you will need to call Application.Quit().
Logged
liujason
Member

Posts: 4


« Reply #4 on: October 14, 2010, 06:08:14 PM »

Hi Derek,

Right, I might have to check if xmetal[0-9]{2}.exe is running. That is one way of doing it for sure.

To my other question, no, I didn't want to close XMetaL. Users would want to have their order of closing applications.

1. close the tool, keep working in XMetaL, close XMetaL - XMetaL should completely close (no hanging session).

2. close XMetaL, Java Tool gets a call back (via any listener?), and do some clean up, and reset to initial state (or shut down).

Are those two scenarios possible?

Thanks!

Jason
Logged
Pages: 1
Print
Jump to:  

email us