2 * $Id: UnknownElementWizardPage.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.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;
40 public class UnknownElementWizardPage extends EditElementWizardPage {
42 // key of TableCell for attribute editor.
44 NAME = "ColumnProperty-name",
45 VALUE = "ColumnProperty-value";
47 Button emptyElementCheck, addButton, removeButton, upButton, downButton;
49 TableViewer unknownElementAttrs;
51 attrs = new ArrayList(),
52 listeners = new ArrayList();
54 SelectionListener elemTypeChangeListener = new SelectionListener() {
55 public void widgetSelected(SelectionEvent e) {
59 public void widgetDefaultSelected(SelectionEvent e) {
63 public UnknownElementWizardPage(){
64 super("UnknownElementEditPage");
66 setDescription("Editor for any HTML element.");
69 static IInputValidator attrValidator = new IInputValidator() {
70 public String isValid(String newText) {
71 if( newText.length() == 0){
72 return "Need to specify name";
74 if( newText.indexOf(' ') != -1 || newText.indexOf('\n') != -1 || newText.indexOf('\t') != -1 ){
75 return "Not contain blank";
81 protected void createChildControl(Composite parent) {
83 parent.setLayout( new GridLayout(2, false) );
86 Label labe = new Label(parent, SWT.NONE);
87 labe.setText("Element &Attribute:");
88 GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
89 labe.setLayoutData(gd);
90 new Label(parent, SWT.NONE);
92 // attribute display table setting
93 unknownElementAttrs = new TableViewer(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
94 gd = new GridData(GridData.FILL_BOTH);
95 gd.horizontalSpan = 1;
97 unknownElementAttrs.getControl().setLayoutData(gd);
99 final Table table = unknownElementAttrs.getTable();
100 new TableColumn(table, SWT.LEFT).setText("Name");
101 new TableColumn(table, SWT.LEFT).setText("Value");
103 table.setLinesVisible(true);
104 table.setHeaderVisible(true);
106 unknownElementAttrs.setColumnProperties(new String[]{ NAME, VALUE });
107 unknownElementAttrs.setContentProvider(new ArrayContentProvider());
109 unknownElementAttrs.setCellEditors( new CellEditor[]{
110 new TextCellEditor(table),
111 new TextCellEditor(table)
113 unknownElementAttrs.setCellModifier(new ICellModifier() {
114 public boolean canModify(Object element, String property) {
118 public Object getValue(Object element, String property) {
119 return ((String[])element)[property.equals(NAME) ? 0 : 1];
122 public void modify(Object element, String property, Object value) {
123 if (element instanceof Item) {
124 ((String[])((Item) element).getData())[property.equals(NAME) ? 0 : 1] = HTMLUtilities.unescape( (String)value );
130 unknownElementAttrs.setLabelProvider( new ITableLabelProvider() {
131 public Image getColumnImage(Object element, int columnIndex) {
135 public String getColumnText(Object element, int columnIndex) {
136 return ((String[])element)[columnIndex];
139 public void addListener(ILabelProviderListener listener) {}
141 public void removeListener(ILabelProviderListener listener) {}
143 public void dispose() {}
145 public boolean isLabelProperty(Object element, String property) {
146 return property.equals(NAME) || property.equals(VALUE);
151 unknownElementAttrs.setInput(attrs);
153 TableColumn[] columns = table.getColumns();
154 for (int i = 0; i < columns.length; i++) {
159 upButton = createButton(parent, "&Up");
160 upButton.addSelectionListener(new SelectionListener() {
162 public void widgetSelected(SelectionEvent e) {
163 int index = getSelectionIndex();
165 attrs.add(index-1, attrs.remove(index));
169 public void widgetDefaultSelected(SelectionEvent e) {}
172 downButton = createButton(parent, "&Down");
173 downButton.addSelectionListener(new SelectionListener() {
174 public void widgetSelected(SelectionEvent e) {
175 int index = getSelectionIndex();
176 if( index < attrs.size()-1 ){
177 attrs.add(index+1, attrs.remove(index));
181 public void widgetDefaultSelected(SelectionEvent e) {}
184 addButton = createButton(parent, "&Add");
185 addButton.addSelectionListener(new SelectionListener() {
186 public void widgetSelected(SelectionEvent e) {
187 int insertIndex = getSelectionIndex();
188 String[] newData = inputValue();
195 String[] inputValue(){
196 SomeItemInputDialog dialog = new SomeItemInputDialog(
198 "Input new attribute",
199 new String[]{"Attribute name", "Attribute value"},
200 new IInputValidator[] {attrValidator, null});
203 if( dialog.open() == Window.OK){
204 return dialog.getValues();
208 public void widgetDefaultSelected(SelectionEvent e) {}
211 removeButton = createButton(parent, "&Remove");
212 removeButton.addSelectionListener(new SelectionListener() {
213 public void widgetSelected(SelectionEvent e) {
214 int index = getSelectionIndex();
220 public void widgetDefaultSelected(SelectionEvent e) {}
223 emptyElementCheck = new Button(parent, SWT.CHECK);
224 gd = new GridData(GridData.FILL_HORIZONTAL);
225 emptyElementCheck.setLayoutData(gd);
226 emptyElementCheck.setText("&Empty Element");
227 emptyElementCheck.addSelectionListener(elemTypeChangeListener);
228 emptyElementCheck.setSelection(isEmptyAsText());
230 new Label(parent, SWT.NONE);
233 static Button createButton(Composite parent, String text){
234 Button button = new Button(parent, SWT.PUSH);
235 GridData gd = new GridData(
236 GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END);
238 button.setLayoutData( gd);
239 button.setText(text);
243 public String getPreviewText(){
244 String elemName = getElementName();
245 if(elemName == null){
251 boolean empty = false;
252 if( emptyElementCheck == null ){
254 empty = isEmptyAsText();
257 empty = emptyElementCheck.getSelection();
260 String content = getSelectionText();
261 if( !empty && getEditType() == MODIFY){
262 content = chooseContent( content );
265 String previewText = "<" + elemName + attrsCode();
267 previewText += " />";
269 previewText += ">" + content + "</" + elemName + ">";
274 boolean isEmptyAsText(){
275 String selText = getSelectionText();
276 if(getEditType() == MODIFY){
277 int len = selText.length();
278 return selText.substring(len-2, len).equals("/>");
283 void resetAttributes(){
286 Element elem = getParsedSelectionText();
288 NamedNodeMap as = elem.getAttributes();
289 for (int i = 0; i < as.getLength(); i++) {
291 attrs.add( new String[]{ n.getNodeName(), n.getNodeValue()});
297 StringBuffer buff = new StringBuffer();
298 Object[] as = attrs.toArray();
299 for (int i = 0; i < as.length; i++) {
300 String[] a = (String[])as[i];
301 buff.append(" " + a[0] + "=\"" + HTMLUtilities.escape(a[1]) + "\"");
303 return buff.toString();
306 int getSelectionIndex(){
307 Object sel = unknownElementAttrs.getSelection();
308 if( sel instanceof IStructuredSelection){
309 Object item = ((IStructuredSelection)sel).getFirstElement();
310 return attrs.indexOf(item);
316 public void refreshPreview() {
317 unknownElementAttrs.refresh();
318 super.refreshPreview();
321 public void setElementName(String elemName) {
322 super.setElementName(elemName);
323 setTitle("\"" + elemName + "\" Element");