001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.session.SessionManager;
016: import org.wings.conf.CmsDetail;
017: import org.wings.conf.Configuration;
018: import org.wings.adapter.AbstractCmsAdapter;
019: import org.wings.adapter.CmsAdapter;
020:
021: import javax.xml.bind.Unmarshaller;
022: import javax.xml.bind.JAXBContext;
023: import javax.xml.bind.JAXBException;
024: import java.lang.reflect.Constructor;
025: import java.lang.reflect.InvocationTargetException;
026:
027: /**
028: * <code>CmsFrame<code>.
029: * <p/>
030: * User: raedler
031: * Date: 08.08.2007
032: * Time: 09:27:35
033: *
034: * @author raedler
035: * @version $Id
036: */
037: public class CmsFrame extends SFrame {
038:
039: private CmsLayout layout = new CmsLayout();
040:
041: /**
042: * Default constructor
043: */
044: public CmsFrame() {
045: setContentPane(new CmsForm());
046: getContentPane().setLayout(layout);
047:
048: Configuration cfg = readConfiguration();
049:
050: CmsDetail cmsConfig = cfg.getCmsDetail();
051:
052: CmsAdapter adapter = createAdapter(cmsConfig);
053: adapter.setFrame(this );
054: adapter.setConfiguration(cmsConfig);
055:
056: // Set the resource mapper which is the distinguishable cms adapter.
057: SessionManager.getSession().setResourceMapper(adapter);
058: }
059:
060: /**
061: * Reads the wings to cms configuration file and returns an object containing all required data.
062: *
063: * @return A configuration object with all required data to access the cms.
064: */
065: private Configuration readConfiguration() {
066: try {
067: JAXBContext ctx = JAXBContext
068: .newInstance(Configuration.class);
069: Unmarshaller unmarshaller = ctx.createUnmarshaller();
070: return (Configuration) unmarshaller.unmarshal(getClass()
071: .getClassLoader().getResourceAsStream(
072: "wings-2-cms.xml"));
073: } catch (JAXBException e) {
074: e.printStackTrace();
075: }
076: return null;
077: }
078:
079: private CmsAdapter createAdapter(CmsDetail cfg) {
080: Class type = cfg.getAdapter();
081:
082: assert type != null : "Adapter class cannot be null.";
083:
084: try {
085: if (AbstractCmsAdapter.class.isAssignableFrom(type)) {
086: Constructor constructor = type.getConstructor(
087: SFrame.class, STemplateLayout.class,
088: CmsDetail.class);
089: return (CmsAdapter) constructor.newInstance(this ,
090: layout, cfg);
091: } else {
092: return (CmsAdapter) type.newInstance();
093: }
094: } catch (NoSuchMethodException e) {
095: e.printStackTrace();
096: } catch (InstantiationException e) {
097: e.printStackTrace();
098: } catch (IllegalAccessException e) {
099: e.printStackTrace();
100: } catch (InvocationTargetException e) {
101: e.printStackTrace();
102: }
103: return null;
104: }
105:
106: @Override
107: public final SComponent add(SComponent c) {
108: throw new UnsupportedOperationException(
109: "This method won't be supported by CmsFrame. Use add(SComponent c, Object constraint) instead.");
110: }
111:
112: @Override
113: public void add(SComponent c, Object constraint) {
114: getContentPane().add(c, constraint);
115: }
116:
117: @Override
118: public SComponent add(SComponent c, int index) {
119: throw new UnsupportedOperationException(
120: "This method won't be supported by CmsFrame. Use add(SComponent c, Object constraint) instead.");
121: }
122:
123: @Override
124: public void add(SComponent c, Object constraint, int index) {
125: throw new UnsupportedOperationException(
126: "This method won't be supported by CmsFrame. Use add(SComponent c, Object constraint) instead.");
127: }
128: }
|