1 11 package org.eclipse.pde.ui.templates; 12 import java.net.MalformedURLException ; 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 16 import org.eclipse.jface.wizard.IWizardContainer; 17 import org.eclipse.jface.wizard.WizardPage; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.pde.internal.ui.PDEUIMessages; 20 21 42 43 public abstract class OptionTemplateSection extends BaseOptionTemplateSection { 44 private ArrayList pages = new ArrayList (); 45 46 private static class TemplatePage { 47 WizardPage page; 48 ArrayList options; 49 public TemplatePage() { 50 options = new ArrayList (); 51 } 52 } 53 54 57 public OptionTemplateSection() { 58 } 59 60 70 public abstract String getSectionId(); 71 72 79 protected String getTemplateDirectory() { 80 String schemaVersion = model.getPluginBase().getSchemaVersion(); 81 if (schemaVersion != null) 82 return "templates_" + schemaVersion; return "templates"; } 85 90 protected abstract URL getInstallURL(); 91 92 101 public URL getTemplateLocation() { 102 URL url = getInstallURL(); 103 try { 104 String location = getTemplateDirectory() + "/" + getSectionId() + "/"; return new URL (url, location); 107 } catch (MalformedURLException e) { 108 return null; 109 } 110 } 111 119 public WizardPage getPage(int pageIndex) { 120 if (pageIndex < 0 || pageIndex >= pages.size()) 121 return null; 122 TemplatePage tpage = (TemplatePage) pages.get(pageIndex); 123 return tpage.page; 124 } 125 126 140 public WizardPage createPage(int pageIndex) { 141 if (pageIndex < 0 || pageIndex >= pages.size()) 142 return null; 143 TemplatePage tpage = (TemplatePage) pages.get(pageIndex); 144 tpage.page = new OptionTemplateWizardPage(this, tpage.options, null); 145 return tpage.page; 146 } 147 165 public WizardPage createPage(int pageIndex, String helpContextId) { 166 if (pageIndex < 0 || pageIndex >= pages.size()) 167 return null; 168 TemplatePage tpage = (TemplatePage) pages.get(pageIndex); 169 tpage.page = new OptionTemplateWizardPage(this, tpage.options, 170 helpContextId); 171 return tpage.page; 172 } 173 179 public int getPageCount() { 180 return pages.size(); 181 } 182 183 193 public void setPageCount(int count) { 194 pages.clear(); 195 for (int i = 0; i < count; i++) { 196 pages.add(new TemplatePage()); 197 } 198 } 199 200 207 208 public TemplateOption[] getOptions(int pageIndex) { 209 if (pageIndex < 0 || pageIndex >= pages.size()) 210 return new TemplateOption[0]; 211 TemplatePage page = (TemplatePage) pages.get(pageIndex); 212 return (TemplateOption[]) page.options 213 .toArray(new TemplateOption[page.options.size()]); 214 } 215 216 223 224 public TemplateOption[] getOptions(WizardPage page) { 225 for (int i = 0; i < pages.size(); i++) { 226 TemplatePage tpage = (TemplatePage) pages.get(i); 227 if (tpage.page.equals(page)) 228 return getOptions(i); 229 } 230 return new TemplateOption[0]; 231 } 232 233 241 public int getPageIndex(TemplateOption option) { 242 for (int i = 0; i < pages.size(); i++) { 243 TemplatePage tpage = (TemplatePage) pages.get(i); 244 if (tpage.options.contains(option)) 245 return i; 246 } 247 return -1; 248 } 249 250 258 public String getLabel() { 259 String key = "template." + getSectionId() + ".name"; return getPluginResourceString(key); 261 } 262 271 public String getDescription() { 272 String key = "template." + getSectionId() + ".desc"; return getPluginResourceString(key); 274 } 275 284 protected void flagMissingRequiredOption(TemplateOption option) { 285 WizardPage page = null; 286 for (int i = 0; i < pages.size(); i++) { 287 TemplatePage tpage = (TemplatePage) pages.get(i); 288 ArrayList list = tpage.options; 289 if (list.contains(option)) { 290 page = tpage.page; 291 break; 292 } 293 } 294 if (page != null) { 295 page.setPageComplete(false); 296 String message = NLS.bind(PDEUIMessages.OptionTemplateSection_mustBeSet, option.getMessageLabel()); 297 page.setErrorMessage(message); 298 } 299 } 300 301 305 protected void resetPageState() { 306 if (pages.size() == 0) 307 return; 308 WizardPage firstPage = ((TemplatePage) pages.get(0)).page; 309 IWizardContainer container = firstPage.getWizard().getContainer(); 310 WizardPage currentPage = (WizardPage) container.getCurrentPage(); 311 currentPage.setErrorMessage(null); 312 currentPage.setPageComplete(true); 313 } 314 315 protected void registerOption(TemplateOption option, Object value, 316 int pageIndex) { 317 super.registerOption(option, value, pageIndex); 318 if (pageIndex >= 0 && pageIndex < pages.size()) { 319 TemplatePage tpage = (TemplatePage) pages.get(pageIndex); 320 tpage.options.add(option); 321 } 322 } 323 324 327 public void validateOptions(TemplateOption source) { 328 if (source.isRequired() && source.isEmpty()) { 329 flagMissingRequiredOption(source); 330 } 331 validateContainerPage(source); 332 } 333 334 private void validateContainerPage(TemplateOption source) { 335 TemplateOption[] allPageOptions = getOptions(0); 336 for (int i = 0; i < allPageOptions.length; i++) { 337 TemplateOption nextOption = allPageOptions[i]; 338 if (nextOption.isRequired() && nextOption.isEmpty()) { 339 flagMissingRequiredOption(nextOption); 340 return; 341 } 342 } 343 resetPageState(); 344 } 345 346 } | Popular Tags |