X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Forg.argeo.cms.swt%2Fsrc%2Forg%2Fargeo%2Fcms%2Fswt%2FCmsSwtUtils.java;h=5d964090b9abbd908ff1fc3f007c85afe11a76f8;hb=ff7b3e2954398c5a6d36684725d4527c961ae080;hp=a94d70706121eeb881c09fa045f5827cd0215159;hpb=8282011b0e20e80704b209ad55fa9fb132e16280;p=lgpl%2Fargeo-commons.git diff --git a/eclipse/org.argeo.cms.swt/src/org/argeo/cms/swt/CmsSwtUtils.java b/eclipse/org.argeo.cms.swt/src/org/argeo/cms/swt/CmsSwtUtils.java index a94d70706..5d964090b 100644 --- a/eclipse/org.argeo.cms.swt/src/org/argeo/cms/swt/CmsSwtUtils.java +++ b/eclipse/org.argeo.cms.swt/src/org/argeo/cms/swt/CmsSwtUtils.java @@ -1,16 +1,22 @@ package org.argeo.cms.swt; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; +import java.util.StringTokenizer; -import org.argeo.api.cms.CmsStyle; -import org.argeo.api.cms.CmsTheme; -import org.argeo.api.cms.CmsView; +import org.argeo.api.cms.ux.CmsIcon; +import org.argeo.api.cms.ux.CmsStyle; +import org.argeo.api.cms.ux.CmsTheme; +import org.argeo.api.cms.ux.CmsView; import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; +import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.RowData; @@ -19,25 +25,25 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Layout; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Widget; /** SWT utilities. */ public class CmsSwtUtils { + /* + * THEME AND VIEW + */ - /** Singleton. */ - private CmsSwtUtils() { - } - - public static CmsTheme getCmsTheme(Composite parent) { - CmsTheme theme = (CmsTheme) parent.getData(CmsTheme.class.getName()); + public static CmsSwtTheme getCmsTheme(Composite parent) { + CmsSwtTheme theme = (CmsSwtTheme) parent.getData(CmsTheme.class.getName()); if (theme == null) { // find parent shell Shell topShell = parent.getShell(); while (topShell.getParent() != null) topShell = (Shell) topShell.getParent(); - theme = (CmsTheme) topShell.getData(CmsTheme.class.getName()); + theme = (CmsSwtTheme) topShell.getData(CmsTheme.class.getName()); parent.setData(CmsTheme.class.getName(), theme); } return theme; @@ -78,6 +84,10 @@ public class CmsSwtUtils { shell.setData(CmsView.class.getName(), view); } + /* + * EVENTS + */ + /** Sends an event via {@link CmsView#sendEvent(String, Map)}. */ public static void sendEventOnSelect(Control control, String topic, Map properties) { SelectionListener listener = (Selected) (e) -> { @@ -99,13 +109,48 @@ public class CmsSwtUtils { sendEventOnSelect(control, topic, properties); } + /* + * ICONS + */ + /** Get a small icon from this theme. */ + public static Image getSmallIcon(CmsTheme theme, CmsIcon icon) { + return ((CmsSwtTheme) theme).getSmallIcon(icon); + } + + /** Get a big icon from this theme. */ + public static Image getBigIcon(CmsTheme theme, CmsIcon icon) { + return ((CmsSwtTheme) theme).getBigIcon(icon); + } + + /* + * LAYOUT INDEPENDENT + */ + /** Takes the most space possible, depending on parent layout. */ + public static void fill(Control control) { + Layout parentLayout = control.getParent().getLayout(); + if (parentLayout == null) + throw new IllegalStateException("Parent layout is not set"); + if (parentLayout instanceof GridLayout) { + control.setLayoutData(fillAll()); + } else if (parentLayout instanceof FormLayout) { + control.setLayoutData(coverAll()); + } else { + throw new IllegalArgumentException("Unsupported parent layout " + parentLayout.getClass().getName()); + } + } + /* * GRID LAYOUT */ + /** A {@link GridLayout} without any spacing and one column. */ public static GridLayout noSpaceGridLayout() { return noSpaceGridLayout(new GridLayout()); } + /** + * A {@link GridLayout} without any spacing and multiple columns of unequal + * width. + */ public static GridLayout noSpaceGridLayout(int columns) { return noSpaceGridLayout(new GridLayout(columns, false)); } @@ -248,4 +293,23 @@ public class CmsSwtUtils { for (Control child : composite.getChildren()) child.dispose(); } + + /** Clean reserved URL characters for use in HTTP links. */ + public static String cleanPathForUrl(String path) { + StringTokenizer st = new StringTokenizer(path, "/"); + StringBuilder sb = new StringBuilder(); + while (st.hasMoreElements()) { + sb.append('/'); + String encoded = URLEncoder.encode(st.nextToken(), StandardCharsets.UTF_8); + encoded = encoded.replace("+", "%20"); + sb.append(encoded); + + } + return sb.toString(); + } + + /** Singleton. */ + private CmsSwtUtils() { + } + }