Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8c19239a14c1c34dd713a8ea0553b21ab8f804a1 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*******************************************************************************
 * 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.jsf.ui.elementedit.util;

import java.util.List;

import org.eclipse.jst.jsf.core.internal.tld.IJSFConstants;
import org.eclipse.jst.pagedesigner.jsf.core.dom.JSFDOMUtil;
import org.w3c.dom.Element;

import org.eclipse.jst.pagedesigner.utils.DOMUtil;

/**
 * @author mengbo
 * @version 1.5
 */
public class PanelGridUtil
{
    private static final int INVALID_POSITION = -10;
    private Element          _panelGrid       = null;

    /**
     * @param panelGrid
     */
    public PanelGridUtil(Element panelGrid)
    {
        this._panelGrid = panelGrid;
    }

    /**
     * @param domIndex
     * @return the row index in the panel of the relative dom index
     */
    public int convertRowIndexFromDomToView(int domIndex)
    {
        boolean hasHeaderRow = (JSFDOMUtil.findFacet(this._panelGrid, "header") != null); //$NON-NLS-1$
        boolean hasFooterRow = (JSFDOMUtil.findFacet(this._panelGrid, "footer") != null); //$NON-NLS-1$
        if (!hasFooterRow)
        {
            return domIndex;
        }
        if (domIndex == 0)
        {
            return domIndex;
        }
        if (hasHeaderRow)
        {
            if (domIndex > 1)
            {
                return domIndex - 1;
            }
        }
        else
        {
            if (domIndex > 0)
            {
                return domIndex - 1;
            }
        }
        //must be footer
        int uiRows = getUIRowCount();
        return domIndex + uiRows;
    }

    /**
     * @param cell
     * @return the dom row index of cell in the panel
     */
    public int getDomRowIndex(Element cell)
    {
        boolean hasHeaderRow = (JSFDOMUtil.findFacet(this._panelGrid, "header") != null); //$NON-NLS-1$
        boolean hasFooterRow = (JSFDOMUtil.findFacet(this._panelGrid, "footer") != null); //$NON-NLS-1$
        //if cell is header or footer
        boolean isFacet = JSFDOMUtil.isFacet(cell);
        Element parent = (Element) cell.getParentNode();
        boolean isParentFacet = JSFDOMUtil.isFacet(parent);
        if (isParentFacet)
        {
            cell = parent;
        }
        if (isFacet || isParentFacet)
        {
            String attrName = cell.getAttribute("name"); //$NON-NLS-1$
            if ("header".equalsIgnoreCase(attrName)) //$NON-NLS-1$
            {
                return 0;
            }
            else if ("footer".equalsIgnoreCase(attrName)) //$NON-NLS-1$
            {
                if (hasHeaderRow)
                {
                    return 1;
                }
                return 0;
            }
        }

        int columns = DOMUtil.getIntAttributeIgnoreCase(this._panelGrid, IJSFConstants.ATTR_COLUMNS, 1);
        if (columns < 1)
        {
            columns = 1;
        }
        int pos = getPosition(cell);
        int rowIndex = pos / columns;

        if (hasHeaderRow)
        {
            rowIndex++;
        }
        if (hasFooterRow)
        {
            rowIndex++;
        }

        return rowIndex;
    }

    /**
     * @param cell
     * @return the column index of cell in the panel
     */
    public int getDomColumnIndex(Element cell)
    {
        int columns = DOMUtil.getIntAttributeIgnoreCase(this._panelGrid, IJSFConstants.ATTR_COLUMNS, 1);
        if (columns < 1)
        {
            columns = 1;
        }
        int pos = getPosition(cell);
        //if position is at header or footer,then insert column action should be disabled
        if (pos == INVALID_POSITION)
        {
            return pos;
        }

        int columnIndex = pos % columns;
        return columnIndex;
    }

    /**
     * @return the row count in the panel
     */
    public int getUIRowCount()
    {
        int columns = DOMUtil.getIntAttributeIgnoreCase(this._panelGrid, IJSFConstants.ATTR_COLUMNS, 1);
        if (columns < 1)
        {
            columns = 1;
        }
        List children = JSFDOMUtil.getUIComponentChildren(this._panelGrid);
        int numRows = (children.size() + columns - 1) / columns;

        return numRows;
    }

    private int getPosition(Element cell)
    {
        List children = JSFDOMUtil.getUIComponentChildren(this._panelGrid);
        int size = children.size();
        int i = 0;
        for (i = 0; i < size; i++)
        {
            if (cell == children.get(i))
            {
                break;
            }
        }
        if (i == size)
        {
            return INVALID_POSITION;
        }
        return i;
    }

}

Back to the top