improved codetemplate wizards; new html tag wizards
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / EditElementWizardPage.java
1 /*
2  * $Id: EditElementWizardPage.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 java.io.IOException;
8 import java.io.StringReader;
9
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.ParserConfigurationException;
12
13 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.jface.text.BadLocationException;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.ITextSelection;
20 import org.eclipse.jface.text.TextSelection;
21 import org.eclipse.jface.wizard.IWizard;
22 import org.eclipse.jface.wizard.WizardPage;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Text;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXException;
33
34 /**
35  * 
36  *  
37  */
38 public abstract class EditElementWizardPage extends WizardPage implements IPreviewer {
39
40   final public static int NEW = 0, MODIFY = 1;
41
42   private static DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
43
44   Composite extendComp;
45
46   Text preview;
47
48   private String elementName = null;
49
50   int editType = NEW;
51
52   protected EditElementWizardPage(String pageName) {
53     super(pageName);
54   }
55
56   public void createControl(Composite parent) {
57     Composite base = new Composite(parent, SWT.NONE);
58     setControl(base);
59     base.setLayout(new GridLayout(1, false));
60
61     // create child control.
62     Composite childControlBase = new Composite(base, SWT.NONE);
63     childControlBase.setLayoutData(new GridData(GridData.FILL_BOTH));
64     try {
65       createChildControl(childControlBase);
66     } catch (CoreException e) {
67       PHPeclipsePlugin.log(e);
68       return;
69     }
70
71     // preview components.
72     Composite previewBase = new Composite(base, SWT.NONE);
73
74     GridData gd = new GridData(GridData.FILL_HORIZONTAL);
75     previewBase.setLayoutData(gd);
76     previewBase.setLayout(new GridLayout(1, false));
77
78     Label labe = new Label(previewBase, SWT.NONE);
79     labe.setText("Preview:");
80     gd = new GridData(GridData.FILL_HORIZONTAL);
81     labe.setLayoutData(gd);
82
83     preview = new Text(previewBase, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
84     gd = new GridData(GridData.FILL_HORIZONTAL);
85     gd.widthHint = 0;
86     gd.heightHint = preview.getLineHeight() * 4;
87     preview.setLayoutData(gd);
88
89     refreshPreview();
90   }
91
92   abstract protected void createChildControl(Composite parent) throws CoreException;
93
94   public abstract String getPreviewText();
95
96   public void refreshPreview() {
97     if (preview != null) {
98       String text = getPreviewText();
99       preview.setText(text == null ? "" : text);
100     }
101   }
102
103   public String getElementName() {
104     return elementName;
105   }
106
107   public void setElementName(String string) {
108     elementName = string;
109   }
110
111   protected IFile getEditFile() {
112     IFile file = null;
113     IWizard wiz = getWizard();
114     if (wiz instanceof EditElementWizard) {
115       file = ((EditElementWizard) wiz).getCurrentEditFile();
116     }
117     return file;
118   }
119
120   protected void performFinish() {
121     EditElementWizard wiz = (EditElementWizard) getWizard();
122     ITextSelection sel = wiz.getSelection();
123     IDocument doc = wiz.getDocument();
124     int offset = sel.getOffset();
125     try {
126       doc.replace(offset, sel.getLength(), getPreviewText());
127     } catch (BadLocationException e) {
128       PHPeclipsePlugin.log(e);
129     }
130     int index = doc.get().indexOf('>', offset);
131     if (index != -1) {
132       wiz.setSelection(new TextSelection(index + 1, 0));
133     }
134   }
135
136   /**
137    * Returns edit type.
138    */
139   public int getEditType() {
140     return editType;
141   }
142
143   /**
144    * Sets edit type that types are EditElementWizardPage.NEW, EditElementWizardPage.MODIFY. Default value is NEW.
145    */
146   public void setEditType(int i) {
147     editType = i;
148   }
149
150   protected String getSelectionText() {
151     return ((EditElementWizard) getWizard()).getSelection().getText();
152   }
153
154   protected Element getParsedSelectionText() {
155     String selText = getSelectionText();
156     try {
157       InputSource source = new InputSource(new StringReader(selText));
158       Document doc = docBuilderFactory.newDocumentBuilder().parse(source);
159       return doc.getDocumentElement();
160     } catch (SAXException e) {
161     } catch (IOException e) {
162     } catch (ParserConfigurationException e) {
163     }
164     return null;
165
166   }
167
168   protected static String chooseContent(String text) {
169     int b = -1, e = -1, len = text.length();
170     for (int i = 0; i < len; i++) {
171       if (text.charAt(i) == '>') {
172         b = i + 1;
173         break;
174       }
175     }
176     for (int i = len - 1; i >= 0; i--) {
177       if (text.charAt(i) == '<') {
178         e = i;
179         break;
180       }
181     }
182     return (b != -1 && e != -1 && b < len && e < len) ? text.substring(b, e) : "";
183
184   }
185
186 }