]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms.ui/src/org/argeo/cms/util/CmsLink.java
Introduce argeo-node RPM packaging
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / util / CmsLink.java
index bd5052c5b9a79f1bf1b548e23a31fc7a0db09ffe..a395bd23095eb3296336d12b7af22533e3c8ce5a 100644 (file)
@@ -10,8 +10,8 @@ import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.CmsException;
-import org.argeo.cms.CmsStyles;
-import org.argeo.cms.CmsUiProvider;
+import org.argeo.cms.ui.CmsStyles;
+import org.argeo.cms.ui.CmsUiProvider;
 import org.eclipse.rap.rwt.RWT;
 import org.eclipse.rap.rwt.service.ResourceManager;
 import org.eclipse.swt.SWT;
@@ -22,13 +22,11 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
 
 /** A link to an internal or external location. */
 public class CmsLink implements CmsUiProvider {
        private final static Log log = LogFactory.getLog(CmsLink.class);
-       private BundleContext bc = FrameworkUtil.getBundle(getClass())
-                       .getBundleContext();
+       private BundleContext bundleContext;
 
        private String label;
        private String custom;
@@ -69,13 +67,17 @@ public class CmsLink implements CmsUiProvider {
        /** @return {@link Composite} with a single {@link Label} child. */
        @Override
        public Control createUi(final Composite parent, Node context) {
+               if (image != null && (imageWidth == null || imageHeight == null)) {
+                       throw new CmsException("Image is not properly configured."
+                                       + " Make sure bundleContext property is set and init() method has been called.");
+               }
+
                Composite comp = new Composite(parent, SWT.BOTTOM);
                comp.setLayout(CmsUtils.noSpaceGridLayout());
 
                Label link = new Label(comp, SWT.NONE);
                link.setData(RWT.MARKUP_ENABLED, Boolean.TRUE);
-               GridData layoutData = new GridData(SWT.CENTER, verticalAlignment, true,
-                               true);
+               GridData layoutData = new GridData(SWT.CENTER, verticalAlignment, true, true);
                if (image != null) {
                        layoutData.heightHint = imageHeight;
                        if (label == null)
@@ -94,8 +96,7 @@ public class CmsLink implements CmsUiProvider {
                // label
                StringBuilder labelText = new StringBuilder();
                if (target != null) {
-                       labelText
-                                       .append("<a style='color:inherit;text-decoration:inherit;' href=\"");
+                       labelText.append("<a style='color:inherit;text-decoration:inherit;' href=\"");
                        // if (!isUrl)
                        // labelText.append('#');
                        labelText.append(target);
@@ -104,8 +105,7 @@ public class CmsLink implements CmsUiProvider {
                if (image != null) {
                        registerImageIfNeeded();
                        String imageLocation = RWT.getResourceManager().getLocation(image);
-                       labelText.append("<img width='").append(imageWidth)
-                                       .append("' height='").append(imageHeight)
+                       labelText.append("<img width='").append(imageWidth).append("' height='").append(imageHeight)
                                        .append("' src=\"").append(imageLocation).append("\"/>");
 
                        // final Image img = loadImage(parent.getDisplay());
@@ -174,7 +174,7 @@ public class CmsLink implements CmsUiProvider {
                        // pure URL
                        url = new URL(image);
                } catch (MalformedURLException e1) {
-                       url = bc.getBundle().getResource(image);
+                       url = bundleContext.getBundle().getResource(image);
                }
 
                if (url == null)
@@ -183,6 +183,10 @@ public class CmsLink implements CmsUiProvider {
                return url;
        }
 
+       public void setBundleContext(BundleContext bundleContext) {
+               this.bundleContext = bundleContext;
+       }
+
        public void setLabel(String label) {
                this.label = label;
        }
@@ -217,8 +221,7 @@ public class CmsLink implements CmsUiProvider {
                } else if ("center".equals(vAlign)) {
                        verticalAlignment = SWT.CENTER;
                } else {
-                       throw new CmsException("Unsupported vertical allignment " + vAlign
-                                       + " (must be: top, bottom or center)");
+                       throw new CmsException("Unsupported vertical allignment " + vAlign + " (must be: top, bottom or center)");
                }
        }
 }