#757 - net.sourceforge.phpeclipse.xdebug.php.launching.PHPRemoteLaunchConfigurationDe...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / EditElementWizardPage.java
1 /*
2  * $Id: EditElementWizardPage.java,v 1.2 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 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
39                 IPreviewer {
40
41         final public static int NEW = 0, MODIFY = 1;
42
43         private static DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
44                         .newInstance();
45
46         Composite extendComp;
47
48         Text preview;
49
50         private String elementName = null;
51
52         int editType = NEW;
53
54         protected EditElementWizardPage(String pageName) {
55                 super(pageName);
56         }
57
58         public void createControl(Composite parent) {
59                 Composite base = new Composite(parent, SWT.NONE);
60                 setControl(base);
61                 base.setLayout(new GridLayout(1, false));
62
63                 // create child control.
64                 Composite childControlBase = new Composite(base, SWT.NONE);
65                 childControlBase.setLayoutData(new GridData(GridData.FILL_BOTH));
66                 try {
67                         createChildControl(childControlBase);
68                 } catch (CoreException e) {
69                         PHPeclipsePlugin.log(e);
70                         return;
71                 }
72
73                 // preview components.
74                 Composite previewBase = new Composite(base, SWT.NONE);
75
76                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
77                 previewBase.setLayoutData(gd);
78                 previewBase.setLayout(new GridLayout(1, false));
79
80                 Label labe = new Label(previewBase, SWT.NONE);
81                 labe.setText("Preview:");
82                 gd = new GridData(GridData.FILL_HORIZONTAL);
83                 labe.setLayoutData(gd);
84
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);
88                 gd.widthHint = 0;
89                 gd.heightHint = preview.getLineHeight() * 4;
90                 preview.setLayoutData(gd);
91
92                 refreshPreview();
93         }
94
95         abstract protected void createChildControl(Composite parent)
96                         throws CoreException;
97
98         public abstract String getPreviewText();
99
100         public void refreshPreview() {
101                 if (preview != null) {
102                         String text = getPreviewText();
103                         preview.setText(text == null ? "" : text);
104                 }
105         }
106
107         public String getElementName() {
108                 return elementName;
109         }
110
111         public void setElementName(String string) {
112                 elementName = string;
113         }
114
115         protected IFile getEditFile() {
116                 IFile file = null;
117                 IWizard wiz = getWizard();
118                 if (wiz instanceof EditElementWizard) {
119                         file = ((EditElementWizard) wiz).getCurrentEditFile();
120                 }
121                 return file;
122         }
123
124         protected void performFinish() {
125                 EditElementWizard wiz = (EditElementWizard) getWizard();
126                 ITextSelection sel = wiz.getSelection();
127                 IDocument doc = wiz.getDocument();
128                 int offset = sel.getOffset();
129                 try {
130                         doc.replace(offset, sel.getLength(), getPreviewText());
131                 } catch (BadLocationException e) {
132                         PHPeclipsePlugin.log(e);
133                 }
134                 int index = doc.get().indexOf('>', offset);
135                 if (index != -1) {
136                         wiz.setSelection(new TextSelection(index + 1, 0));
137                 }
138         }
139
140         /**
141          * Returns edit type.
142          */
143         public int getEditType() {
144                 return editType;
145         }
146
147         /**
148          * Sets edit type that types are EditElementWizardPage.NEW,
149          * EditElementWizardPage.MODIFY. Default value is NEW.
150          */
151         public void setEditType(int i) {
152                 editType = i;
153         }
154
155         protected String getSelectionText() {
156                 return ((EditElementWizard) getWizard()).getSelection().getText();
157         }
158
159         protected Element getParsedSelectionText() {
160                 String selText = getSelectionText();
161                 try {
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) {
168                 }
169                 return null;
170
171         }
172
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) == '>') {
177                                 b = i + 1;
178                                 break;
179                         }
180                 }
181                 for (int i = len - 1; i >= 0; i--) {
182                         if (text.charAt(i) == '<') {
183                                 e = i;
184                                 break;
185                         }
186                 }
187                 return (b != -1 && e != -1 && b < len && e < len) ? text
188                                 .substring(b, e) : "";
189
190         }
191
192 }