improved codetemplate wizards; new html tag wizards
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / EditElementWizard.java
1 /*
2  * $Id: EditElementWizard.java,v 1.1 2004-10-05 20:51:57 jsurfer Exp $
3  * Copyright Narushima Hironori. All rights reserved.
4  */
5 package net.sourceforge.phpeclipse.wizards.html;
6
7 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
8 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
9
10 import org.eclipse.core.resources.IFile;
11 import org.eclipse.jface.text.IDocument;
12 import org.eclipse.jface.text.ITextSelection;
13 import org.eclipse.jface.wizard.IWizardPage;
14 import org.eclipse.jface.wizard.Wizard;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IFileEditorInput;
17 import org.eclipse.ui.texteditor.ITextEditor;
18
19 /**
20  * EditElementWizard. TODO: privides extension point element editor. pluggable element edit page.
21  */
22 public class EditElementWizard extends Wizard {
23
24   static Object[] elementEditPages = new Object[] { 
25   //  "a", AElementWizardPage.class,
26   //  "img", ImgElementWizardPage.class,
27       "dl", ListElementWizardPage.class, 
28       "ul", ListElementWizardPage.class, 
29       "ol", ListElementWizardPage.class, 
30       "table", TableElementWizardPage.class };
31
32   String targetElemName;
33
34   ITextEditor htEditor;
35
36   EditElementWizardPage rootPage;
37
38   /**
39    * Second argument specify element name, If specify null, call new element edit wizard page.
40    */
41   public EditElementWizard(ITextEditor editor, String targetElemName) {
42     htEditor = editor;
43     this.targetElemName = targetElemName;
44
45     setWindowTitle("Edit HTML Element");
46     setDefaultPageImageDescriptor(PHPUiImages.getImageRegistry().getDescriptor("wizban/editelem_wiz.gif"));
47
48     setForcePreviousAndNextButtons(true);
49   }
50
51   public void addPages() {
52     if (targetElemName == null) {
53       rootPage = new NewElementWizardPage();
54     } else {
55       IDocument doc = getDocument();
56       rootPage = createElementEditPage(targetElemName);
57       rootPage.setEditType(EditElementWizardPage.MODIFY);
58     }
59     addPage(rootPage);
60   }
61
62   public boolean performFinish() {
63     IWizardPage page = rootPage;
64     for (IWizardPage p; (p = page.getNextPage()) != null;) {
65       page = p;
66     }
67     if (page instanceof EditElementWizardPage) {
68       ((EditElementWizardPage) page).performFinish();
69     }
70     return true;
71   }
72
73   public IDocument getDocument() {
74     return htEditor.getDocumentProvider().getDocument(htEditor.getEditorInput());
75   }
76
77   public ITextSelection getSelection() {
78     return (ITextSelection) htEditor.getSelectionProvider().getSelection();
79   }
80
81   public void setSelection(ITextSelection sel) {
82     htEditor.getSelectionProvider().setSelection(sel);
83   }
84
85   public IFile getCurrentEditFile() {
86     IEditorInput input = htEditor.getEditorInput();
87     return (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile() : null;
88   }
89
90   /**
91    * If not edit target returns UnknownElementWizardPage.
92    */
93   public EditElementWizardPage createElementEditPage(String elementName) {
94     EditElementWizardPage page = null;
95     try {
96       for (int i = 0; i < elementEditPages.length; i += 2) {
97         if (((String) elementEditPages[i]).equalsIgnoreCase(elementName)) {
98           Class klass = (Class) elementEditPages[i + 1];
99           page = (EditElementWizardPage) klass.newInstance();
100         }
101       }
102     } catch (InstantiationException e) {
103       PHPeclipsePlugin.log(e);
104     } catch (IllegalAccessException e) {
105       PHPeclipsePlugin.log(e);
106     }
107     if (page == null) {
108       page = new UnknownElementWizardPage();
109     }
110     page.setElementName(elementName);
111     page.setWizard(this);
112
113     return page;
114   }
115
116 }