1 23 package org.enhydra.kelp.jbuilder.wizard; 24 25 import org.enhydra.tool.ToolBoxInfo; 27 import org.enhydra.tool.common.IconSet; 28 import org.enhydra.tool.common.ToolException; 29 import org.enhydra.tool.codegen.ValidationException; 30 import org.enhydra.tool.codegen.GeneratorException; 31 import org.enhydra.tool.codegen.wizard.CodeGenPanel; 32 33 import com.borland.jbuilder.JBuilderHelp; 35 import com.borland.jbuilder.build.JavaBuildPropertyGroup; 36 import com.borland.jbuilder.node.JBProject; 37 import com.borland.jbuilder.node.HTMLFileNode; 38 import com.borland.jbuilder.paths.JDKPathSet; 39 import com.borland.jbuilder.paths.PathSetManager; 40 import com.borland.jbuilder.wizard.project.JBProjectWizard; 41 import com.borland.primetime.help.ZipHelpBook; 42 import com.borland.primetime.help.ZipHelpTopic; 43 import com.borland.primetime.ide.Browser; 44 import com.borland.primetime.ide.BrowserIcons; 45 import com.borland.primetime.node.Node; 46 import com.borland.primetime.node.Project; 47 import com.borland.primetime.node.DuplicateNodeException; 48 import com.borland.primetime.util.VetoException; 49 import com.borland.primetime.vfs.Url; 50 import com.borland.primetime.wizard.BasicWizard; 51 import com.borland.primetime.wizard.BasicWizardPage; 52 import com.borland.primetime.wizard.Wizard; 53 import com.borland.primetime.wizard.WizardAction; 54 import com.borland.primetime.wizard.WizardDialog; 55 import com.borland.primetime.wizard.WizardHost; 56 import com.borland.primetime.wizard.WizardManager; 57 import com.borland.primetime.wizard.WizardPage; 58 59 import org.enhydra.kelp.KelpInfo; 61 import org.enhydra.kelp.common.Constants; 62 import org.enhydra.kelp.common.ResUtil; 63 import org.enhydra.kelp.common.node.OtterFolderNode; 64 import org.enhydra.kelp.common.node.OtterNodeFactory; 65 import org.enhydra.kelp.common.node.OtterProject; 66 import org.enhydra.kelp.common.codegen.ProjectGenUtil; 67 import org.enhydra.kelp.common.codegen.CodeGenException; 68 69 import org.enhydra.kelp.jbuilder.node.PrimeProject; 71 import org.enhydra.kelp.jbuilder.node.PrimeProject; 72 73 import java.awt.BorderLayout ; 75 import java.io.File ; 76 import java.util.Properties ; 77 import java.util.ArrayList ; 78 import java.util.Iterator ; 79 import javax.swing.JOptionPane ; 80 import java.util.ResourceBundle ; 81 82 abstract public class AbstractCodeGenWizard extends AbstractBasicWizard { 84 static ResourceBundle res = 85 ResourceBundle.getBundle("org.enhydra.kelp.jbuilder.Res"); 87 private ProjectGenUtil util = null; 89 private JBProject nativeProject = null; 90 91 public abstract String getHelpTopic(); 92 public abstract String getDisplayName(); 93 94 101 public void help(WizardPage wizardPage, WizardHost wizardHost) { 102 ZipHelpTopic topic = null; 103 ZipHelpBook book = null; 104 105 book = JBuilderHelp.createBook(Constants.KELP); 106 topic = new ZipHelpTopic(book, getHelpTopic()); 107 topic.show(wizardPage.getPageComponent(wizardHost)); 108 } 109 110 118 abstract public WizardPage invokeWizard(WizardHost host); 119 120 127 public WizardPage invokeCodeGenWizard(WizardHost host, ProjectGenUtil util) { 128 WizardPage wizardPage = null; 129 boolean invoke = true; 130 131 this.util = util; 132 if (!isProjectEmpty()) { 133 StringBuffer buf = new StringBuffer (); 134 int choice = JOptionPane.CANCEL_OPTION; 135 136 buf.append(res.getString("NotEmptyWarning1")); 137 buf.append(res.getString("NotEmptyWarning2")); 138 buf.append(res.getString("NotEmptyWarning3")); 139 choice = 140 JOptionPane.showConfirmDialog(host.getDialogParent(), 141 buf.toString(), 142 ResUtil.format(res.getString("_0_Wizard_Error"), getDisplayName()), 143 JOptionPane.WARNING_MESSAGE); 144 if (choice == JOptionPane.CANCEL_OPTION) { 145 invoke = false; 146 } 147 } 148 if (invoke) { 149 try { 150 setWizardTitle(ResUtil.format(res.getString("_0_Wizard"), 151 getDisplayName())); 152 if ((nativeProject != null) 153 && (nativeProject instanceof JBProject)) { 154 for (int i = 0; i < util.getWizardPanels().length; i++) { 155 LocalWizardPage page = new LocalWizardPage(); 156 157 page.setPanel(util.getWizardPanels()[i]); 158 addWizardPage(page); 159 } 160 wizardPage = super.invokeWizard(host); 161 } 162 } catch (CodeGenException e) { 163 JOptionPane.showMessageDialog(host.getDialogParent(), 164 e.getMessage(), 165 ResUtil.format(res.getString("_0_Wizard_Error"), getDisplayName()), 166 JOptionPane.WARNING_MESSAGE); 167 } 168 } 169 return wizardPage; 170 } 171 172 178 private boolean isProjectEmpty() { 179 Node[] nodes = null; 180 boolean empty = true; 181 182 if (nativeProject != null) { 183 nodes = nativeProject.getChildren(); 184 } 185 if (nodes != null) { 186 if (nodes.length > 1) { 187 empty = false; 188 } else if (nodes.length == 1) { 189 if (!(nodes[0] instanceof HTMLFileNode)) { 190 empty = false; 191 } 192 } 193 } 194 return empty; 195 } 196 197 203 protected OtterProject initProject(WizardHost host) { 204 OtterProject otterProj = null; 205 206 if (host.getBrowser().getActiveProject() == null) { 207 nativeProject = null; 208 } else if (host.getBrowser().getActiveProject() 209 instanceof JBProject) { 210 nativeProject = (JBProject) host.getBrowser().getActiveProject(); 211 } 212 if (nativeProject == null) { 213 WizardDialog dlg = null; 214 JBProjectWizard wiz = null; 215 216 wiz = new JBProjectWizard(); 217 dlg = new WizardDialog(host.getBrowser(), wiz); 218 dlg.show(); 219 if (host.getBrowser().getActiveProject() == null) { 220 nativeProject = null; 221 } else if (host.getBrowser().getActiveProject() 222 instanceof JBProject) { 223 nativeProject = 224 (JBProject) host.getBrowser().getActiveProject(); 225 } 226 } 227 if (nativeProject != null) { 228 if (nativeProject instanceof JBProject) { 229 otterProj = new PrimeProject((JBProject) nativeProject); 230 } 231 } 232 return otterProj; 233 } 234 235 241 public WizardPage finish(WizardPage currentPage, 242 WizardHost host) throws VetoException { 243 JDKPathSet jdk = null; 244 boolean continueBuild = true; 245 246 jdk = getTargetJDK(); 247 if (currentPage instanceof BasicWizardPage) { 248 BasicWizardPage basicPage = (BasicWizardPage) currentPage; 249 250 basicPage.checkPage(); 251 } 252 if (nativeProject != null && util != null) { 253 try { 254 if (getSteps().length > 0) { 255 util.getGenerator().setAddinSteps(getSteps()); 256 } 257 if (jdk != null) { 258 String javaPath = new String (); 259 260 javaPath = 261 jdk.getHomePath().getFileObject().getAbsolutePath(); 262 util.getGenerator().setJavaPath(javaPath); 263 } 264 util.generate(); 265 nativeSetupProject(jdk); 266 StringBuffer buf = new StringBuffer (); 267 268 buf.append(ResUtil.format(res.getString("AddedFiles"), 269 getDisplayName())); 270 buf.append('\n'); 271 buf.append(Constants.TAB4); 272 buf.append(nativeProject.getDisplayName()); 273 buf.append('\n'); 274 buf.append(res.getString("ReferReadme1")); 275 buf.append(ResUtil.format(res.getString("ReferReadme2"), 276 getDisplayName())); 277 JOptionPane.showMessageDialog(host.getDialogParent(), 278 buf.toString()); 279 openReadme(host.getBrowser()); 280 } catch (CodeGenException e) { 281 JOptionPane.showMessageDialog(this.wizardHost.getDialogParent(), 282 e.getMessage(), 283 ResUtil.format(res.getString("_0_Wizard_Error"), getDisplayName()), 284 JOptionPane.WARNING_MESSAGE); 285 throw new VetoException(); 286 } 287 } 288 return null; 289 } 290 291 295 private void nativeSetupProject(JDKPathSet jdk) { 296 File sourceDir = null; 297 if (!nativeProject.getPaths().putClassOnFullPath(ToolBoxInfo.getEnhydraMainClass())) { 298 299 } 301 if (!ToolBoxInfo.isEnhydra3()) { 302 if (jdk != null) { 303 nativeProject.getPaths().setJDKPathSet(jdk); 304 } 305 } 306 JavaBuildPropertyGroup.SYNC_DIR.setValue(nativeProject, 307 String.valueOf(false)); 308 } 309 310 private JDKPathSet getTargetJDK() { 311 ArrayList jdkList = PathSetManager.getJDKs(); 312 Iterator jdkIterator = jdkList.iterator(); 313 JDKPathSet jdk = null; 314 315 while (jdkIterator.hasNext()) { 316 Object next = jdkIterator.next(); 317 318 if (next instanceof JDKPathSet) { 319 jdk = (JDKPathSet) next; 320 if (jdk.getName().indexOf(Constants.JDK13) > 0) { 321 break; 322 } 323 } 324 } 325 return jdk; 326 } 327 328 private void openReadme(Browser browser) { 329 Node[] list = new Node[0]; 330 Node node = null; 331 332 list = nativeProject.getChildren(); 333 for (int i = 0; i < list.length; i++) { 334 if (list[i].getDisplayName().toLowerCase().endsWith(Constants.FILE_README)) { 335 node = list[i]; 336 break; 337 } 338 } 339 list = new Node[0]; 340 try { 341 browser.setActiveNode(node, true); 342 } catch (Exception e) { 343 e.printStackTrace(); 344 } 345 } 346 347 353 abstract public String [] getSteps(); 354 355 361 private class LocalWizardPage extends BasicWizardPage { 362 private CodeGenPanel panel; 363 364 368 public LocalWizardPage() { 369 BorderLayout layout = new BorderLayout (); 370 371 layout.setHgap(5); 372 layout.setVgap(5); 373 this.setLayout(layout); 374 if (KelpInfo.isToolBoxVersionInSynch()) { 375 this.setLargeIcon(IconSet.getOtterWizard()); 376 } 377 } 378 379 385 public void activated(WizardHost host) { 386 host.setFinishEnabled(true); 387 } 388 389 public void checkPage() throws VetoException { 390 try { 391 panel.validateOptionSet(); 392 } catch (ValidationException e) { 393 JOptionPane.showMessageDialog(panel.getTopLevelAncestor(), 394 e.getValidationMessage(), 395 ResUtil.format(res.getString("_0_Wizard_Error"), getDisplayName()), 396 JOptionPane.WARNING_MESSAGE); 397 throw new VetoException(e.getValidationMessage()); 398 } 399 } 400 401 protected void setPanel(CodeGenPanel p) { 402 panel = p; 403 setPageTitle(panel.getPageTitle()); 404 setInstructions(panel.getInstructions()); 405 add(panel, BorderLayout.CENTER); 406 } 407 408 protected CodeGenPanel getPanel() { 409 return panel; 410 } 411 412 } 413 } 414 | Popular Tags |