View unanswered posts | View active topics It is currently Sat May 27, 2017 9:09 am



Reply to topic  [ 3 posts ] 
 layout(changed) does not clear the MigLayout cache 
Author Message

Joined: Wed Jun 15, 2011 10:37 am
Posts: 3
Post layout(changed) does not clear the MigLayout cache
In the following code snippet I create a Composite "container" and some children. The Action "refresh" disposes of all children and recreates them in the same Composite. Calling "container".layout(true) will throw a SWTException "Widget is disposed". Digging in the MigLayout source you will find that the implementation of MigLayout.layout(Composite parent, boolean flushCache) uses its own method "checkCache(Composite)" to determine whether or not to clear the cache instead of relying on the second argument "flushCache".

This problem cannot be fixed by using "nocache" in the MigLayout Constructor but can be worked around by calling "container".changed("container".getCHildren()) before calling layout(). The MigLayout implementation of changed() will call flushCache() at some point and after that layouting will work properly. in my point of view this classifies as a bug. By ignoring the parameter "flushCache" the implementation of checkCache() must yield the same result as "flushCache" demands. What do you think?

Here is the code snippet:

Code:
import java.util.Date;

import net.miginfocom.swt.MigLayout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class CachingBug {

  public static void main(final String[] args) {
    final Display display = new Display();

    final Shell shell = new Shell(display);
    shell.setLayout(new MigLayout("wrap 1", "", ""));

    final Composite container = new Composite(shell, SWT.BORDER);
    container.setLayout(new MigLayout("wrap 1"));
    createLabels(container);

    final Button refreshButton = new Button(shell, SWT.PUSH);
    refreshButton.setText("Refresh");
    refreshButton.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseUp(final MouseEvent event) {
        for (final Control control : container.getChildren()) {
          control.dispose();
        }
        createLabels(container);
        // container.changed(container.getChildren());
        container.layout(true);
      }
    });

    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }

  private static void createLabels(final Composite container) {
    final String date = new Date().toString();
    for (int i = 0; i < 5; i++) {
      new Label(container, SWT.NONE).setText("My Text " + i + " - " + date);
    }
  }

}


Wed Feb 08, 2012 11:59 am
Profile
Site Admin

Joined: Mon Dec 06, 2004 4:24 pm
Posts: 2485
Location: Sweden
Post Re: layout(changed) does not clear the MigLayout cache
Sorry for missing this.

I have fixed by always flushing the cache if the flushCache is true. The fix will be in the next version.

Cheers,
Mikael


Thu Feb 21, 2013 12:20 am
Profile WWW

Joined: Wed Jun 15, 2011 10:37 am
Posts: 3
Post Re: layout(changed) does not clear the MigLayout cache
Dear Mikael,

thanks for your reply and the fix.

Unfortunately, we switched to using GridLayout consistently which is more reliable.

Greetings
Hendrik


Thu Feb 21, 2013 4:45 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.