2 * $Id: EditElementWizardPage.java,v 1.1 2004-10-05 20:51:57 jsurfer 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 IPreviewer {
40 final public static int NEW = 0, MODIFY = 1;
42 private static DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
48 private String elementName = null;
52 protected EditElementWizardPage(String pageName) {
56 public void createControl(Composite parent) {
57 Composite base = new Composite(parent, SWT.NONE);
59 base.setLayout(new GridLayout(1, false));
61 // create child control.
62 Composite childControlBase = new Composite(base, SWT.NONE);
63 childControlBase.setLayoutData(new GridData(GridData.FILL_BOTH));
65 createChildControl(childControlBase);
66 } catch (CoreException e) {
67 PHPeclipsePlugin.log(e);
71 // preview components.
72 Composite previewBase = new Composite(base, SWT.NONE);
74 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
75 previewBase.setLayoutData(gd);
76 previewBase.setLayout(new GridLayout(1, false));
78 Label labe = new Label(previewBase, SWT.NONE);
79 labe.setText("Preview:");
80 gd = new GridData(GridData.FILL_HORIZONTAL);
81 labe.setLayoutData(gd);
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);
86 gd.heightHint = preview.getLineHeight() * 4;
87 preview.setLayoutData(gd);
92 abstract protected void createChildControl(Composite parent) throws CoreException;
94 public abstract String getPreviewText();
96 public void refreshPreview() {
97 if (preview != null) {
98 String text = getPreviewText();
99 preview.setText(text == null ? "" : text);
103 public String getElementName() {
107 public void setElementName(String string) {
108 elementName = string;
111 protected IFile getEditFile() {
113 IWizard wiz = getWizard();
114 if (wiz instanceof EditElementWizard) {
115 file = ((EditElementWizard) wiz).getCurrentEditFile();
120 protected void performFinish() {
121 EditElementWizard wiz = (EditElementWizard) getWizard();
122 ITextSelection sel = wiz.getSelection();
123 IDocument doc = wiz.getDocument();
124 int offset = sel.getOffset();
126 doc.replace(offset, sel.getLength(), getPreviewText());
127 } catch (BadLocationException e) {
128 PHPeclipsePlugin.log(e);
130 int index = doc.get().indexOf('>', offset);
132 wiz.setSelection(new TextSelection(index + 1, 0));
139 public int getEditType() {
144 * Sets edit type that types are EditElementWizardPage.NEW, EditElementWizardPage.MODIFY. Default value is NEW.
146 public void setEditType(int i) {
150 protected String getSelectionText() {
151 return ((EditElementWizard) getWizard()).getSelection().getText();
154 protected Element getParsedSelectionText() {
155 String selText = getSelectionText();
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) {
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) == '>') {
176 for (int i = len - 1; i >= 0; i--) {
177 if (text.charAt(i) == '<') {
182 return (b != -1 && e != -1 && b < len && e < len) ? text.substring(b, e) : "";