From: Jussi Koskela Date: Mon, 21 Nov 2016 08:21:09 +0000 (+0200) Subject: Introduce WrapLayout to replace FlowLayout X-Git-Tag: v1.25.0~43 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=ac5f1da15cc639da880fea86a7b828c8fa2e1b7e;p=simantics%2Fplatform.git Introduce WrapLayout to replace FlowLayout FlowLayout does not really flow components into separate lines when they overrun their horizontal space. WrapLayout does this. Also moved the contents of org.simantics.utils.ui.awt into org.simantics.utils.ui.internal.awt since that code was never meant for public use. Moved WrapLayout into org.simantics.utils.ui.awt and published that package. refs #6814 Change-Id: I65600f9a5edff021ab011d980bb3352f6141177f --- diff --git a/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SpreadsheetModel.java b/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SpreadsheetModel.java index d534c9488..dbe3e8264 100644 --- a/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SpreadsheetModel.java +++ b/bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SpreadsheetModel.java @@ -2,10 +2,9 @@ package org.simantics.spreadsheet.ui; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.FlowLayout; import java.awt.Font; import java.awt.Frame; -import java.awt.GridBagConstraints; -import java.awt.GridBagLayout; import java.awt.Image; import java.awt.Rectangle; import java.awt.datatransfer.Clipboard; @@ -80,6 +79,7 @@ import org.simantics.ui.dnd.LocalObjectTransfer; import org.simantics.ui.dnd.LocalObjectTransferable; import org.simantics.ui.fonts.Fonts; import org.simantics.utils.ui.dialogs.ShowMessage; +import org.simantics.utils.ui.awt.WrapLayout; import org.simantics.utils.ui.jface.ActiveSelectionProvider; @SuppressWarnings({ "rawtypes", "unchecked" }) @@ -938,81 +938,48 @@ public class SpreadsheetModel { if (addExpressionField) { - JPanel tools = new JPanel(new GridBagLayout()); + JPanel tools = new JPanel(new WrapLayout(FlowLayout.LEADING, 0, 0)); panel.add(tools, BorderLayout.PAGE_START); - GridBagConstraints c = new GridBagConstraints(); + tools.add(this.font); - c.gridx = 0; - c.anchor = GridBagConstraints.WEST; - tools.add(this.font, c); + tools.add(foreground); - c.gridx = 1; - tools.add(foreground, c); + tools.add(background); - c.gridx = 2; - tools.add(background, c); + tools.add(align_left); + tools.add(align_hcenter); + tools.add(align_right); - c.gridx = 3; - tools.add(align_left, c); - c.gridx = 4; - tools.add(align_hcenter, c); - c.gridx = 5; - tools.add(align_right, c); + tools.add(align_top); + tools.add(align_vcenter); + tools.add(align_bottom); - c.gridx = 6; - tools.add(align_top, c); - c.gridx = 7; - tools.add(align_vcenter, c); - c.gridx = 8; - tools.add(align_bottom, c); + tools.add(borders); - c.gridx = 9; - tools.add(borders, c); + tools.add(lock); + tools.add(unlock); - c.gridx = 10; - tools.add(lock, c); - c.gridx = 11; - tools.add(unlock, c); + tools.add(merge); + tools.add(unmerge); - c.gridx = 12; - tools.add(merge, c); - c.gridx = 13; - tools.add(unmerge, c); - - c.gridx = 14; - tools.add(inputSource, c); + tools.add(inputSource); - c.gridx = 15; - tools.add(sheets, c); + tools.add(sheets); - c.gridx = 16; - tools.add(initialConditions, c); - - c.gridx = 17; - tools.add(saveIc, c); - - c.gridx = 18; - tools.add(context, c); - - c.gridx = 19; - tools.add(operationMode, c); - - c.gridx = 20; - tools.add(iterationEnabled, c); - - c.gridx = 21; - tools.add(iterationLimit, c); - - c.gridx = 0; - c.gridy = 1; - c.gridwidth = 22; - c.gridheight = 1; - c.weighty = 0.0; - c.weightx = 1.0; - c.fill = GridBagConstraints.HORIZONTAL; - - tools.add(expression,c); + tools.add(initialConditions); + + tools.add(saveIc); + + tools.add(context); + + tools.add(operationMode); + + tools.add(iterationEnabled); + + tools.add(iterationLimit); + + tools.add(expression); } panel.add(scroll, BorderLayout.CENTER); diff --git a/bundles/org.simantics.utils.ui/META-INF/MANIFEST.MF b/bundles/org.simantics.utils.ui/META-INF/MANIFEST.MF index 3b9caaccd..9790dcd06 100644 --- a/bundles/org.simantics.utils.ui/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.utils.ui/META-INF/MANIFEST.MF @@ -12,6 +12,7 @@ Require-Bundle: org.eclipse.core.runtime, org.eclipse.core.expressions Export-Package: org.simantics.utils.ui, org.simantics.utils.ui.action, + org.simantics.utils.ui.awt, org.simantics.utils.ui.color, org.simantics.utils.ui.dialogs, org.simantics.utils.ui.gfx, diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTAWTComponent.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTAWTComponent.java index a57067565..06d893911 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTAWTComponent.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/SWTAWTComponent.java @@ -54,12 +54,12 @@ import org.simantics.utils.threads.AWTThread; import org.simantics.utils.threads.ThreadUtils; import org.simantics.utils.threads.logger.ITask; import org.simantics.utils.threads.logger.ThreadLogger; -import org.simantics.utils.ui.awt.AwtEnvironment; -import org.simantics.utils.ui.awt.AwtFocusHandler; -import org.simantics.utils.ui.awt.CleanResizeListener; -import org.simantics.utils.ui.awt.EmbeddedChildFocusTraversalPolicy; -import org.simantics.utils.ui.awt.SwtFocusHandler; import org.simantics.utils.ui.internal.Activator; +import org.simantics.utils.ui.internal.awt.AwtEnvironment; +import org.simantics.utils.ui.internal.awt.AwtFocusHandler; +import org.simantics.utils.ui.internal.awt.CleanResizeListener; +import org.simantics.utils.ui.internal.awt.EmbeddedChildFocusTraversalPolicy; +import org.simantics.utils.ui.internal.awt.SwtFocusHandler; /** diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/WrapLayout.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/WrapLayout.java new file mode 100644 index 000000000..4ff2ee6bd --- /dev/null +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/WrapLayout.java @@ -0,0 +1,212 @@ +/******************************************************************************* + * Copyright (c) 2016 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation, courtesy of Rob Camick + * https://tips4java.wordpress.com/2008/11/06/wrap-layout/ + *******************************************************************************/ +package org.simantics.utils.ui.awt; + +import java.awt.Component; +import java.awt.Container; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.Insets; + +import javax.swing.JScrollPane; +import javax.swing.SwingUtilities; + +/** + * FlowLayout subclass that fully supports wrapping of components. + */ +public class WrapLayout extends FlowLayout +{ + /** + * + */ + private static final long serialVersionUID = -4312800883442354753L; + + /** + * Constructs a new WrapLayout with a left + * alignment and a default 5-unit horizontal and vertical gap. + */ + public WrapLayout() + { + super(); + } + + /** + * Constructs a new FlowLayout with the specified + * alignment and a default 5-unit horizontal and vertical gap. + * The value of the alignment argument must be one of + * WrapLayout, WrapLayout, + * or WrapLayout. + * @param align the alignment value + */ + public WrapLayout(int align) + { + super(align); + } + + /** + * Creates a new flow layout manager with the indicated alignment + * and the indicated horizontal and vertical gaps. + *

+ * The value of the alignment argument must be one of + * WrapLayout, WrapLayout, + * or WrapLayout. + * @param align the alignment value + * @param hgap the horizontal gap between components + * @param vgap the vertical gap between components + */ + public WrapLayout(int align, int hgap, int vgap) + { + super(align, hgap, vgap); + } + + /** + * Returns the preferred dimensions for this layout given the + * visible components in the specified target container. + * @param target the component which needs to be laid out + * @return the preferred dimensions to lay out the + * subcomponents of the specified container + */ + @Override + public Dimension preferredLayoutSize(Container target) + { + return layoutSize(target, true); + } + + /** + * Returns the minimum dimensions needed to layout the visible + * components contained in the specified target container. + * @param target the component which needs to be laid out + * @return the minimum dimensions to lay out the + * subcomponents of the specified container + */ + @Override + public Dimension minimumLayoutSize(Container target) + { + Dimension minimum = layoutSize(target, false); + minimum.width -= (getHgap() + 1); + return minimum; + } + + /** + * Returns the minimum or preferred dimension needed to layout the target + * container. + * + * @param target target to get layout size for + * @param preferred should preferred size be calculated + * @return the dimension to layout the target container + */ + private Dimension layoutSize(Container target, boolean preferred) + { + synchronized (target.getTreeLock()) + { + // Each row must fit with the width allocated to the containter. + // When the container width = 0, the preferred width of the container + // has not yet been calculated so lets ask for the maximum. + + int targetWidth = target.getSize().width; + Container container = target; + + while (container.getSize().width == 0 && container.getParent() != null) + { + container = container.getParent(); + } + + targetWidth = container.getSize().width; + + if (targetWidth == 0) + targetWidth = Integer.MAX_VALUE; + + int hgap = getHgap(); + int vgap = getVgap(); + Insets insets = target.getInsets(); + int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2); + int maxWidth = targetWidth - horizontalInsetsAndGap; + + // Fit components into the allowed width + + Dimension dim = new Dimension(0, 0); + int rowWidth = 0; + int rowHeight = 0; + + int nmembers = target.getComponentCount(); + + for (int i = 0; i < nmembers; i++) + { + Component m = target.getComponent(i); + + if (m.isVisible()) + { + Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize(); + + // Can't add the component to current row. Start a new row. + + if (rowWidth + d.width > maxWidth) + { + addRow(dim, rowWidth, rowHeight); + rowWidth = 0; + rowHeight = 0; + } + + // Add a horizontal gap for all components after the first + + if (rowWidth != 0) + { + rowWidth += hgap; + } + + rowWidth += d.width; + rowHeight = Math.max(rowHeight, d.height); + } + } + + addRow(dim, rowWidth, rowHeight); + + dim.width += horizontalInsetsAndGap; + dim.height += insets.top + insets.bottom + vgap * 2; + + // When using a scroll pane or the DecoratedLookAndFeel we need to + // make sure the preferred size is less than the size of the + // target containter so shrinking the container size works + // correctly. Removing the horizontal gap is an easy way to do this. + + Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target); + + if (scrollPane != null && target.isValid()) + { + dim.width -= (hgap + 1); + } + + return dim; + } + } + + /* + * A new row has been completed. Use the dimensions of this row + * to update the preferred size for the container. + * + * @param dim update the width and height when appropriate + * @param rowWidth the width of the row to add + * @param rowHeight the height of the row to add + */ + private void addRow(Dimension dim, int rowWidth, int rowHeight) + { + dim.width = Math.max(dim.width, rowWidth); + + if (dim.height > 0) + { + dim.height += getVgap(); + } + + dim.height += rowHeight; + } +} \ No newline at end of file diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/AwtDialogListener.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/AwtDialogListener.java similarity index 96% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/AwtDialogListener.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/AwtDialogListener.java index 25728b8e2..23cf6673b 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/AwtDialogListener.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/AwtDialogListener.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import java.awt.AWTEvent; import java.awt.Dialog; diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/AwtEnvironment.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/AwtEnvironment.java similarity index 97% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/AwtEnvironment.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/AwtEnvironment.java index dcb975967..9a9d84ae1 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/AwtEnvironment.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/AwtEnvironment.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import java.awt.EventQueue; import java.awt.Frame; diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/AwtFocusHandler.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/AwtFocusHandler.java similarity index 96% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/AwtFocusHandler.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/AwtFocusHandler.java index 924f44ce2..15b9d967d 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/AwtFocusHandler.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/AwtFocusHandler.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import java.awt.Component; import java.awt.Container; diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/CleanResizeListener.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/CleanResizeListener.java similarity index 95% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/CleanResizeListener.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/CleanResizeListener.java index 3f2081f2f..c2101580d 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/CleanResizeListener.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/CleanResizeListener.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import org.eclipse.swt.events.ControlAdapter; import org.eclipse.swt.events.ControlEvent; diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/EmbeddedChildFocusTraversalPolicy.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/EmbeddedChildFocusTraversalPolicy.java similarity index 96% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/EmbeddedChildFocusTraversalPolicy.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/EmbeddedChildFocusTraversalPolicy.java index e786d3a80..b43832a3d 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/EmbeddedChildFocusTraversalPolicy.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/EmbeddedChildFocusTraversalPolicy.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import java.awt.Component; import java.awt.Container; diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/EmbeddedSwingComposite.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/EmbeddedSwingComposite.java similarity index 97% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/EmbeddedSwingComposite.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/EmbeddedSwingComposite.java index 714dfab68..886f50511 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/EmbeddedSwingComposite.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/EmbeddedSwingComposite.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import java.awt.Container; import java.awt.EventQueue; diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/Platform.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/Platform.java similarity index 92% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/Platform.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/Platform.java index 9b704d53e..f3854f93b 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/Platform.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/Platform.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import org.eclipse.swt.SWT; diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/RecursiveContainerListener.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/RecursiveContainerListener.java similarity index 96% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/RecursiveContainerListener.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/RecursiveContainerListener.java index 151734fa0..4e561f7cd 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/RecursiveContainerListener.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/RecursiveContainerListener.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import java.awt.Component; import java.awt.Container; diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/SwtFocusHandler.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/SwtFocusHandler.java similarity index 96% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/SwtFocusHandler.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/SwtFocusHandler.java index e4c0eb485..9c73ab6f9 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/SwtFocusHandler.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/SwtFocusHandler.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import java.awt.EventQueue; diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/SwtInputBlocker.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/SwtInputBlocker.java similarity index 96% rename from bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/SwtInputBlocker.java rename to bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/SwtInputBlocker.java index cac9d5661..dd9eb80c5 100644 --- a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/awt/SwtInputBlocker.java +++ b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/internal/awt/SwtInputBlocker.java @@ -8,7 +8,7 @@ * Contributors: * SAS Institute - initial API and implementation *******************************************************************************/ -package org.simantics.utils.ui.awt; +package org.simantics.utils.ui.internal.awt; import org.eclipse.swt.SWT; import org.eclipse.swt.events.FocusAdapter;