2 * $Id: UnknownElementWizardPage.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.util.ArrayList;
9 import org.eclipse.jface.dialogs.IInputValidator;
10 import org.eclipse.jface.viewers.ArrayContentProvider;
11 import org.eclipse.jface.viewers.CellEditor;
12 import org.eclipse.jface.viewers.ICellModifier;
13 import org.eclipse.jface.viewers.ILabelProviderListener;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.jface.viewers.ITableLabelProvider;
16 import org.eclipse.jface.viewers.TableViewer;
17 import org.eclipse.jface.viewers.TextCellEditor;
18 import org.eclipse.jface.window.Window;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Item;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Table;
30 import org.eclipse.swt.widgets.TableColumn;
31 import org.w3c.dom.Element;
32 import org.w3c.dom.NamedNodeMap;
33 import org.w3c.dom.Node;
39 public class UnknownElementWizardPage extends EditElementWizardPage {
41 // key of TableCell for attribute editor.
42 final static String NAME = "ColumnProperty-name",
43 VALUE = "ColumnProperty-value";
45 Button emptyElementCheck, addButton, removeButton, upButton, downButton;
47 TableViewer unknownElementAttrs;
49 ArrayList attrs = new ArrayList(), listeners = new ArrayList();
51 SelectionListener elemTypeChangeListener = new SelectionListener() {
52 public void widgetSelected(SelectionEvent e) {
56 public void widgetDefaultSelected(SelectionEvent e) {
60 public UnknownElementWizardPage() {
61 super("UnknownElementEditPage");
63 setDescription("Editor for any HTML element.");
66 static IInputValidator attrValidator = new IInputValidator() {
67 public String isValid(String newText) {
68 if (newText.length() == 0) {
69 return "Need to specify name";
71 if (newText.indexOf(' ') != -1 || newText.indexOf('\n') != -1
72 || newText.indexOf('\t') != -1) {
73 return "Not contain blank";
79 protected void createChildControl(Composite parent) {
81 parent.setLayout(new GridLayout(2, false));
83 // // attribute editor
84 Label labe = new Label(parent, SWT.NONE);
85 labe.setText("Element &Attribute:");
86 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
87 labe.setLayoutData(gd);
88 new Label(parent, SWT.NONE);
90 // attribute display table setting
91 unknownElementAttrs = new TableViewer(parent, SWT.BORDER | SWT.SINGLE
92 | SWT.FULL_SELECTION);
93 gd = new GridData(GridData.FILL_BOTH);
94 gd.horizontalSpan = 1;
96 unknownElementAttrs.getControl().setLayoutData(gd);
98 final Table table = unknownElementAttrs.getTable();
99 new TableColumn(table, SWT.LEFT).setText("Name");
100 new TableColumn(table, SWT.LEFT).setText("Value");
102 table.setLinesVisible(true);
103 table.setHeaderVisible(true);
105 unknownElementAttrs.setColumnProperties(new String[] { NAME, VALUE });
106 unknownElementAttrs.setContentProvider(new ArrayContentProvider());
108 unknownElementAttrs.setCellEditors(new CellEditor[] {
109 new TextCellEditor(table), new TextCellEditor(table) });
110 unknownElementAttrs.setCellModifier(new ICellModifier() {
111 public boolean canModify(Object element, String property) {
115 public Object getValue(Object element, String property) {
116 return ((String[]) element)[property.equals(NAME) ? 0 : 1];
119 public void modify(Object element, String property, Object value) {
120 if (element instanceof Item) {
121 ((String[]) ((Item) element).getData())[property
122 .equals(NAME) ? 0 : 1] = HTMLUtilities
123 .unescape((String) value);
129 unknownElementAttrs.setLabelProvider(new ITableLabelProvider() {
130 public Image getColumnImage(Object element, int columnIndex) {
134 public String getColumnText(Object element, int columnIndex) {
135 return ((String[]) element)[columnIndex];
138 public void addListener(ILabelProviderListener listener) {
141 public void removeListener(ILabelProviderListener listener) {
144 public void dispose() {
147 public boolean isLabelProperty(Object element, String property) {
148 return property.equals(NAME) || property.equals(VALUE);
153 unknownElementAttrs.setInput(attrs);
155 TableColumn[] columns = table.getColumns();
156 for (int i = 0; i < columns.length; i++) {
161 upButton = createButton(parent, "&Up");
162 upButton.addSelectionListener(new SelectionListener() {
164 public void widgetSelected(SelectionEvent e) {
165 int index = getSelectionIndex();
167 attrs.add(index - 1, attrs.remove(index));
172 public void widgetDefaultSelected(SelectionEvent e) {
176 downButton = createButton(parent, "&Down");
177 downButton.addSelectionListener(new SelectionListener() {
178 public void widgetSelected(SelectionEvent e) {
179 int index = getSelectionIndex();
180 if (index < attrs.size() - 1) {
181 attrs.add(index + 1, attrs.remove(index));
186 public void widgetDefaultSelected(SelectionEvent e) {
190 addButton = createButton(parent, "&Add");
191 addButton.addSelectionListener(new SelectionListener() {
192 public void widgetSelected(SelectionEvent e) {
193 //int insertIndex = getSelectionIndex();
194 String[] newData = inputValue();
195 if (newData != null) {
201 String[] inputValue() {
202 SomeItemInputDialog dialog = new SomeItemInputDialog(
203 getShell(), "Input new attribute", new String[] {
204 "Attribute name", "Attribute value" },
205 new IInputValidator[] { attrValidator, null });
207 if (dialog.open() == Window.OK) {
208 return dialog.getValues();
213 public void widgetDefaultSelected(SelectionEvent e) {
217 removeButton = createButton(parent, "&Remove");
218 removeButton.addSelectionListener(new SelectionListener() {
219 public void widgetSelected(SelectionEvent e) {
220 int index = getSelectionIndex();
227 public void widgetDefaultSelected(SelectionEvent e) {
231 emptyElementCheck = new Button(parent, SWT.CHECK);
232 gd = new GridData(GridData.FILL_HORIZONTAL);
233 emptyElementCheck.setLayoutData(gd);
234 emptyElementCheck.setText("&Empty Element");
235 emptyElementCheck.addSelectionListener(elemTypeChangeListener);
236 emptyElementCheck.setSelection(isEmptyAsText());
238 new Label(parent, SWT.NONE);
241 static Button createButton(Composite parent, String text) {
242 Button button = new Button(parent, SWT.PUSH);
243 GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING
244 | GridData.HORIZONTAL_ALIGN_END);
246 button.setLayoutData(gd);
247 button.setText(text);
251 public String getPreviewText() {
252 String elemName = getElementName();
253 if (elemName == null) {
259 boolean empty = false;
260 if (emptyElementCheck == null) {
262 empty = isEmptyAsText();
265 empty = emptyElementCheck.getSelection();
268 String content = getSelectionText();
269 if (!empty && getEditType() == MODIFY) {
270 content = chooseContent(content);
273 String previewText = "<" + elemName + attrsCode();
275 previewText += " />";
277 previewText += ">" + content + "</" + elemName + ">";
282 boolean isEmptyAsText() {
283 String selText = getSelectionText();
284 if (getEditType() == MODIFY) {
285 int len = selText.length();
286 return selText.substring(len - 2, len).equals("/>");
291 void resetAttributes() {
294 Element elem = getParsedSelectionText();
296 NamedNodeMap as = elem.getAttributes();
297 for (int i = 0; i < as.getLength(); i++) {
299 attrs.add(new String[] { n.getNodeName(), n.getNodeValue() });
305 StringBuffer buff = new StringBuffer();
306 Object[] as = attrs.toArray();
307 for (int i = 0; i < as.length; i++) {
308 String[] a = (String[]) as[i];
309 buff.append(" " + a[0] + "=\"" + HTMLUtilities.escape(a[1]) + "\"");
311 return buff.toString();
314 int getSelectionIndex() {
315 Object sel = unknownElementAttrs.getSelection();
316 if (sel instanceof IStructuredSelection) {
317 Object item = ((IStructuredSelection) sel).getFirstElement();
318 return attrs.indexOf(item);
324 public void refreshPreview() {
325 unknownElementAttrs.refresh();
326 super.refreshPreview();
329 public void setElementName(String elemName) {
330 super.setElementName(elemName);
331 setTitle("\"" + elemName + "\" Element");