001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.aspect.view.form;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.romaframework.aspect.session.SessionAspect;
025: import org.romaframework.aspect.session.SessionInfo;
026: import org.romaframework.aspect.session.SessionListener;
027: import org.romaframework.aspect.view.screen.Screen;
028: import org.romaframework.aspect.view.screen.ScreenContainer;
029: import org.romaframework.core.config.RomaApplicationContext;
030: import org.romaframework.core.flow.Controller;
031:
032: public class FormViewer implements SessionListener {
033:
034: private Map<Object, ScreenContainer> userView;
035: private SessionAspect sessionManager;
036:
037: private static FormViewer instance = new FormViewer();
038: private static Log log = LogFactory.getLog(FormViewer.class);
039:
040: protected FormViewer() {
041: sessionManager = RomaApplicationContext.getInstance().getBean(
042: SessionAspect.class);
043: userView = new HashMap<Object, ScreenContainer>();
044:
045: Controller.getInstance().registerListener(
046: SessionListener.class, this );
047: }
048:
049: public Screen getScreen() {
050: return getScreen(sessionManager.getActiveSessionInfo());
051: }
052:
053: public Screen getScreen(Object iSession) {
054: ScreenContainer container = userView.get(iSession);
055:
056: if (container == null)
057: return null;
058:
059: return container.getScreen();
060: }
061:
062: /**
063: * Change the current desktop.
064: *
065: * @param iScreen
066: */
067: public void setScreen(Screen iScreen) {
068: ScreenContainer current = userView.get(sessionManager
069: .getActiveSessionInfo());
070: if (iScreen != current.getScreen())
071: // DESKTOP CHANGED: SET IT
072: current.setScreen(iScreen);
073: }
074:
075: public void setScreenContainer(ScreenContainer iScreenContainer) {
076: userView.put(sessionManager.getActiveSessionInfo(),
077: iScreenContainer);
078: }
079:
080: public void display(String iArea, Object iForm) {
081: Object session = sessionManager.getActiveSessionInfo();
082:
083: if (session != null) {
084: ScreenContainer deskCont = userView.get(session);
085:
086: if (deskCont != null) {
087: Screen currentDesktop = deskCont.getScreen();
088: display(iArea, iForm, currentDesktop);
089: }
090: }
091: }
092:
093: public String display(String iArea, Object iForm, Screen iDesktop) {
094: return iDesktop.view(iArea, iForm);
095: }
096:
097: public void onSessionCreating(SessionInfo iSession) {
098: }
099:
100: /**
101: * Remove user session desktop if any
102: */
103: public void onSessionDestroying(SessionInfo iSession) {
104: ScreenContainer desk = userView.remove(iSession);
105: if (desk != null)
106: desk.destroy();
107:
108: if (log.isDebugEnabled())
109: log
110: .debug("[FormViewer.onSessionDestroying] Removed desktop container: "
111: + desk);
112:
113: }
114:
115: /**
116: * Get the singleton instance.
117: *
118: * @return The singleton instance.
119: */
120: public static FormViewer getInstance() {
121: return instance;
122: }
123: }
|