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?



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>


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.