Source code: javax/ide/wizard/WizardManager.java
1 package javax.ide.wizard;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Map;
6
7 import javax.ide.Service;
8 import javax.ide.command.Context;
9 import javax.ide.extension.ExtensionRegistry;
10 import javax.ide.spi.ProviderNotFoundException;
11 import javax.ide.util.MetaClass;
12 import javax.ide.wizard.spi.WizardHook;
13
14 /**
15 * The WizardManager provides the interface through which the IDE invokes
16 * Wizards.<p>
17 */
18 public class WizardManager extends Service
19 {
20 private final Map _wizardInstancesById = new HashMap();
21
22
23 /**
24 * Invokes a wizard with the specified execution context and
25 * parameters.<p>
26 *
27 * @param context The execution context for the invokable.
28 * @param id The id identifying the wizard.
29 *
30 * @return <code>true</code> if the wizard completed successfully,
31 * otherwise <code>false</code>.
32 *
33 */
34 public boolean invokeWizard( Context context, String id )
35 {
36 Wizard theWizard = findOrCreateWizard( id );
37 return theWizard.invoke( context );
38 }
39
40 /**
41 * Initialize the wizard manager.
42 */
43 protected final WizardHook getWizardHook()
44 {
45 return (WizardHook)ExtensionRegistry.getExtensionRegistry().getHook( WizardHook.ELEMENT );
46 }
47
48 private MetaClass getWizardClass( String id )
49 {
50 return getWizardHook().getWizardInfo( id ).getWizardClass();
51 }
52
53 /**
54 * Get the class names of all registered wizards.
55 *
56 * @return a collection of string class names of all registered wizards.
57 */
58 protected final Collection /*<String>*/ getAllWizardClasses()
59 {
60 return getWizardHook().getAllWizardClasses();
61 }
62
63 /**
64 * Find or create a wizard instance.
65 *
66 * @param id the id of the wizard to find or create.
67 * @return an existing instance of the Wizard or a new instance if none has
68 * been created.
69 *
70 * @throws IllegalArgumentException if no such wizard is registered.
71 * @throws IllegalStateException if the wizard could not be created.
72 */
73 protected final Wizard findOrCreateWizard( String id )
74 {
75 Wizard theWizard = (Wizard) _wizardInstancesById.get( id );
76 if ( theWizard == null )
77 {
78 MetaClass wizardClass = getWizardClass( id );
79 if ( wizardClass == null )
80 {
81 throw new IllegalArgumentException( "No such wizard: " + id );
82 }
83 try
84 {
85 theWizard = (Wizard) wizardClass.newInstance();
86 _wizardInstancesById.put( id, theWizard );
87 }
88 catch ( Exception e )
89 {
90 throw new IllegalStateException( "Failed to create wizard " + id );
91 }
92 }
93 return theWizard;
94 }
95
96 /**
97 * Get the wizard manager implementation for this IDE.
98 *
99 * @return the wizard manager implementation for this ide.
100 */
101 public static WizardManager getWizardManager()
102 {
103 try
104 {
105 return (WizardManager) getService( WizardManager.class );
106 }
107 catch ( ProviderNotFoundException lnfe )
108 {
109 lnfe.printStackTrace();
110 throw new IllegalStateException( "no wizard manager" );
111 }
112 }
113 }