001: package org.gui4j.component;
002:
003: import java.awt.CardLayout;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import javax.swing.JPanel;
009:
010: import org.apache.commons.logging.Log;
011: import org.apache.commons.logging.LogFactory;
012: import org.gui4j.Gui4jCallBase;
013: import org.gui4j.core.Gui4jCall;
014: import org.gui4j.core.Gui4jComponentContainer;
015: import org.gui4j.core.Gui4jComponentInstance;
016: import org.gui4j.core.Gui4jQualifiedComponent;
017: import org.gui4j.core.Gui4jSwingContainer;
018: import org.gui4j.core.Gui4jThreadManager;
019: import org.gui4j.event.Gui4jEventListener;
020:
021: public final class Gui4jCardLayout extends Gui4jJComponent {
022: private static final String DFLT = "dflt";
023:
024: protected static final Log mLogger = LogFactory
025: .getLog(Gui4jCardLayout.class);
026: protected final List mConditions; // List(Gui4jCall)
027: protected final List mGui4jComponents; // List(Gui4jComponentInPath)
028: protected final Gui4jQualifiedComponent mGui4jComponentDefault;
029:
030: private Gui4jCall[] mRefresh;
031:
032: /**
033: * Constructor for Gui4jSwitch.
034: * @param gui4jComponentContainer
035: * @param id
036: * @param gui4jComponentDefault
037: */
038: public Gui4jCardLayout(
039: Gui4jComponentContainer gui4jComponentContainer, String id,
040: Gui4jQualifiedComponent gui4jComponentDefault) {
041: super (gui4jComponentContainer, JPanel.class, id);
042: mConditions = new ArrayList();
043: mGui4jComponents = new ArrayList();
044: assert gui4jComponentDefault != null;
045: mGui4jComponentDefault = gui4jComponentDefault;
046: }
047:
048: public void setRefresh(Gui4jCall[] refresh) {
049: mRefresh = refresh;
050: }
051:
052: public void addPlacement(Gui4jCall condition,
053: Gui4jQualifiedComponent gui4jComponentInPath) {
054: assert condition != null;
055: assert gui4jComponentInPath != null;
056: mConditions.add(condition);
057: mGui4jComponents.add(gui4jComponentInPath);
058: }
059:
060: /* (non-Javadoc)
061: * @see org.gui4j.core.Gui4jAbstractComponent#createComponentInstance(org.gui4j.core.Gui4jSwingContainer, org.gui4j.Gui4jCallBase, org.gui4j.core.Gui4jQualifiedComponent)
062: */
063: protected Gui4jComponentInstance createComponentInstance(
064: Gui4jSwingContainer gui4jSwingContainer,
065: Gui4jCallBase gui4jCallBase,
066: Gui4jQualifiedComponent gui4jComponentInPath) {
067: CardLayout cardLayout = new CardLayout();
068: JPanel panel = new JPanel(cardLayout);
069: Gui4jComponentInstance gui4jComponentInstance = new Gui4jComponentInstance(
070: gui4jSwingContainer, panel, gui4jComponentInPath);
071: {
072: Gui4jComponentInstance subInstance = gui4jSwingContainer
073: .getGui4jComponentInstance(gui4jComponentInPath
074: .getGui4jComponentPath(),
075: mGui4jComponentDefault);
076: panel.add(subInstance.getComponent(), DFLT);
077: }
078: int idx = 0;
079: for (Iterator it = mGui4jComponents.iterator(); it.hasNext(); idx++) {
080: Gui4jQualifiedComponent qualifiedComponent = (Gui4jQualifiedComponent) it
081: .next();
082: if (!qualifiedComponent.equals(mGui4jComponentDefault)) {
083: Gui4jComponentInstance subInstance = gui4jSwingContainer
084: .getGui4jComponentInstance(gui4jComponentInPath
085: .getGui4jComponentPath(),
086: qualifiedComponent);
087: panel.add(subInstance.getComponent(), "" + idx);
088: }
089: }
090: SwitchListener listener = new SwitchListener(
091: gui4jComponentInstance);
092: registerEvents(gui4jSwingContainer, gui4jCallBase, mRefresh,
093: listener);
094: listener.eventOccured();
095: return gui4jComponentInstance;
096: }
097:
098: class SwitchListener implements Gui4jEventListener {
099: private final Gui4jComponentInstance mGui4jComponentInstance;
100: private final Gui4jCallBase mGui4jCallBase;
101:
102: // private final Gui4jSwingContainer mGui4jSwingContainer;
103:
104: public SwitchListener(
105: Gui4jComponentInstance gui4jComponentInstance) {
106: this .mGui4jComponentInstance = gui4jComponentInstance;
107: this .mGui4jCallBase = gui4jComponentInstance
108: .getGui4jCallBase();
109: // this.mGui4jSwingContainer =
110: // gui4jComponentInstance.getGui4jSwingContainer();
111: }
112:
113: public void eventOccured() {
114: // evaluate conditions in given order and display first
115: // element where the condition is valid
116: boolean found = false;
117: int idx = 0;
118: for (Iterator itCondition = mConditions.iterator(), itComponent = mGui4jComponents
119: .iterator(); !found && itCondition.hasNext(); idx++) {
120: Gui4jCall condition = (Gui4jCall) itCondition.next();
121: Gui4jQualifiedComponent gui4jComponentInPath = (Gui4jQualifiedComponent) itComponent
122: .next();
123: Boolean result = (Boolean) condition.getValueNoParams(
124: mGui4jCallBase, Boolean.FALSE);
125: if (Boolean.TRUE.equals(result)) {
126: useGui4jComponent(idx, gui4jComponentInPath);
127: found = true;
128: }
129: }
130: if (!found) {
131: useGui4jComponent(idx, mGui4jComponentDefault);
132: }
133: }
134:
135: private void useGui4jComponent(int idx,
136: Gui4jQualifiedComponent gui4jComponentInPath) {
137: mLogger.debug("Using component "
138: + gui4jComponentInPath.getQualifiedId()
139: + " for cardLayout with id " + getId());
140: final JPanel panel = (JPanel) mGui4jComponentInstance
141: .getComponent();
142: final CardLayout cardLayout = (CardLayout) panel
143: .getLayout();
144: final String cardName = gui4jComponentInPath
145: .equals(mGui4jComponentDefault) ? DFLT : "" + idx;
146: Runnable run = new Runnable() {
147: public void run() {
148: cardLayout.show(panel, cardName);
149: }
150: };
151: Gui4jThreadManager.executeInSwingThreadAndWait(run);
152: }
153:
154: }
155: }
|