Problem saving and restoring DF resources

Hi,

I am trying to save and persist the DF resources using:

CControl control;
control.getResources().writeFile(file);

Then when application restarts, I load the resources using:

control.getResources().readFile(file);
bIsRestored = true;

and then:

control.add( sourceTreeDockable );
control.add( projInfoDockable );
control.add( scanReportDockable );
control.add( sourceViewerDockable );
control.add( statusDockable );

if (bIsRestored) {
	sourceTreeDockable.setVisible(true);
	projInfoDockable.setVisible(true);
	scanReportDockable.setVisible(true);
	sourceViewerDockable.setVisible(true);
	statusDockable.setVisible(true);
} else {
	CGrid grid = new CGrid( m_control );
	
	grid.add( 0, 0, 0.2, 1.0,  sourceTreeDockable );
	grid.add( 0, 1, 0.2, 1.0,  projInfoDockable );
	grid.add( 1, 0, 1.0, 0.9,  scanReportDockable );
	grid.add( 1, 1, 1.0, 0.9,  sourceViewerDockable );
	grid.add( 1, 2, 1.0, 0.2,  statusDockable );
	
	m_control.getContentArea().deploy( grid );
}

But in first setVisible(true), I am getting NPE:

java.lang.NullPointerException
at bibliothek.gui.dock.facile.action.StateManager.normalize(Unknown Source)
at bibliothek.gui.dock.facile.action.StateManager.change(Unknown Source)
at bibliothek.gui.dock.facile.action.StateManager.change(Unknown Source)
at bibliothek.gui.dock.facile.action.StateManager.transition(Unknown Source)
at bibliothek.gui.dock.support.action.ModeTransitionManager.goIn(Unknown Source)
at bibliothek.gui.dock.support.action.ModeTransitionManager.setMode(Unknown Source)
at bibliothek.gui.dock.common.intern.CStateManager.setMode(Unknown Source)
at bibliothek.gui.dock.common.intern.CStateManager.ensureValidLocation(Unknown Source)
at bibliothek.gui.dock.common.CControl$Access.show(Unknown Source)
at bibliothek.gui.dock.common.intern.AbstractCDockable.setVisible(Unknown Source)

What am I doing wrong and what is the proper way to restore and display the DF resources/layout?

Thanks for the great docking framework!

Lubos

Whom should I contact to resolve this problem? I have tried to file a bug at DF project on javaforge.com, but it doesn’t let me to file a bug.
I might be doing something wrong when retrieving the stored layout and initializing DF with restored layout.

The NPE is most likely a bug, but I was not able to reproduce it. Could you link the source of the framework to its jar’s, and then rerun the application? The line numbers in the stack-trace could help.

There is another problem here: you need first to add the Dockable to the Control, and then load the layout. Otherwise the framwork cannot place anything (because it does not know of the Dockables).

I always put “control.getResources().readFile(file);” to the very end (and asume “bIsRestored” is false).

That resolved it, and works great now. I tried all the different combination, but not moving the restore at the end and always creating grid.

Maybe a note should be added to documentation instead of tracking down the problem that was caused by me not creating a grid (ad deploying it) and trying to make dockable visible.

Thanks for your help! Lubos

Yes, that is a good idea. I’m currently rewriting the guides (the “books”, not the API-documentation) but things like this need to be in the API-doc as well.

Hi there. I can’t load layout from file.
Here’s my code. (I edited the demo code provided from docking frames web site.)

import java.awt.Color;

import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;

import bibliothek.gui.dock.common.*;
import bibliothek.gui.dock.common.menu.SingleCDockableListMenuPiece;
import bibliothek.gui.dock.facile.menu.RootMenuPiece;
import java.awt.event.WindowAdapter;
import java.io.File;

public class Dock {

    public static void main(String[] args) {
        //File outFile = new File("xml.out");
        final File inFile = new File("xml.in");

        JFrame frame = new JFrame("Demo");
        final CControl control = new CControl(frame);

        frame.add(control.getContentArea());

        CGrid grid = new CGrid(control);
        control.getContentArea().deploy(grid);

        SingleCDockable black = createDockable("Black", Color.BLACK);
        SingleCDockable red = createDockable("Red", Color.RED);
        SingleCDockable green = createDockable("Green", Color.GREEN);
        SingleCDockable blue = createDockable("Blue", Color.BLUE);
        SingleCDockable yellow = createDockable("Yellow", Color.YELLOW);
        
        RootMenuPiece menu = new RootMenuPiece("Colors", false);
        menu.add(new SingleCDockableListMenuPiece(control));
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(menu.getMenu());
        frame.setJMenuBar(menuBar);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(20, 20, 400, 400);
        frame.setVisible(true);

        boolean isRestored = false;
        try {
            control.getResources().readFile(inFile);
            System.out.println("restored");
            isRestored = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        control.add(black);
        control.add(red);
        control.add(green);
        control.add(blue);
        control.add(yellow);
        if (isRestored) {
            black.setVisible(true);
            red.setVisible(true);
            green.setVisible(true);
            blue.setVisible(true);
            yellow.setVisible(true);
        } else {
            black.setLocation(CLocation.base().minimalNorth());
            black.setVisible(true);
            grid.add(0, 0, 1, 1, red);
            grid.add(0, 1, 1, 1, green);
            grid.add(1, 0, 1, 1, blue);
            grid.add(1, 1, 1, 1, yellow);
        }

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                try {
                    control.getResources().writeFile(inFile);
                    System.out.println("written file");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public static SingleCDockable createDockable(String title, Color color) {
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBackground(color);
        DefaultSingleCDockable dockable = new DefaultSingleCDockable(title, title, panel);
        dockable.setCloseable(true);
        return dockable;
    }
}

Edit: Oh sorry. That was my mistake.
I moved following code to line before reading file.


control.add(black);
control.add(red);
control.add(green);
control.add(blue);
control.add(yellow);