1 2 24 package org.enhydra.tool.codegen.wizard; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 import org.enhydra.tool.common.SwingUtil; 29 import org.enhydra.tool.common.wizard.TBWizard; 30 import org.enhydra.tool.codegen.CodeGen; 31 import org.enhydra.tool.codegen.Generator; 32 import org.enhydra.tool.codegen.GeneratorException; 33 import org.enhydra.tool.codegen.ValidationException; 34 35 import javax.swing.*; 39 import javax.swing.border.*; 40 import java.awt.*; 41 import java.awt.event.ActionEvent ; 42 import java.awt.event.ActionListener ; 43 import java.io.File ; 44 import java.beans.*; 45 import java.util.ArrayList ; 46 import java.util.Arrays ; 47 import java.util.ResourceBundle ; 48 49 60 public class CodeGenWizard extends TBWizard implements RootEditor { 61 62 private static ResourceBundle cgRes = 64 ResourceBundle.getBundle("org.enhydra.tool.codegen.Res"); private final static String CODEGEN_TITLE = "Kelp Application Wizard"; 66 67 private File [] files = new File [0]; 69 private GenSelectionPanel selectionPanel = null; 70 private CodeGen codeGen = null; 71 private GenSelectionListener[] selectionListeners = 72 new GenSelectionListener[0]; 73 74 public static void main(String [] args) { 75 CodeGenWizard wizard = null; 76 77 try { 78 wizard = new CodeGenWizard(); 79 wizard.showDialog(null); 80 } catch (GeneratorException e) { 81 e.printStackTrace(System.err); 82 } 83 System.exit(0); 84 } 85 86 public CodeGenWizard(CodeGen cg) { 87 super(); 88 codeGenInit(); 89 setCodeGen(cg); 90 } 91 92 public CodeGenWizard() throws GeneratorException { 93 super(); 94 codeGenInit(); 95 setCodeGen(new CodeGen()); 96 } 97 98 public boolean isAllowRootEdit() { 100 return getCodeGenDeck().isEnabled(); 101 } 102 103 public void setAllowRootEdit(boolean allow) { 105 getCodeGenDeck().setAllowRootEdit(allow); 106 } 107 108 116 public File [] getGeneratedFiles() { 117 return files; 118 } 119 120 126 public Generator[] getGenerators() { 127 return codeGen.getGenerators(); 128 } 129 130 136 public void setCodeGen(CodeGen cd) { 137 codeGen = cd; 138 selectionPanel.setGenerators(codeGen.getGenerators()); 139 } 140 141 public CodeGen getCodeGen() { 142 return codeGen; 143 } 144 145 151 public Generator getSelection() { 152 return getCodeGenDeck().getSelection(); 153 } 154 155 166 public void setSelection(Generator selection) throws GeneratorException { 167 getCodeGenDeck().setSelection(selection); 168 if (selection != null) { 169 getCodeGenDeck().setSelectionPageHidden(true); 170 getInnerPanel().getDialog().setTitle(selection.getWizardTitle()); 171 } 172 } 173 174 public void back() { 176 if (getCodeGenDeck().isSelectionPageHidden()) { 177 178 } else if (getCodeGenDeck().getPageIndex() == 1) { 180 try { 181 Generator select = getCodeGenDeck().getSelection(); 182 setCodeGen(new CodeGen()); 183 selectionPanel.setSelection(select); 184 } catch (GeneratorException e) { 185 e.printStackTrace(System.err); 186 } 187 } 188 super.back(); 189 } 190 191 public void next() { 193 if (getCodeGenDeck().isSelectionPageShowing()) { 194 try { 195 Generator gen = selectionPanel.getSelection(); 196 197 codeGen.setSelection(gen); 198 notifySelectionListeners(gen); 199 200 gen = codeGen.getSelection(); 202 getCodeGenDeck().setSelection(gen); 203 super.next(); 204 } catch (GeneratorException e) { 205 JOptionPane.showMessageDialog(getInnerPanel(), 206 e.getMessage(), 207 cgRes.getString("Generator_Error"), 208 JOptionPane.ERROR_MESSAGE); 209 if (CodeGen.debug) { 210 e.printStackTrace(); 211 } 212 } 213 } else { 214 super.next(); 215 } 216 } 217 218 public void finish() { 220 Generator selection; 221 222 try { 223 getCodeGenDeck().writeOptionSet(); 224 selection = selectionPanel.getSelection(); 225 files = selection.generate(); 226 super.finish(); 227 } catch (ValidationException e) { 228 JOptionPane.showMessageDialog(getInnerPanel(), 229 e.getValidationMessage(), 230 cgRes.getString("Validation_Error"), 231 JOptionPane.ERROR_MESSAGE); 232 } catch (GeneratorException e) { 233 JOptionPane.showMessageDialog(getInnerPanel(), e.getMessage(), 234 cgRes.getString("Generation_Error"), 235 JOptionPane.ERROR_MESSAGE); 236 } 237 } 238 239 public static String getDefaultTitle() { 240 return CODEGEN_TITLE; 241 } 242 243 protected void clearAll() { 245 super.clearAll(); 246 selectionPanel.removeAll(); 247 selectionListeners = new GenSelectionListener[0]; 248 selectionPanel = null; 249 codeGen = null; 250 251 } 253 254 public String getTitle() { 256 return CodeGenWizard.getDefaultTitle(); 257 } 258 259 protected String getProgressTitle() { 261 return "Component Generation Progress"; 262 } 263 264 public synchronized void addSelectionListener(GenSelectionListener l) { 266 ArrayList list = null; 267 268 list = new ArrayList (Arrays.asList(selectionListeners)); 269 if (!list.contains(l)) { 270 list.add(l); 271 list.trimToSize(); 272 selectionListeners = new GenSelectionListener[list.size()]; 273 selectionListeners = 274 (GenSelectionListener[]) list.toArray(selectionListeners); 275 } 276 list.clear(); 277 } 278 279 public synchronized void removeSelecitonListener(GenSelectionListener l) { 280 ArrayList list = null; 281 282 list = new ArrayList (Arrays.asList(selectionListeners)); 283 if (list.contains(l)) { 284 list.remove(l); 285 list.trimToSize(); 286 selectionListeners = new GenSelectionListener[list.size()]; 287 selectionListeners = 288 (GenSelectionListener[]) list.toArray(selectionListeners); 289 } 290 list.clear(); 291 } 292 293 private void notifySelectionListeners(Generator gen) { 294 GenSelectionEvent event = null; 295 296 event = new GenSelectionEvent(this, gen); 297 for (int i = 0; i < selectionListeners.length; i++) { 298 selectionListeners[i].onSelection(event); 299 } 300 } 301 302 306 private void codeGenInit() { 307 CodeGenDeck newDeck = null; 308 CodeGenPage page = null; 309 310 newDeck = new CodeGenDeck(); 311 selectionPanel = new GenSelectionPanel(); 312 setDeck(newDeck); 313 page = new CodeGenPage(); 314 page.setPageTitle(selectionPanel.getPageTitle()); 315 page.setInstructions(selectionPanel.getInstructions()); 316 page.add(selectionPanel); 317 addWizardPage(page); 318 } 319 320 private CodeGenDeck getCodeGenDeck() { 321 return (CodeGenDeck) getDeck(); 322 } 323 324 } 325 | Popular Tags |