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