improved codetemplate wizards; new html tag wizards
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / TableElementWizardPage.java
1 /*
2  * $Id: TableElementWizardPage.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.io.IOException;
8
9 import javax.xml.parsers.ParserConfigurationException;
10
11 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
12
13 import org.eclipse.jface.util.IPropertyChangeListener;
14 import org.eclipse.jface.util.PropertyChangeEvent;
15 import org.eclipse.jface.viewers.CellEditor;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.jface.viewers.TextCellEditor;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.events.ModifyEvent;
23 import org.eclipse.swt.events.ModifyListener;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.events.SelectionListener;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Combo;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Table;
33 import org.eclipse.swt.widgets.TableColumn;
34 import org.eclipse.swt.widgets.Text;
35 import org.w3c.dom.Element;
36 import org.xml.sax.SAXException;
37
38 /**
39  * TableElementWizardPage.
40  */
41 public class TableElementWizardPage extends EditElementWizardPage {
42
43   final public static int COLUMNS_MAX = 32, ROWS_MAX = 256;
44
45   final static String[] expandStyleLabels = { "Flat", "Table", "Enumerate", };
46
47   TableElementModel model;
48
49   TableViewer viewer;
50
51   CellEditor[] editors;
52
53   Combo expandStyleCombo = null;
54
55   Text colsText, rowsText;
56
57   Button addButton, removeButton, upButton, downButton;
58
59   SelectionListener buttonListener = new SelectionListener() {
60     public void widgetSelected(SelectionEvent ev) {
61       Element e = getCurrentSelection();
62       if (ev.widget == addButton) {
63         model.insertNewRowBefore(e);
64       } else if (ev.widget == removeButton) {
65         model.removeRow(e);
66       } else if (ev.widget == upButton) {
67         model.move(e, -1);
68       } else if (ev.widget == downButton) {
69         model.move(e, 1);
70       }
71       refreshAll();
72     }
73
74     public void widgetDefaultSelected(SelectionEvent e) {
75     }
76   };
77
78   ModifyListener cellCountChangeListener = new ModifyListener() {
79     public void modifyText(ModifyEvent e) {
80       try {
81         if (e.widget == colsText) {
82           int cols = Integer.parseInt(colsText.getText());
83           if (cols < 1)
84             cols = 1;
85           if (cols > COLUMNS_MAX)
86             cols = COLUMNS_MAX;
87           model.setColumnCount(cols);
88         } else if (e.widget == rowsText) {
89           int rows = Integer.parseInt(rowsText.getText());
90           if (rows < 1)
91             rows = 1;
92           if (rows > ROWS_MAX)
93             rows = ROWS_MAX;
94           model.setRowCount(rows);
95         }
96         refreshAll();
97       } catch (NumberFormatException x) {
98       }
99     }
100   };
101
102   public TableElementWizardPage() {
103     super("TableElementWizardPage");
104     setTitle("Table");
105     setDescription("Edit table element and cells modifier.");
106   }
107
108   public String getPreviewText() {
109     if (model == null) {
110       initModel();
111     }
112     return (model != null) ? model.expandCodes() : null;
113   }
114
115   void initModel() {
116     String content = ((EditElementWizard) getWizard()).getSelection().getText().trim();
117     try {
118       model = new TableElementModel(content, getEditType() == NEW);
119     } catch (ParserConfigurationException e) {
120       PHPeclipsePlugin.log(e);
121     } catch (SAXException e) {
122       PHPeclipsePlugin.log(e);
123     } catch (IOException e) {
124       PHPeclipsePlugin.log(e);
125     }
126   }
127
128   protected void createChildControl(Composite parent) {
129     parent.setLayout(new GridLayout(2, false));
130
131     // table settings
132     viewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
133     refreshTableHeaderColumns();
134
135     viewer.setContentProvider(new TableElementContentProvider());
136     viewer.setLabelProvider(new TableElementLabelProvider());
137     viewer.setCellModifier(new TableElementCellModifier(new IPropertyChangeListener() {
138       public void propertyChange(PropertyChangeEvent event) {
139         refreshAll();
140       }
141     }));
142
143     viewer.setInput(model);
144     viewer.addSelectionChangedListener(new ISelectionChangedListener() {
145       public void selectionChanged(SelectionChangedEvent event) {
146         refreshButtonState();
147         refreshPreview();
148       }
149     });
150
151     Table table = viewer.getTable();
152     table.setLinesVisible(true);
153     table.setHeaderVisible(true);
154
155     GridData gd = new GridData(GridData.FILL_BOTH);
156     gd.verticalSpan = 2;
157     table.setLayoutData(gd);
158
159     // text input area setting
160     Composite textInputArea = new Composite(parent, SWT.NONE);
161     textInputArea.setLayout(new GridLayout(1, false));
162     textInputArea.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING));
163     rowsText = createNumInputText(textInputArea, "&Rows:");
164     colsText = createNumInputText(textInputArea, "&Columns:");
165
166     // button area.
167     Composite buttonArea = new Composite(parent, SWT.NONE);
168     buttonArea.setLayout(new GridLayout(1, false));
169     buttonArea.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_END));
170     addButton = createButton(buttonArea, "&Add");
171     removeButton = createButton(buttonArea, "&Remove");
172     upButton = createButton(buttonArea, "&Up");
173     downButton = createButton(buttonArea, "&Down");
174
175     // init state
176     TableColumn[] cols = table.getColumns();
177     for (int i = 0; i < cols.length; i++) {
178       cols[i].pack();
179     }
180     refreshTableLengthText();
181     refreshButtonState();
182   }
183
184   Button createButton(Composite parent, String text) {
185     Button button = new Button(parent, SWT.NONE);
186     button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
187     button.setText(text);
188     button.addSelectionListener(buttonListener);
189     return button;
190   }
191
192   Text createNumInputText(Composite parent, String label) {
193     Label labe = new Label(parent, SWT.NONE);
194     labe.setText(label);
195
196     Text text = new Text(parent, SWT.BORDER);
197     text.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
198     text.setTextLimit(2);
199     text.addVerifyListener(new NumVerifyListener());
200     text.addModifyListener(cellCountChangeListener);
201     return text;
202   }
203
204   void refreshAll() {
205     refreshTableHeaderColumns();
206     refreshTableLengthText();
207     refreshButtonState();
208     refreshPreview();
209     viewer.refresh();
210   }
211
212   void refreshTableHeaderColumns() {
213     if (model == null) {
214       initModel();
215     }
216
217     Table table = viewer.getTable();
218     TableColumn[] cols = table.getColumns();
219     CellEditor[] editors = viewer.getCellEditors();
220
221     String[] props = model.getColumnProperties();
222     viewer.setColumnProperties(props);
223     // modify cell length
224     if (props.length > cols.length) {
225       CellEditor[] newEditors = new CellEditor[props.length];
226       if (editors != null) {
227         System.arraycopy(editors, 0, newEditors, 0, editors.length);
228       }
229       for (int i = cols.length; i < props.length; i++) {
230         TableColumn col = new TableColumn(table, SWT.LEFT);
231         col.setText(TableElementModel.toColumnName(i));
232         newEditors[i] = new TextCellEditor(table);
233       }
234       viewer.setCellEditors(newEditors);
235     } else if (props.length < cols.length) {
236       for (int i = props.length; i < cols.length; i++) {
237         cols[i].dispose();
238         editors[i].dispose();
239       }
240       CellEditor[] newEditors = new CellEditor[props.length];
241       System.arraycopy(editors, 0, newEditors, 0, props.length);
242       viewer.setCellEditors(newEditors);
243     }
244
245     // adjust table fields.
246     viewer.refresh();
247     cols = table.getColumns();
248     for (int i = 0; i < cols.length; i++) {
249       cols[i].pack();
250     }
251   }
252
253   void refreshTableLengthText() {
254     String cols = String.valueOf(model.getColumnCount());
255     if (!cols.equals(colsText.getText())) {
256       colsText.setText(cols);
257     }
258     String rows = String.valueOf(model.getRowCount());
259     if (!rows.equals(rowsText.getText())) {
260       rowsText.setText(rows);
261     }
262   }
263
264   void refreshButtonState() {
265     Element e = getCurrentSelection();
266     boolean enable = (e != null);
267
268     removeButton.setEnabled(enable);
269     int currentIndex = -1;
270     Element[] rows = model.getRows();
271     for (int i = 0; i < rows.length; i++) {
272       if (rows[i].equals(e)) {
273         currentIndex = i;
274       }
275     }
276     upButton.setEnabled(enable && currentIndex > 0);
277     downButton.setEnabled(enable && currentIndex < rows.length - 1);
278   }
279
280   Element getCurrentSelection() {
281     IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
282     return (sel != null) ? (Element) sel.getFirstElement() : null;
283   }
284
285 }