Fixed: 1760857 - Avoid refreshing the preview when phpeditor got focus.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / wizards / html / TableElementWizardPage.java
1 /*
2  * $Id: TableElementWizardPage.java,v 1.2 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 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()
117                                 .getText().trim();
118                 try {
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);
126                 }
127         }
128
129         protected void createChildControl(Composite parent) {
130                 parent.setLayout(new GridLayout(2, false));
131
132                 // table settings
133                 viewer = new TableViewer(parent, SWT.SINGLE | SWT.FULL_SELECTION
134                                 | SWT.BORDER);
135                 refreshTableHeaderColumns();
136
137                 viewer.setContentProvider(new TableElementContentProvider());
138                 viewer.setLabelProvider(new TableElementLabelProvider());
139                 viewer.setCellModifier(new TableElementCellModifier(
140                                 new IPropertyChangeListener() {
141                                         public void propertyChange(PropertyChangeEvent event) {
142                                                 refreshAll();
143                                         }
144                                 }));
145
146                 viewer.setInput(model);
147                 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
148                         public void selectionChanged(SelectionChangedEvent event) {
149                                 refreshButtonState();
150                                 refreshPreview();
151                         }
152                 });
153
154                 Table table = viewer.getTable();
155                 table.setLinesVisible(true);
156                 table.setHeaderVisible(true);
157
158                 GridData gd = new GridData(GridData.FILL_BOTH);
159                 gd.verticalSpan = 2;
160                 table.setLayoutData(gd);
161
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:");
169
170                 // button area.
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");
179
180                 // init state
181                 TableColumn[] cols = table.getColumns();
182                 for (int i = 0; i < cols.length; i++) {
183                         cols[i].pack();
184                 }
185                 refreshTableLengthText();
186                 refreshButtonState();
187         }
188
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);
194                 return button;
195         }
196
197         Text createNumInputText(Composite parent, String label) {
198                 Label labe = new Label(parent, SWT.NONE);
199                 labe.setText(label);
200
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);
206                 return text;
207         }
208
209         void refreshAll() {
210                 refreshTableHeaderColumns();
211                 refreshTableLengthText();
212                 refreshButtonState();
213                 refreshPreview();
214                 viewer.refresh();
215         }
216
217         void refreshTableHeaderColumns() {
218                 if (model == null) {
219                         initModel();
220                 }
221
222                 Table table = viewer.getTable();
223                 TableColumn[] cols = table.getColumns();
224                 CellEditor[] editors = viewer.getCellEditors();
225
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);
233                         }
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);
238                         }
239                         viewer.setCellEditors(newEditors);
240                 } else if (props.length < cols.length) {
241                         for (int i = props.length; i < cols.length; i++) {
242                                 cols[i].dispose();
243                                 editors[i].dispose();
244                         }
245                         CellEditor[] newEditors = new CellEditor[props.length];
246                         System.arraycopy(editors, 0, newEditors, 0, props.length);
247                         viewer.setCellEditors(newEditors);
248                 }
249
250                 // adjust table fields.
251                 viewer.refresh();
252                 cols = table.getColumns();
253                 for (int i = 0; i < cols.length; i++) {
254                         cols[i].pack();
255                 }
256         }
257
258         void refreshTableLengthText() {
259                 String cols = String.valueOf(model.getColumnCount());
260                 if (!cols.equals(colsText.getText())) {
261                         colsText.setText(cols);
262                 }
263                 String rows = String.valueOf(model.getRowCount());
264                 if (!rows.equals(rowsText.getText())) {
265                         rowsText.setText(rows);
266                 }
267         }
268
269         void refreshButtonState() {
270                 Element e = getCurrentSelection();
271                 boolean enable = (e != null);
272
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)) {
278                                 currentIndex = i;
279                         }
280                 }
281                 upButton.setEnabled(enable && currentIndex > 0);
282                 downButton.setEnabled(enable && currentIndex < rows.length - 1);
283         }
284
285         Element getCurrentSelection() {
286                 IStructuredSelection sel = (IStructuredSelection) viewer.getSelection();
287                 return (sel != null) ? (Element) sel.getFirstElement() : null;
288         }
289
290 }