001: /*
002: * Created on Mar 10, 2003
003: *
004: * Dbmjui is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License version 2 as
006: * published by the Free Software Foundation.
007: *
008: * Dbmjui is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public
014: * License along with dbmjui; see the file COPYING. If not,
015: * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
016: * Boston, MA 02111-1307, USA.
017: *
018: */
019: package fr.aliacom.dbmjui.ui;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.util.ArrayList;
026:
027: import org.apache.log4j.Logger;
028:
029: import fr.aliacom.commands.CommandPool;
030: import fr.aliacom.dbmjui.DbState;
031: import fr.aliacom.form.common.FormContext;
032: import fr.aliacom.form.common.ToolkitManager;
033:
034: /**
035: * This helper class will enable/disable the commands to
036: * reflect the state of the selected instance.
037: *
038: * @author tom
039: *
040: * (c) 2001, 2003 Thomas Cataldo
041: */
042: public final class CommandStateHelper {
043:
044: /** List all the script names used by commands disabled in ADMIN mode */
045: private static final ArrayList ADMIN_COMMANDS = new ArrayList();
046:
047: /** List all the script names used by commands disabled in OFFLINE mode */
048: private static final ArrayList OFFLINE_COMMANDS = new ArrayList();
049:
050: /** List all the script names used by commands disabled in ONLINE mode */
051: private static final ArrayList ONLINE_COMMANDS = new ArrayList();
052:
053: private static final Logger log = Logger
054: .getLogger(CommandStateHelper.class);
055:
056: static {
057: try {
058: initialiseCommandStateLists();
059: log.debug("Command state list read correctly.");
060: } catch (IOException e) {
061: e.printStackTrace();
062: log.debug("Cannot read disabled command list");
063: System.exit(1);
064: }
065: }
066:
067: private static void initialiseCommandStateLists()
068: throws IOException {
069: fillList(
070: ADMIN_COMMANDS,
071: ClassLoader
072: .getSystemResourceAsStream("ressources/adminCommands.txt"));
073: fillList(
074: OFFLINE_COMMANDS,
075: ClassLoader
076: .getSystemResourceAsStream("ressources/offlineCommands.txt"));
077: fillList(
078: ONLINE_COMMANDS,
079: ClassLoader
080: .getSystemResourceAsStream("ressources/onlineCommands.txt"));
081: }
082:
083: private static void fillList(ArrayList list, InputStream is)
084: throws IOException {
085: BufferedReader reader = new BufferedReader(
086: new InputStreamReader(is));
087: String line;
088: while ((line = reader.readLine()) != null) {
089: line = line.trim();
090: if (!(line.length() == 0) && !line.startsWith("#")) {
091: list.add(line);
092: }
093: }
094: reader.close();
095: }
096:
097: /**
098: * Change the UI state from oldState to newState.
099: * All the disabled oldState commands will be re-enabled,
100: * then all the newState commands will be disabled
101: *
102: * @param oldState one of the {@link DbState} constants
103: * @param newState one of the {@link DbState} constants
104: */
105: public static void setCommandStates(int oldState, int newState) {
106: setEnabled(getList(oldState), true);
107: setEnabled(getList(newState), false);
108: }
109:
110: /**
111: * Get the list of scripts disabled in a given database state.
112: *
113: * @param state one of the {@link DbState} constants
114: * @return the list of scripts disabled in the given mode
115: */
116: private static ArrayList getList(int state) {
117: switch (state) {
118: case DbState.COLD_STATE:
119: return ADMIN_COMMANDS;
120: case DbState.WARM_STATE:
121: return ONLINE_COMMANDS;
122: default:
123: case DbState.STOP_STATE:
124: return OFFLINE_COMMANDS;
125: }
126: }
127:
128: private static void setEnabled(ArrayList commandNames,
129: boolean enabled) {
130: FormContext ctx = ToolkitManager.getToolkit().getMainWindow()
131: .getFormContext();
132: for (int i = 0, n = commandNames.size(); i < n; i++) {
133: CommandPool.getInstance().setEnabled(ctx,
134: (String) commandNames.get(i), enabled);
135: }
136: }
137:
138: }
|