2 * $Id: EditElementWizardPage.java,v 1.2 2006-10-21 23:18:43 pombredanne Exp $
3 * Copyright Narushima Hironori. All rights reserved.
5 package net.sourceforge.phpeclipse.wizards.html;
7 import java.io.IOException;
8 import java.io.StringReader;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.ParserConfigurationException;
13 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
38 public abstract class EditElementWizardPage extends WizardPage implements
41 final public static int NEW = 0, MODIFY = 1;
43 private static DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
50 private String elementName = null;
54 protected EditElementWizardPage(String pageName) {
58 public void createControl(Composite parent) {
59 Composite base = new Composite(parent, SWT.NONE);
61 base.setLayout(new GridLayout(1, false));
63 // create child control.
64 Composite childControlBase = new Composite(base, SWT.NONE);
65 childControlBase.setLayoutData(new GridData(GridData.FILL_BOTH));
67 createChildControl(childControlBase);
68 } catch (CoreException e) {
69 PHPeclipsePlugin.log(e);
73 // preview components.
74 Composite previewBase = new Composite(base, SWT.NONE);
76 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
77 previewBase.setLayoutData(gd);
78 previewBase.setLayout(new GridLayout(1, false));
80 Label labe = new Label(previewBase, SWT.NONE);
81 labe.setText("Preview:");
82 gd = new GridData(GridData.FILL_HORIZONTAL);
83 labe.setLayoutData(gd);
85 preview = new Text(previewBase, SWT.BORDER | SWT.MULTI | SWT.READ_ONLY
86 | SWT.WRAP | SWT.V_SCROLL | SWT.H_SCROLL);
87 gd = new GridData(GridData.FILL_HORIZONTAL);
89 gd.heightHint = preview.getLineHeight() * 4;
90 preview.setLayoutData(gd);
95 abstract protected void createChildControl(Composite parent)
98 public abstract String getPreviewText();
100 public void refreshPreview() {
101 if (preview != null) {
102 String text = getPreviewText();
103 preview.setText(text == null ? "" : text);
107 public String getElementName() {
111 public void setElementName(String string) {
112 elementName = string;
115 protected IFile getEditFile() {
117 IWizard wiz = getWizard();
118 if (wiz instanceof EditElementWizard) {
119 file = ((EditElementWizard) wiz).getCurrentEditFile();
124 protected void performFinish() {
125 EditElementWizard wiz = (EditElementWizard) getWizard();
126 ITextSelection sel = wiz.getSelection();
127 IDocument doc = wiz.getDocument();
128 int offset = sel.getOffset();
130 doc.replace(offset, sel.getLength(), getPreviewText());
131 } catch (BadLocationException e) {
132 PHPeclipsePlugin.log(e);
134 int index = doc.get().indexOf('>', offset);
136 wiz.setSelection(new TextSelection(index + 1, 0));
143 public int getEditType() {
148 * Sets edit type that types are EditElementWizardPage.NEW,
149 * EditElementWizardPage.MODIFY. Default value is NEW.
151 public void setEditType(int i) {
155 protected String getSelectionText() {
156 return ((EditElementWizard) getWizard()).getSelection().getText();
159 protected Element getParsedSelectionText() {
160 String selText = getSelectionText();
162 InputSource source = new InputSource(new StringReader(selText));
163 Document doc = docBuilderFactory.newDocumentBuilder().parse(source);
164 return doc.getDocumentElement();
165 } catch (SAXException e) {
166 } catch (IOException e) {
167 } catch (ParserConfigurationException e) {
173 protected static String chooseContent(String text) {
174 int b = -1, e = -1, len = text.length();
175 for (int i = 0; i < len; i++) {
176 if (text.charAt(i) == '>') {
181 for (int i = len - 1; i >= 0; i--) {
182 if (text.charAt(i) == '<') {
187 return (b != -1 && e != -1 && b < len && e < len) ? text
188 .substring(b, e) : "";