1) Fixed issue #764: Ignore strings when looking for single line comments.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / UnknownElementWizardPage.java
1 /*
2  * $Id: UnknownElementWizardPage.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.util.ArrayList;
8
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;
34
35 /**
36  * 
37  * 
38  */
39 public class UnknownElementWizardPage extends EditElementWizardPage {
40
41         // key of TableCell for attribute editor.
42         final static String NAME = "ColumnProperty-name",
43                         VALUE = "ColumnProperty-value";
44
45         Button emptyElementCheck, addButton, removeButton, upButton, downButton;
46
47         TableViewer unknownElementAttrs;
48
49         ArrayList attrs = new ArrayList(), listeners = new ArrayList();
50
51         SelectionListener elemTypeChangeListener = new SelectionListener() {
52                 public void widgetSelected(SelectionEvent e) {
53                         refreshPreview();
54                 }
55
56                 public void widgetDefaultSelected(SelectionEvent e) {
57                 }
58         };
59
60         public UnknownElementWizardPage() {
61                 super("UnknownElementEditPage");
62                 setTitle("Unknown");
63                 setDescription("Editor for any HTML element.");
64         }
65
66         static IInputValidator attrValidator = new IInputValidator() {
67                 public String isValid(String newText) {
68                         if (newText.length() == 0) {
69                                 return "Need to specify name";
70                         }
71                         if (newText.indexOf(' ') != -1 || newText.indexOf('\n') != -1
72                                         || newText.indexOf('\t') != -1) {
73                                 return "Not contain blank";
74                         }
75                         return null;
76                 }
77         };
78
79         protected void createChildControl(Composite parent) {
80                 // empty eleemnt
81                 parent.setLayout(new GridLayout(2, false));
82
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);
89
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;
95                 gd.verticalSpan = 4;
96                 unknownElementAttrs.getControl().setLayoutData(gd);
97
98                 final Table table = unknownElementAttrs.getTable();
99                 new TableColumn(table, SWT.LEFT).setText("Name");
100                 new TableColumn(table, SWT.LEFT).setText("Value");
101
102                 table.setLinesVisible(true);
103                 table.setHeaderVisible(true);
104                 // modifier setting
105                 unknownElementAttrs.setColumnProperties(new String[] { NAME, VALUE });
106                 unknownElementAttrs.setContentProvider(new ArrayContentProvider());
107
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) {
112                                 return true;
113                         }
114
115                         public Object getValue(Object element, String property) {
116                                 return ((String[]) element)[property.equals(NAME) ? 0 : 1];
117                         }
118
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);
124                                         refreshPreview();
125                                 }
126                         }
127                 });
128
129                 unknownElementAttrs.setLabelProvider(new ITableLabelProvider() {
130                         public Image getColumnImage(Object element, int columnIndex) {
131                                 return null;
132                         }
133
134                         public String getColumnText(Object element, int columnIndex) {
135                                 return ((String[]) element)[columnIndex];
136                         }
137
138                         public void addListener(ILabelProviderListener listener) {
139                         }
140
141                         public void removeListener(ILabelProviderListener listener) {
142                         }
143
144                         public void dispose() {
145                         }
146
147                         public boolean isLabelProperty(Object element, String property) {
148                                 return property.equals(NAME) || property.equals(VALUE);
149                         }
150                 });
151
152                 resetAttributes();
153                 unknownElementAttrs.setInput(attrs);
154
155                 TableColumn[] columns = table.getColumns();
156                 for (int i = 0; i < columns.length; i++) {
157                         columns[i].pack();
158                 }
159
160                 // buttonss
161                 upButton = createButton(parent, "&Up");
162                 upButton.addSelectionListener(new SelectionListener() {
163
164                         public void widgetSelected(SelectionEvent e) {
165                                 int index = getSelectionIndex();
166                                 if (index > 0) {
167                                         attrs.add(index - 1, attrs.remove(index));
168                                         refreshPreview();
169                                 }
170                         }
171
172                         public void widgetDefaultSelected(SelectionEvent e) {
173                         }
174                 });
175
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));
182                                         refreshPreview();
183                                 }
184                         }
185
186                         public void widgetDefaultSelected(SelectionEvent e) {
187                         }
188                 });
189
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) {
196                                         attrs.add(newData);
197                                         refreshPreview();
198                                 }
199                         }
200
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 });
206
207                                 if (dialog.open() == Window.OK) {
208                                         return dialog.getValues();
209                                 }
210                                 return null;
211                         }
212
213                         public void widgetDefaultSelected(SelectionEvent e) {
214                         }
215                 });
216
217                 removeButton = createButton(parent, "&Remove");
218                 removeButton.addSelectionListener(new SelectionListener() {
219                         public void widgetSelected(SelectionEvent e) {
220                                 int index = getSelectionIndex();
221                                 if (index != -1) {
222                                         attrs.remove(index);
223                                         refreshPreview();
224                                 }
225                         }
226
227                         public void widgetDefaultSelected(SelectionEvent e) {
228                         }
229                 });
230
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());
237
238                 new Label(parent, SWT.NONE);
239         }
240
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);
245                 gd.widthHint = 60;
246                 button.setLayoutData(gd);
247                 button.setText(text);
248                 return button;
249         }
250
251         public String getPreviewText() {
252                 String elemName = getElementName();
253                 if (elemName == null) {
254                         return null;
255                 }
256
257                 // sets values
258
259                 boolean empty = false;
260                 if (emptyElementCheck == null) {
261                         // ui uninitialized
262                         empty = isEmptyAsText();
263                 } else {
264                         // ui initialized
265                         empty = emptyElementCheck.getSelection();
266                 }
267
268                 String content = getSelectionText();
269                 if (!empty && getEditType() == MODIFY) {
270                         content = chooseContent(content);
271                 }
272
273                 String previewText = "<" + elemName + attrsCode();
274                 if (empty) {
275                         previewText += " />";
276                 } else {
277                         previewText += ">" + content + "</" + elemName + ">";
278                 }
279                 return previewText;
280         }
281
282         boolean isEmptyAsText() {
283                 String selText = getSelectionText();
284                 if (getEditType() == MODIFY) {
285                         int len = selText.length();
286                         return selText.substring(len - 2, len).equals("/>");
287                 }
288                 return false;
289         }
290
291         void resetAttributes() {
292                 attrs.clear();
293
294                 Element elem = getParsedSelectionText();
295                 if (elem != null) {
296                         NamedNodeMap as = elem.getAttributes();
297                         for (int i = 0; i < as.getLength(); i++) {
298                                 Node n = as.item(i);
299                                 attrs.add(new String[] { n.getNodeName(), n.getNodeValue() });
300                         }
301                 }
302         }
303
304         String attrsCode() {
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]) + "\"");
310                 }
311                 return buff.toString();
312         }
313
314         int getSelectionIndex() {
315                 Object sel = unknownElementAttrs.getSelection();
316                 if (sel instanceof IStructuredSelection) {
317                         Object item = ((IStructuredSelection) sel).getFirstElement();
318                         return attrs.indexOf(item);
319                 } else {
320                         return -1;
321                 }
322         }
323
324         public void refreshPreview() {
325                 unknownElementAttrs.refresh();
326                 super.refreshPreview();
327         }
328
329         public void setElementName(String elemName) {
330                 super.setElementName(elemName);
331                 setTitle("\"" + elemName + "\" Element");
332         }
333
334 }