Problem with saving/restoring layout

Hi,
here is my modest code :

        frontend = new DockFrontend(frame);
        frame.destroyOnClose(frontend);
        frontend.getController().setTheme(new NoStackTheme(new SmoothTheme()));
       
        station = new SplitDockStation();
        frame.add(station);       
        
        frame.addWindowListener(new WindowAdapter() {
            // Invoked when a window is in the process of being closed.
            // The close operation can be overridden at this point.
            @Override
            public void windowClosing(WindowEvent e) {
                Logger.getLogger(PersistentLayoutExample.class.getSimpleName()).log(Level.WARNING, "Window Closing Event", "Window Closing Event");
                /** saving the flagged (AnnSave) properties **/
                PreferenceMgr.getPersistenceMgr().save();
            }
        });

        frontend.addRoot("layout", station);        
         
        /* Prepare the Dockables we are going to put onto "station" */
       
        Dockable magenta = new ColorDockable("Magenta", Color.MAGENTA, 2.5f);

        /* Never forget to register all Dockables at the DockFrontend */
        
        frontend.addDockable("magenta", magenta);
       
        /* Now we tell the framework that our Dockables can be closed */
        frontend.setHideable(magenta, true);
        
        /* Adding the Dockables to "station" */
        grid = new SplitDockGrid();
       
        grid.addDockable(60, 30, 40, 70, magenta);        

        station.dropTree(grid.toTree());

        /* We build a menu that allows us to call the different methods easily */
        JMenu menu = new JMenu("Layout");

        /* The ReadAction calls DockFrontend.readXML */
        ReadAction read = new ReadAction(frontend, frame);
        menu.add(read);

        /* The WriteAction calls DockFrontend.writeXML */
        menu.add(new WriteAction(frontend, frame, station));

        /* We build a menu that allows us to call the different methods easily */
        JMenu window = new JMenu("Window");
        window.add(new AddAction(frontend, grid, station));

        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu);
        menuBar.add(window);

        frame.setJMenuBar(menuBar);
        frame.setVisible(true);```
then the actions :
add
```/* Let's create one Dockables */
        //Dockable red = new MyColorDockable("My Window " + (i++), Color.RED);
         Dockable red = new ColorDockable("Red", Color.RED, 2.5f);
        /* We need to register our Dockable. Each Dockable gets associated with a 
         * unique identifier. */
        frontend.addDockable("Red"+System.currentTimeMillis(), red);
        
        /* Now we tell the framework that our Dockables can be closed */
        frontend.setHideable(red, true);      
       
        grid.addDockable(60, 30, 40, 70, red);
        station.addDockable(red);       ```
read


/* And now we can safely apply the old layouts */
if (inputStream != null) {
XElement xroot = XIO.read(parseISToString(inputStream));
frontend.readXML(xroot);
}
inputStream.close();```
write

                       
            output = new FileOutputStream(file.getAbsolutePath());
            frontend.writeXML(xroot);
            String st  = xroot.toString();
            output.write(st.getBytes());
            output.flush();
            output.close();```
When the Dockables are built from the main it works fine save/load but those built from the "Add" action are never loaded back to the frame :-(
what am i doing wrong .

You mean, you close the application, right? Obviously the framework cannot just guess to find out what kind of class „Red1234164156146554“ was and what its content was… :wink:

When using a DockFrontend you only have one option: you need to install a backup-DockFactory that is able to create new „red“ Dockables. In this case a subclass of DefaultDockableFactory is enough, because a ColorDockable is a DefaultDockable, and because we do not need to store any settings. But in a real application you might need to completely implement the DockFactory interface.

			@Override
			public DefaultDockable layout( Object layout, PlaceholderStrategy placeholders ){
				return new RedDockable( "Red" );
			}
		}, true ); // don't forget the "true" at the end!

In order for the framework to associate the „red“ Dockables with the factory, both the factory and the Dockable must share the same „factory id“. So you need to override the original Dockable:

		public RedDockable( String title ){
			super( title, Color.RED, 2.5f );
		}
		
		@Override
		public String getFactoryID(){
			return "red factory";
		}
	}```

The framework does not store your configuration, but with a little listener you are informed when a Dockable shows up and can reconfigure it:
```		frontend.addFrontendListener( new DockFrontendAdapter(){
			public void added( DockFrontend frontend, Dockable dockable ){
				if( dockable.getFactoryID().equals( "red factory" )){
					frontend.setHideable( dockable, true );
				}
			}
		} );```


As usual using Common makes things easier: you could add a SingleCDockableFactory to the CControl, these factories have only one method, and the framework decides depending on the identifier which factory to take. Or you could use a MultipleCDockable which has the added bonus of automatic identifier creation and automatic cleanup once a Dockable is closed.

Thanks Beni i understand better now, i also used the Common instead. Thank’s a lot for your quick and precise answer.