Maximizing area of SplitDocGrid

Hi,
we have problem with maximizing area of SplitDocGrid after save and load layout.
we are using DockFrontend to Load and Save layouts;*layout is stored in xml-format.
We have a SplitDocGrid with four areas; one of them has two dockables (panels), others have single dockable inside.
In case I maximize some area of SplitDocGrid with single dockable, after re-open (save and load of layout)
I always get maximized the split area with two dockables.

Thanks Pitris.

I would need an example (some code I can compile and run) to check what exactly is going wrong. I don’t remember such a bug, so perhaps it could also be caused by your code.

P.S. DockFrontend? SplitDockGrid? Looks like you are using the Core library. You should know, that the Common library (the one offering CControl and CDockable) is for most clients the much better choice, as it offers a better set of default settings, and some nice additional features.

Thanks for answer. Here is example:

    File xmlFile = null;
    SplitDockStation station;
    DockFrontend frontend;

    SplitDockGrid grid;
    Dockable dockable1;
    Dockable dockable2;
    Dockable dockable3;
    Dockable dockable4;
    Dockable dockable5;

    public DockingFrameTest() {
        setTitle("Test Docking");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);
                dispose();
                System.exit(0);
            }
        });

        setPreferredSize(new Dimension(800, 600));
        init();

        pack();
    }

    private void init() {
        xmlFile = new File("layout.xml");
        station = new SplitDockStation();
        getContentPane().add(station);
        frontend = new DockFrontend();
        frontend.addRoot("station", station);
        frontend.setDefaultStation(station);

        dockable1 = new DefaultDockable(new JLabel("Dockable 1"), "dock1");
        dockable2 = new DefaultDockable(new JLabel("Dockable 2"), "dock2");
        dockable3 = new DefaultDockable(new JLabel("Dockable 3"), "dock3");
        dockable4 = new DefaultDockable(new JLabel("Dockable 4"), "dock4");
        dockable5 = new DefaultDockable(new JLabel("Dockable 5"), "dock5");
        grid = new SplitDockGrid();
        grid.addDockable(0, 0, 60, 50, dockable1);
        grid.addDockable(60, 0, 40, 50, dockable2);
        grid.addDockable(0, 50, 60, 50, dockable3);
        grid.addDockable(60, 50, 40, 50, dockable4);
        grid.addDockable(60, 50, 40, 50, dockable5);

        station.dropTree(grid.toTree());
        frontend.addDockable("dock1", dockable1);
        frontend.addDockable("dock2", dockable2);
        frontend.addDockable("dock3", dockable3);
        frontend.addDockable("dock4", dockable4);
        frontend.addDockable("dock5", dockable5);

        loadLayout();
    }

    private void loadLayout() {

        try {
            FileReader reader = new FileReader(xmlFile);
            BufferedReader br = new BufferedReader(reader);
            StringBuffer layoutSettings = new StringBuffer();
            String line = br.readLine();
            while(line != null){
                layoutSettings.append(line);
                line = br.readLine();
            }
            reader.close();
            frontend.readXML(XIO.read(layoutSettings.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void saveLayout() {
        XElement xRoot = new XElement("layout");
        frontend.writeXML(xRoot);
        xmlFile.delete();
        xmlFile = new File("layout.xml");
        try {
            FileWriter writer = new FileWriter(xmlFile);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write(xRoot.toString());
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Layout saved " + xmlFile.getAbsolutePath());
    }

    @Override
    public void setVisible(boolean b) {
        if (b) {
            loadLayout();
        } else {
            saveLayout();
        }
        super.setVisible(b);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                DockingFrameTest frame = new DockingFrameTest();
                if (frame != null) {
                    frame.setVisible(true);
                } else {
                    System.exit(0);
                }
            }
        });
    }
}```

You found a bug: it is a focus issue! Because during loading of the layout the theme gets updated, the stack did request focus for its newest child - and the framework did handle the request by making sure that child is visible, which required a change of the maximized Dockable.

I have fixed that behavior in the newest version, 1.1.2p9d. You can download it from here.

Btw.: your application using the Common API. Notice the additional features like minimizing and floating Dockables… :wink:


import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.DefaultSingleCDockable;

public class DockingFrameTest extends JFrame {
    File xmlFile = null;
    CControl control;

    DefaultSingleCDockable dockable1;
    DefaultSingleCDockable dockable2;
    DefaultSingleCDockable dockable3;
    DefaultSingleCDockable dockable4;
    DefaultSingleCDockable dockable5;

    public DockingFrameTest() {
        setTitle("Test Docking");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);
                dispose();
                System.exit(0);
            }
        });

        setPreferredSize(new Dimension(800, 600));
        init();

        pack();
    }

    private void init() {
        xmlFile = new File("layout.xml");
        control = new CControl( this );

        // you need this additional line if you want to enable loading of maximized Dockable
        control.setRevertToBasicModes( false );

        add( control.getContentArea() );
        
        dockable1 = new DefaultSingleCDockable("dock1", "dock 1", new JLabel("Dockable 1"));
        dockable2 = new DefaultSingleCDockable("dock2", "dock 2", new JLabel("Dockable 2"));
        dockable3 = new DefaultSingleCDockable("dock3", "dock 3", new JLabel("Dockable 3"));
        dockable4 = new DefaultSingleCDockable("dock4", "dock 4", new JLabel("Dockable 4"));
        dockable5 = new DefaultSingleCDockable("dock5", "dock 5", new JLabel("Dockable 5"));
        
        CGrid grid = new CGrid( control );
        grid.add(0, 0, 60, 50, dockable1);
        grid.add(60, 0, 40, 50, dockable2);
        grid.add(0, 50, 60, 50, dockable3);
        grid.add(60, 50, 40, 50, dockable4);
        grid.add(60, 50, 40, 50, dockable5);
        
        control.getContentArea().deploy( grid );

        // don't need that
        // loadLayout();
    }

    private void loadLayout() {

        try {
        	control.readXML( xmlFile );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void saveLayout() {
        try {
        	control.writeXML( xmlFile );
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Layout saved " + xmlFile.getAbsolutePath());
    }

    @Override
    public void setVisible(boolean b) {
        if (b) {
            loadLayout();
        } else {
            saveLayout();
        }
        super.setVisible(b);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                DockingFrameTest frame = new DockingFrameTest();
                if (frame != null) {
                    frame.setVisible(true);
                } else {
                    System.exit(0);
                }
            }
        });
    }
}```

Thank you for your quickly troubleshooting. You are great.

Pitris