Improve links.
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 16 Jul 2021 08:18:27 +0000 (10:18 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 16 Jul 2021 08:18:27 +0000 (10:18 +0200)
core/org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteApp.java
core/org.argeo.suite.ui/src/org/argeo/suite/ui/SuiteUiUtils.java
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkImageManager.java
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkTextInterpreter.java
publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/DbkServlet.java

index dabd8afbce15714807fc6db7e477c88a39c112c0..0343f710592b9f77784a70480663fef3b3fc77dc 100644 (file)
@@ -365,7 +365,8 @@ public class SuiteApp extends AbstractCmsApp implements EventHandler {
                }
        }
 
-       private String nodeToState(Node node) {
+       // TODO move it to an internal package?
+       static String nodeToState(Node node) {
                return '/' + Jcr.getWorkspaceName(node) + Jcr.getPath(node);
        }
 
index 0cfee182596230c9875f0d87b84a49dc0ea966d5..884981081f07fe0f6f6e3bf2c42ec0f3015642e0 100644 (file)
@@ -16,11 +16,14 @@ import org.argeo.cms.ui.CmsView;
 import org.argeo.cms.ui.dialogs.LightweightDialog;
 import org.argeo.cms.ui.util.CmsEvent;
 import org.argeo.cms.ui.util.CmsIcon;
+import org.argeo.cms.ui.util.CmsLink;
+import org.argeo.cms.ui.util.CmsStyle;
 import org.argeo.cms.ui.util.CmsUiUtils;
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.entity.EntityNames;
 import org.argeo.entity.EntityType;
 import org.argeo.jcr.Jcr;
+import org.argeo.jcr.JcrException;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.suite.SuiteRole;
 import org.eclipse.swt.SWT;
@@ -222,6 +225,11 @@ public class SuiteUiUtils {
        }
 
        public static Label addPicture(Composite parent, Node fileNode, Integer maxWidth) throws RepositoryException {
+               return addPicture(parent, fileNode, maxWidth, null);
+       }
+
+       public static Label addPicture(Composite parent, Node fileNode, Integer maxWidth, Node link)
+                       throws RepositoryException {
                Node content = fileNode.getNode(Node.JCR_CONTENT);
                // TODO move it deeper in the middleware.
                if (!content.isNodeType(EntityType.box.get())) {
@@ -261,63 +269,87 @@ public class SuiteUiUtils {
                }
                Label img = new Label(parent, SWT.NONE);
                CmsUiUtils.markup(img);
-               img.setText(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
+               StringBuffer txt = new StringBuffer();
+               String target = toLink(link);
+               if (target != null)
+                       txt.append("<a href='").append(target).append("'>");
+               txt.append(CmsUiUtils.img(fileNode, width.toString(), height.toString()));
+               if (target != null)
+                       txt.append("</a>");
+               img.setText(txt.toString());
                if (parent.getLayout() instanceof GridLayout) {
                        GridData gd = new GridData(SWT.CENTER, SWT.CENTER, false, false);
                        gd.widthHint = width.intValue();
                        gd.heightHint = height.intValue();
                        img.setLayoutData(gd);
                }
-               img.addMouseListener(new MouseListener() {
-                       private static final long serialVersionUID = -1362242049325206168L;
 
-                       @Override
-                       public void mouseUp(MouseEvent e) {
-                       }
+               if (target == null)
+                       img.addMouseListener(new MouseListener() {
+                               private static final long serialVersionUID = -1362242049325206168L;
 
-                       @Override
-                       public void mouseDown(MouseEvent e) {
-                       }
+                               @Override
+                               public void mouseUp(MouseEvent e) {
+                               }
 
-                       @Override
-                       public void mouseDoubleClick(MouseEvent e) {
-                               LightweightDialog dialog = new LightweightDialog(img.getShell()) {
-
-                                       @Override
-                                       protected Control createDialogArea(Composite parent) {
-                                               parent.setLayout(new GridLayout());
-                                               ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
-                                               scroll.setLayoutData(CmsUiUtils.fillAll());
-                                               scroll.setLayout(CmsUiUtils.noSpaceGridLayout());
-                                               scroll.setExpandHorizontal(true);
-                                               scroll.setExpandVertical(true);
-                                               // scroll.setAlwaysShowScrollBars(true);
-
-                                               Composite c = new Composite(scroll, SWT.NONE);
-                                               scroll.setContent(c);
-                                               c.setLayout(new GridLayout());
-                                               c.setLayoutData(CmsUiUtils.fillAll());
-                                               Label bigImg = new Label(c, SWT.NONE);
-                                               CmsUiUtils.markup(bigImg);
-                                               bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
-                                                               Jcr.get(content, EntityNames.SVG_HEIGHT)));
-                                               bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
-                                               return bigImg;
-                                       }
-
-                                       @Override
-                                       protected Point getInitialSize() {
-                                               Point shellSize = img.getShell().getSize();
-                                               return new Point(shellSize.x - 100, shellSize.y - 100);
-                                       }
-
-                               };
-                               dialog.open();
-                       }
-               });
+                               @Override
+                               public void mouseDown(MouseEvent e) {
+                               }
+
+                               @Override
+                               public void mouseDoubleClick(MouseEvent e) {
+                                       LightweightDialog dialog = new LightweightDialog(img.getShell()) {
+
+                                               @Override
+                                               protected Control createDialogArea(Composite parent) {
+                                                       parent.setLayout(new GridLayout());
+                                                       ScrolledComposite scroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
+                                                       scroll.setLayoutData(CmsUiUtils.fillAll());
+                                                       scroll.setLayout(CmsUiUtils.noSpaceGridLayout());
+                                                       scroll.setExpandHorizontal(true);
+                                                       scroll.setExpandVertical(true);
+                                                       // scroll.setAlwaysShowScrollBars(true);
+
+                                                       Composite c = new Composite(scroll, SWT.NONE);
+                                                       scroll.setContent(c);
+                                                       c.setLayout(new GridLayout());
+                                                       c.setLayoutData(CmsUiUtils.fillAll());
+                                                       Label bigImg = new Label(c, SWT.NONE);
+                                                       CmsUiUtils.markup(bigImg);
+                                                       bigImg.setText(CmsUiUtils.img(fileNode, Jcr.get(content, EntityNames.SVG_WIDTH),
+                                                                       Jcr.get(content, EntityNames.SVG_HEIGHT)));
+                                                       bigImg.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
+                                                       return bigImg;
+                                               }
+
+                                               @Override
+                                               protected Point getInitialSize() {
+                                                       Point shellSize = img.getShell().getSize();
+                                                       return new Point(shellSize.x - 100, shellSize.y - 100);
+                                               }
+
+                                       };
+                                       dialog.open();
+                               }
+                       });
                return img;
        }
 
+       public static String toLink(Node node) {
+               try {
+                       return node != null ? "#" + CmsUiUtils.cleanPathForUrl(SuiteApp.nodeToState(node)) : null;
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot get link from " + node, e);
+               }
+       }
+
+       public static Control addLink(Composite parent, String label, Node node, CmsStyle style)
+                       throws RepositoryException {
+               String target = toLink(node);
+               CmsLink link = new CmsLink(label, target, style);
+               return link.createUi(parent, node);
+       }
+
        public static boolean isCoworker(CmsView cmsView) {
                boolean coworker = cmsView.doAs(() -> CurrentUser.isInRole(SuiteRole.coworker.dn()));
                return coworker;
index 08b3db7454c78fa3ce76746d2c37cdae88a9df15..25a466ab7695c3153cde5e565a28afb86a22422d 100644 (file)
@@ -123,7 +123,7 @@ public class DbkImageManager extends DefaultImageManager {
                        return fileUri.toString();
                // local
                Node fileNode = getFileNode(imageDataNode);
-               String url = getCleanDataPath(fileNode);
+               String url = CmsUiUtils.getDataPathForUrl(fileNode);
                return url;
        }
 
index c6fe0d6212c7c8b983e66a1867374be60c05df2c..b195692f863fb83a85a77fd5bc2ceccf8148f951 100644 (file)
@@ -30,6 +30,8 @@ import org.argeo.jcr.JcrException;
 public class DbkTextInterpreter implements TextInterpreter {
        private DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
 
+       private String linkCssClass = DbkType.link.name();
+
        @Override
        public void write(Item item, String content) {
                try {
@@ -199,7 +201,7 @@ public class DbkTextInterpreter implements TextInterpreter {
                                        String href = child.getProperty(DbkAttr.XLINK_HREF).getString();
                                        // TODO deal with other forbidden XML characters?
                                        href = href.replace("&", "&amp;");
-                                       sb.append("<a href=\"").append(href).append("\">");
+                                       sb.append("<a class='" + linkCssClass + "' href='").append(href).append("'>");
                                        readAsSimpleHtml(child, sb);
                                        sb.append("</a>");
                                }
index cb3dffa373ddd55eff74414ab86115467a584071..ca94c229652f33e6580c468d2a646bbb267c8911 100644 (file)
@@ -133,6 +133,7 @@ public class DbkServlet extends HttpServlet {
                                                        sb.append(req.getContextPath()).append(req.getServletPath()).append('/');
                                                        sb.append(themeId).append('/').append(cssPath).append(' ');
                                                }
+                                               // FIXME make it more generic
                                                sb.append("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap")
                                                                .append(' ');
                                                sb.append(