diff options
Diffstat (limited to 'prototypes/miglayout/net/miginfocom/swt')
3 files changed, 976 insertions, 976 deletions
diff --git a/prototypes/miglayout/net/miginfocom/swt/MigLayout.java b/prototypes/miglayout/net/miginfocom/swt/MigLayout.java index a36408fb..7cb9a6fa 100644 --- a/prototypes/miglayout/net/miginfocom/swt/MigLayout.java +++ b/prototypes/miglayout/net/miginfocom/swt/MigLayout.java @@ -1,506 +1,506 @@ -package net.miginfocom.swt;
-/*
- * License (BSD):
- * ==============
- *
- * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice, this list
- * of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or other
- * materials provided with the distribution.
- * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
- * used to endorse or promote products derived from this software without specific
- * prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- * OF SUCH DAMAGE.
- *
- * @version 1.0
- * @author Mikael Grev, MiG InfoCom AB
- * Date: 2006-sep-08
- */
-
-import net.miginfocom.layout.*;
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.swt.graphics.Rectangle;
-import org.eclipse.swt.widgets.*;
-import org.eclipse.swt.widgets.Composite;
-
-import java.io.*;
-import java.util.*;
-
-/** A very flexbile layout manager.
- * <p>
- * Read the documentation that came with this layout manager for information on usage.
- */
-public final class MigLayout extends Layout implements Externalizable
-{
- // ******** Instance part ********
-
- /** The component to string constraints mappings.
- */
- private final Map<Control, Object> scrConstrMap = new IdentityHashMap<Control, Object>(8);
-
- /** Hold the serializable text representation of the constraints.
- */
- private Object layoutConstraints = "", colConstraints = "", rowConstraints = ""; // Should never be null!
-
- // ******** Transient part ********
-
- private transient ContainerWrapper cacheParentW = null;
-
- private transient final Map<ComponentWrapper, CC> ccMap = new HashMap<ComponentWrapper, CC>(8);
-
- private transient LC lc = null;
- private transient AC colSpecs = null, rowSpecs = null;
- private transient Grid grid = null;
-
- private transient java.util.Timer debugTimer = null;
- private transient long curDelay = -1;
- private transient int lastModCount = PlatformDefaults.getModCount();
- private transient int lastHash = -1;
-
- private transient ArrayList<LayoutCallback> callbackList = null;
-
- /** Constructor with no constraints.
- */
- public MigLayout()
- {
- this("", "", "");
- }
-
- /** Constructor.
- * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as "".
- */
- public MigLayout(String layoutConstraints)
- {
- this(layoutConstraints, "", "");
- }
-
- /** Constructor.
- * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as "".
- * @param colConstraints The constraints for the columns in the grid. <code>null</code> will be treated as "".
- */
- public MigLayout(String layoutConstraints, String colConstraints)
- {
- this(layoutConstraints, colConstraints, "");
- }
-
- /** Constructor.
- * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as "".
- * @param colConstraints The constraints for the columns in the grid. <code>null</code> will be treated as "".
- * @param rowConstraints The constraints for the rows in the grid. <code>null</code> will be treated as "".
- */
- public MigLayout(String layoutConstraints, String colConstraints, String rowConstraints)
- {
- setLayoutConstraints(layoutConstraints);
- setColumnConstraints(colConstraints);
- setRowConstraints(rowConstraints);
- }
-
- /** Constructor.
- * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as an empty cosntraint.
- */
- public MigLayout(LC layoutConstraints)
- {
- this(layoutConstraints, null, null);
- }
-
- /** Constructor.
- * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as an empty cosntraint.
- * @param colConstraints The constraints for the columns in the grid. <code>null</code> will be treated as an empty constraint.
- */
- public MigLayout(LC layoutConstraints, AC colConstraints)
- {
- this(layoutConstraints, colConstraints, null);
- }
-
- /** Constructor.
- * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as an empty cosntraint.
- * @param colConstraints The constraints for the columns in the grid. <code>null</code> will be treated as an empty constraint.
- * @param rowConstraints The constraints for the rows in the grid. <code>null</code> will be treated as an empty constraint.
- */
- public MigLayout(LC layoutConstraints, AC colConstraints, AC rowConstraints)
- {
- setLayoutConstraints(layoutConstraints);
- setColumnConstraints(colConstraints);
- setRowConstraints(rowConstraints);
- }
-
- /** Returns layout constraints eighter as a <code>String</code> or {@link net.miginfocom.layout.LC} depending what was sent in
- * to the constructor or set with {@link #setLayoutConstraints(Object)}.
- * @return The layout constraints eighter as a <code>String</code> or {@link net.miginfocom.layout.LC} depending what was sent in
- * to the constructor or set with {@link #setLayoutConstraints(Object)}. Never <code>null</code>.
- */
- public Object getLayoutConstraints()
- {
- return layoutConstraints;
- }
-
- /** Sets the layout constraints for the layout manager instance as a String.
- * <p>
- * See the class JavaDocs for information on how this string is formatted.
- * @param s The layout constraints as a String representation. <code>null</code> is converted to <code>""</code> for storage.
- * @throws RuntimeException if the constaint was not valid.
- */
- public void setLayoutConstraints(Object s)
- {
- if (s == null || s instanceof String) {
- s = ConstraintParser.prepare((String) s);
- lc = ConstraintParser.parseLayoutConstraint((String) s);
- } else if (s instanceof LC) {
- lc = (LC) s;
- } else {
- throw new IllegalArgumentException("Illegal constraint type: " + s.getClass().toString());
- }
- layoutConstraints = s;
- grid = null;
- }
-
- /** Returns the column layout constraints either as a <code>String</code> or {@link net.miginfocom.layout.AC}.
- * @return The column constraints eighter as a <code>String</code> or {@link net.miginfocom.layout.LC} depending what was sent in
- * to the constructor or set with {@link #setLayoutConstraints(Object)}. Never <code>null</code>.
- */
- public Object getColumnConstraints()
- {
- return colConstraints;
- }
-
- /** Sets the column layout constraints for the layout manager instance as a String.
- * <p>
- * See the class JavaDocs for information on how this string is formatted.
- * @param constr The column layout constraints as a String representation. <code>null</code> is converted to <code>""</code> for storage.
- * @throws RuntimeException if the constaint was not valid.
- */
- public void setColumnConstraints(Object constr)
- {
- if (constr == null || constr instanceof String) {
- constr = ConstraintParser.prepare((String) constr);
- colSpecs = ConstraintParser.parseColumnConstraints((String) constr);
- } else if (constr instanceof AC) {
- colSpecs = (AC) constr;
- } else {
- throw new IllegalArgumentException("Illegal constraint type: " + constr.getClass().toString());
- }
- colConstraints = constr;
- grid = null;
- }
-
- /** Returns the row layout constraints as a String representation. This string is the exact string as set with {@link #setRowConstraints(Object)}
- * or sent into the constructor.
- * <p>
- * See the class JavaDocs for information on how this string is formatted.
- * @return The row layout constraints as a String representation. Never <code>null</code>.
- */
- public Object getRowConstraints()
- {
- return rowConstraints;
- }
-
- /** Sets the row layout constraints for the layout manager instance as a String.
- * <p>
- * See the class JavaDocs for information on how this string is formatted.
- * @param constr The row layout constraints as a String representation. <code>null</code> is converted to <code>""</code> for storage.
- * @throws RuntimeException if the constaint was not valid.
- */
- public void setRowConstraints(Object constr)
- {
- if (constr == null || constr instanceof String) {
- constr = ConstraintParser.prepare((String) constr);
- rowSpecs = ConstraintParser.parseRowConstraints((String) constr);
- } else if (constr instanceof AC) {
- rowSpecs = (AC) constr;
- } else {
- throw new IllegalArgumentException("Illegal constraint type: " + constr.getClass().toString());
- }
- rowConstraints = constr;
- grid = null;
- }
-
- /** Returns the component constraints as a String representation. This string is the exact string as set with {@link #setComponentConstraints(org.eclipse.swt.widgets.Control, Object)}
- * or set when adding the component to the parent component.
- * <p>
- * See the class JavaDocs for information on how this string is formatted.
- * @param comp The component to return the constraints for.
- * @return The component constraints as a String representation ir <code>null</code> if the component is not registered
- * with this layout manager. The returned values is wither a String or a {@link net.miginfocom.layout.CC}
- * depending on what constraint was sent in when the component was added. May be <code>null</code>.
- */
- public Object getComponentConstraints(Control comp)
- {
- return scrConstrMap.get(comp);
- }
-
- /** Sets the component constraint for the component that already must be handleded by this layout manager.
- * <p>
- * See the class JavaDocs for information on how this string is formatted.
- * @param constr The component constraints as a String or {@link net.miginfocom.layout.CC}. <code>null</code> is ok.
- * @param comp The component to set the constraints for.
- * @throws RuntimeException if the constaint was not valid.
- * @throws IllegalArgumentException If the component is not handlering the component.
- */
- public void setComponentConstraints(Control comp, Object constr)
- {
- setComponentConstraintsImpl(comp, constr, false);
- }
-
- /** Sets the component constraint for the component that already must be handleded by this layout manager.
- * <p>
- * See the class JavaDocs for information on how this string is formatted.
- * @param constr The component constraints as a String or {@link net.miginfocom.layout.CC}. <code>null</code> is ok.
- * @param comp The component to set the constraints for.
- * @throws RuntimeException if the constaint was not valid.
- * @throws IllegalArgumentException If the component is not handling the component.
- */
- private void setComponentConstraintsImpl(Control comp, Object constr, boolean noCheck)
- {
- if (noCheck == false && scrConstrMap.containsKey(comp) == false)
- throw new IllegalArgumentException("Component must already be added to parent!");
-
- ComponentWrapper cw = new SwtComponentWrapper(comp);
-
- if (constr == null || constr instanceof String) {
- String cStr = ConstraintParser.prepare((String) constr);
-
- scrConstrMap.put(comp, constr);
- ccMap.put(cw, ConstraintParser.parseComponentConstraint(cStr));
-
- } else if (constr instanceof CC) {
-
- scrConstrMap.put(comp, constr);
- ccMap.put(cw, (CC) constr);
-
- } else {
- throw new IllegalArgumentException("Constraint must be String or ComponentConstraint: " + constr.getClass().toString());
- }
- grid = null;
- }
-
- /** Returns if this layout manager is currently managing this component.
- * @param c The component to check. If <code>null</code> then <code>false</code> will be returned.
- * @return If this layout manager is currently managing this component.
- */
- public boolean isManagingComponent(Control c)
- {
- return scrConstrMap.containsKey(c);
- }
-
- /** Adds the callback function that will be called at different stages of the layout cylce.
- * @param callback The callback. Not <code>null</code>.
- */
- public void addLayoutCallback(LayoutCallback callback)
- {
- if (callback == null)
- throw new NullPointerException();
-
- if (callbackList == null)
- callbackList = new ArrayList<LayoutCallback>(1);
-
- callbackList.add(callback);
- }
-
- /** Removes the callback if it exists.
- * @param callback The callback. May be <code>null</code>.
- */
- public void removeLayoutCallback(LayoutCallback callback)
- {
- if (callbackList != null)
- callbackList.remove(callback);
- }
-
- /** Sets the debugging state for this layout manager instance. If debug is turned on a timer will repaint the last laid out parent
- * with debug information on top.
- * <p>
- * Red fill and dashed darked red outline is used to indicate occupied cells in the grid. Blue dashed outline indicate indicate
- * component bounds set.
- * <p>
- * Note that debug can also be set on the layout constraints. There it will be persisted. The calue set here will not. See the class
- * JavaDocs for information.
- * @param b <code>true</code> means debug is turned on.
- */
- private synchronized void setDebug(final ComponentWrapper parentW, boolean b)
- {
- if (b && (debugTimer == null || curDelay != lc.getDebugMillis())) {
- if (debugTimer != null)
- debugTimer.cancel();
-
- TimerTask debugTask = new TimerTask() {
- public void run()
- {
- Display.getDefault ().asyncExec(new Runnable () {
- public void run () {
- grid.paintDebug();
- }
- });
- }
- };
-
- debugTimer = new Timer(true);
- curDelay = lc.getDebugMillis();
- debugTimer.schedule(debugTask, curDelay, curDelay);
-
- ContainerWrapper pCW = parentW.getParent();
- Composite parent = pCW != null ? (Composite) pCW.getComponent() : null;
- if (parent != null)
- parent.layout();
- } else if (!b && debugTimer != null) {
- debugTimer.cancel();
- debugTimer = null;
- }
- }
-
- /** Returns the current debugging state.
- * @return The current debugging state.
- */
- private boolean getDebug()
- {
- return debugTimer != null;
- }
-
- /** Check if something has changed and if so recrete it to the cached objects.
- * @param parent The parent that is the target for this layout manager.
- */
- private final void checkCache(Composite parent)
- {
- if (parent == null)
- return;
-
- checkConstrMap(parent);
-
- ContainerWrapper par = checkParent(parent);
-
- // Check if the grid is valid
- int mc = PlatformDefaults.getModCount();
- if (lastModCount != mc) {
- grid = null;
- lastModCount = mc;
- }
-
- int hash = parent.getSize().hashCode();
- for (Iterator<ComponentWrapper> it = ccMap.keySet().iterator(); it.hasNext();)
- hash += it.next().getLayoutHashCode();
-
- if (hash != lastHash) {
- grid = null;
- lastHash = hash;
- }
-
- setDebug(par, lc.getDebugMillis() > 0);
-
- if (grid == null)
- grid = new Grid(par, lc, rowSpecs, colSpecs, ccMap, callbackList);
- }
-
- private boolean checkConstrMap(Composite parent)
- {
- Control[] comps = parent.getChildren();
- boolean changed = comps.length != scrConstrMap.size();
-
- if (changed == false) {
- for (int i = 0; i < comps.length; i++) {
- Control c = comps[i];
- if (scrConstrMap.get(c) != c.getLayoutData()) {
- changed = true;
- break;
- }
- }
- }
-
- if (changed) {
- scrConstrMap.clear();
- for (int i = 0; i < comps.length; i++) {
- Control c = comps[i];
- setComponentConstraintsImpl(c, c.getLayoutData(), true);
- }
- }
- return changed;
- }
-
- private final ContainerWrapper checkParent(Composite parent)
- {
- if (parent == null)
- return null;
-
- if (cacheParentW == null || cacheParentW.getComponent() != parent)
- cacheParentW = new SwtContainerWrapper(parent);
-
- return cacheParentW;
- }
-
- public float getLayoutAlignmentX(Composite parent)
- {
- return lc != null && lc.getAlignX() != null ? lc.getAlignX().getPixels(1, checkParent(parent), null) : 0;
- }
-
- public float getLayoutAlignmentY(Composite parent)
- {
- return lc != null && lc.getAlignY() != null ? lc.getAlignY().getPixels(1, checkParent(parent), null) : 0;
- }
-
- protected Point computeSize(Composite parent, int wHint, int hHint, boolean flushCache)
- {
- checkCache(parent);
-
- int w = LayoutUtil.getSizeSafe(grid != null ? grid.getWidth() : null, LayoutUtil.PREF);
- int h = LayoutUtil.getSizeSafe(grid != null ? grid.getHeight() : null, LayoutUtil.PREF);
-
- return new Point(w, h);
- }
-
- protected void layout(Composite parent, boolean flushCache)
- {
- checkCache(parent);
-
- Rectangle r = parent.getClientArea();
- int[] b = new int[] {r.x, r.y, r.width, r.height};
-
- final boolean layoutAgain = grid.layout(b, lc.getAlignX(), lc.getAlignY(), getDebug(), true);
-
- if (layoutAgain) {
- grid = null;
- checkCache(parent);
- grid.layout(b, lc.getAlignX(), lc.getAlignY(), getDebug(), false);
- }
- }
-
- protected boolean flushCache(Control control)
- {
- if (lc.isNoCache())
- grid = null;
-
- return true;
- }
-
- // ************************************************
- // Persistence Delegate and Serializable combined.
- // ************************************************
-
- private Object readResolve() throws ObjectStreamException
- {
- return LayoutUtil.getSerializedObject(this);
- }
-
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- {
- LayoutUtil.setSerializedObject(this, LayoutUtil.readAsXML(in));
- }
-
- public void writeExternal(ObjectOutput out) throws IOException
- {
- if (getClass() == MigLayout.class)
- LayoutUtil.writeAsXML(out, this);
- }
+package net.miginfocom.swt; +/* + * License (BSD): + * ============== + * + * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * Neither the name of the MiG InfoCom AB nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * @version 1.0 + * @author Mikael Grev, MiG InfoCom AB + * Date: 2006-sep-08 + */ + +import net.miginfocom.layout.*; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.*; +import org.eclipse.swt.widgets.Composite; + +import java.io.*; +import java.util.*; + +/** A very flexbile layout manager. + * <p> + * Read the documentation that came with this layout manager for information on usage. + */ +public final class MigLayout extends Layout implements Externalizable +{ + // ******** Instance part ******** + + /** The component to string constraints mappings. + */ + private final Map<Control, Object> scrConstrMap = new IdentityHashMap<Control, Object>(8); + + /** Hold the serializable text representation of the constraints. + */ + private Object layoutConstraints = "", colConstraints = "", rowConstraints = ""; // Should never be null! + + // ******** Transient part ******** + + private transient ContainerWrapper cacheParentW = null; + + private transient final Map<ComponentWrapper, CC> ccMap = new HashMap<ComponentWrapper, CC>(8); + + private transient LC lc = null; + private transient AC colSpecs = null, rowSpecs = null; + private transient Grid grid = null; + + private transient java.util.Timer debugTimer = null; + private transient long curDelay = -1; + private transient int lastModCount = PlatformDefaults.getModCount(); + private transient int lastHash = -1; + + private transient ArrayList<LayoutCallback> callbackList = null; + + /** Constructor with no constraints. + */ + public MigLayout() + { + this("", "", ""); + } + + /** Constructor. + * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as "". + */ + public MigLayout(String layoutConstraints) + { + this(layoutConstraints, "", ""); + } + + /** Constructor. + * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as "". + * @param colConstraints The constraints for the columns in the grid. <code>null</code> will be treated as "". + */ + public MigLayout(String layoutConstraints, String colConstraints) + { + this(layoutConstraints, colConstraints, ""); + } + + /** Constructor. + * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as "". + * @param colConstraints The constraints for the columns in the grid. <code>null</code> will be treated as "". + * @param rowConstraints The constraints for the rows in the grid. <code>null</code> will be treated as "". + */ + public MigLayout(String layoutConstraints, String colConstraints, String rowConstraints) + { + setLayoutConstraints(layoutConstraints); + setColumnConstraints(colConstraints); + setRowConstraints(rowConstraints); + } + + /** Constructor. + * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as an empty cosntraint. + */ + public MigLayout(LC layoutConstraints) + { + this(layoutConstraints, null, null); + } + + /** Constructor. + * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as an empty cosntraint. + * @param colConstraints The constraints for the columns in the grid. <code>null</code> will be treated as an empty constraint. + */ + public MigLayout(LC layoutConstraints, AC colConstraints) + { + this(layoutConstraints, colConstraints, null); + } + + /** Constructor. + * @param layoutConstraints The constraints that concern the whole layout. <code>null</code> will be treated as an empty cosntraint. + * @param colConstraints The constraints for the columns in the grid. <code>null</code> will be treated as an empty constraint. + * @param rowConstraints The constraints for the rows in the grid. <code>null</code> will be treated as an empty constraint. + */ + public MigLayout(LC layoutConstraints, AC colConstraints, AC rowConstraints) + { + setLayoutConstraints(layoutConstraints); + setColumnConstraints(colConstraints); + setRowConstraints(rowConstraints); + } + + /** Returns layout constraints eighter as a <code>String</code> or {@link net.miginfocom.layout.LC} depending what was sent in + * to the constructor or set with {@link #setLayoutConstraints(Object)}. + * @return The layout constraints eighter as a <code>String</code> or {@link net.miginfocom.layout.LC} depending what was sent in + * to the constructor or set with {@link #setLayoutConstraints(Object)}. Never <code>null</code>. + */ + public Object getLayoutConstraints() + { + return layoutConstraints; + } + + /** Sets the layout constraints for the layout manager instance as a String. + * <p> + * See the class JavaDocs for information on how this string is formatted. + * @param s The layout constraints as a String representation. <code>null</code> is converted to <code>""</code> for storage. + * @throws RuntimeException if the constaint was not valid. + */ + public void setLayoutConstraints(Object s) + { + if (s == null || s instanceof String) { + s = ConstraintParser.prepare((String) s); + lc = ConstraintParser.parseLayoutConstraint((String) s); + } else if (s instanceof LC) { + lc = (LC) s; + } else { + throw new IllegalArgumentException("Illegal constraint type: " + s.getClass().toString()); + } + layoutConstraints = s; + grid = null; + } + + /** Returns the column layout constraints either as a <code>String</code> or {@link net.miginfocom.layout.AC}. + * @return The column constraints eighter as a <code>String</code> or {@link net.miginfocom.layout.LC} depending what was sent in + * to the constructor or set with {@link #setLayoutConstraints(Object)}. Never <code>null</code>. + */ + public Object getColumnConstraints() + { + return colConstraints; + } + + /** Sets the column layout constraints for the layout manager instance as a String. + * <p> + * See the class JavaDocs for information on how this string is formatted. + * @param constr The column layout constraints as a String representation. <code>null</code> is converted to <code>""</code> for storage. + * @throws RuntimeException if the constaint was not valid. + */ + public void setColumnConstraints(Object constr) + { + if (constr == null || constr instanceof String) { + constr = ConstraintParser.prepare((String) constr); + colSpecs = ConstraintParser.parseColumnConstraints((String) constr); + } else if (constr instanceof AC) { + colSpecs = (AC) constr; + } else { + throw new IllegalArgumentException("Illegal constraint type: " + constr.getClass().toString()); + } + colConstraints = constr; + grid = null; + } + + /** Returns the row layout constraints as a String representation. This string is the exact string as set with {@link #setRowConstraints(Object)} + * or sent into the constructor. + * <p> + * See the class JavaDocs for information on how this string is formatted. + * @return The row layout constraints as a String representation. Never <code>null</code>. + */ + public Object getRowConstraints() + { + return rowConstraints; + } + + /** Sets the row layout constraints for the layout manager instance as a String. + * <p> + * See the class JavaDocs for information on how this string is formatted. + * @param constr The row layout constraints as a String representation. <code>null</code> is converted to <code>""</code> for storage. + * @throws RuntimeException if the constaint was not valid. + */ + public void setRowConstraints(Object constr) + { + if (constr == null || constr instanceof String) { + constr = ConstraintParser.prepare((String) constr); + rowSpecs = ConstraintParser.parseRowConstraints((String) constr); + } else if (constr instanceof AC) { + rowSpecs = (AC) constr; + } else { + throw new IllegalArgumentException("Illegal constraint type: " + constr.getClass().toString()); + } + rowConstraints = constr; + grid = null; + } + + /** Returns the component constraints as a String representation. This string is the exact string as set with {@link #setComponentConstraints(org.eclipse.swt.widgets.Control, Object)} + * or set when adding the component to the parent component. + * <p> + * See the class JavaDocs for information on how this string is formatted. + * @param comp The component to return the constraints for. + * @return The component constraints as a String representation ir <code>null</code> if the component is not registered + * with this layout manager. The returned values is wither a String or a {@link net.miginfocom.layout.CC} + * depending on what constraint was sent in when the component was added. May be <code>null</code>. + */ + public Object getComponentConstraints(Control comp) + { + return scrConstrMap.get(comp); + } + + /** Sets the component constraint for the component that already must be handleded by this layout manager. + * <p> + * See the class JavaDocs for information on how this string is formatted. + * @param constr The component constraints as a String or {@link net.miginfocom.layout.CC}. <code>null</code> is ok. + * @param comp The component to set the constraints for. + * @throws RuntimeException if the constaint was not valid. + * @throws IllegalArgumentException If the component is not handlering the component. + */ + public void setComponentConstraints(Control comp, Object constr) + { + setComponentConstraintsImpl(comp, constr, false); + } + + /** Sets the component constraint for the component that already must be handleded by this layout manager. + * <p> + * See the class JavaDocs for information on how this string is formatted. + * @param constr The component constraints as a String or {@link net.miginfocom.layout.CC}. <code>null</code> is ok. + * @param comp The component to set the constraints for. + * @throws RuntimeException if the constaint was not valid. + * @throws IllegalArgumentException If the component is not handling the component. + */ + private void setComponentConstraintsImpl(Control comp, Object constr, boolean noCheck) + { + if (noCheck == false && scrConstrMap.containsKey(comp) == false) + throw new IllegalArgumentException("Component must already be added to parent!"); + + ComponentWrapper cw = new SwtComponentWrapper(comp); + + if (constr == null || constr instanceof String) { + String cStr = ConstraintParser.prepare((String) constr); + + scrConstrMap.put(comp, constr); + ccMap.put(cw, ConstraintParser.parseComponentConstraint(cStr)); + + } else if (constr instanceof CC) { + + scrConstrMap.put(comp, constr); + ccMap.put(cw, (CC) constr); + + } else { + throw new IllegalArgumentException("Constraint must be String or ComponentConstraint: " + constr.getClass().toString()); + } + grid = null; + } + + /** Returns if this layout manager is currently managing this component. + * @param c The component to check. If <code>null</code> then <code>false</code> will be returned. + * @return If this layout manager is currently managing this component. + */ + public boolean isManagingComponent(Control c) + { + return scrConstrMap.containsKey(c); + } + + /** Adds the callback function that will be called at different stages of the layout cylce. + * @param callback The callback. Not <code>null</code>. + */ + public void addLayoutCallback(LayoutCallback callback) + { + if (callback == null) + throw new NullPointerException(); + + if (callbackList == null) + callbackList = new ArrayList<LayoutCallback>(1); + + callbackList.add(callback); + } + + /** Removes the callback if it exists. + * @param callback The callback. May be <code>null</code>. + */ + public void removeLayoutCallback(LayoutCallback callback) + { + if (callbackList != null) + callbackList.remove(callback); + } + + /** Sets the debugging state for this layout manager instance. If debug is turned on a timer will repaint the last laid out parent + * with debug information on top. + * <p> + * Red fill and dashed darked red outline is used to indicate occupied cells in the grid. Blue dashed outline indicate indicate + * component bounds set. + * <p> + * Note that debug can also be set on the layout constraints. There it will be persisted. The calue set here will not. See the class + * JavaDocs for information. + * @param b <code>true</code> means debug is turned on. + */ + private synchronized void setDebug(final ComponentWrapper parentW, boolean b) + { + if (b && (debugTimer == null || curDelay != lc.getDebugMillis())) { + if (debugTimer != null) + debugTimer.cancel(); + + TimerTask debugTask = new TimerTask() { + public void run() + { + Display.getDefault ().asyncExec(new Runnable () { + public void run () { + grid.paintDebug(); + } + }); + } + }; + + debugTimer = new Timer(true); + curDelay = lc.getDebugMillis(); + debugTimer.schedule(debugTask, curDelay, curDelay); + + ContainerWrapper pCW = parentW.getParent(); + Composite parent = pCW != null ? (Composite) pCW.getComponent() : null; + if (parent != null) + parent.layout(); + } else if (!b && debugTimer != null) { + debugTimer.cancel(); + debugTimer = null; + } + } + + /** Returns the current debugging state. + * @return The current debugging state. + */ + private boolean getDebug() + { + return debugTimer != null; + } + + /** Check if something has changed and if so recrete it to the cached objects. + * @param parent The parent that is the target for this layout manager. + */ + private final void checkCache(Composite parent) + { + if (parent == null) + return; + + checkConstrMap(parent); + + ContainerWrapper par = checkParent(parent); + + // Check if the grid is valid + int mc = PlatformDefaults.getModCount(); + if (lastModCount != mc) { + grid = null; + lastModCount = mc; + } + + int hash = parent.getSize().hashCode(); + for (Iterator<ComponentWrapper> it = ccMap.keySet().iterator(); it.hasNext();) + hash += it.next().getLayoutHashCode(); + + if (hash != lastHash) { + grid = null; + lastHash = hash; + } + + setDebug(par, lc.getDebugMillis() > 0); + + if (grid == null) + grid = new Grid(par, lc, rowSpecs, colSpecs, ccMap, callbackList); + } + + private boolean checkConstrMap(Composite parent) + { + Control[] comps = parent.getChildren(); + boolean changed = comps.length != scrConstrMap.size(); + + if (changed == false) { + for (int i = 0; i < comps.length; i++) { + Control c = comps[i]; + if (scrConstrMap.get(c) != c.getLayoutData()) { + changed = true; + break; + } + } + } + + if (changed) { + scrConstrMap.clear(); + for (int i = 0; i < comps.length; i++) { + Control c = comps[i]; + setComponentConstraintsImpl(c, c.getLayoutData(), true); + } + } + return changed; + } + + private final ContainerWrapper checkParent(Composite parent) + { + if (parent == null) + return null; + + if (cacheParentW == null || cacheParentW.getComponent() != parent) + cacheParentW = new SwtContainerWrapper(parent); + + return cacheParentW; + } + + public float getLayoutAlignmentX(Composite parent) + { + return lc != null && lc.getAlignX() != null ? lc.getAlignX().getPixels(1, checkParent(parent), null) : 0; + } + + public float getLayoutAlignmentY(Composite parent) + { + return lc != null && lc.getAlignY() != null ? lc.getAlignY().getPixels(1, checkParent(parent), null) : 0; + } + + protected Point computeSize(Composite parent, int wHint, int hHint, boolean flushCache) + { + checkCache(parent); + + int w = LayoutUtil.getSizeSafe(grid != null ? grid.getWidth() : null, LayoutUtil.PREF); + int h = LayoutUtil.getSizeSafe(grid != null ? grid.getHeight() : null, LayoutUtil.PREF); + + return new Point(w, h); + } + + protected void layout(Composite parent, boolean flushCache) + { + checkCache(parent); + + Rectangle r = parent.getClientArea(); + int[] b = new int[] {r.x, r.y, r.width, r.height}; + + final boolean layoutAgain = grid.layout(b, lc.getAlignX(), lc.getAlignY(), getDebug(), true); + + if (layoutAgain) { + grid = null; + checkCache(parent); + grid.layout(b, lc.getAlignX(), lc.getAlignY(), getDebug(), false); + } + } + + protected boolean flushCache(Control control) + { + if (lc.isNoCache()) + grid = null; + + return true; + } + + // ************************************************ + // Persistence Delegate and Serializable combined. + // ************************************************ + + private Object readResolve() throws ObjectStreamException + { + return LayoutUtil.getSerializedObject(this); + } + + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException + { + LayoutUtil.setSerializedObject(this, LayoutUtil.readAsXML(in)); + } + + public void writeExternal(ObjectOutput out) throws IOException + { + if (getClass() == MigLayout.class) + LayoutUtil.writeAsXML(out, this); + } }
\ No newline at end of file diff --git a/prototypes/miglayout/net/miginfocom/swt/SwtComponentWrapper.java b/prototypes/miglayout/net/miginfocom/swt/SwtComponentWrapper.java index aa7647a7..e994f790 100644 --- a/prototypes/miglayout/net/miginfocom/swt/SwtComponentWrapper.java +++ b/prototypes/miglayout/net/miginfocom/swt/SwtComponentWrapper.java @@ -1,359 +1,359 @@ -package net.miginfocom.swt;
-/*
- * License (BSD):
- * ==============
- *
- * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice, this list
- * of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or other
- * materials provided with the distribution.
- * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
- * used to endorse or promote products derived from this software without specific
- * prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- * OF SUCH DAMAGE.
- *
- * @version 1.0
- * @author Mikael Grev, MiG InfoCom AB
- * Date: 2006-sep-08
- */
-
-import net.miginfocom.layout.ComponentWrapper;
-import net.miginfocom.layout.ContainerWrapper;
-import net.miginfocom.layout.PlatformDefaults;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.graphics.*;
-import org.eclipse.swt.widgets.*;
-
-/**
- */
-public class SwtComponentWrapper implements ComponentWrapper
-{
- /** Debug color for component bounds outline.
- */
- private static Color DB_COMP_OUTLINE = new Color(Display.getCurrent(), 0, 0, 200);
-
- private static boolean vp = false;
- private static boolean mz = false;
-
- private final Control c;
- private int compType = TYPE_UNSET;
-
- public SwtComponentWrapper(Control c)
- {
- this.c = c;
- }
-
- public final int getBaseline(int width, int height)
- {
- return -1;
- }
-
- public final Object getComponent()
- {
- return c;
- }
-
- public final float getPixelUnitFactor(boolean isHor)
- {
- switch (PlatformDefaults.getLogicalPixelBase()) {
- case PlatformDefaults.BASE_FONT_SIZE:
- GC gc = new GC(c);
- FontMetrics fm = gc.getFontMetrics();
- float f = isHor ? fm.getAverageCharWidth() / 5f : fm.getHeight() / 13f;
- gc.dispose();
- return f;
-
- case PlatformDefaults.BASE_SCALE_FACTOR:
- Float s = isHor ? PlatformDefaults.getHorizontalScaleFactor() : PlatformDefaults.getVerticalScaleFactor();
- if (s != null)
- return s.floatValue();
- return (isHor ? getHorizontalScreenDPI() : getVerticalScreenDPI()) / (float) PlatformDefaults.getDefaultDPI();
-
- default:
- return 1f;
- }
- }
-
-// /** Cache.
-// */
-// private final static IdentityHashMap<FontMetrics, Point.Float> FM_MAP2 = new IdentityHashMap<FontMetrics, Point.Float>(4);
-// private final static Font SUBST_FONT2 = new Font("sansserif", Font.PLAIN, 11);
-//
-// public float getDialogUnit(boolean isHor)
-// {
-// Font font = c.getFont();
-// FontMetrics fm = c.getFontMetrics(font != null ? font : SUBST_FONT2);
-// Point.Float dluP = FM_MAP2.get(fm);
-// if (dluP == null) {
-// float w = fm.charWidth('X') / 4f;
-// int ascent = fm.getAscent();
-// float h = (ascent > 14 ? ascent : ascent + (15 - ascent) / 3) / 8f;
-//
-// dluP = new Point.Float(w, h);
-// FM_MAP2.put(fm, dluP);
-// }
-// return isHor ? dluP.x : dluP.y;
-// }
-
- public final int getX()
- {
- return c.getLocation().x;
- }
-
- public final int getY()
- {
- return c.getLocation().y;
- }
-
- public final int getWidth()
- {
- return c.getSize().x;
- }
-
- public final int getHeight()
- {
- return c.getSize().y;
- }
-
- public final int getScreenLocationX()
- {
- return c.toDisplay(0, 0).x;
- }
-
- public final int getScreenLocationY()
- {
- return c.toDisplay(0, 0).y;
- }
-
- public final int getMinimumHeight()
- {
- return mz ? 0 : computeSize(false).y;
- }
-
- public final int getMinimumWidth()
- {
- return mz ? 0 : computeSize(true).x;
- }
-
- public final int getPreferredHeight()
- {
- return computeSize(false).y;
- }
-
- public final int getPreferredWidth()
- {
- return computeSize(true).x;
- }
-
- public final int getMaximumHeight()
- {
- return Short.MAX_VALUE;
- }
-
- public final int getMaximumWidth()
- {
- return Short.MAX_VALUE;
- }
-
- private final Point computeSize(boolean h)
- {
- Point p = c.getSize();
- int wHint = (h || p.x <= 0) ? SWT.DEFAULT : p.x;
- int hHint = (!h || p.y <= 0) ? SWT.DEFAULT : p.y;
-
- if (wHint != SWT.DEFAULT || hHint != SWT.DEFAULT) {
- int trim = 0;
- if (c instanceof Scrollable) {
- Rectangle rect = ((Scrollable) c).computeTrim (0, 0, 0, 0);
- trim = h ? rect.height : rect.width;
- } else {
- trim = (c.getBorderWidth () << 1);
- }
- if (wHint == SWT.DEFAULT) {
- hHint = Math.max (0, hHint - trim);
- } else {
- wHint = Math.max (0, wHint - trim);
- }
- }
-
- return c.computeSize(wHint, hHint);
- }
-
- public final ContainerWrapper getParent()
- {
- return new SwtContainerWrapper(c.getParent());
- }
-
- public int getHorizontalScreenDPI()
- {
- return c.getDisplay().getDPI().x;
- }
-
- public int getVerticalScreenDPI()
- {
- return c.getDisplay().getDPI().y;
- }
-
- public final int getScreenWidth()
- {
- return c.getDisplay().getBounds().width;
- }
-
- public final int getScreenHeight()
- {
- return c.getDisplay().getBounds().height;
- }
-
- public final boolean hasBaseline()
- {
- return false;
- }
-
- public final String getLinkId()
- {
- return null;
- }
-
- public final void setBounds(int x, int y, int width, int height)
- {
- c.setBounds(x, y, width, height);
- }
-
- public boolean isVisible()
- {
- return c.getVisible();
- }
-
- public final int[] getVisualPadding()
- {
- return null;
- }
-
- public final static boolean isUseVisualPadding()
- {
- return vp;
- }
-
- public final static void setUseVisualPadding(boolean b)
- {
- vp = b;
- }
-
- /** Sets if minimum size for SWT components should be preferred size (default, false) or 0.
- * @return <code>true</code> means minimum size is 0.
- */
- public static boolean isMinimumSizeZero()
- {
- return mz;
- }
-
- /** Sets if minimum size for SWT components should be preferred size (default, false) or 0.
- * @param b <code>true</code> means minimum size is 0.
- */
- public static void setMinimumSizeZero(boolean b)
- {
- mz = b;
- }
-
- public int getLayoutHashCode()
- {
- if (c.isDisposed())
- return -1;
-
- Point sz = c.getSize();
- Point p = c.computeSize(sz.x > 0 ? sz.x : SWT.DEFAULT, SWT.DEFAULT, false);
- int h = p.x + (p.y << 12) + (sz.x << 22) + (sz.y << 16);
-
- if (c.isVisible())
- h |= (1 << 25);
-
- String id = getLinkId();
- if (id != null)
- h += id.hashCode();
- return h;
- }
-
- public final void paintDebugOutline()
- {
- if (c.isDisposed())
- return;
-
- GC gc = new GC(c);
-
- gc.setLineJoin(SWT.JOIN_MITER);
- gc.setLineCap(SWT.CAP_SQUARE);
- gc.setLineStyle(SWT.LINE_DOT);
-
- gc.setForeground(DB_COMP_OUTLINE);
- gc.drawRectangle(0, 0, getWidth() - 1, getHeight() - 1);
-
- gc.dispose();
- }
-
- public int getComponetType(boolean disregardScrollPane)
- {
- if (compType == TYPE_UNSET)
- compType = checkType();
-
- return compType;
- }
-
- private final int checkType()
- {
- int s = c.getStyle();
-
- if (c instanceof Text) {
- return (s & SWT.MULTI) > 0 ? TYPE_TEXT_AREA : TYPE_TEXT_FIELD;
- } else if (c instanceof Label) {
- return (s & SWT.SEPARATOR) > 0 ? TYPE_SEPARATOR : TYPE_LABEL;
- } else if (c instanceof Button) {
- if ((s & SWT.CHECK) > 0 || (s & SWT.RADIO) > 0)
- return TYPE_CHECK_BOX;
- return TYPE_BUTTON;
- } else if (c instanceof Canvas) {
- return TYPE_PANEL;
- } else if (c instanceof List) {
- return TYPE_LIST;
- } else if (c instanceof Table) {
- return TYPE_TABLE;
- } else if (c instanceof Spinner) {
- return TYPE_SPINNER;
- } else if (c instanceof ProgressBar) {
- return TYPE_PROGRESS_BAR;
- } else if (c instanceof Slider) {
- return TYPE_SLIDER;
- } else if (c instanceof Composite) { // only AWT components is not containers.
- return TYPE_CONTAINER;
- }
- return TYPE_UNKNOWN;
- }
-
- public final int hashCode()
- {
- return getComponent().hashCode();
- }
-
- public final boolean equals(Object o)
- {
- if (o == null || o instanceof ComponentWrapper == false)
- return false;
-
- return getComponent().equals(((ComponentWrapper) o).getComponent());
- }
-}
+package net.miginfocom.swt; +/* + * License (BSD): + * ============== + * + * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * Neither the name of the MiG InfoCom AB nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * @version 1.0 + * @author Mikael Grev, MiG InfoCom AB + * Date: 2006-sep-08 + */ + +import net.miginfocom.layout.ComponentWrapper; +import net.miginfocom.layout.ContainerWrapper; +import net.miginfocom.layout.PlatformDefaults; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +/** + */ +public class SwtComponentWrapper implements ComponentWrapper +{ + /** Debug color for component bounds outline. + */ + private static Color DB_COMP_OUTLINE = new Color(Display.getCurrent(), 0, 0, 200); + + private static boolean vp = false; + private static boolean mz = false; + + private final Control c; + private int compType = TYPE_UNSET; + + public SwtComponentWrapper(Control c) + { + this.c = c; + } + + public final int getBaseline(int width, int height) + { + return -1; + } + + public final Object getComponent() + { + return c; + } + + public final float getPixelUnitFactor(boolean isHor) + { + switch (PlatformDefaults.getLogicalPixelBase()) { + case PlatformDefaults.BASE_FONT_SIZE: + GC gc = new GC(c); + FontMetrics fm = gc.getFontMetrics(); + float f = isHor ? fm.getAverageCharWidth() / 5f : fm.getHeight() / 13f; + gc.dispose(); + return f; + + case PlatformDefaults.BASE_SCALE_FACTOR: + Float s = isHor ? PlatformDefaults.getHorizontalScaleFactor() : PlatformDefaults.getVerticalScaleFactor(); + if (s != null) + return s.floatValue(); + return (isHor ? getHorizontalScreenDPI() : getVerticalScreenDPI()) / (float) PlatformDefaults.getDefaultDPI(); + + default: + return 1f; + } + } + +// /** Cache. +// */ +// private final static IdentityHashMap<FontMetrics, Point.Float> FM_MAP2 = new IdentityHashMap<FontMetrics, Point.Float>(4); +// private final static Font SUBST_FONT2 = new Font("sansserif", Font.PLAIN, 11); +// +// public float getDialogUnit(boolean isHor) +// { +// Font font = c.getFont(); +// FontMetrics fm = c.getFontMetrics(font != null ? font : SUBST_FONT2); +// Point.Float dluP = FM_MAP2.get(fm); +// if (dluP == null) { +// float w = fm.charWidth('X') / 4f; +// int ascent = fm.getAscent(); +// float h = (ascent > 14 ? ascent : ascent + (15 - ascent) / 3) / 8f; +// +// dluP = new Point.Float(w, h); +// FM_MAP2.put(fm, dluP); +// } +// return isHor ? dluP.x : dluP.y; +// } + + public final int getX() + { + return c.getLocation().x; + } + + public final int getY() + { + return c.getLocation().y; + } + + public final int getWidth() + { + return c.getSize().x; + } + + public final int getHeight() + { + return c.getSize().y; + } + + public final int getScreenLocationX() + { + return c.toDisplay(0, 0).x; + } + + public final int getScreenLocationY() + { + return c.toDisplay(0, 0).y; + } + + public final int getMinimumHeight() + { + return mz ? 0 : computeSize(false).y; + } + + public final int getMinimumWidth() + { + return mz ? 0 : computeSize(true).x; + } + + public final int getPreferredHeight() + { + return computeSize(false).y; + } + + public final int getPreferredWidth() + { + return computeSize(true).x; + } + + public final int getMaximumHeight() + { + return Short.MAX_VALUE; + } + + public final int getMaximumWidth() + { + return Short.MAX_VALUE; + } + + private final Point computeSize(boolean h) + { + Point p = c.getSize(); + int wHint = (h || p.x <= 0) ? SWT.DEFAULT : p.x; + int hHint = (!h || p.y <= 0) ? SWT.DEFAULT : p.y; + + if (wHint != SWT.DEFAULT || hHint != SWT.DEFAULT) { + int trim = 0; + if (c instanceof Scrollable) { + Rectangle rect = ((Scrollable) c).computeTrim (0, 0, 0, 0); + trim = h ? rect.height : rect.width; + } else { + trim = (c.getBorderWidth () << 1); + } + if (wHint == SWT.DEFAULT) { + hHint = Math.max (0, hHint - trim); + } else { + wHint = Math.max (0, wHint - trim); + } + } + + return c.computeSize(wHint, hHint); + } + + public final ContainerWrapper getParent() + { + return new SwtContainerWrapper(c.getParent()); + } + + public int getHorizontalScreenDPI() + { + return c.getDisplay().getDPI().x; + } + + public int getVerticalScreenDPI() + { + return c.getDisplay().getDPI().y; + } + + public final int getScreenWidth() + { + return c.getDisplay().getBounds().width; + } + + public final int getScreenHeight() + { + return c.getDisplay().getBounds().height; + } + + public final boolean hasBaseline() + { + return false; + } + + public final String getLinkId() + { + return null; + } + + public final void setBounds(int x, int y, int width, int height) + { + c.setBounds(x, y, width, height); + } + + public boolean isVisible() + { + return c.getVisible(); + } + + public final int[] getVisualPadding() + { + return null; + } + + public final static boolean isUseVisualPadding() + { + return vp; + } + + public final static void setUseVisualPadding(boolean b) + { + vp = b; + } + + /** Sets if minimum size for SWT components should be preferred size (default, false) or 0. + * @return <code>true</code> means minimum size is 0. + */ + public static boolean isMinimumSizeZero() + { + return mz; + } + + /** Sets if minimum size for SWT components should be preferred size (default, false) or 0. + * @param b <code>true</code> means minimum size is 0. + */ + public static void setMinimumSizeZero(boolean b) + { + mz = b; + } + + public int getLayoutHashCode() + { + if (c.isDisposed()) + return -1; + + Point sz = c.getSize(); + Point p = c.computeSize(sz.x > 0 ? sz.x : SWT.DEFAULT, SWT.DEFAULT, false); + int h = p.x + (p.y << 12) + (sz.x << 22) + (sz.y << 16); + + if (c.isVisible()) + h |= (1 << 25); + + String id = getLinkId(); + if (id != null) + h += id.hashCode(); + return h; + } + + public final void paintDebugOutline() + { + if (c.isDisposed()) + return; + + GC gc = new GC(c); + + gc.setLineJoin(SWT.JOIN_MITER); + gc.setLineCap(SWT.CAP_SQUARE); + gc.setLineStyle(SWT.LINE_DOT); + + gc.setForeground(DB_COMP_OUTLINE); + gc.drawRectangle(0, 0, getWidth() - 1, getHeight() - 1); + + gc.dispose(); + } + + public int getComponetType(boolean disregardScrollPane) + { + if (compType == TYPE_UNSET) + compType = checkType(); + + return compType; + } + + private final int checkType() + { + int s = c.getStyle(); + + if (c instanceof Text) { + return (s & SWT.MULTI) > 0 ? TYPE_TEXT_AREA : TYPE_TEXT_FIELD; + } else if (c instanceof Label) { + return (s & SWT.SEPARATOR) > 0 ? TYPE_SEPARATOR : TYPE_LABEL; + } else if (c instanceof Button) { + if ((s & SWT.CHECK) > 0 || (s & SWT.RADIO) > 0) + return TYPE_CHECK_BOX; + return TYPE_BUTTON; + } else if (c instanceof Canvas) { + return TYPE_PANEL; + } else if (c instanceof List) { + return TYPE_LIST; + } else if (c instanceof Table) { + return TYPE_TABLE; + } else if (c instanceof Spinner) { + return TYPE_SPINNER; + } else if (c instanceof ProgressBar) { + return TYPE_PROGRESS_BAR; + } else if (c instanceof Slider) { + return TYPE_SLIDER; + } else if (c instanceof Composite) { // only AWT components is not containers. + return TYPE_CONTAINER; + } + return TYPE_UNKNOWN; + } + + public final int hashCode() + { + return getComponent().hashCode(); + } + + public final boolean equals(Object o) + { + if (o == null || o instanceof ComponentWrapper == false) + return false; + + return getComponent().equals(((ComponentWrapper) o).getComponent()); + } +} diff --git a/prototypes/miglayout/net/miginfocom/swt/SwtContainerWrapper.java b/prototypes/miglayout/net/miginfocom/swt/SwtContainerWrapper.java index 033bb2bf..b27dbe84 100644 --- a/prototypes/miglayout/net/miginfocom/swt/SwtContainerWrapper.java +++ b/prototypes/miglayout/net/miginfocom/swt/SwtContainerWrapper.java @@ -1,112 +1,112 @@ -package net.miginfocom.swt;
-/*
- * License (BSD):
- * ==============
- *
- * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com)
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice, this list
- * of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or other
- * materials provided with the distribution.
- * Neither the name of the MiG InfoCom AB nor the names of its contributors may be
- * used to endorse or promote products derived from this software without specific
- * prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- * OF SUCH DAMAGE.
- *
- * @version 1.0
- * @author Mikael Grev, MiG InfoCom AB
- * Date: 2006-sep-08
- */
-
-import net.miginfocom.layout.ComponentWrapper;
-import net.miginfocom.layout.ContainerWrapper;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.SWT;
-
-/**
- */
-public final class SwtContainerWrapper extends SwtComponentWrapper implements ContainerWrapper
-{
- public SwtContainerWrapper(Composite c)
- {
- super(c);
- }
-
- public ComponentWrapper[] getComponents()
- {
- Composite c = (Composite) getComponent();
- Control[] cons = c.getChildren();
- ComponentWrapper[] cws = new ComponentWrapper[cons.length];
- for (int i = 0; i < cws.length; i++)
- cws[i] = new SwtComponentWrapper(cons[i]);
- return cws;
- }
-
- public int getComponentCount()
- {
- return ((Composite) getComponent()).getChildren().length;
- }
-
- public Object getLayout()
- {
- return ((Composite) getComponent()).getLayout();
- }
-
- public final boolean isLeftToRight()
- {
- return (((Composite) getComponent()).getStyle() & SWT.LEFT_TO_RIGHT) > 0;
- }
-
- public final void paintDebugCell(int x, int y, int width, int height)
- {
- // A Composite can not draw above its children, so the cells can not be painted.
-
-// if (c.isDisposed())
-// return;
-// GC gc = new GC(c);
-//
-// gc.setLineStyle(SWT.LINE_DASHDOTDOT);
-// gc.setLineJoin(SWT.JOIN_MITER);
-// gc.setLineCap(SWT.CAP_SQUARE);
-//
-// gc.setBackground(DB_CELL_BG);
-// gc.fillRectangle(x, y, width, height);
-//
-// gc.setForeground(DB_CELL_OUTLINE);
-// gc.drawRectangle(x, y, width - 1, height - 1);
-//
-// gc.dispose();
- }
-
- public int getComponetType(boolean disregardScrollPane)
- {
- return TYPE_CONTAINER;
- }
-
-
- public int getLayoutHashCode()
- {
- int h = super.getLayoutHashCode();
-
- if (isLeftToRight())
- h |= (1 << 26);
-
- return h;
- }
-}
+package net.miginfocom.swt; +/* + * License (BSD): + * ============== + * + * Copyright (c) 2004, Mikael Grev, MiG InfoCom AB. (miglayout (at) miginfocom (dot) com) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * Neither the name of the MiG InfoCom AB nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific + * prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + * + * @version 1.0 + * @author Mikael Grev, MiG InfoCom AB + * Date: 2006-sep-08 + */ + +import net.miginfocom.layout.ComponentWrapper; +import net.miginfocom.layout.ContainerWrapper; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.SWT; + +/** + */ +public final class SwtContainerWrapper extends SwtComponentWrapper implements ContainerWrapper +{ + public SwtContainerWrapper(Composite c) + { + super(c); + } + + public ComponentWrapper[] getComponents() + { + Composite c = (Composite) getComponent(); + Control[] cons = c.getChildren(); + ComponentWrapper[] cws = new ComponentWrapper[cons.length]; + for (int i = 0; i < cws.length; i++) + cws[i] = new SwtComponentWrapper(cons[i]); + return cws; + } + + public int getComponentCount() + { + return ((Composite) getComponent()).getChildren().length; + } + + public Object getLayout() + { + return ((Composite) getComponent()).getLayout(); + } + + public final boolean isLeftToRight() + { + return (((Composite) getComponent()).getStyle() & SWT.LEFT_TO_RIGHT) > 0; + } + + public final void paintDebugCell(int x, int y, int width, int height) + { + // A Composite can not draw above its children, so the cells can not be painted. + +// if (c.isDisposed()) +// return; +// GC gc = new GC(c); +// +// gc.setLineStyle(SWT.LINE_DASHDOTDOT); +// gc.setLineJoin(SWT.JOIN_MITER); +// gc.setLineCap(SWT.CAP_SQUARE); +// +// gc.setBackground(DB_CELL_BG); +// gc.fillRectangle(x, y, width, height); +// +// gc.setForeground(DB_CELL_OUTLINE); +// gc.drawRectangle(x, y, width - 1, height - 1); +// +// gc.dispose(); + } + + public int getComponetType(boolean disregardScrollPane) + { + return TYPE_CONTAINER; + } + + + public int getLayoutHashCode() + { + int h = super.getLayoutHashCode(); + + if (isLeftToRight()) + h |= (1 << 26); + + return h; + } +} |