# CustomInsertWizard.java Sample

This topic inludes the source code for the CustomInsertWizard.java Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/ExtensionDevKit/ControlDevKit/ControlFeatures/insertWizardCustom/ide/

## Sample Source Code

`001 package insertWizardCustom.ide; 002 003 import com.bea.ide.Application; 004 import com.bea.ide.control.ControlWizard; 005 import com.bea.ide.control.EditorContext; 006 import com.bea.ide.filesystem.FileSvc; 007 import com.bea.ide.filesystem.IFile; 008 import com.bea.ide.ui.IURISelectionContext; 009 import com.bea.ide.util.URIUtil; 010 import com.bea.ide.util.swing.DialogUtil; 011 import com.bea.ide.workspace.WorkspaceSvc; 012 import java.awt.Dialog; 013 import java.awt.Frame; 014 import java.io.File; 015 import java.net.URI; 016 import java.net.URL; 017 import java.text.MessageFormat; 018 import java.util.StringTokenizer; 019 import javax.swing.JDialog; 020 import javax.swing.JFileChooser; 021 import javax.swing.JLabel; 022 023 /** 024 * A control wizard implementation that returns a full dialog to prompt 025 * the control's user for information when they are inserting or creating 026 * the control. Contrast this with the insertWizard.ide.ServerCheckWizard 027 * class in this application, which returns only a panel that will be 028 * included in a dialog constructed by the IDE. The full dialog that 029 * this implementation uses is defined as insertWizardCustom.ide.CustomInsertDialog. 030 * 031 * When implementing ControlWizard, you are responsible for prompting the 032 * user for control instance and JCX names, and for validating the names that 033 * the user specifies. 034 * 035 * Note that a ControlWizard implementation does not support allowing the user 036 * to specify an existing JCX file -- a new file must always be created. 037 */ 038 public class CustomInsertWizard extends ControlWizard 039 { 040 protected String m_variableName = null; 041 protected String m_jcxName = null; 042 protected String m_jcxPackageName = null; 043 protected String m_currentProjectPath = null; 044 protected URI m_activeDocumentUri = null; 045 private EditorContext m_context = null; 046 private ControlWizard.NameValidator m_validator = null; 047 protected boolean m_okayToInsert = false; 048 protected boolean m_nameHasBeenSet = false; 049 protected boolean m_packageHasBeenSet = false; 050 protected int m_requestedConfig = -1; 051 private Frame m_container = null; 052 053 public CustomInsertDialog m_insertDialog; 054 055 /** 056 * Called by the IDE to get possible configurations. 057 */ 058 public int getConfigurationInfo() 059 { 060 return (CONFIG_CREATE_EXTENSION_FILE | CONFIG_INSERT_INSTANCE); 061 } 062 063 /** 064 * Called by the IDE to set the insert configuration 065 * requested by the user. 066 * 067 * @param config A sum of the ControlWizardSimple configuration constants that 068 * represent the user's insert request. 069 */ 070 public void setConfiguration(int config) 071 { 072 m_requestedConfig = config; 073 } 074 075 /** 076 * Called by the IDE to get the custom dialog to show. 077 * 078 * @return The insert dialog to display. 079 */ 080 public Dialog getDialog(Frame container) 081 { 082 m_insertDialog = new CustomInsertDialog(container, this); 083 m_container = container; 084 return m_insertDialog; 085 } 086 087 /** 088 * Called by the IDE to get the name of the interface defined 089 * in the JCX. 090 * 091 * @return The control JCX and interface name. 092 */ 093 public String getExtensionName() 094 { 095 m_jcxName = m_insertDialog.m_jcxName; 096 return m_jcxName; 097 } 098 099 /** 100 * Called by the IDE to get the control instance name specified by 101 * the user in the insert dialog. 102 * 103 * @return The control instance name specified by the user. 104 */ 105 public String getInstanceName() 106 { 107 m_variableName = m_insertDialog.m_variableName; 108 return m_variableName; 109 } 110 111 /** 112 * Called by the IDE to set the package name. This value is 113 * used in the JCX template's <code>package</code> statement. 114 * 115 * @param packageName The package name. 116 */ 117 public void setPackage(String packageName) 118 { 119 if (!m_packageHasBeenSet) 120 { 121 if (m_activeDocumentUri != null) 122 { 123 String uriPath = m_activeDocumentUri.getPath(); 124 if (uriPath.startsWith("/")) 125 { 126 uriPath = uriPath.substring(uriPath.indexOf("/") + 1); 127 } 128 } 129 if (packageName.endsWith("/")) 130 { 131 packageName = packageName.substring(0, packageName.lastIndexOf("/")); 132 } 133 if (packageName.endsWith(".jcx")) 134 { 135 packageName = packageName.substring(0, packageName.lastIndexOf("/")); 136 } 137 if (packageName.indexOf("/") > 0) 138 { 139 if (packageName.endsWith("/")) 140 { 141 packageName = packageName.substring(0, packageName.lastIndexOf("/")); 142 } 143 packageName = packageName.replaceAll("/", "."); 144 } 145 m_jcxPackageName = packageName; 146 m_packageHasBeenSet = true; 147 } 148 } 149 150 /** 151 * Displays a yes/no dialog. Used to ask the user if they want to choose a folder 152 * for their JCX. 153 * 154 * @return <code>true</code> if the user selected "Yes" in the dialog; <code>false</code> 155 * if they selected "No". 156 */ 157 private boolean showPackageAlert() 158 { 159 String alertText = "A Java control must reside in a subfolder of the " + 160 "project. Do you want to choose a subfolder in " + 161 "which to create this control?"; 162 return DialogUtil.showYesNoDialog(m_container, alertText); 163 } 164 165 /** 166 * Called to set the name of the JCX file to create. 167 * 168 * @param name The name specified by the user. 169 */ 170 public void setName(String name) 171 { 172 if (!m_nameHasBeenSet) 173 { 174 m_jcxName = name; 175 m_nameHasBeenSet = true; 176 } 177 } 178 179 /** 180 * Called by the IDE to pass in a validator that can be used to validate 181 * the names of control variable and JCX names. 182 * 183 * @param validator The validator instance. 184 */ 185 public void setNameValidator(ControlWizard.NameValidator validator) 186 { 187 m_validator = validator; 188 } 189 190 /** 191 * Gets the validator passed in by the IDE. This is called from the 192 * insert dialog. 193 * 194 * @return The validator instance. 195 */ 196 public ControlWizard.NameValidator getNameValidator() 197 { 198 return m_validator; 199 } 200 201 /** 202 * Called by the IDE just before it inserts the control. 203 * 204 * @return <code>true</code> if the control may be inserted; otherwise, <code>false</code>. 205 */ 206 public boolean onFinish() 207 { 208 if (m_insertDialog.m_controlInsertRequested) 209 { 210 if (!m_nameHasBeenSet) 211 { 212 m_jcxName = m_insertDialog.m_jcxName; 213 } 214 m_okayToInsert = true; 215 } 216 m_insertDialog.dispose(); 217 return m_okayToInsert; 218 } 219 220 /** 221 * Called by the IDE to pass in an EditorContext instance that contains 222 * information about, for example, the current project. 223 * 224 * @param context Details about the context into which this control 225 * is being inserted. 226 */ 227 public void setContext(EditorContext context) 228 { 229 m_context = context; 230 m_currentProjectPath = URIUtil.getFilePath(URIUtil.fromFile(m_context.getProjectDir())); 231 if (Application.getActiveDocument() != null) 232 { 233 m_activeDocumentUri = Application.getActiveDocument().getURI(); 234 } 235 } 236 237 /** 238 * Provides a place for the IDE to retrieve the content that it 239 * should insert into newly created JCX files. Here, the package name 240 * and interface name passed in by the IDE are inserted into a 241 * template using the MessageFormat class. 242 * 243 * @return The text of the JCX file to insert. 244 */ 245 public String getExtensionFileContent() 246 { 247 String jcxTemplate = this.getTemplate(); 248 String jcxContent = 249 MessageFormat.format(jcxTemplate, new Object[] {m_jcxPackageName, m_jcxName}); 250 return jcxContent; 251 } 252 253 /** 254 * Returns a template for text to be inserted into a new JCX file. 255 * 256 * @return The template. 257 */ 258 private String getTemplate() 259 { 260 String template = 261 "package {0}; \n\n" + 262 263 "public interface {1} extends com.bea.control.ControlExtension, insertWizardCustom.CustomWiz \n " + 264 "'{' \n\n" + 265 " static final long serialVersionUID = 1L; \n\n" + 266 " \n\n" + 267 " public String echoInput(String input); \n\n" + 268 "'}' \n "; 269 return template; 270 } 271 }`