c2be1856748e0d5179f80980ddb8b21eb94eb1ae
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / wizards / html / EditElementWizard.java
1 /*
2  * $Id: EditElementWizard.java,v 1.3 2006-10-21 23:18:43 pombredanne 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
21  * element edit page.
22  */
23 public class EditElementWizard extends Wizard {
24
25         static Object[] elementEditPages = new Object[] {
26                         // "a", AElementWizardPage.class,
27                         // "img", ImgElementWizardPage.class,
28                         "dl", ListElementWizardPage.class, "ul",
29                         ListElementWizardPage.class, "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
40          * edit wizard page.
41          */
42         public EditElementWizard(ITextEditor editor, String targetElemName) {
43                 htEditor = editor;
44                 this.targetElemName = targetElemName;
45
46                 setWindowTitle("Edit HTML Element");
47                 setDefaultPageImageDescriptor(PHPUiImages
48                                 .getDescriptor("wizban/editelem_wiz.gif"));
49
50                 setForcePreviousAndNextButtons(true);
51         }
52
53         public void addPages() {
54                 if (targetElemName == null) {
55                         rootPage = new NewElementWizardPage();
56                 } else {
57                         IDocument doc = getDocument();
58                         rootPage = createElementEditPage(targetElemName);
59                         rootPage.setEditType(EditElementWizardPage.MODIFY);
60                 }
61                 addPage(rootPage);
62         }
63
64         public boolean performFinish() {
65                 IWizardPage page = rootPage;
66                 for (IWizardPage p; (p = page.getNextPage()) != null;) {
67                         page = p;
68                 }
69                 if (page instanceof EditElementWizardPage) {
70                         ((EditElementWizardPage) page).performFinish();
71                 }
72                 return true;
73         }
74
75         public IDocument getDocument() {
76                 return htEditor.getDocumentProvider().getDocument(
77                                 htEditor.getEditorInput());
78         }
79
80         public ITextSelection getSelection() {
81                 return (ITextSelection) htEditor.getSelectionProvider().getSelection();
82         }
83
84         public void setSelection(ITextSelection sel) {
85                 htEditor.getSelectionProvider().setSelection(sel);
86         }
87
88         public IFile getCurrentEditFile() {
89                 IEditorInput input = htEditor.getEditorInput();
90                 return (input instanceof IFileEditorInput) ? ((IFileEditorInput) input)
91                                 .getFile() : null;
92         }
93
94         /**
95          * If not edit target returns UnknownElementWizardPage.
96          */
97         public EditElementWizardPage createElementEditPage(String elementName) {
98                 EditElementWizardPage page = null;
99                 try {
100                         for (int i = 0; i < elementEditPages.length; i += 2) {
101                                 if (((String) elementEditPages[i])
102                                                 .equalsIgnoreCase(elementName)) {
103                                         Class klass = (Class) elementEditPages[i + 1];
104                                         page = (EditElementWizardPage) klass.newInstance();
105                                 }
106                         }
107                 } catch (InstantiationException e) {
108                         PHPeclipsePlugin.log(e);
109                 } catch (IllegalAccessException e) {
110                         PHPeclipsePlugin.log(e);
111                 }
112                 if (page == null) {
113                         page = new UnknownElementWizardPage();
114                 }
115                 page.setElementName(elementName);
116                 page.setWizard(this);
117
118                 return page;
119         }
120
121 }