X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2FCmsApplication.java;h=c428a0bf95e622056ffe4f97a978300f76b1871e;hb=7eab8a52b56a763e0d0e5153d298ce658e41a22f;hp=af8fa1c99fc25963aa302535afe16d34d7a0c6e0;hpb=f26fa850fab8c3666e6cc27683991f2d11378e5b;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/CmsApplication.java b/org.argeo.cms/src/org/argeo/cms/CmsApplication.java index af8fa1c99..c428a0bf9 100644 --- a/org.argeo.cms/src/org/argeo/cms/CmsApplication.java +++ b/org.argeo.cms/src/org/argeo/cms/CmsApplication.java @@ -1,204 +1,405 @@ package org.argeo.cms; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.eclipse.gemini.blueprint.context.BundleContextAware; -import org.eclipse.rap.rwt.application.Application; -import org.eclipse.rap.rwt.application.Application.OperationMode; -import org.eclipse.rap.rwt.application.ApplicationConfiguration; -import org.eclipse.rap.rwt.application.EntryPointFactory; -import org.eclipse.rap.rwt.application.ExceptionHandler; -import org.eclipse.rap.rwt.client.WebClient; -import org.eclipse.rap.rwt.lifecycle.PhaseEvent; -import org.eclipse.rap.rwt.lifecycle.PhaseId; -import org.eclipse.rap.rwt.lifecycle.PhaseListener; -import org.eclipse.rap.rwt.service.ResourceLoader; -import org.osgi.framework.BundleContext; - -/** Configures an Argeo CMS RWT application. */ -public class CmsApplication implements CmsConstants, ApplicationConfiguration, - BundleContextAware { - final static Log log = LogFactory.getLog(CmsApplication.class); - - private Map entryPoints = new HashMap(); - private Map> entryPointsBranding = new HashMap>(); - private Map> styleSheets = new HashMap>(); - - private List resources = new ArrayList(); - - // private Bundle clientScriptingBundle; - private BundleContext bundleContext; - - public void configure(Application application) { - try { - application.setOperationMode(OperationMode.SWT_COMPATIBILITY); - application.setExceptionHandler(new CmsExceptionHandler()); - - // TODO load all pics under icons - // loading animated gif - application.addResource(LOADING_IMAGE, - createResourceLoader(LOADING_IMAGE)); - // empty image - application.addResource(NO_IMAGE, createResourceLoader(NO_IMAGE)); - - for (String resource : resources) { - // URL res = bundleContext.getBundle().getResource(resource); - // if (res == null) - // throw new CmsException("Resource " + resource - // + " not found"); - application.addResource(resource, new BundleResourceLoader( - bundleContext)); - if (log.isDebugEnabled()) - log.debug("Registered resource " + resource); - } - - // entry points - for (String entryPoint : entryPoints.keySet()) { - Map properties = new HashMap(); - if (entryPointsBranding.containsKey(entryPoint)) { - properties = entryPointsBranding.get(entryPoint); - if (properties.containsKey(WebClient.FAVICON)) { - String faviconRelPath = properties - .get(WebClient.FAVICON); - // URL res = bundleContext.getBundle().getResource( - // faviconRelPath); - application.addResource(faviconRelPath, - new BundleResourceLoader(bundleContext)); - if (log.isTraceEnabled()) - log.trace("Registered favicon " + faviconRelPath); - - } - - if (!properties.containsKey(WebClient.BODY_HTML)) - properties.put(WebClient.BODY_HTML, - DEFAULT_LOADING_BODY); - } - - application.addEntryPoint("/" + entryPoint, - entryPoints.get(entryPoint), properties); - log.info("Registered entry point /" + entryPoint); - } - - // stylesheets - for (String themeId : styleSheets.keySet()) { - List cssLst = styleSheets.get(themeId); - for (String css : cssLst) { - // URL res = bundleContext.getBundle().getResource(css); - // if (res == null) - // throw new CmsException("Stylesheet " + css - // + " not found"); - application.addStyleSheet(themeId, css, - new BundleResourceLoader(bundleContext)); - } - - } - - application.addPhaseListener(new CmsPhaseListener()); - - // registerClientScriptingResources(application); - } catch (RuntimeException e) { - // Easier access to initialisation errors - log.error("Unexpected exception when configuring RWT application.", - e); - throw e; - } +import org.argeo.cms.util.SimpleApp; + +/** + * Configures an Argeo CMS RWT application. + * + * @deprecated Use {@link SimpleApp} instead (same method signatures) + */ +@Deprecated +public class CmsApplication extends SimpleApp { + private final static Log log = LogFactory.getLog(CmsApplication.class); + + public CmsApplication() { + super(); + log.warn("org.argeo.cms.CmsApplication will be removed soon, use org.argeo.cms.util.SimpleApp"); } - // see Eclipse.org bug 369957 - // private void registerClientScriptingResources(Application application) { - // if (clientScriptingBundle != null) { - // String className = - // "org.eclipse.rap.clientscripting.internal.resources.ClientScriptingResources"; + // + // private Map> branding = new HashMap>(); + // private Map> styleSheets = new HashMap>(); + // + // private List resources = new ArrayList(); + // + // private BundleContext bundleContext; + // + // private Repository repository; + // private String workspace = null; + // private String basePath = "/"; + // private List roPrincipals = Arrays.asList("anonymous", + // "everyone"); + // private List rwPrincipals = Arrays.asList("everyone"); + // + // private CmsUiProvider header; + // private Map pages = new LinkedHashMap(); + // + // private Integer headerHeight = 40; + // + // // Managers + // private CmsImageManager imageManager = new ImageManagerImpl(); + // + // public void configure(Application application) { // try { - // Class resourceClass = clientScriptingBundle - // .loadClass(className); - // Method registerMethod = resourceClass.getMethod("register", - // Application.class); - // registerMethod.invoke(null, application); - // } catch (Exception exception) { - // throw new RuntimeException(exception); + // application.setOperationMode(OperationMode.SWT_COMPATIBILITY); + // application.setExceptionHandler(new CmsExceptionHandler()); + // + // // TODO load all pics under icons + // // loading animated gif + // application.addResource(LOADING_IMAGE, + // createResourceLoader(LOADING_IMAGE)); + // // empty image + // application.addResource(NO_IMAGE, createResourceLoader(NO_IMAGE)); + // + // for (String resource : resources) { + // application.addResource(resource, new BundleResourceLoader( + // bundleContext)); + // if (log.isDebugEnabled()) + // log.debug("Registered resource " + resource); // } + // + // Map defaultBranding = null; + // if (branding.containsKey("*")) + // defaultBranding = branding.get("*"); + // + // // entry points + // for (String page : pages.keySet()) { + // Map properties = defaultBranding != null ? new + // HashMap( + // defaultBranding) : new HashMap(); + // if (branding.containsKey(page)) { + // properties.putAll(branding.get(page)); // } + // // favicon + // if (properties.containsKey(WebClient.FAVICON)) { + // String faviconRelPath = properties.get(WebClient.FAVICON); + // application.addResource(faviconRelPath, + // new BundleResourceLoader(bundleContext)); + // if (log.isTraceEnabled()) + // log.trace("Registered favicon " + faviconRelPath); + // // } - - private static ResourceLoader createResourceLoader(final String resourceName) { - return new ResourceLoader() { - public InputStream getResourceAsStream(String resourceName) - throws IOException { - return getClass().getClassLoader().getResourceAsStream( - resourceName); - } - }; - } - - public void setEntryPoints( - Map entryPointFactories) { - this.entryPoints = entryPointFactories; - } - - public void setEntryPointsBranding( - Map> entryPointBranding) { - this.entryPointsBranding = entryPointBranding; - } - - public void setStyleSheets(Map> styleSheets) { - this.styleSheets = styleSheets; - } - - // public void setClientScriptingBundle(Bundle clientScriptingBundle) { - // this.clientScriptingBundle = clientScriptingBundle; + // + // // page title + // if (!properties.containsKey(WebClient.PAGE_TITLE)) + // properties.put( + // WebClient.PAGE_TITLE, + // Character.toUpperCase(page.charAt(0)) + // + page.substring(1)); + // + // // default body HTML + // if (!properties.containsKey(WebClient.BODY_HTML)) + // properties.put(WebClient.BODY_HTML, DEFAULT_LOADING_BODY); + // + // // + // // ADD ENTRY POINT + // // + // application.addEntryPoint("/" + page, new CmsEntryPointFactory( + // pages.get(page), repository, workspace, properties), + // properties); + // log.info("Registered entry point /" + page); // } - - public void setBundleContext(BundleContext bundleContext) { - this.bundleContext = bundleContext; - } - - public void setResources(List resources) { - this.resources = resources; - } - - class CmsExceptionHandler implements ExceptionHandler { - - @Override - public void handleException(Throwable throwable) { - CmsSession.current.get().exception(throwable); - } - - } - - class CmsPhaseListener implements PhaseListener { - private static final long serialVersionUID = -1966645586738534609L; - - @Override - public PhaseId getPhaseId() { - return PhaseId.RENDER; - } - - @Override - public void beforePhase(PhaseEvent event) { - CmsSession cmsSession = CmsSession.current.get(); - String state = cmsSession.getState(); - if (state == null) - cmsSession.navigateTo("~"); - } - - @Override - public void afterPhase(PhaseEvent event) { - } - } - - /* - * TEXTS - */ - private static String DEFAULT_LOADING_BODY = "" - + "" - + ""; + // + // // stylesheets + // for (String themeId : styleSheets.keySet()) { + // List cssLst = styleSheets.get(themeId); + // for (String css : cssLst) { + // application.addStyleSheet(themeId, css, + // new BundleResourceLoader(bundleContext)); + // } + // + // } + // } catch (RuntimeException e) { + // // Easier access to initialisation errors + // log.error("Unexpected exception when configuring RWT application.", + // e); + // throw e; + // } + // } + // + // public void init() throws RepositoryException { + // Session session = null; + // try { + // session = JcrUtils.loginOrCreateWorkspace(repository, workspace); + // VersionManager vm = session.getWorkspace().getVersionManager(); + // if (!vm.isCheckedOut("/")) + // vm.checkout("/"); + // JcrUtils.mkdirs(session, basePath); + // for (String principal : rwPrincipals) + // JcrUtils.addPrivilege(session, basePath, principal, + // Privilege.JCR_WRITE); + // for (String principal : roPrincipals) + // JcrUtils.addPrivilege(session, basePath, principal, + // Privilege.JCR_READ); + // + // for (String pageName : pages.keySet()) { + // try { + // initPage(session, pages.get(pageName)); + // session.save(); + // } catch (Exception e) { + // throw new CmsException( + // "Cannot initialize page " + pageName, e); + // } + // } + // + // } finally { + // JcrUtils.logoutQuietly(session); + // } + // } + // + // protected void initPage(Session adminSession, CmsUiProvider page) + // throws RepositoryException { + // if (page instanceof LifeCycleUiProvider) + // ((LifeCycleUiProvider) page).init(adminSession); + // } + // + // public void destroy() { + // for (String pageName : pages.keySet()) { + // try { + // CmsUiProvider page = pages.get(pageName); + // if (page instanceof LifeCycleUiProvider) + // ((LifeCycleUiProvider) page).destroy(); + // } catch (Exception e) { + // log.error("Cannot destroy page " + pageName, e); + // } + // } + // } + // + // public void setRepository(Repository repository) { + // this.repository = repository; + // } + // + // public void setWorkspace(String workspace) { + // this.workspace = workspace; + // } + // + // public void setCmsLogin(@SuppressWarnings("deprecation") CmsLogin + // cmsLogin) { + // // this.cmsLogin = cmsLogin; + // log.warn("cmsLogin" + // + + // " is deprecated and will be removed soon. Adapt your configuration ASAP."); + // } + // + // public void setHeader(CmsUiProvider header) { + // this.header = header; + // } + // + // public void setPages(Map pages) { + // this.pages = pages; + // } + // + // public void setBasePath(String basePath) { + // this.basePath = basePath; + // } + // + // public void setRoPrincipals(List roPrincipals) { + // this.roPrincipals = roPrincipals; + // } + // + // public void setRwPrincipals(List rwPrincipals) { + // this.rwPrincipals = rwPrincipals; + // } + // + // public void setHeaderHeight(Integer headerHeight) { + // this.headerHeight = headerHeight; + // } + // + // public void setBranding(Map> branding) { + // this.branding = branding; + // } + // + // public void setStyleSheets(Map> styleSheets) { + // this.styleSheets = styleSheets; + // } + // + // public void setBundleContext(BundleContext bundleContext) { + // this.bundleContext = bundleContext; + // } + // + // public void setResources(List resources) { + // this.resources = resources; + // } + // + // class CmsExceptionHandler implements ExceptionHandler { + // + // @Override + // public void handleException(Throwable throwable) { + // CmsSession.current.get().exception(throwable); + // } + // + // } + // + // private class CmsEntryPointFactory implements EntryPointFactory { + // private final CmsUiProvider page; + // private final Repository repository; + // private final String workspace; + // private final Map properties; + // + // public CmsEntryPointFactory(CmsUiProvider page, Repository repository, + // String workspace, Map properties) { + // this.page = page; + // this.repository = repository; + // this.workspace = workspace; + // this.properties = properties; + // } + // + // @Override + // public EntryPoint create() { + // CmsEntryPoint entryPoint = new CmsEntryPoint(repository, workspace, + // page, properties); + // entryPoint.setState(""); + // CmsSession.current.set(entryPoint); + // return entryPoint; + // } + // + // } + // + // private class CmsEntryPoint extends AbstractCmsEntryPoint { + // private Composite headerArea; + // private Composite bodyArea; + // private final CmsUiProvider uiProvider; + // + // public CmsEntryPoint(Repository repository, String workspace, + // CmsUiProvider uiProvider, Map factoryProperties) { + // super(repository, workspace, factoryProperties); + // this.uiProvider = uiProvider; + // } + // + // @Override + // protected void createContents(Composite parent) { + // try { + // getShell().getDisplay().setData(CmsSession.KEY, this); + // + // parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, + // true)); + // parent.setLayout(CmsUtils.noSpaceGridLayout()); + // + // headerArea = new Composite(parent, SWT.NONE); + // headerArea.setLayout(new FillLayout()); + // GridData headerData = new GridData(SWT.FILL, SWT.FILL, false, + // false); + // headerData.heightHint = headerHeight; + // headerArea.setLayoutData(headerData); + // refreshHeader(); + // + // bodyArea = new Composite(parent, SWT.NONE); + // bodyArea.setData(RWT.CUSTOM_VARIANT, CmsStyles.CMS_BODY); + // bodyArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, + // true)); + // // Should not be set here: it then prevent all children + // // composite to define a background color via CSS + // // bodyArea.setBackgroundMode(SWT.INHERIT_DEFAULT); + // bodyArea.setLayout(CmsUtils.noSpaceGridLayout()); + // refreshBody(); + // } catch (Exception e) { + // throw new CmsException("Cannot create entrypoint contents", e); + // } + // } + // + // @Override + // protected void refreshHeader() { + // if (headerArea == null) + // return; + // for (Control child : headerArea.getChildren()) + // child.dispose(); + // try { + // header.createUi(headerArea, getNode()); + // } catch (RepositoryException e) { + // throw new CmsException("Cannot refresh header", e); + // } + // headerArea.layout(true, true); + // } + // + // @Override + // protected void refreshBody() { + // if (bodyArea == null) + // return; + // // Exception + // Throwable exception = getException(); + // if (exception != null) { + // // new Label(bodyArea, SWT.NONE).setText("Unreachable state : " + // // + getState()); + // // if (getNode() != null) + // // new Label(bodyArea, SWT.NONE).setText("Context : " + // // + getNode()); + // // + // // Text errorText = new Text(bodyArea, SWT.MULTI | SWT.H_SCROLL + // // | SWT.V_SCROLL); + // // errorText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, + // // true, + // // true)); + // // StringWriter sw = new StringWriter(); + // // exception.printStackTrace(new PrintWriter(sw)); + // // errorText.setText(sw.toString()); + // // IOUtils.closeQuietly(sw); + // SystemNotifications systemNotifications = new SystemNotifications( + // bodyArea); + // systemNotifications.notifyException(exception); + // resetException(); + // return; + // // TODO report + // } + // + // // clear + // for (Control child : bodyArea.getChildren()) + // child.dispose(); + // bodyArea.setLayout(CmsUtils.noSpaceGridLayout()); + // + // String state = getState(); + // try { + // if (state == null) + // throw new CmsException("State cannot be null"); + // uiProvider.createUi(bodyArea, getNode()); + // } catch (RepositoryException e) { + // throw new CmsException("Cannot refresh body", e); + // } + // + // bodyArea.layout(true, true); + // } + // + // @Override + // protected Node getDefaultNode(Session session) + // throws RepositoryException { + // if (!session.hasPermission(basePath, "read")) { + // if (session.getUserID().equals("anonymous")) + // throw new LoginRequiredException(); + // else + // throw new CmsException("Unauthorized"); + // } + // return session.getNode(basePath); + // } + // + // @Override + // public CmsImageManager getImageManager() { + // return imageManager; + // } + // + // } + // + // private static ResourceLoader createResourceLoader(final String + // resourceName) { + // return new ResourceLoader() { + // public InputStream getResourceAsStream(String resourceName) + // throws IOException { + // return getClass().getClassLoader().getResourceAsStream( + // resourceName); + // } + // }; + // } + // + // /* + // * TEXTS + // */ + // private static String DEFAULT_LOADING_BODY = "" + // + + // "" + // + ""; }