1 package com.sri.emo.wizard.wizardgateway; 2 3 import com.jcorporate.expresso.core.controller.Controller; 4 import com.jcorporate.expresso.core.controller.ControllerException; 5 import com.jcorporate.expresso.core.dbobj.ValidValue; 6 import com.jcorporate.expresso.core.misc.ConfigManager; 7 import com.sri.emo.controller.SelectionWizardManager; 8 import com.sri.emo.wizard.completion.management.CompletionEditor; 9 import com.sri.emo.wizard.completion.management.PromptChooseNode; 10 11 import java.util.ArrayList; 12 import java.util.List; 13 14 15 /*** 16 * Base class for the 'prompt add' 'do add' Wizard Gateway interfaces. 17 * 18 * @author Michael Rimov 19 */ 20 abstract public class AddWizardHandler { 21 22 /*** 23 * The name of the parameter that gets the 'wizard type' 24 */ 25 static final String PARAM_WIZARD_TYPE = "WizardType"; 26 27 /*** 28 * The key name for selection wizards. 29 */ 30 static final String SELECTION_WIZARD = "selectionWizard"; 31 32 /*** 33 * The key name for completion wizards. 34 */ 35 static final String COMPLETION_WIZARD = "completionWizard"; 36 37 38 /*** 39 * The list of wizards currently available to the system. 40 * <pre> 41 * wizardlist[x][0] = Key name 42 * wizardlist[x][1] = Description 43 * wizardlist[x][2] = Controller Class 44 * wizardlist[x][3] = State Name 45 * </pre> 46 * This list may eventually be dropped to a database table. 47 */ 48 private static final String[][] wizardList = new String[][]{ 49 {COMPLETION_WIZARD, "Completion Wizard", CompletionEditor.class.getName(), PromptChooseNode.STATE_NAME}, 50 {SELECTION_WIZARD, "Selection Wizard", SelectionWizardManager.class.getName(), SelectionWizardManager.STATE_PROMPT_ADDWIZARD} 51 }; 52 53 /*** 54 * The key index into the wizard list array. 55 */ 56 private static final int KEY_INDEX = 0; 57 58 /*** 59 * The description index into the wizard list array. 60 */ 61 private static final int DESCRIPTION_INDEX = 1; 62 63 /*** 64 * The classname index into the wizard list array. 65 */ 66 private static final int CLASSNAME_INDEX = 2; 67 68 /*** 69 * The statename index into the wizard list array. 70 */ 71 private static final int STATENAME_INDEX = 3; 72 73 74 private final Controller myOwner; 75 76 /*** 77 * Default constructor. 78 * 79 * @param owner The owning controller. 80 */ 81 public AddWizardHandler(final Controller owner) { 82 assert owner != null; 83 myOwner = owner; 84 } 85 86 /*** 87 * Retrieve the owning controller of this state handler. 88 * 89 * @return Controller 90 */ 91 protected Controller getOwner() { 92 return myOwner; 93 } 94 95 96 /*** 97 * Grab a common list among types of handlers. 98 * 99 * @return List 100 */ 101 protected List getWizardTypeList() { 102 final List returnValue = new ArrayList(); 103 for (int i = 0; i < wizardList.length; i++) { 104 returnValue.add(new ValidValue(wizardList[i][KEY_INDEX], wizardList[i][DESCRIPTION_INDEX])); 105 106 } 107 return returnValue; 108 } 109 110 /*** 111 * Retrieve the index intot he storage array based on the key. 112 * 113 * @param key String 114 * @return int 115 */ 116 private int getIndexFromKey(final String key) { 117 assert key != null; 118 119 for (int i = 0; i < wizardList.length; i++) { 120 if (key.equals(wizardList[i][KEY_INDEX])) { 121 return i; 122 } 123 } 124 125 throw new IllegalArgumentException("Could not find matching wizard for key: " + key); 126 127 } 128 129 /*** 130 * Grab the controller based on the key. 131 * 132 * @param key String 133 * @return Controller 134 * @throws ControllerException 135 */ 136 protected Controller getControllerForKey(String key) throws ControllerException { 137 int index = getIndexFromKey(key); 138 assert index >= 0; 139 140 Controller returnValue = ConfigManager.getControllerFactory().getController(wizardList[index][CLASSNAME_INDEX]); 141 assert returnValue != null; 142 143 return returnValue; 144 } 145 146 /*** 147 * Grab the state name based on the key. 148 * 149 * @param key String 150 * @return String 151 */ 152 protected String getStateNameForKey(String key) { 153 int index = getIndexFromKey(key); 154 assert index >= 0; 155 return wizardList[index][STATENAME_INDEX]; 156 157 } 158 }