Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4db65c888f9668747b358c0260f5f572b6d2b6e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.commands.html;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jst.pagedesigner.IHTMLConstants;
import org.eclipse.jst.pagedesigner.commands.CommandResources;
import org.eclipse.jst.pagedesigner.commands.DesignerCommand;
import org.eclipse.jst.pagedesigner.dom.html.TableUtil;
import org.eclipse.jst.pagedesigner.utils.DOMUtil;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author mengbo
 * @version 1.5
 */
public class TableInsertColumnCommand extends DesignerCommand {
	private Element _tableEle;

	private int _columnIndex;

	private TableUtil _tableUtil;

	/**
	 * @param viewer
	 * @param dataTable
	 * @param index
	 */
	public TableInsertColumnCommand(IHTMLGraphicalViewer viewer,
			Element dataTable, int index) {
		super(
				CommandResources
						.getString("TableInsertColumnCommand.Label.InsertColumn"), viewer); //$NON-NLS-1$
		this._tableEle = dataTable;
		this._columnIndex = index;
		this._tableUtil = new TableUtil(this._tableEle);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		if (this._columnIndex < 0) {
			return false;
		}
		boolean isAffectedByColSpan = _tableUtil
				.isAffectedByColSpan(this._columnIndex);
		if (isAffectedByColSpan) {
			return false;
		}

		return super.canExecute();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.commands.DesignerCommand#getAfterCommandDesignerSelection()
	 */
	protected ISelection getAfterCommandDesignerSelection() {
		return toDesignSelection(_tableEle);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.commands.DesignerCommand#doExecute()
	 */
	protected void doExecute() {
		List trList = new ArrayList();
		TableUtil.getTrElements(this._tableEle, trList);

		List[] lists = _tableUtil.getTrCellLists();
		int maxColumn = _tableUtil.getColumnCount();

		boolean isAtLastColumn = false;
		if (this._columnIndex >= maxColumn) {
			isAtLastColumn = true;
		}

		for (int i = 0, size = trList.size(); i < size; i++) {
			Element tr = (Element) trList.get(i);
			boolean hasTH = (DOMUtil.getChildElementsByTagIgnoreCase(tr,
					IHTMLConstants.TAG_TH).size() > 0);

			List cells = lists[i];
			if (isAtLastColumn || (cells.size() <= this._columnIndex)) {
				int index = this._columnIndex + 1;
				for (int k = cells.size(); k < index; k++) {
					tr.appendChild(createDefaultElement(hasTH));
				}
			} else {
				Element cell = (Element) cells.get(this._columnIndex);
				if (!cell.getTagName().equalsIgnoreCase("fake")) //$NON-NLS-1$
				{
					tr.insertBefore(createDefaultElement(hasTH), cell);
				} else {
					boolean hasRealElement = false;
					for (int k = _columnIndex + 1; k < cells.size(); k++) {
						Element td = (Element) cells.get(k);
						if (!td.getTagName().equalsIgnoreCase("fake")) //$NON-NLS-1$
						{
							hasRealElement = true;
							tr.insertBefore(createDefaultElement(hasTH), td);
							break;
						}
					}
					if (!hasRealElement) {
						tr.appendChild(createDefaultElement(hasTH));
					}
				}
			}

		}
		formatNode(this._tableEle);
	}

	private Element createDefaultElement(boolean createTH) {
		Document doc = this._tableEle.getOwnerDocument();
		Element td = null;
		if (createTH) {
			td = doc.createElement(IHTMLConstants.TAG_TH);
		} else {
			td = doc.createElement(IHTMLConstants.TAG_TD);
		}

		Node node = doc.createTextNode(""); //$NON-NLS-1$
		td.appendChild(node);
		return td;
	}
}

Back to the top