Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Open Handler, when is it called?



Another approach would be just to open the document from within the MRJOpenApplicationHandler. That way you don't have to worry about it when you launch your application, you can just wait for the event to tell your app to open a new document.

Another thing to consider: how will your application work when not (*gasp*) running under OS X. (I usually just have conditional code based on os.name, or somesuch, myself.)

Thanks for asking the question, and thanks everyone for the discussion. I now know a lot more about how to make my Java apps play nicely with the OS.

Cheers,
-Ian

On Tuesday, May 15, 2001, at 12:25 PM, Steve Roy wrote:

Steve Roy <email@hidden> wrote:

How did you determine that you needed to show an untitled document?

Thank you to everyone who shared a bit of their wisdom, in here and in
private. I ended up doing something that seems fullproof. I am sharing the
basic technique as it could benefit everyone.

What I did is register an MRJOpenApplicationHandler and an
MRJOpenDocumentHandler. I have a flag called launchedWithDocument which is
set to true when the MRJOpenDocumentHandler is called and set to false when
MRJOpenApplication is called. This allows me to know if I should open a
blank document or not.

Now, the trick was to make sure that, whenever I take the decision whether
to open a blank document or not, I have all the information I need to make
such a decision. I do this with a flag called openHandlerCalled, initially
set to false, that is set to true whenever one of MRJOpenApplicationHandler
or MRJOpenDocumentHandler is called.

At the start of my application, I initialize the things I need (including
the two open handlers), then I enter a loop which calls Thread.sleep() until
the openHandlerCalled flag is set to true. When the loop exits, I am 100%
certain if I need to open a blank document or not. :)

A stripped down implementation follows.

Usage: new Application().start();

public class Application implements Runnable
{
private boolean openHandlerCalled = false;
private boolean launchedWithDocument = false;

public Application()
{
if (MRJApplicationUtils.isMRJToolkitAvailable())
{
// Register the open application handler for Mac OS
MRJApplicationUtils.registerOpenApplicationHandler(new
MRJOpenApplicationHandler()
{
public void handleOpenApplication()
{
openHandlerCalled = true;
openApplication();
}
});

// Register the open document handler for Mac OS
MRJApplicationUtils.registerOpenDocumentHandler(new
MRJOpenDocumentHandler()
{
public void handleOpenFile(File file)
{
openHandlerCalled = true;
launchedWithDocument = true;
openDocument(file);
}
});
}
}

/**
* Start the application. This method ensures proper handling of
documents
* to be opened (if double-clicked or dragged onto the application
icon).
* It then calls the <code>run()</code> method where the application can
* take over. Do not override this method unless you know what you're
doing.
*/
public void start()
{
if (MRJApplicationUtils.isMRJToolkitAvailable())
{
// Wait for the open application or open document event, so we
// know if we should open a new document or not later
try
{
while (!openHandlerCalled)
Thread.sleep(10);
}
catch (InterruptedException e)
{
// Nothing to do, just resume execution
}
}

// Let the application take over
run();
}

/**
* Run the application. This method should be overriden by the subclass.
* It should never be called directly; use the <code>start()</code>
method
* instead. The default <code>run()</code> method does nothing.
*/
public void run()
{
if (!isLaunchedWithDocument())
{
// Open a new blank document
}
}

protected void openApplication()
{
}

protected void openDocument(File file)
{
// Open the document
}

public boolean isLaunchedWithDocument()
{
return launchedWithDocument;
}
}

Steve

--
Steve Roy <email@hidden>
_______________________________________________
java-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/java-dev


References: 
 >Re: Open Handler, when is it called? (From: Steve Roy <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.