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