Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [albireo-dev] size/layout management

Hi Gordon,

> > Since you added the font propagation code, I get this exception very often.
> > To reproduce, I only need to open the EmbeddedJTable view and close Eclipse.
> 
> Interesting. I can't map the line numbers in the stack trace to the 
> code, but perhaps there is a problem with calling validate too early or 
> too late in the AWT component's life cycle?

The line numbers were old because the stack trace was already a week or two
old... The cause of the problem is that while setting the UI the Metal L&F
creates new L&F objects while doing a recursive traversal, and while this
L&F object is initializing itself, it calls invalidate() - for which we
did a recursive validateTree(), which ended trying to use the half-
initialized object. So, it was definitely too many validate() calls.

> I can't reproduce the problem.

Maybe if you switch to Metal L&F?

> Unfortunately, we already have an example that is affected by this 
> change. If you open the "Relayout Example" view and click the "grow" 
> button a few times, you'll see that the label gets truncated and the 
> "..." characters are shown. Originally, the label would be properly 
> resized, at least until the containing GridLayout ran out of space.

Very good example. Thank you! I'm adding a button "layout" that redoes
the layout - then the "..." gets cleaned up -, and also adding the same
buttons on the SWT side so that I can debug it by setting a breakpoint at
invokeLater.

> My RepaintManager implementation would take care of this problem. It 
> simply hooks the addInvalidComponent() method and calls 
> updateCachedAWTSizes. As you've correctly noted in the past, it is not a 
> complete solution.
> 
> I propose that I add the RepaintManager hack as an optional feature.

Let me see first if I can find another way of getting the "Relayout Example"
to work...

Bruno


Index: src/org/eclipse/albireo/examples/plugin/views/RelayoutExampleView.java
===================================================================
RCS file: /cvsroot/technology/org.eclipse.albireo/org.eclipse.albireo.examples.plugin/src/org/eclipse/albireo/examples/plugin/views/RelayoutExampleView.java,v
retrieving revision 1.1
diff -c -3 -r1.1 RelayoutExampleView.java
*** src/org/eclipse/albireo/examples/plugin/views/RelayoutExampleView.java	7 Feb 2008 21:05:06 -0000	1.1
--- src/org/eclipse/albireo/examples/plugin/views/RelayoutExampleView.java	8 Feb 2008 18:31:06 -0000
***************
*** 1,5 ****
--- 1,6 ----
  package org.eclipse.albireo.examples.plugin.views;
  
+ import java.awt.EventQueue;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
***************
*** 9,21 ****
--- 10,26 ----
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.SwingConstants;
+ import javax.swing.SwingUtilities;
  
  import org.eclipse.albireo.core.SwingControl;
  import org.eclipse.swt.SWT;
+ import org.eclipse.swt.events.SelectionEvent;
+ import org.eclipse.swt.events.SelectionListener;
  import org.eclipse.swt.layout.GridData;
  import org.eclipse.swt.layout.GridLayout;
  import org.eclipse.swt.widgets.Button;
  import org.eclipse.swt.widgets.Composite;
+ import org.eclipse.swt.widgets.Display;
  import org.eclipse.ui.part.ViewPart;
  
  
***************
*** 24,39 ****
   * successfully participate in a SWT layout. As in the case SWT controls, it is necessary to
   * explicitly re-layout the containing composite.  
   */
! public class RelayoutExampleView extends ViewPart implements ActionListener {
      private static final String PREFIX = "Count: ";
      private SwingControl swingControl;
      private JLabel label;
!     private JButton grow;
!     private JButton shrink;
      private JPanel panel;
      private Composite composite;
-     private JLabel count;
-     private int countInt;
  
      public void createPartControl(Composite parent) {
          composite = new Composite(parent, SWT.NONE);
--- 29,47 ----
   * successfully participate in a SWT layout. As in the case SWT controls, it is necessary to
   * explicitly re-layout the containing composite.  
   */
! public class RelayoutExampleView extends ViewPart {
      private static final String PREFIX = "Count: ";
      private SwingControl swingControl;
+ 
+     private int countInt;
+     private JLabel count;
      private JLabel label;
!     private JButton growButton;
!     private JButton shrinkButton;
!     private JButton layoutButton;
      private JPanel panel;
+ 
      private Composite composite;
  
      public void createPartControl(Composite parent) {
          composite = new Composite(parent, SWT.NONE);
***************
*** 42,49 ****
          composite.setLayout(layout);
          swingControl = new SwingControl(composite, SWT.NONE) {
              protected JComponent createSwingComponent() {
!                 panel = new JPanel(new java.awt.GridLayout(4, 1));
!                 addWidgets();
                  return panel;
              }
          };
--- 50,56 ----
          composite.setLayout(layout);
          swingControl = new SwingControl(composite, SWT.NONE) {
              protected JComponent createSwingComponent() {
!                 addSwingWidgets();
                  return panel;
              }
          };
***************
*** 53,106 ****
          data.grabExcessHorizontalSpace = true;
          data.grabExcessVerticalSpace = true;
          swingControl.setLayoutData(data);
!         
!         Button button = new Button(composite, SWT.PUSH);
!         button.setText("SWT Button");
          data = new GridData();
!         data.horizontalAlignment = SWT.CENTER;
          data.verticalAlignment = SWT.CENTER;
!         button.setLayoutData(data);
      }
  
!     private void addWidgets() {
!         label = new JLabel("Abc ", SwingConstants.LEFT);
          countInt = 1;
          count = new JLabel(PREFIX + String.valueOf(countInt), SwingConstants.LEFT); 
-         grow = new JButton("Grow");
-         shrink = new JButton("Shrink");
  
          label.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
  
!         grow.addActionListener(this);
!         shrink.addActionListener(this);
  
          panel.add(count);
          panel.add(label);
!         panel.add(grow);
!         panel.add(shrink);
      }
!     
!     public void actionPerformed(ActionEvent event) {
          String text = label.getText();
!         String countText = count.getText();
!         if (event.getSource() == grow) {
              text += "Abc ";
              countInt++;
!             shrink.setEnabled(true);
          } else {
              text = text.substring(4);
              countInt--;
              if (text.length() <= 4) {
!                 shrink.setEnabled(false);
              }
          }
          count.setText(PREFIX + String.valueOf(countInt));
          label.setText(text);
!         composite.getDisplay().asyncExec(new Runnable() {
!             public void run() {
!                 composite.layout();
!             }
!         });
      }
  
      /**
--- 60,202 ----
          data.grabExcessHorizontalSpace = true;
          data.grabExcessVerticalSpace = true;
          swingControl.setLayoutData(data);
! 
!         Composite buttonList = new Composite(composite, SWT.NONE);
!         GridLayout buttonLayout = new GridLayout();
!         buttonLayout.numColumns = 1;
!         buttonList.setLayout(buttonLayout);
! 
!         Button growSWTButton = new Button(buttonList, SWT.PUSH);
!         growSWTButton.setText("Grow (SWT Button)");
          data = new GridData();
!         data.horizontalAlignment = SWT.LEAD;
!         data.verticalAlignment = SWT.CENTER;
!         growSWTButton.setLayoutData(data);
! 
!         Button shrinkSWTButton = new Button(buttonList, SWT.PUSH);
!         shrinkSWTButton.setText("Shrink (SWT Button)");
!         data = new GridData();
!         data.horizontalAlignment = SWT.LEAD;
!         data.verticalAlignment = SWT.CENTER;
!         shrinkSWTButton.setLayoutData(data);
! 
!         Button layoutSWTButton = new Button(buttonList, SWT.PUSH);
!         layoutSWTButton.setText("SWT-Layout (SWT Button)");
!         data = new GridData();
!         data.horizontalAlignment = SWT.LEAD;
          data.verticalAlignment = SWT.CENTER;
!         layoutSWTButton.setLayoutData(data);
! 
!         growSWTButton.addSelectionListener(
!             new SelectionListener() {
!                 public void widgetSelected(SelectionEvent e) {
!                     SwingUtilities.invokeLater(
!                         new Runnable() {
!                             public void run() {
!                                 growOrShrink(true);
!                             }
!                         });
!                 }
!                 public void widgetDefaultSelected(SelectionEvent e) {
!                 }
!             });
!         shrinkSWTButton.addSelectionListener(
!             new SelectionListener() {
!                 public void widgetSelected(SelectionEvent e) {
!                      SwingUtilities.invokeLater(
!                         new Runnable() {
!                             public void run() {
!                                 growOrShrink(false);
!                             }
!                         });
!                 }
!                 public void widgetDefaultSelected(SelectionEvent e) {
!                 }
!             });
!         layoutSWTButton.addSelectionListener(
!             new SelectionListener() {
!                 public void widgetSelected(SelectionEvent e) {
!                     relayoutFromOutside();
!                 }
!                 public void widgetDefaultSelected(SelectionEvent e) {
!                 }
!             });
      }
  
!     private void addSwingWidgets() {
!         panel = new JPanel(new java.awt.GridLayout(0, 1));
! 
          countInt = 1;
          count = new JLabel(PREFIX + String.valueOf(countInt), SwingConstants.LEFT); 
  
+         label = new JLabel("Abc ", SwingConstants.LEFT);
          label.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
  
!         growButton = new JButton("Grow");
!         shrinkButton = new JButton("Shrink");
!         layoutButton = new JButton("SWT-Layout");
  
          panel.add(count);
          panel.add(label);
!         panel.add(growButton);
!         panel.add(shrinkButton);
!         panel.add(layoutButton);
! 
!         growButton.addActionListener(
!             new ActionListener() {
!                 public void actionPerformed(ActionEvent event) {
!                     growOrShrink(true);
!                 }
!             });
!         shrinkButton.addActionListener(
!             new ActionListener() {
!                 public void actionPerformed(ActionEvent event) {
!                     growOrShrink(false);
!                 }
!             });
!         layoutButton.addActionListener(
!             new ActionListener() {
!                 public void actionPerformed(ActionEvent event) {
!                     composite.getDisplay().asyncExec(
!                         new Runnable() {
!                             public void run() {
!                                 relayoutFromOutside();
!                             }
!                         });
!                 }
!             });
      }
! 
!     void growOrShrink(boolean grow) {
!         assert EventQueue.isDispatchThread();    // On AWT event thread
! 
          String text = label.getText();
!         if (grow) {
              text += "Abc ";
              countInt++;
!             shrinkButton.setEnabled(true);
          } else {
              text = text.substring(4);
              countInt--;
              if (text.length() <= 4) {
!                 shrinkButton.setEnabled(false);
              }
          }
          count.setText(PREFIX + String.valueOf(countInt));
          label.setText(text);
! 
!         composite.getDisplay().asyncExec(
!             new Runnable() {
!                 public void run() {
!                     composite.layout();
!                 }
!             });
!     }
! 
!     void relayoutFromOutside() {
!         assert Display.getCurrent() != null;     // On SWT event thread
! 
!         composite.layout();
      }
  
      /**


Back to the top