Reading one of my colleague’s code I saw this:
GridLayout gl = new GridLayout(); gl .numColumns = 1; gl .verticalSpacing = 0; gl .marginHeight = 0; gl .marginWidth = 0; Composite c1 = new Composite(parent); Composite c2 = new Composite(parent); c1.setLayout(gl); c2.setLayout(gl);
Personally I always create a new layout for each one of my GUI component. It is in my opinion more easy to read that the piece of code above and because there is a small number of such objects (having a UI with thousands of GUI component needing a layout is bad idea I think ), creating a new one for each component doesn’t impact performances.
To go further in the probleme, reading this code lead me to the following question:
How do I know if a given class instance can be “shared” in several contexts ?
In the particular case of the GridLayout above, I was not able to find the answer in the Javadoc here: http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/layout/GridLayout.html. After looking at the source code of GridLayout it seems that there is no internal state saved anywhere, and thus it’s possible to use the same layout instance for several components.
May be I missed something or maybe it should be interesting to indicate this information in the Javadoc. For sure, I’ll take care of that when writing the documentation for my own libraries.