Friday, August 21, 2009

Persisting JXTable's Column Control Settings

Here is how to persist (save/restore) the users settings (ordering of columns, visbility etc) of a JXTable using the Swing Application Framework aka SAF (JSR-296). You will need to download the jar for SAF to get the stuff below working.

(Notice that all of this is inspirede by NetBeans Platform meets Swing Application Framework: SessionStorage but without all the NetBeans clutter.)

/**
*
* @See http://puces-blog.blogspot.com/2009/04/netbeans-platform-meets-swing.html
*/

public class ModuleApplicationContext extends ApplicationContext {

private String storageDirectoryPath = "";

static {

// download from https://jdnc-incubator.dev.java.net/source/browse/jdnc-incubator/trunk/src/kleopatra/java/org/jdesktop/appframework/swingx/XProperties.java?rev=3198&view=markup
new XProperties().registerPersistenceDelegates();
}

public ModuleApplicationContext(String path) {
// Needed due to issue
// https://appframework.dev.java.net/issues/show_bug.cgi?id=112
setLocalStorage(new ModuleLocalStorage(this));
// getLocalStorage().setDirectory(getModuleSessionStorageDir(moduleInfo));
storageDirectoryPath = path;
getLocalStorage().setDirectory(new File(storageDirectoryPath));
getSessionStorage().putProperty(JXTable.class,
new XProperties.XTableProperty());
}

}



/**
* A LocalStorage for modules. It respects the direcory property in JNLP mode.

*

* Needed due to issue * HREF="https://appframework.dev.java.net/issues/show_bug.cgi?id=112">
* https://appframework.dev.java.net/issues/show_bug.cgi?id=112
*
* @author puce
*/

public class ModuleLocalStorage extends LocalStorage {

public ModuleLocalStorage(ApplicationContext context) {
super(context);
}

@Override
public boolean deleteFile(String fileName) throws IOException {
File path = new File(getDirectory(), fileName);
return path.delete();
}

@Override
public InputStream openInputFile(String fileName) throws IOException {
File path = new File(getDirectory(), fileName);
return new BufferedInputStream(new FileInputStream(path));
}

@Override
public OutputStream openOutputFile(String fileName) throws IOException {
File path = new File(getDirectory(), fileName);
return new BufferedOutputStream(new FileOutputStream(path));
}
}


Then you can use the ModuleApplicationContext to save the column control settings of a JXTable doing something like below:


public class TestJXTable extends JFrame {

String data[][] = { { "John", "Sutherland", "Student" },
{ "George", "Davies", "Student" },
{ "Melissa", "Anderson", "Associate" },
{ "Stergios", "Maglaras", "Developer" }, };

String fields[] = { "Name", "Surname", "Status" };

ModuleApplicationContext mac;

JXTable jt;

JScrollPane pane;

public static void main(String[] argv) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TestJXTable myExample = new TestJXTable(
"JXTable Example");
}
});
}

public TestJXTable(String title) {
super(title);
// save settings in users home dir
mac = new ModuleApplicationContext(System.getProperty("user.home"));
setSize(150, 150);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
try {
jt.getParent().remove(jt);
mac.getSessionStorage().save(jt, "testTable.xml");
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
});
init();
pack();
setVisible(true);
}

private void init() {
jt = new JXTable(data, fields);
jt.setColumnControlVisible(true);
jt.setName("testTable");
pane = new JScrollPane(jt);
pane.setName("testPane");
try {
mac.getSessionStorage().restore(jt, "testTable.xml");
} catch (IOException e) {
e.printStackTrace();
}
getContentPane().add(pane);
}
}

5 comments:

Benjamin Winterberg said...
This comment has been removed by the author.
Benjamin Winterberg said...

The Swing Application Framework will not be part of the JDK 7.0 (see here).

Carsten said...

I know but I guess you can still use SAF in its current state to do the, otherwise tedious, work of saving the internal state of the JXTable column control.

Illya Yalovyy said...

Or you could start using one of it's forks.

More details at http://kenai.com/projects/bsaf

Anonymous said...
This comment has been removed by a blog administrator.

Live Traffic Map