In our application we store the settings in an xml file. This works fine, but when ever a new SingleCDockable is added, it wont appear on screen. The reason for that new dockables and invisible dockables are handled the same way. Non-closable dockable arn’t in the ClosableList and can’t be made visible by the user.
My solution is to extend the xml and store the id’s of all SingleCDockables. When restoring the settings, I go throug the list of known dockables and make all new dockables visible:
public class ExampleApp extends JFrame {
private static final long serialVersionUID = -8284759244434428348L;
public static class SingleDockable extends DefaultSingleCDockable {
public SingleDockable(int id, boolean closeable) {
super(SingleDockable.class.getSimpleName() + String.valueOf(id), SingleDockable.class.getSimpleName() + " - " + id);
add(new JLabel(SingleDockable.class.getSimpleName() + " - " + id), BorderLayout.CENTER);
setCloseable(closeable);
}
}
private final CControl fControl;
public ExampleApp() {
fControl = new CControl(this);
CGrid grid = new CGrid(fControl);
grid.add(0, 0, 1, 1, new SingleDockable(1, true));
grid.add(1, 0, 1, 1, new SingleDockable(2, true));
grid.add(0, 1, 1, 1, new SingleDockable(3, false));
grid.add(1, 1, 1, 1, new SingleDockable(4, false));
//grid.add(0, 2, 2, 1, new SingleDockable(5, false)); //NEW DOCKABLE
fControl.getContentArea().deploy(grid);
getContentPane().add(fControl.getContentArea());
JMenuBar menuBar = new JMenuBar();
RootMenuPiece rootPiece = new RootMenuPiece("Closables", true, new SingleCDockableListMenuPiece(fControl));
menuBar.add(rootPiece.getMenu());
setJMenuBar(menuBar);
setBounds(0, 0, 800, 600);
}
public void loadSettings(File file) {
try {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
try {
XElement element = XIO.readUTF(in);
fControl.readXML(element.getElement("controlSettings"));
// handleNewDockables(element.getElement("knownDockables"));
} catch (IOException e) {
in.close();
}
} catch (RuntimeException e) {
//ignore save new file
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void handleNewDockables(XElement element) {
Set<String> knownIds = new HashSet<String>();
for (XElement dockable: element.getElements("dockable")) {
XAttribute dockableId = dockable.getAttribute("id");
knownIds.add(dockableId.getValue());
}
for (int i = 0; i < fControl.getCDockableCount(); i++) {
CDockable cDockable = fControl.getCDockable(i);
if (cDockable instanceof SingleCDockable) {
SingleCDockable singleDockable = (SingleCDockable)cDockable;
if (knownIds.contains(singleDockable.getUniqueId())) continue;
handleNewDockable(singleDockable);
}
}
}
private void handleNewDockable(SingleCDockable singleDockable) {
singleDockable.setVisible(true);
}
public void saveSettings(File file) {
XElement root = new XElement("root");
fControl.writeXML(root.addElement("controlSettings"));
writeKnownDockables(root.addElement("knownDockables"));
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
XIO.writeUTF(root, out);
} catch (IOException e) {
out.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void writeKnownDockables(XElement element) {
for (int i = 0; i < fControl.getCDockableCount(); i++) {
CDockable cDockable = fControl.getCDockable(i);
if (cDockable instanceof SingleCDockable) {
SingleCDockable singleDockable = (SingleCDockable)cDockable;
element.addElement("dockable").addString("id", singleDockable.getUniqueId());
}
}
}
public static void main(String[] args) {
final File settingsFile = new File("exampleSettings.xml");
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final ExampleApp exampleApp = new ExampleApp();
exampleApp.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
exampleApp.setVisible(true);
if (settingsFile.exists()) exampleApp.loadSettings(settingsFile);
exampleApp.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exampleApp.saveSettings(settingsFile);
}
});
}
});
}
}
Is there better way?
Best Regards
Heiko