001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.visualweb.designer;
041:
042: import java.awt.Dimension;
043: import java.awt.Graphics2D;
044: import java.awt.Image;
045: import java.awt.Point;
046: import java.awt.datatransfer.DataFlavor;
047: import java.awt.datatransfer.Transferable;
048: import java.io.IOException;
049: import java.io.StringReader;
050: import java.net.MalformedURLException;
051: import java.net.URL;
052: import java.util.ArrayList;
053: import java.util.List;
054: import javax.swing.JComponent;
055: import javax.xml.parsers.DocumentBuilder;
056: import javax.xml.parsers.ParserConfigurationException;
057: import org.netbeans.modules.visualweb.api.designer.Designer;
058: import org.netbeans.modules.visualweb.api.designer.DomProvider;
059: import org.netbeans.modules.visualweb.api.designer.DomProvider.DomPosition.Bias;
060: import org.netbeans.modules.visualweb.api.designer.DomProviderService;
061: import org.netbeans.modules.visualweb.api.designer.markup.MarkupService;
062: import org.netbeans.modules.visualweb.spi.designer.Decoration;
063: import org.netbeans.spi.palette.PaletteController;
064: import org.openide.util.Exceptions;
065: import org.w3c.dom.Document;
066: import org.w3c.dom.Element;
067: import org.w3c.dom.Node;
068: import org.w3c.dom.NodeList;
069: import org.xml.sax.InputSource;
070: import org.xml.sax.SAXException;
071:
072: /**
073: *
074: * @author Peter Zavadsky
075: */
076: public final class Util {
077:
078: private Util() {
079: }
080:
081: public static DomProvider createDomProvider() {
082: return new TestDomProvider();
083: }
084:
085: private static class TestDomProvider implements DomProvider {
086:
087: private final Document document;
088: private final Element bodyElement;
089: private static final DomProviderService domProviderService = new TestDomProviderService();
090:
091: public TestDomProvider() {
092: this .document = createTestDocument();
093: this .bodyElement = findBodyElement(document
094: .getDocumentElement());
095: }
096:
097: public Document getHtmlDom() {
098: return document;
099: }
100:
101: public Element getHtmlBody() {
102: return bodyElement;
103: }
104:
105: public PaletteController getPaletteController() {
106: return null;
107: }
108:
109: public boolean canImport(JComponent comp,
110: DataFlavor[] transferFlavors, Transferable transferable) {
111: return false;
112: }
113:
114: public int computeActions(Element dropeeComponentRootElement,
115: Transferable transferable) {
116: return -1;
117: }
118:
119: public int processLinks(Element origElement,
120: Element componentRootElement) {
121: return -1;
122: }
123:
124: public boolean isGridMode() {
125: return false;
126: }
127:
128: public URL getBaseUrl() {
129: return null;
130: }
131:
132: public URL resolveUrl(String urlString) {
133: try {
134: return new URL(urlString);
135: } catch (MalformedURLException ex) {
136: Exceptions.printStackTrace(ex);
137: }
138: return null;
139: }
140:
141: public boolean canDropComponentsAtNode(
142: Element[] componentRootElements, Node node) {
143: return false;
144: }
145:
146: public boolean isFormComponent(Element componentRootElement) {
147: return false;
148: }
149:
150: public int getDropType(Element origDropeeComponentRootElement,
151: Element droppeeElement, Transferable t, boolean linkOnly) {
152: return -1;
153: }
154:
155: public int getDropTypeForComponent(
156: Element origDropeeComponentRootElement,
157: Element droppeeElement, Element componentRootElement,
158: boolean linkOnly) {
159: return -1;
160: }
161:
162: public Element getComponentRootElementEquivalentTo(
163: Element oldComponentRootElement) {
164: return oldComponentRootElement;
165: }
166:
167: public boolean canHighlightComponentRootElmenet(
168: Element componentRootElement) {
169: return false;
170: }
171:
172: public boolean moveComponent(Element componentRootElement,
173: Node parentNode, Node before) {
174: return false;
175: }
176:
177: public Designer[] getExternalDesigners(URL url) {
178: return new Designer[0];
179: }
180:
181: public boolean hasCachedExternalFrames() {
182: return false;
183: }
184:
185: public DomDocument getDomDocument() {
186: return null;
187: }
188:
189: public int compareBoundaryPoints(Node endPointA, int offsetA,
190: Node endPointB, int offsetB) {
191: return -1;
192: }
193:
194: public DomPosition createDomPosition(Node node, int offset,
195: Bias bias) {
196: return null;
197: }
198:
199: public DomPosition createDomPosition(Node node, boolean after) {
200: return null;
201: }
202:
203: public DomRange createDomRange(Node dotNode, int dotOffset,
204: Node markNode, int markOffset) {
205: return null;
206: }
207:
208: public DomPosition first(DomPosition dot, DomPosition mark) {
209: return null;
210: }
211:
212: public DomPosition last(DomPosition dot, DomPosition mark) {
213: return null;
214: }
215:
216: public boolean isRenderedNode(Node node) {
217: return node != null && node.getOwnerDocument() == document;
218: }
219:
220: public void paintDesignerDecorations(Graphics2D g,
221: Designer designer) {
222: }
223:
224: public Decoration getDecoration(Element element) {
225: return null;
226: }
227:
228: public InlineEditorSupport createInlineEditorSupport(
229: Element componentRootElement, String propertyName,
230: String xpath) {
231: return null;
232: }
233:
234: public void importString(Designer designer, String string,
235: Point canvasPos, Node documentPosNode,
236: int documentPosOffset, Dimension dimension,
237: boolean isGrid, Element droppeeElement,
238: Element dropeeComponentRootElement) {
239: }
240:
241: public boolean importData(Designer designer, JComponent comp,
242: Transferable t, Point canvasPos, Node documentPosNode,
243: int documentPosOffset, Dimension dimension,
244: boolean isGrid, Element droppeeElement,
245: Element dropeeComponentRootElement, int dropAction) {
246: return false;
247: }
248:
249: public DomProviderService getDomProviderService() {
250: return domProviderService;
251: }
252:
253: } // TestDomProvider.
254:
255: private static class TestDomProviderService implements
256: DomProviderService {
257:
258: public int getExpandedOffset(String unexpanded,
259: int unexpandedOffset) {
260: throw new UnsupportedOperationException(
261: "Not supported yet.");
262: }
263:
264: public int getUnexpandedOffset(String unexpanded,
265: int expandedOffset) {
266: throw new UnsupportedOperationException(
267: "Not supported yet.");
268: }
269:
270: public String expandHtmlEntities(String html, boolean warn,
271: Node node) {
272: throw new UnsupportedOperationException(
273: "Not supported yet.");
274: }
275:
276: public String getHtmlStream(Node node) {
277: throw new UnsupportedOperationException(
278: "Not supported yet.");
279: }
280:
281: public String getDomDocumentReplacedEventConstant() {
282: throw new UnsupportedOperationException(
283: "Not supported yet.");
284: }
285:
286: public Element getSourceElement(Element componentRootElement) {
287: throw new UnsupportedOperationException(
288: "Not supported yet.");
289: }
290:
291: public boolean isPrincipalElement(Element element,
292: Element parentBoxElement) {
293: throw new UnsupportedOperationException(
294: "Not supported yet.");
295: }
296:
297: public boolean isFocusedElement(Element element) {
298: throw new UnsupportedOperationException(
299: "Not supported yet.");
300: }
301:
302: public boolean ignoreDesignBorder(Element element) {
303: throw new UnsupportedOperationException(
304: "Not supported yet.");
305: }
306:
307: public Element getSourceElementWhichRendersChildren(
308: Element element) {
309: throw new UnsupportedOperationException(
310: "Not supported yet.");
311: }
312:
313: public Element[] getChildComponentRootElements(
314: Element componentRootElement) {
315: throw new UnsupportedOperationException(
316: "Not supported yet.");
317: }
318:
319: public boolean isFacesComponent(Element componentRootElement) {
320: throw new UnsupportedOperationException(
321: "Not supported yet.");
322: }
323:
324: public String getRegionDisplayName(Element regionElement) {
325: throw new UnsupportedOperationException(
326: "Not supported yet.");
327: }
328:
329: public boolean isSameRegionOfElement(Element regionElement,
330: Element element) {
331: throw new UnsupportedOperationException(
332: "Not supported yet.");
333: }
334:
335: public Element getComponentRootElementForElement(Element element) {
336: throw new UnsupportedOperationException(
337: "Not supported yet.");
338: }
339:
340: public String getInstanceName(Element componentRootElement) {
341: throw new UnsupportedOperationException(
342: "Not supported yet.");
343: }
344:
345: public boolean isIncludeComponentBox(
346: Element componentRootElement) {
347: throw new UnsupportedOperationException(
348: "Not supported yet.");
349: }
350:
351: public boolean isSpecialComponent(Element componentRootElement) {
352: throw new UnsupportedOperationException(
353: "Not supported yet.");
354: }
355:
356: public boolean isTrayComponent(Element componentRootElement) {
357: throw new UnsupportedOperationException(
358: "Not supported yet.");
359: }
360:
361: public boolean isCssPositionable(Element componentRootElement) {
362: throw new UnsupportedOperationException(
363: "Not supported yet.");
364: }
365:
366: public boolean isEscapedComponent(Element componentRootElement) {
367: throw new UnsupportedOperationException(
368: "Not supported yet.");
369: }
370:
371: public Element getParentComponent(Element componentRootElement) {
372: throw new UnsupportedOperationException(
373: "Not supported yet.");
374: }
375:
376: public Element[] getChildComponents(Element componentRootElement) {
377: throw new UnsupportedOperationException(
378: "Not supported yet.");
379: }
380:
381: public boolean isContainerComponent(Element componentRootElement) {
382: throw new UnsupportedOperationException(
383: "Not supported yet.");
384: }
385:
386: public boolean isContainerTypeComponent(
387: Element componentRootElement) {
388: throw new UnsupportedOperationException(
389: "Not supported yet.");
390: }
391:
392: public String[] getEditableProperties(
393: Element componentRootElement) {
394: throw new UnsupportedOperationException(
395: "Not supported yet.");
396: }
397:
398: public ResizeConstraint[] getResizeConstraintsForComponent(
399: Element componentRootElement) {
400: throw new UnsupportedOperationException(
401: "Not supported yet.");
402: }
403:
404: public boolean isRootContainerComponent(
405: Element componentRootElement) {
406: throw new UnsupportedOperationException(
407: "Not supported yet.");
408: }
409:
410: public boolean hasDefaultProperty(Element componentRootElement) {
411: throw new UnsupportedOperationException(
412: "Not supported yet.");
413: }
414:
415: public boolean focusDefaultProperty(
416: Element componentRootElement, String content) {
417: throw new UnsupportedOperationException(
418: "Not supported yet.");
419: }
420:
421: public Image getIcon(Element componentRootElement) {
422: throw new UnsupportedOperationException(
423: "Not supported yet.");
424: }
425:
426: public Element getComponentRootElementFromNode(
427: org.openide.nodes.Node node) {
428: throw new UnsupportedOperationException(
429: "Not supported yet.");
430: }
431:
432: public boolean hasTableResizeSupport(
433: Element tableComponentRootElement) {
434: throw new UnsupportedOperationException(
435: "Not supported yet.");
436: }
437:
438: public int testResizeColumn(Element tableComponentRootElement,
439: int row, int column, int width) {
440: throw new UnsupportedOperationException(
441: "Not supported yet.");
442: }
443:
444: public int testResizeRow(Element tableComponentRootElement,
445: int row, int column, int height) {
446: throw new UnsupportedOperationException(
447: "Not supported yet.");
448: }
449:
450: public void resizeColumn(Element tableComponentRootElement,
451: int column, int width) {
452: throw new UnsupportedOperationException(
453: "Not supported yet.");
454: }
455:
456: public void resizeRow(Element tableComponentRootElement,
457: int row, int height) {
458: throw new UnsupportedOperationException(
459: "Not supported yet.");
460: }
461:
462: public boolean areLinkedToSameBean(Element oneElement,
463: Element otherElement) {
464: throw new UnsupportedOperationException(
465: "Not supported yet.");
466: }
467:
468: public Node findPropertyNode(Node root, String xpaths) {
469: throw new UnsupportedOperationException(
470: "Not supported yet.");
471: }
472:
473: } // TestDomProviderService.
474:
475: private static Document createTestDocument() {
476: try {
477: DocumentBuilder documentBuilder = MarkupService
478: .createRaveRenderedDocumentBuilder(true);
479: return documentBuilder
480: .parse(new InputSource(
481: new StringReader(
482: "<html>"
483: + "<head>"
484: + "<style type=\"text/css\">"
485: + "h1 {color: #00ff00}"
486: + "h2 {color: #dda0dd}"
487: + "p {color: rgb(0,0,255)}"
488: + "</style>"
489: + "</head>"
490: + "<body>"
491: + "<h1>This is header 1</h1>"
492: + "<h2>This is header 2</h2>"
493: + "<p>This is a paragraph</p>"
494: // Test percentage
495: + "<div style=\"width: 50%\">Here is only 50% width</div>"
496: + "</body>" + "</html>")));
497: } catch (SAXException ex) {
498: Exceptions.printStackTrace(ex);
499: } catch (IOException ex) {
500: Exceptions.printStackTrace(ex);
501: } catch (ParserConfigurationException ex) {
502: Exceptions.printStackTrace(ex);
503: }
504: return null;
505: }
506:
507: private static Element findBodyElement(Element element) {
508: if (element == null) {
509: return null;
510: }
511: if ("body".equals(element.getTagName())) { // TEMP
512: return element;
513: }
514:
515: Element[] children = getChildElements(element);
516: for (Element child : children) {
517: Element bodyElement = findBodyElement(child);
518: if (bodyElement != null) {
519: return bodyElement;
520: }
521: }
522: return null;
523: }
524:
525: private static Element[] getChildElements(Element element) {
526: if (element == null) {
527: return new Element[0];
528: }
529:
530: List<Element> children = new ArrayList<Element>();
531: NodeList nodeList = element.getChildNodes();
532: for (int i = 0; i < nodeList.getLength(); i++) {
533: Node child = nodeList.item(i);
534: if (child instanceof Element) {
535: children.add((Element) child);
536: }
537: }
538: return children.toArray(new Element[children.size()]);
539: }
540: }
|