2 * $Id: TableElementWizardPage.java,v 1.1 2004-10-05 20:51:57 jsurfer 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().getText().trim();
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);
128 protected void createChildControl(Composite parent) {
129 parent.setLayout(new GridLayout(2, false));
132 viewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER);
133 refreshTableHeaderColumns();
135 viewer.setContentProvider(new TableElementContentProvider());
136 viewer.setLabelProvider(new TableElementLabelProvider());
137 viewer.setCellModifier(new TableElementCellModifier(new IPropertyChangeListener() {
138 public void propertyChange(PropertyChangeEvent event) {
143 viewer.setInput(model);
144 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
145 public void selectionChanged(SelectionChangedEvent event) {
146 refreshButtonState();
151 Table table = viewer.getTable();
152 table.setLinesVisible(true);
153 table.setHeaderVisible(true);
155 GridData gd = new GridData(GridData.FILL_BOTH);
157 table.setLayoutData(gd);
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:");
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");
176 TableColumn[] cols = table.getColumns();
177 for (int i = 0; i < cols.length; i++) {
180 refreshTableLengthText();
181 refreshButtonState();
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);
192 Text createNumInputText(Composite parent, String label) {
193 Label labe = new Label(parent, SWT.NONE);
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);
205 refreshTableHeaderColumns();
206 refreshTableLengthText();
207 refreshButtonState();
212 void refreshTableHeaderColumns() {
217 Table table = viewer.getTable();
218 TableColumn[] cols = table.getColumns();
219 CellEditor[] editors = viewer.getCellEditors();
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);
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);
234 viewer.setCellEditors(newEditors);
235 } else if (props.length < cols.length) {
236 for (int i = props.length; i < cols.length; i++) {
238 editors[i].dispose();
240 CellEditor[] newEditors = new CellEditor[props.length];
241 System.arraycopy(editors, 0, newEditors, 0, props.length);
242 viewer.setCellEditors(newEditors);
245 // adjust table fields.
247 cols = table.getColumns();
248 for (int i = 0; i < cols.length; i++) {
253 void refreshTableLengthText() {
254 String cols = String.valueOf(model.getColumnCount());
255 if (!cols.equals(colsText.getText())) {
256 colsText.setText(cols);
258 String rows = String.valueOf(model.getRowCount());
259 if (!rows.equals(rowsText.getText())) {
260 rowsText.setText(rows);
264 void refreshButtonState() {
265 Element e = getCurrentSelection();
266 boolean enable = (e != null);
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)) {
276 upButton.setEnabled(enable && currentIndex > 0);
277 downButton.setEnabled(enable && currentIndex < rows.length - 1);
280 Element getCurrentSelection() {
281 IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
282 return (sel != null) ? (Element) sel.getFirstElement() : null;