2 * $Id: TableElementWizardPage.java,v 1.2 2006-10-21 23:18:43 pombredanne Exp $
3 * Copyright Narushima Hironori. All rights reserved.
5 package net.sourceforge.phpeclipse.wizards.html;
7 import java.io.IOException;
9 import javax.xml.parsers.ParserConfigurationException;
11 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
39 * TableElementWizardPage.
41 public class TableElementWizardPage extends EditElementWizardPage {
43 final public static int COLUMNS_MAX = 32, ROWS_MAX = 256;
45 final static String[] expandStyleLabels = { "Flat", "Table", "Enumerate", };
47 TableElementModel model;
53 Combo expandStyleCombo = null;
55 Text colsText, rowsText;
57 Button addButton, removeButton, upButton, downButton;
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) {
66 } else if (ev.widget == upButton) {
68 } else if (ev.widget == downButton) {
74 public void widgetDefaultSelected(SelectionEvent e) {
78 ModifyListener cellCountChangeListener = new ModifyListener() {
79 public void modifyText(ModifyEvent e) {
81 if (e.widget == colsText) {
82 int cols = Integer.parseInt(colsText.getText());
85 if (cols > COLUMNS_MAX)
87 model.setColumnCount(cols);
88 } else if (e.widget == rowsText) {
89 int rows = Integer.parseInt(rowsText.getText());
94 model.setRowCount(rows);
97 } catch (NumberFormatException x) {
102 public TableElementWizardPage() {
103 super("TableElementWizardPage");
105 setDescription("Edit table element and cells modifier.");
108 public String getPreviewText() {
112 return (model != null) ? model.expandCodes() : null;
116 String content = ((EditElementWizard) getWizard()).getSelection()
119 model = new TableElementModel(content, getEditType() == NEW);
120 } catch (ParserConfigurationException e) {
121 PHPeclipsePlugin.log(e);
122 } catch (SAXException e) {
123 PHPeclipsePlugin.log(e);
124 } catch (IOException e) {
125 PHPeclipsePlugin.log(e);
129 protected void createChildControl(Composite parent) {
130 parent.setLayout(new GridLayout(2, false));
133 viewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION
135 refreshTableHeaderColumns();
137 viewer.setContentProvider(new TableElementContentProvider());
138 viewer.setLabelProvider(new TableElementLabelProvider());
139 viewer.setCellModifier(new TableElementCellModifier(
140 new IPropertyChangeListener() {
141 public void propertyChange(PropertyChangeEvent event) {
146 viewer.setInput(model);
147 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
148 public void selectionChanged(SelectionChangedEvent event) {
149 refreshButtonState();
154 Table table = viewer.getTable();
155 table.setLinesVisible(true);
156 table.setHeaderVisible(true);
158 GridData gd = new GridData(GridData.FILL_BOTH);
160 table.setLayoutData(gd);
162 // text input area setting
163 Composite textInputArea = new Composite(parent, SWT.NONE);
164 textInputArea.setLayout(new GridLayout(1, false));
165 textInputArea.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL
166 | GridData.VERTICAL_ALIGN_BEGINNING));
167 rowsText = createNumInputText(textInputArea, "&Rows:");
168 colsText = createNumInputText(textInputArea, "&Columns:");
171 Composite buttonArea = new Composite(parent, SWT.NONE);
172 buttonArea.setLayout(new GridLayout(1, false));
173 buttonArea.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL
174 | GridData.VERTICAL_ALIGN_END));
175 addButton = createButton(buttonArea, "&Add");
176 removeButton = createButton(buttonArea, "&Remove");
177 upButton = createButton(buttonArea, "&Up");
178 downButton = createButton(buttonArea, "&Down");
181 TableColumn[] cols = table.getColumns();
182 for (int i = 0; i < cols.length; i++) {
185 refreshTableLengthText();
186 refreshButtonState();
189 Button createButton(Composite parent, String text) {
190 Button button = new Button(parent, SWT.NONE);
191 button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
192 button.setText(text);
193 button.addSelectionListener(buttonListener);
197 Text createNumInputText(Composite parent, String label) {
198 Label labe = new Label(parent, SWT.NONE);
201 Text text = new Text(parent, SWT.BORDER);
202 text.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
203 text.setTextLimit(2);
204 text.addVerifyListener(new NumVerifyListener());
205 text.addModifyListener(cellCountChangeListener);
210 refreshTableHeaderColumns();
211 refreshTableLengthText();
212 refreshButtonState();
217 void refreshTableHeaderColumns() {
222 Table table = viewer.getTable();
223 TableColumn[] cols = table.getColumns();
224 CellEditor[] editors = viewer.getCellEditors();
226 String[] props = model.getColumnProperties();
227 viewer.setColumnProperties(props);
228 // modify cell length
229 if (props.length > cols.length) {
230 CellEditor[] newEditors = new CellEditor[props.length];
231 if (editors != null) {
232 System.arraycopy(editors, 0, newEditors, 0, editors.length);
234 for (int i = cols.length; i < props.length; i++) {
235 TableColumn col = new TableColumn(table, SWT.LEFT);
236 col.setText(TableElementModel.toColumnName(i));
237 newEditors[i] = new TextCellEditor(table);
239 viewer.setCellEditors(newEditors);
240 } else if (props.length < cols.length) {
241 for (int i = props.length; i < cols.length; i++) {
243 editors[i].dispose();
245 CellEditor[] newEditors = new CellEditor[props.length];
246 System.arraycopy(editors, 0, newEditors, 0, props.length);
247 viewer.setCellEditors(newEditors);
250 // adjust table fields.
252 cols = table.getColumns();
253 for (int i = 0; i < cols.length; i++) {
258 void refreshTableLengthText() {
259 String cols = String.valueOf(model.getColumnCount());
260 if (!cols.equals(colsText.getText())) {
261 colsText.setText(cols);
263 String rows = String.valueOf(model.getRowCount());
264 if (!rows.equals(rowsText.getText())) {
265 rowsText.setText(rows);
269 void refreshButtonState() {
270 Element e = getCurrentSelection();
271 boolean enable = (e != null);
273 removeButton.setEnabled(enable);
274 int currentIndex = -1;
275 Element[] rows = model.getRows();
276 for (int i = 0; i < rows.length; i++) {
277 if (rows[i].equals(e)) {
281 upButton.setEnabled(enable && currentIndex > 0);
282 downButton.setEnabled(enable && currentIndex < rows.length - 1);
285 Element getCurrentSelection() {
286 IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
287 return (sel != null) ? (Element) sel.getFirstElement() : null;