org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/WizardContext.java
changeset 465 87920e15f8eb
parent 464 0b02f3d6f52c
child 466 129c94e78375
equal deleted inserted replaced
464:0b02f3d6f52c 465:87920e15f8eb
    11 import org.eclipse.core.databinding.DataBindingContext;
    11 import org.eclipse.core.databinding.DataBindingContext;
    12 import org.eclipse.core.databinding.UpdateValueStrategy;
    12 import org.eclipse.core.databinding.UpdateValueStrategy;
    13 import org.eclipse.core.databinding.beans.BeansObservables;
    13 import org.eclipse.core.databinding.beans.BeansObservables;
    14 import org.eclipse.core.databinding.observable.Observables;
    14 import org.eclipse.core.databinding.observable.Observables;
    15 import org.eclipse.core.databinding.observable.map.IObservableMap;
    15 import org.eclipse.core.databinding.observable.map.IObservableMap;
       
    16 import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
    16 import org.eclipse.core.databinding.observable.value.IObservableValue;
    17 import org.eclipse.core.databinding.observable.value.IObservableValue;
       
    18 import org.eclipse.core.databinding.observable.value.ValueDiff;
    17 import org.eclipse.core.databinding.validation.IValidator;
    19 import org.eclipse.core.databinding.validation.IValidator;
    18 import org.eclipse.core.resources.IContainer;
    20 import org.eclipse.core.resources.IContainer;
    19 import org.eclipse.core.resources.IFile;
    21 import org.eclipse.core.resources.IFile;
    20 import org.eclipse.core.resources.IFolder;
    22 import org.eclipse.core.resources.IFolder;
    21 import org.eclipse.core.resources.IProject;
    23 import org.eclipse.core.resources.IProject;
    37 import org.symbian.tools.tmw.ui.TMWCoreUI;
    39 import org.symbian.tools.tmw.ui.TMWCoreUI;
    38 import org.symbian.tools.tmw.ui.project.IProjectTemplate;
    40 import org.symbian.tools.tmw.ui.project.IProjectTemplate;
    39 import org.symbian.tools.tmw.ui.project.IProjectTemplateContext;
    41 import org.symbian.tools.tmw.ui.project.IProjectTemplateContext;
    40 
    42 
    41 public class WizardContext implements IProjectTemplateContext {
    43 public class WizardContext implements IProjectTemplateContext {
    42     //    public static final String CSS_FILE = "cssFile";
    44     public static final String PROJECT_NAME = "projectName";
    43     //    public static final String HTML_FILE = "htmlFile";
    45     public static final String RUNTIME = "runtime";
    44     //    public static final String JS_FILE = "jsFile";
       
    45     public static final String TEMPLATE = "template";
    46     public static final String TEMPLATE = "template";
       
    47     public static final String TEMPLATES = "templates";
    46     public static final String WIDGET_ID = "widgetId";
    48     public static final String WIDGET_ID = "widgetId";
    47     public static final String WIDGET_NAME = "widgetName";
    49     public static final String WIDGET_NAME = "widgetName";
    48     //    public static final String HOME_SCREEN = "homeScreen";
    50 
    49     public static final String RUNTIME = "runtime";
    51     private final Map<String, Object> extensions = new TreeMap<String, Object>();
    50     //    public static final String LIBRARIES = "libraries";
    52     private final Map<String, IObservableValue> observables = new TreeMap<String, IObservableValue>();
    51     public static final String PROJECT_NAME = "projectName";
       
    52     public static final String TEMPLATES = "templates";
       
    53 
       
    54     //    private String cssFile;
       
    55     private String projectName = "";
    53     private String projectName = "";
    56     //    private String htmlFile;
       
    57     //    private String jsFile;
       
    58     private final PropertyChangeSupport propertySupport = new PropertyChangeSupport(this);
    54     private final PropertyChangeSupport propertySupport = new PropertyChangeSupport(this);
       
    55     private IMobileWebRuntime runtime;
    59     private IProjectTemplate template = null;
    56     private IProjectTemplate template = null;
       
    57 
    60     private String widgetId;
    58     private String widgetId;
       
    59 
    61     private String widgetName;
    60     private String widgetName;
    62     private Map<String, String> extensions = new TreeMap<String, String>();
       
    63     //    private boolean homeScreen;
       
    64     private IMobileWebRuntime runtime;
       
    65 
       
    66     //    private Set<JSLibrary> libraries = new HashSet<JSLibrary>();
       
    67 
    61 
    68     public WizardContext() {
    62     public WizardContext() {
    69         IMobileWebRuntime[] runtimes = TMWCore.getRuntimesManager().getAllRuntimes();
    63         IMobileWebRuntime[] runtimes = TMWCore.getRuntimesManager().getAllRuntimes();
    70         if (runtimes.length > 0) {
    64         if (runtimes.length > 0) {
    71             runtime = runtimes[0];
    65             runtime = runtimes[0];
    72         }
    66         }
    73     }
    67     }
    74 
    68 
       
    69     public void addFile(IProject project, IPath name, InputStream contents, IProgressMonitor monitor)
       
    70             throws CoreException {
       
    71         monitor.beginTask(name.toOSString(), 100);
       
    72         final IFile file = project.getFile(name);
       
    73         if (!file.exists()) {
       
    74             create(file.getParent());
       
    75         }
       
    76         file.create(contents, false, new SubProgressMonitor(monitor, 100));
       
    77         monitor.done();
       
    78     }
       
    79 
    75     public void addPropertyChangeListener(PropertyChangeListener arg0) {
    80     public void addPropertyChangeListener(PropertyChangeListener arg0) {
    76         propertySupport.addPropertyChangeListener(arg0);
    81         propertySupport.addPropertyChangeListener(arg0);
    77     }
    82     }
    78 
    83 
    79     public void addPropertyChangeListener(String arg0, PropertyChangeListener arg1) {
    84     public void addPropertyChangeListener(String arg0, PropertyChangeListener arg1) {
    80         propertySupport.addPropertyChangeListener(arg0, arg1);
    85         propertySupport.addPropertyChangeListener(arg0, arg1);
    81     }
    86     }
    82 
    87 
       
    88     private void create(IContainer parent) throws CoreException {
       
    89         if (!parent.exists() && parent instanceof IFolder) {
       
    90             create(parent.getParent());
       
    91             ((IFolder) parent).create(false, true, new NullProgressMonitor());
       
    92         }
       
    93     }
       
    94 
       
    95     protected void createLabel(Composite root, String text) {
       
    96         Label label = new Label(root, SWT.NONE);
       
    97         label.setText(text);
       
    98     }
       
    99 
       
   100     private Text createText(Composite root, IObservableValue model, String propertyName,
       
   101             DataBindingContext bindingContext, AbstractDataBindingPage page, IValidator... validators) {
       
   102         Text text = new Text(root, SWT.BORDER);
       
   103         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   104         ISWTObservableValue view = SWTObservables.observeText(text, SWT.Modify);
       
   105         UpdateValueStrategy strategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
       
   106         NonEmptyStringValidator validator = new NonEmptyStringValidator(propertyName, page);
       
   107         strategy.setBeforeSetValidator(validators.length == 0 ? validator
       
   108                 : new CompoundValidator(validator, validators));
       
   109         bindingContext.bindValue(view, model, strategy, null);
       
   110         return text;
       
   111     }
       
   112 
       
   113     protected Text createText(Composite root, String property, String propertyName, DataBindingContext bindingContext,
       
   114             AbstractDataBindingPage page, IValidator... validators) {
       
   115         return createText(root, BeansObservables.observeValue(this, property), propertyName, bindingContext, page,
       
   116                 validators);
       
   117     }
       
   118 
       
   119     protected Text createTextForExt(Composite root, String property, String propertyName,
       
   120             DataBindingContext bindingContext, AbstractDataBindingPage page) {
       
   121         IObservableMap map = BeansObservables.observeMap(this, "extensions");
       
   122         IObservableValue entry = Observables.observeMapEntry(map, property, String.class);
       
   123         return createText(root, entry, propertyName, bindingContext, page);
       
   124     }
       
   125 
       
   126     private IProjectTemplate getDefaultTemplate(IMobileWebRuntime runtime) {
       
   127         return TMWCoreUI.getProjectTemplateManager().getDefaultTemplate(runtime);
       
   128     }
       
   129 
       
   130     private String getDefaultWidgetId() {
       
   131         String name = Util.removeSpaces(getWidgetName());
       
   132         return MessageFormat.format("com.company.{0}", name.length() > 0 ? name : "ApplicationName");
       
   133     }
       
   134 
       
   135     public Map<String, Object> getExtensions() {
       
   136         return extensions;
       
   137     }
       
   138 
       
   139     public Object getParameter(String parameter) {
       
   140         return getTemplateVars().get(parameter);
       
   141     }
       
   142 
       
   143     public String[] getParameterNames() {
       
   144         Set<String> keys = getTemplateVars().keySet();
       
   145         return keys.toArray(new String[keys.size()]);
       
   146     }
       
   147 
       
   148     public IObservableValue getParameterObservable(String name) {
       
   149         IObservableValue value = observables.get(name);
       
   150         if (value == null) {
       
   151             value = new ObservableParameter(name);
       
   152             observables.put(name, value);
       
   153         }
       
   154         return value;
       
   155     }
       
   156 
    83     public String getProjectName() {
   157     public String getProjectName() {
    84         return projectName;
   158         return projectName;
       
   159     }
       
   160 
       
   161     public IMobileWebRuntime getRuntime() {
       
   162         return runtime;
       
   163     }
       
   164 
       
   165     public IProjectTemplate getTemplate() {
       
   166         if (template == null) {
       
   167             return getDefaultTemplate(getRuntime());
       
   168         }
       
   169         return template;
       
   170     }
       
   171 
       
   172     public IProjectTemplate[] getTemplates() {
       
   173         return TMWCoreUI.getProjectTemplateManager().getProjectTemplates(getRuntime());
       
   174     }
       
   175 
       
   176     private Map<String, Object> getTemplateVars() {
       
   177         Map<String, Object> vars = new TreeMap<String, Object>();
       
   178 
       
   179         if (runtime != null) {
       
   180             vars.putAll(TMWCoreUI.getProjectTemplateManager().getDefaultTemplateParameterValues(runtime));
       
   181         }
       
   182         final IProjectTemplate t = getTemplate();
       
   183         if (t != null) {
       
   184             vars.putAll(t.getDefaultParameterValues());
       
   185         }
       
   186 
       
   187         vars.put("widgetName", getWidgetName());
       
   188         vars.put("widgetId", getWidgetId());
       
   189         vars.putAll(extensions);
       
   190 
       
   191         return vars;
       
   192     }
       
   193 
       
   194     public String getWidgetId() {
       
   195         if (widgetId == null) {
       
   196             return getDefaultWidgetId();
       
   197         }
       
   198         return widgetId;
       
   199     }
       
   200 
       
   201     public String getWidgetName() {
       
   202         return widgetName == null ? getProjectName() : widgetName;
       
   203     }
       
   204 
       
   205     public void initialize(IProject project, IProgressMonitor monitor) {
       
   206         final IProjectTemplate template = getTemplate();
       
   207         if (template != null) {
       
   208             template.init(project, this, monitor);
       
   209         }
       
   210     }
       
   211 
       
   212     public void putParameter(String key, Object value) {
       
   213         if (value != null) {
       
   214             extensions.put(key, value);
       
   215         } else {
       
   216             extensions.remove(key);
       
   217         }
       
   218     }
       
   219 
       
   220     public void removePropertyChangeListener(PropertyChangeListener arg0) {
       
   221         propertySupport.removePropertyChangeListener(arg0);
       
   222     }
       
   223 
       
   224     public void removePropertyChangeListener(String arg0, PropertyChangeListener arg1) {
       
   225         propertySupport.removePropertyChangeListener(arg0, arg1);
    85     }
   226     }
    86 
   227 
    87     public void setProjectName(String projectName) {
   228     public void setProjectName(String projectName) {
    88         String prev = getProjectName();
   229         String prev = getProjectName();
    89         String prevId = getWidgetId();
   230         String prevId = getWidgetId();
    96                 propertySupport.firePropertyChange(WIDGET_ID, getWidgetId(), prevId);
   237                 propertySupport.firePropertyChange(WIDGET_ID, getWidgetId(), prevId);
    97             }
   238             }
    98         }
   239         }
    99     }
   240     }
   100 
   241 
   101     //    public String getCssFile() {
   242     public void setRuntime(IMobileWebRuntime runtime) {
   102     //        if (cssFile == null) {
   243         final IProjectTemplate[] prevTemplates = getTemplates();
   103     //            if (template != null) {
   244         final IProjectTemplate prevTemplate;
   104     //                return template.getDefaultCssFile();
       
   105     //            }
       
   106     //        }
       
   107     //        return cssFile;
       
   108     //    }
       
   109     //
       
   110     private String getDefaultWidgetId() {
       
   111         String name = Util.removeSpaces(getWidgetName());
       
   112         return MessageFormat.format("com.company.{0}", name.length() > 0 ? name : "ApplicationName");
       
   113     }
       
   114 
       
   115     //
       
   116     //    public String getHtmlFile() {
       
   117     //        if (htmlFile == null) {
       
   118     //            if (template != null) {
       
   119     //                return template.getDefaultHtmlFile();
       
   120     //            }
       
   121     //        }
       
   122     //        return htmlFile;
       
   123     //    }
       
   124     //
       
   125     //    public String getJsFile() {
       
   126     //        if (jsFile == null) {
       
   127     //            if (template != null) {
       
   128     //                return template.getDefaultJsFile();
       
   129     //            }
       
   130     //        }
       
   131     //        return jsFile;
       
   132     //    }
       
   133 
       
   134     public IProjectTemplate getTemplate() {
       
   135         if (template == null) {
   245         if (template == null) {
   136             return getDefaultTemplate(getRuntime());
   246             prevTemplate = getTemplate();
   137         }
   247         } else {
   138         return template;
   248             prevTemplate = null;
   139     }
   249         }
   140 
   250         final IMobileWebRuntime prev = this.runtime;
   141     private IProjectTemplate getDefaultTemplate(IMobileWebRuntime runtime) {
   251         this.runtime = runtime;
   142         return TMWCoreUI.getProjectTemplateManager().getDefaultTemplate(runtime);
   252         propertySupport.firePropertyChange(RUNTIME, prev, runtime);
   143     }
   253         propertySupport.firePropertyChange(TEMPLATES, prevTemplates, getTemplates());
   144 
   254         if (prevTemplate == null) {
   145     public String getWidgetId() {
   255             propertySupport.firePropertyChange(TEMPLATE, prevTemplate, getTemplate());
   146         if (widgetId == null) {
   256         }
   147             return getDefaultWidgetId();
   257     }
   148         }
   258 
   149         return widgetId;
       
   150     }
       
   151 
       
   152     public String getWidgetName() {
       
   153         return widgetName == null ? getProjectName() : widgetName;
       
   154     }
       
   155 
       
   156     public void removePropertyChangeListener(PropertyChangeListener arg0) {
       
   157         propertySupport.removePropertyChangeListener(arg0);
       
   158     }
       
   159 
       
   160     public void removePropertyChangeListener(String arg0, PropertyChangeListener arg1) {
       
   161         propertySupport.removePropertyChangeListener(arg0, arg1);
       
   162     }
       
   163 
       
   164     //    public void setCssFile(String cssFile) {
       
   165     //        if (template != null && template.getDefaultCssFile().equals(cssFile)) {
       
   166     //            cssFile = null;
       
   167     //        }
       
   168     //        String prev = this.cssFile;
       
   169     //        this.cssFile = cssFile;
       
   170     //        propertySupport.firePropertyChange(CSS_FILE, cssFile, prev);
       
   171     //    }
       
   172     //
       
   173     //    public void setHtmlFile(String htmlFile) {
       
   174     //        if (template != null && template.getDefaultHtmlFile().equals(htmlFile)) {
       
   175     //            htmlFile = null;
       
   176     //        }
       
   177     //        String prev = this.htmlFile;
       
   178     //        this.htmlFile = htmlFile;
       
   179     //        propertySupport.firePropertyChange(HTML_FILE, htmlFile, prev);
       
   180     //    }
       
   181     //
       
   182     //    public void setJsFile(String jsFile) {
       
   183     //        if (template != null && template.getDefaultJsFile().equals(jsFile)) {
       
   184     //            jsFile = null;
       
   185     //        }
       
   186     //        String prev = this.jsFile;
       
   187     //        this.jsFile = jsFile;
       
   188     //        propertySupport.firePropertyChange(JS_FILE, jsFile, prev);
       
   189     //    }
       
   190     //
       
   191     public void setTemplate(IProjectTemplate template) {
   259     public void setTemplate(IProjectTemplate template) {
   192         //        String html = getHtmlFile();
       
   193         //        String js = getJsFile();
       
   194         //        String css = getCssFile();
       
   195         IProjectTemplate prev = this.template;
   260         IProjectTemplate prev = this.template;
   196         this.template = template;
   261         this.template = template;
   197         propertySupport.firePropertyChange(TEMPLATE, template, prev);
   262         propertySupport.firePropertyChange(TEMPLATE, template, prev);
   198         //        if (htmlFile == null) {
       
   199         //            propertySupport.firePropertyChange(HTML_FILE, getHtmlFile(), html);
       
   200         //        }
       
   201         //        if (jsFile == null) {
       
   202         //            propertySupport.firePropertyChange(JS_FILE, getJsFile(), js);
       
   203         //        }
       
   204         //        if (cssFile == null) {
       
   205         //            propertySupport.firePropertyChange(CSS_FILE, getCssFile(), css);
       
   206         //        }
       
   207         //        if (cssFile == null) {
       
   208         //            propertySupport.firePropertyChange(LIBRARIES, getLibraries(), libraries);
       
   209         //        }
       
   210     }
   263     }
   211 
   264 
   212     public void setWidgetId(String widgetId) {
   265     public void setWidgetId(String widgetId) {
   213         String prev = getWidgetId();
   266         String prev = getWidgetId();
   214         if (getDefaultWidgetId().equals(widgetId)) {
   267         if (getDefaultWidgetId().equals(widgetId)) {
   230         if (widgetId == null) {
   283         if (widgetId == null) {
   231             propertySupport.firePropertyChange(WIDGET_ID, getWidgetId(), prevId);
   284             propertySupport.firePropertyChange(WIDGET_ID, getWidgetId(), prevId);
   232         }
   285         }
   233     }
   286     }
   234 
   287 
   235     public void setExtensions(Map<String, String> extensions) {
   288     private final class ObservableParameter extends AbstractObservableValue {
   236         this.extensions = extensions;
   289         private final String name;
   237     }
   290 
   238 
   291         public ObservableParameter(String name) {
   239     public Map<String, String> getExtensions() {
   292             this.name = name;
   240         return extensions;
   293         }
   241     }
   294 
   242 
   295         public Object getValueType() {
   243     private Map<String, String> getTemplateVars() {
   296             return Object.class;
   244         Map<String, String> vars = new TreeMap<String, String>();
   297         }
   245 
   298 
   246         if (runtime != null) {
   299         @Override
   247             vars.putAll(TMWCoreUI.getProjectTemplateManager().getDefaultTemplateParameterValues(runtime));
   300         protected Object doGetValue() {
   248         }
   301             return getParameter(name);
   249         final IProjectTemplate t = getTemplate();
   302         }
   250         if (t != null) {
   303 
   251             vars.putAll(t.getDefaultParameterValues());
   304         @Override
   252         }
   305         protected void doSetValue(final Object value) {
   253 
   306             final Object prev = getParameter(name);
   254         vars.put("widgetName", getWidgetName());
   307             putParameter(name, value);
   255         vars.put("widgetId", getWidgetId());
   308             fireValueChange(new ValueDiff() {
   256         //        vars.put("mainHtml", getHtmlFileName());
   309 
   257         //        vars.put("mainCss", getCssFileName());
   310                 @Override
   258         //        vars.put("mainJs", getJsFileName());
   311                 public Object getOldValue() {
   259         //        vars.put("homeScreen", String.valueOf(isHomeScreen()));
   312                     return prev;
   260         vars.putAll(extensions);
   313                 }
   261 
   314 
   262         return vars;
   315                 @Override
   263     }
   316                 public Object getNewValue() {
   264 
   317                     return value;
   265     //
   318                 }
   266     //    public boolean isHomeScreen() {
   319             });
   267     //        return homeScreen;
       
   268     //    }
       
   269 
       
   270     //    public void setHomeScreen(boolean homeScreen) {
       
   271     //        boolean old = homeScreen;
       
   272     //        this.homeScreen = homeScreen;
       
   273     //        propertySupport.firePropertyChange(HOME_SCREEN, old, homeScreen);
       
   274     //    }
       
   275 
       
   276     //    public String getHtmlFileName() {
       
   277     //        return stripExtension(getHtmlFile(), "htm", "html");
       
   278     //    }
       
   279     //
       
   280     //    public String getJsFileName() {
       
   281     //        return stripExtension(getJsFile(), "js");
       
   282     //    }
       
   283     //
       
   284     //    public String getCssFileName() {
       
   285     //        return stripExtension(getCssFile(), "css");
       
   286     //    }
       
   287 
       
   288     //    private String stripExtension(String fileName, String... extensions) {
       
   289     //        for (String extension : extensions) {
       
   290     //            String extensionAndDot = "." + extension;
       
   291     //            if (fileName.endsWith(extensionAndDot)) {
       
   292     //                return fileName.substring(0, fileName.length() - extensionAndDot.length());
       
   293     //            }
       
   294     //        }
       
   295     //        return fileName;
       
   296     //    }
       
   297 
       
   298     //    public boolean isRequiredLibrary(JSLibrary element) {
       
   299     //        return template != null && template.requires(element);
       
   300     //    }
       
   301     //
       
   302     //    public Set<JSLibrary> getLibraries() {
       
   303     //        final Set<JSLibrary> set = new HashSet<JSLibrary>(libraries);
       
   304     //        if (template != null) {
       
   305     //            set.addAll(Arrays.asList(template.getRequiredLibraries()));
       
   306     //        }
       
   307     //        return set;
       
   308     //    }
       
   309 
       
   310     //    public void setLibraries(Set<JSLibrary> libraries) {
       
   311     //        Set<JSLibrary> prev = this.libraries;
       
   312     //        this.libraries = libraries;
       
   313     //        propertySupport.firePropertyChange(LIBRARIES, prev, libraries);
       
   314     //    }
       
   315 
       
   316     //    public Map<String, String> getLibraryParameters(JSLibrary library) {
       
   317     //        return Collections.emptyMap();
       
   318     //    }
       
   319 
       
   320     protected Text createText(Composite root, String property, String propertyName, DataBindingContext bindingContext,
       
   321             AbstractDataBindingPage page, IValidator... validators) {
       
   322         return createText(root, BeansObservables.observeValue(this, property), propertyName, bindingContext, page,
       
   323                 validators);
       
   324     }
       
   325 
       
   326     protected Text createTextForExt(Composite root, String property, String propertyName,
       
   327             DataBindingContext bindingContext, AbstractDataBindingPage page) {
       
   328         IObservableMap map = BeansObservables.observeMap(this, "extensions");
       
   329         IObservableValue entry = Observables.observeMapEntry(map, property, String.class);
       
   330         return createText(root, entry, propertyName, bindingContext, page);
       
   331     }
       
   332 
       
   333     private Text createText(Composite root, IObservableValue model, String propertyName,
       
   334             DataBindingContext bindingContext, AbstractDataBindingPage page, IValidator... validators) {
       
   335         Text text = new Text(root, SWT.BORDER);
       
   336         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
       
   337         ISWTObservableValue view = SWTObservables.observeText(text, SWT.Modify);
       
   338         UpdateValueStrategy strategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
       
   339         NonEmptyStringValidator validator = new NonEmptyStringValidator(propertyName, page);
       
   340         strategy.setBeforeSetValidator(validators.length == 0 ? validator
       
   341                 : new CompoundValidator(validator, validators));
       
   342         bindingContext.bindValue(view, model, strategy, null);
       
   343         return text;
       
   344     }
       
   345 
       
   346     protected void createLabel(Composite root, String text) {
       
   347         Label label = new Label(root, SWT.NONE);
       
   348         label.setText(text);
       
   349     }
       
   350 
       
   351     public void setRuntime(IMobileWebRuntime runtime) {
       
   352         final IProjectTemplate[] prevTemplates = getTemplates();
       
   353         final IProjectTemplate prevTemplate;
       
   354         if (template == null) {
       
   355             prevTemplate = getTemplate();
       
   356         } else {
       
   357             prevTemplate = null;
       
   358         }
       
   359         final IMobileWebRuntime prev = this.runtime;
       
   360         this.runtime = runtime;
       
   361         propertySupport.firePropertyChange(RUNTIME, prev, runtime);
       
   362         propertySupport.firePropertyChange(TEMPLATES, prevTemplates, getTemplates());
       
   363         if (prevTemplate == null) {
       
   364             propertySupport.firePropertyChange(TEMPLATE, prevTemplate, getTemplate());
       
   365         }
       
   366     }
       
   367 
       
   368     public IMobileWebRuntime getRuntime() {
       
   369         return runtime;
       
   370     }
       
   371 
       
   372     public IProjectTemplate[] getTemplates() {
       
   373         return TMWCoreUI.getProjectTemplateManager().getProjectTemplates(getRuntime());
       
   374     }
       
   375 
       
   376     public void initialize(IProject project, IProgressMonitor monitor) {
       
   377         final IProjectTemplate template = getTemplate();
       
   378         if (template != null) {
       
   379             template.init(project, this, monitor);
       
   380         }
       
   381     }
       
   382 
       
   383     public Object getParameter(String parameter) {
       
   384         return getTemplateVars().get(parameter);
       
   385     }
       
   386 
       
   387     public String[] getParameterNames() {
       
   388         Set<String> keys = getTemplateVars().keySet();
       
   389         return keys.toArray(new String[keys.size()]);
       
   390     }
       
   391 
       
   392     public void putParameter(String key, Object value) {
       
   393         if (value instanceof String) {
       
   394             extensions.put(key, (String) value);
       
   395         }
       
   396     }
       
   397 
       
   398     public void addFile(IProject project, IPath name, InputStream contents, IProgressMonitor monitor)
       
   399             throws CoreException {
       
   400         monitor.beginTask(name.toOSString(), 100);
       
   401         final IFile file = project.getFile(name);
       
   402         if (!file.exists()) {
       
   403             create(file.getParent());
       
   404         }
       
   405         file.create(contents, false, new SubProgressMonitor(monitor, 100));
       
   406         monitor.done();
       
   407     }
       
   408 
       
   409     private void create(IContainer parent) throws CoreException {
       
   410         if (!parent.exists() && parent instanceof IFolder) {
       
   411             create(parent.getParent());
       
   412             ((IFolder) parent).create(false, true, new NullProgressMonitor());
       
   413         }
   320         }
   414     }
   321     }
   415 }
   322 }