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.echo2.screen;
018:
019: import java.util.Iterator;
020: import java.util.LinkedHashMap;
021: import java.util.Map;
022:
023: import nextapp.echo2.app.Component;
024: import nextapp.echo2.app.WindowPane;
025: import nextapp.echo2.app.event.WindowPaneEvent;
026: import nextapp.echo2.app.event.WindowPaneListener;
027:
028: import org.romaframework.aspect.view.echo2.component.PopupWindow;
029: import org.romaframework.aspect.view.echo2.component.WindowFormContainer;
030: import org.romaframework.aspect.view.echo2.form.EntityForm;
031: import org.romaframework.aspect.view.echo2.look.LookAndFeelManager;
032: import org.romaframework.aspect.view.screen.Screen;
033: import org.romaframework.core.GlobalConstants;
034: import org.romaframework.core.flow.ObjectContext;
035:
036: import echopointng.ContentPaneEx;
037:
038: public abstract class Echo2Screen extends ContentPaneEx implements
039: WindowPaneListener, Screen {
040:
041: protected LinkedHashMap<String, WindowPane> popups;
042: protected String lastArea = BODY;
043:
044: public static final String BODY = "//body";
045: public static final String POPUP = "popup";
046: public static final String NULL = "null";
047:
048: public Echo2Screen() {
049: ObjectContext.getInstance().getComponent(
050: LookAndFeelManager.class).assignStyle(this );
051: }
052:
053: /**
054: * Display a component in the specified area
055: */
056: public abstract String view(String iArea, Component iComponent);
057:
058: /**
059: * Display a component in the specified area
060: */
061: public final String view(String iArea, Object iComponent) {
062: return view(iArea, (Component) iComponent);
063: }
064:
065: public void close(Object iWindow) {
066: }
067:
068: // @Override
069: // public void dispose() {
070: // if (popups != null)
071: // popups.clear();
072: // super.dispose();
073: // }
074:
075: public Object getComponentInArea(String iArea) {
076: if (iArea.startsWith(POPUP) && popups != null)
077: return popups.get(iArea);
078: return null;
079: }
080:
081: protected void displayPopUp(String iName, Component iComponent) {
082: if (iName == null)
083: iName = GlobalConstants.ROOT_CLASS;
084:
085: WindowPane window;
086:
087: if (popups == null) {
088: // CREATE ONCE THE POPUPS MAP
089: popups = new LinkedHashMap<String, WindowPane>();
090: window = null;
091: } else
092: // FIND THE POPUP IF ANY
093: window = popups.get(iName);
094:
095: if (window == null) {
096: // POPUP NOT FOUND: CREATE A NEW ONE
097: if (iComponent instanceof WindowPane) {
098: window = (WindowPane) iComponent;
099: } else {
100: // CREATE A WRAPPER WINDOW FOR OBJECT BEFORE TO DISPLAY
101: window = new PopupWindow((EntityForm) iComponent);
102: }
103:
104: window.addWindowPaneListener(this );
105: add(window);
106: popups.put(iName, window);
107: } else {
108: // REPLACE THE FORM BEHIND THE WIND
109: if (iComponent instanceof WindowFormContainer) {
110: WindowFormContainer windowFormContainer = (WindowFormContainer) iComponent;
111: ((WindowFormContainer) window).setForm(
112: windowFormContainer.getForm(),
113: windowFormContainer.getForm());
114: } else {
115: // window.removeAll();
116: // window.add(iComponent);
117: ((WindowFormContainer) window).setForm(
118: (EntityForm) iComponent,
119: (EntityForm) iComponent);
120: }
121: }
122: }
123:
124: public void windowPaneClosing(WindowPaneEvent e) {
125: // REMOVE THE WINDOW JUST CLOSED BY MAP
126: Object winToClose = e.getSource();
127:
128: // CHECK IF THE WINDOW TO REMOVE WAS ALSO THE LAST AREA. IF TRUE RESET LAST
129: // AREA
130: boolean reassignLastArea = false;
131: for (Map.Entry<String, WindowPane> w : popups.entrySet()) {
132: if (w.getValue().equals(winToClose)) {
133: if (w.getKey().equals(lastArea)) {
134: reassignLastArea = true;
135: break;
136: }
137: }
138: }
139: popups.values().remove(winToClose);
140:
141: if (reassignLastArea) {
142: String lastInMap = BODY;
143: Iterator<Map.Entry<String, WindowPane>> it = popups
144: .entrySet().iterator();
145: while (it.hasNext()) {
146: lastInMap = it.next().getKey();
147: }
148: lastArea = lastInMap;
149: }
150: }
151: }
|