001: package fr.aliacom.dbmjui;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.beans.PropertyChangeListener;
005:
006: import org.apache.log4j.Logger;
007:
008: import fr.aliacom.common.ui.ITabbedPane;
009: import fr.aliacom.common.ui.ITable;
010: import fr.aliacom.dbmjui.ui.CommandStateHelper;
011: import fr.aliacom.form.common.FormContext;
012: import fr.aliacom.form.common.IForm;
013: import fr.aliacom.form.common.ToolkitManager;
014: import fr.aliacom.form.common.events.ITabbedPaneListener;
015:
016: /**
017: * Holds and provides references on DBMJUI major ui components.
018: *
019: * This component also handles instance un/registrations
020: * and selections in the instances list.
021: *
022: * @author tom
023: *
024: * (C) 2001, 2003 Thomas Cataldo
025: */
026: public final class Controller implements IInstanceSelectionListener,
027: IRegistryListener, ITabbedPaneListener, PropertyChangeListener {
028:
029: private static Controller singleton;
030: private ITable dbiList;
031: private ITable logs;
032: private DbInstance current;
033: private ITabbedPane documents;
034: private InstanceSelectionSupport iss;
035: private Logger log;
036:
037: static {
038: singleton = new Controller();
039: }
040:
041: /**
042: * Return the dbmjui controller instance.
043: *
044: * @return the controller instance
045: */
046: public static Controller getInstance() {
047: return singleton;
048: }
049:
050: private Controller() {
051: log = Logger.getLogger(Controller.class);
052: log.info("Creating DBMJUI Controller");
053: IForm form = ToolkitManager.getToolkit().getMainWindow();
054: FormContext ctx = form.getFormContext();
055: dbiList = (ITable) ctx.get("dbiList").getValue();
056: logs = (ITable) ctx.get("logs").getValue();
057: documents = (ITabbedPane) ctx.get("tabs").getValue();
058: DbRegistry registry = DbRegistry.getInstance();
059: registry.addRegistryListener(this );
060: iss = new InstanceSelectionSupport();
061: addInstanceSelectionListener(this );
062: documents.addTabbedPaneListener(this );
063: log.info("DBMJUI Controller created");
064: }
065:
066: /**
067: * When the instance selected in the dbi list changes,
068: * the controller will stop listening to state changes
069: * in the previously selected instance, and will listen to
070: * changes in the newly selected one.
071: *
072: * It will then request an update of the commands in the UI so that only
073: * those working with the state of selected instance are available.
074: *
075: * @see fr.aliacom.dbmjui.IInstanceSelectionListener#instanceSelected(fr.aliacom.dbmjui.DbInstance)
076: */
077: public void instanceSelected(DbInstance dbi) {
078: log.debug("instance selected");
079: int oldState = DbState.WARM_STATE;
080: if (current != null) {
081: current.getInformations().removePropertyChangeListener(
082: "state", this );
083: oldState = current.getInformations().getState();
084: }
085: current = dbi;
086: current.getInformations().addPropertyChangeListener("state",
087: this );
088: setCommandState(oldState, dbi.getInformations().getState());
089: }
090:
091: /**
092: * Called when an instance is registered in dbmjui.
093: * This might happen at application startup, when loading saved
094: * registrations, or at runtime, when using the registration wizard.
095: *
096: * @param dbi the newly registered instance
097: */
098: public void instanceRegistered(final DbInstance dbi) {
099: log.debug("instance registered");
100: final Runnable task = new Runnable() {
101: public void run() {
102: dbiList.addRow(dbi.getInformations());
103: }
104: };
105: ToolkitManager.getToolkit().asyncExec(task);
106: }
107:
108: /**
109: * @param dbi called when an instance is unregistered
110: */
111: public void instanceUnregistered(final DbInstance dbi) {
112: log.debug("instance unregistered");
113: final Runnable task = new Runnable() {
114: public void run() {
115: dbiList.removeRow(dbi.getInformations());
116: }
117: };
118: ToolkitManager.getToolkit().asyncExec(task);
119: }
120:
121: /**
122: * Allows scripts to easily get a reference on the selected instance.
123: *
124: * @return the instance selected in the dbi list, or null if none.
125: */
126: public DbInstance getCurrentInstance() {
127: return current;
128: }
129:
130: /**
131: * @see fr.aliacom.form.common.events.ITabbedPaneListener#tabAdded(int)
132: */
133: public void tabAdded(int tabIndex) {
134: if (current != null) {
135: documents.setTabTitle(current.getFullName() + " - "
136: + documents.getTabTitle(tabIndex), tabIndex);
137: }
138: }
139:
140: /**
141: * @see fr.aliacom.form.common.events.ITabbedPaneListener#tabRemoved(int)
142: */
143: public void tabRemoved(int tabIndex) {
144: }
145:
146: public void fireInstanceSelectionChangeEvent(DbInstance dbi) {
147: iss.fireInstanceSelectionChangeEvent(dbi);
148: }
149:
150: public void addInstanceSelectionListener(
151: IInstanceSelectionListener sl) {
152: iss.addInstanceSelectionListener(sl);
153: }
154:
155: public void removeInstanceSelectionListener(
156: IInstanceSelectionListener sl) {
157: iss.removeInstanceSelectionListener(sl);
158: }
159:
160: /**
161: * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
162: */
163: public void propertyChange(PropertyChangeEvent evt) {
164: log.debug("Controller : instance state change !");
165: setCommandState(((Integer) evt.getOldValue()).intValue(),
166: ((Integer) evt.getNewValue()).intValue());
167: }
168:
169: private void setCommandState(int oldState, int newState) {
170: CommandStateHelper.setCommandStates(oldState, newState);
171: }
172:
173: }
|