Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / commands / range / BlockNodeFinder.java
1 /*******************************************************************************\r
2  * Copyright (c) 2006 Sybase, Inc. and others.\r
3  *\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Sybase, Inc. - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.eclipse.jst.pagedesigner.commands.range;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Arrays;\r
16 import java.util.List;\r
17 \r
18 import org.eclipse.jst.jsf.core.internal.tld.IJSFConstants;\r
19 import org.eclipse.jst.pagedesigner.IHTMLConstants;\r
20 import org.eclipse.jst.pagedesigner.dom.DOMPosition;\r
21 import org.eclipse.jst.pagedesigner.dom.DOMRefPosition;\r
22 import org.eclipse.jst.pagedesigner.dom.EditModelQuery;\r
23 import org.eclipse.jst.pagedesigner.dom.IDOMPosition;\r
24 import org.w3c.dom.Node;\r
25 \r
26 /**\r
27  * @author mengbo\r
28  */\r
29 public class BlockNodeFinder {\r
30         private String[] _additionalTags;\r
31 \r
32         private IDOMPosition _position;\r
33 \r
34         /**\r
35          * @param position\r
36          * @param additionalTags\r
37          */\r
38         public BlockNodeFinder(IDOMPosition position, String[] additionalTags) {\r
39                 _position = position;\r
40                 _additionalTags = additionalTags;\r
41         }\r
42 \r
43         private Node findInlineSiblings(IDOMPosition position, List result,\r
44                         boolean forward) {\r
45                 Node container = EditModelQuery.getInstance().getSibling(position,\r
46                                 forward);\r
47                 if (!forward) {\r
48                         while (container != null) {\r
49                                 if (EditModelQuery.isInline(container)) {\r
50                                         result.add(container);\r
51                                 } else {\r
52                                         return container;\r
53                                 }\r
54                                 container = container.getPreviousSibling();\r
55                         }\r
56                 } else {\r
57                         while (container != null) {\r
58                                 if (EditModelQuery.isInline(container)) {\r
59                                         result.add(container);\r
60                                 } else {\r
61                                         return container;\r
62                                 }\r
63                                 container = container.getNextSibling();\r
64                         }\r
65                 }\r
66                 // the result will be non-zero length.\r
67                 return null;\r
68         }\r
69 \r
70         private Node getParagraphNodes(IDOMPosition position, List result,\r
71                         boolean forward) {\r
72                 Node sResult = findInlineSiblings(position, result, forward);\r
73                 Node container = position.getContainerNode();\r
74                 container = position.isText() ? container.getParentNode() : container;\r
75                 while (sResult == null) {\r
76                         // stop at block, special container and H style nodes.\r
77                         if (EditModelQuery.isBlockNode(container)\r
78                                         || EditModelQuery.isDocument(container)\r
79                                         || (container.getLocalName() != null && (container\r
80                                                         .getLocalName().equals(IJSFConstants.TAG_VIEW) || container\r
81                                                         .getLocalName().equalsIgnoreCase(\r
82                                                                         IHTMLConstants.TAG_HTML))) || //\r
83                                         (_additionalTags != null\r
84                                                         && Arrays.asList(_additionalTags).contains(\r
85                                                                         getTagName()) && Arrays.asList(\r
86                                                         _additionalTags).contains(container))) {\r
87                                 return container;\r
88                         }\r
89                         position = new DOMRefPosition(container, forward);\r
90                         sResult = findInlineSiblings(position, result, forward);\r
91                         container = container.getParentNode();\r
92                 }\r
93                 return sResult;\r
94         }\r
95 \r
96         /**\r
97          * Search for an area between two block nodes or within a block node, search\r
98          * will stop before or under a node which has block display-type, or\r
99          * particular container like "html", jsf "view", .etc, two positions (left\r
100          * and right) are returned in result.\r
101          * \r
102          * The searcher will search parent's directly children, if no block node is\r
103          * found, then go up the node tree to search again.\r
104          * \r
105          * @param position\r
106          * @param result\r
107          */\r
108         public void getParagraphNodes(IDOMPosition position, List result) {\r
109                 List tempResult = new ArrayList();\r
110                 Node r1 = getParagraphNodes(position, tempResult, true);\r
111                 if (EditModelQuery.isChild(r1, position.getContainerNode())) {\r
112                         result.add(new DOMPosition(r1, r1.getChildNodes().getLength()));\r
113                 } else {\r
114                         result.add(new DOMRefPosition(r1, false));\r
115                 }\r
116 \r
117                 Node r2 = getParagraphNodes(position, tempResult, false);\r
118                 if (EditModelQuery.isChild(r2, position.getContainerNode())) {\r
119                         result.add(new DOMPosition(r2, 0));\r
120                 } else {\r
121                         result.add(new DOMRefPosition(r2, true));\r
122                 }\r
123         }\r
124 \r
125         private String getTagName() {\r
126                 String name = _position.getContainerNode().getNodeName();\r
127                 name = name == null ? "" : name.toLowerCase(); //$NON-NLS-1$\r
128                 return name;\r
129         }\r
130 }\r