d49f96df27e906a1aff363fadfbc2199ee41c3e6
[phpeclipse.git] /
1 /*
2  * $Id: ListElementWizardPage.java,v 1.3 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 org.eclipse.swt.SWT;
8 import org.eclipse.swt.events.SelectionEvent;
9 import org.eclipse.swt.events.SelectionListener;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Combo;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Label;
15
16 /**
17  * 
18  */
19 public class ListElementWizardPage extends EditElementWizardPage {
20
21         final static String[] LIST_TYPES = { "ul", "ol", "dl" };
22
23         Combo types;
24
25         public ListElementWizardPage() {
26                 super("ListElementWizardPage");
27                 setTitle("List");
28                 setDescription("Editing list element.");
29         }
30
31         protected void createChildControl(Composite parent) {
32                 parent.setLayout(new GridLayout(2, false));
33                 Label labe = new Label(parent, SWT.NONE);
34                 labe.setText("List &Type:");
35
36                 types = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
37
38                 for (int i = 0; i < LIST_TYPES.length; i++) {
39                         String type = LIST_TYPES[i];
40                         types.add(type);
41                         if (getElementName().equals(type)) {
42                                 types.select(i);
43                         }
44                 }
45
46                 types.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
47                 types.addSelectionListener(new SelectionListener() {
48                         public void widgetSelected(SelectionEvent e) {
49                                 setElementName(types.getText());
50                                 refreshPreview();
51                         }
52
53                         public void widgetDefaultSelected(SelectionEvent e) {
54                         }
55                 });
56         }
57
58         public String getPreviewText() {
59                 String content = ((EditElementWizard) getWizard()).getSelection()
60                                 .getText().trim();
61
62                 String elemName = getElementName();
63                 switch (getEditType()) {
64                 case MODIFY:
65                         content = chooseContent(content).trim();
66                         break;
67
68                 case NEW:
69                         String[] lines = content.split("\n+");
70                         StringBuffer result = new StringBuffer();
71                         for (int i = 0; i < lines.length; i++) {
72                                 String itemElemName;
73                                 if (elemName.equals("dl")) {
74                                         itemElemName = (i % 2 == 0) ? "dt" : "dd";
75                                 } else {
76                                         itemElemName = "li";
77                                 }
78                                 result.append("<" + itemElemName + ">" + lines[i].trim() + "</"
79                                                 + itemElemName + ">\n");
80                         }
81                         content = result.toString();
82                         break;
83                 }
84
85                 return "<" + elemName + ">\n" + content + "</" + elemName + ">\n";
86         }
87
88 }