001: package org.wings.portlet;
002:
003: import javax.portlet.RenderRequest;
004: import javax.swing.event.EventListenerList;
005:
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.wings.session.Session;
009: import org.wings.session.SessionManager;
010:
011: /**
012: * This class provides the actual portlet window state as events. The static
013: * Strings MAXIMIZED_WS, MINIMIZED_WS and NORMAL_WS are provided for comparing
014: * with the actual window state. Also custom window states can be used. To get
015: * an instance use getInstance() instead of the cunstrucor which is hidden. For
016: * getting notified about a window state change use the
017: * addWindowStateChangeListner Method
018: *
019: * @author <a href="mailto:marc.musch@mercatis.com">Marc Musch</a>
020: *
021: */
022: public class WindowStateProvider {
023:
024: private final transient static Log log = LogFactory
025: .getLog(WindowStateProvider.class);
026:
027: /**
028: * The String maximized
029: */
030: public static final String MAXIMIZED_WS = "maximized";
031:
032: /**
033: * The String minimized
034: */
035: public static final String MINIMIZED_WS = "minimized";
036:
037: /**
038: * The String normal
039: */
040: public static final String NORMAL_WS = "normal";
041:
042: /**
043: * Listeners intereted in an event fired if the window state changes
044: */
045: private EventListenerList listeners = new EventListenerList();
046:
047: /**
048: * The old window state, used so that only events are fired, if the window
049: * state really has changed
050: */
051: private String oldWindowState = NORMAL_WS;
052:
053: private WindowStateProvider() {
054:
055: }
056:
057: /**
058: * Use this method to get an instance of this class, the constructir is
059: * hidden.
060: *
061: * @return Instance of this class
062: */
063: public static WindowStateProvider getInstance() {
064: Session session = SessionManager.getSession();
065:
066: WindowStateProvider pwsp = (WindowStateProvider) session
067: .getProperty(Const.WINGS_SESSION_PROPERTY_WINDOW_STATE_PROVIDER);
068: if (pwsp != null) {
069: return pwsp;
070: } else {
071: WindowStateProvider newPwsp = new WindowStateProvider();
072: session.setProperty(
073: Const.WINGS_SESSION_PROPERTY_WINDOW_STATE_PROVIDER,
074: newPwsp);
075: return newPwsp;
076: }
077:
078: }
079:
080: /**
081: * Adds a new listner for window state changes
082: *
083: * @param listener -
084: * the listner for these events
085: */
086: public void addWindowStateChangeListener(
087: WindowStateListener listener) {
088: listeners.add(WindowStateListener.class, listener);
089: }
090:
091: /**
092: * Forces the delivering of the window change event, but only if the window
093: * state really has changed
094: */
095: public void updateWindowState() {
096: Session session = SessionManager.getSession();
097: RenderRequest request = (RenderRequest) session
098: .getProperty(Const.WINGS_SESSION_PROPERTY_RENDER_REQUEST);
099: if (request != null) {
100: String ws = request.getWindowState().toString();
101: log.debug("WingS-Portlet-Bridge: providing window state \""
102: + ws + "\"");
103: if (ws != null && !ws.equals(oldWindowState)) {
104: oldWindowState = ws;
105: notifyWindowState(new WindowStateEvent(this , ws));
106: }
107: }
108: }
109:
110: /**
111: * Notifies the listeners
112: *
113: * @param e -
114: * the event to deliver
115: */
116: private synchronized void notifyWindowState(WindowStateEvent e) {
117: for (WindowStateListener l : listeners
118: .getListeners(WindowStateListener.class))
119: l.windowStateChanged(e);
120: }
121:
122: }
|