Konstantin has posted 11 posts at DZone. View Full User Profile

NetBeans Platform Tip: Restore windows after restart

10.03.2008
| 8064 views |
  • submit to reddit
In last version 0.3.2 of Epictetus I've added new feature - restore opened windows and content after application restart. NetBeans Platform allows you to implement this feature in three easy steps.

Step 1. To save or not to save.

Look at the method getPersistenceType() of your TopComponent. This method returns one these values: PERSISTENCE_NEVER, PERSISTENCE_ONLY_OPENED, PERSISTENCE_ALWAYS. When application is closing platform checks all TopComponents in registry and according to the value of this method serialize them or not.
PERSISTENCE_NEVER - do not store and restore this component.
PERSISTENCE_ONLY_OPENED - restore it if component is opened in Mode.
PERSISTENCE_ALWAYS - always restore
Set persistence type to PERSISTENCE_ONLY_OPENED or PERSISTENCE_ALWAYS and your application will restore windows and even focus on the last active.

Step 2. Save your data.

For example, you have an editor inside your TopComponent and you want to restore text and caret position after start. In that case you need to override method writeExternal(ObjectOutput objectOutput).

    @Override
public void writeExternal(ObjectOutput objectOutput) throws IOException
{
objectOutput.writeInt(editor.getCaretPosition());
String text = editor.getText();
objectOutput.writeUTF(text != null ? text : "");
}

Step 3. Load your data.

Override method readExternal(ObjectInput objectInput) and read all saved data one by one:
    @Override
public void readExternal(ObjectInput objectInput) throws IOException, ClassNotFoundException
{
try
{
int position = objectInput.readInt();
editor.setText(objectInput.readUTF());
editor.setCaretPosition(position);
}
catch (IOException e)
{
System.out.println("Can't restore previously saved component " + getDisplayName());
}
}

You can make empty catch block and swallow exception but I wouldn't recomend that.


Remember, all restored windows are deserialized from application configuration directory. If you change the code of your TopComponent and then run the application the restored windows won't get your changes until you reopen them.
0
Your rating: None
Published at DZone with permission of its author, Konstantin Chikarev.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Eder Freitas replied on Mon, 2011/05/09 - 11:53am

Hi, I know that following this tutorial http://netbeans.dzone.com/tips/netbeans-platform-tip-restore- you can save window positions, but how can I manipulate them, ie when I want to open the positioning of these windows from file windows2local can I do this without being tied to the platform?

 

Thank you

Matt Coleman replied on Wed, 2012/12/19 - 12:52am

saving data is always a great thing,,.what can be lost can be valuable

buffalo ny SEO 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.