KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > product > TextEditUtilities


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.product;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.TextUtilities;
16 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
17 import org.eclipse.pde.internal.core.text.IDocumentNode;
18 import org.eclipse.pde.internal.core.util.PDEXMLHelper;
19 import org.eclipse.text.edits.InsertEdit;
20 import org.eclipse.text.edits.ReplaceEdit;
21 import org.eclipse.text.edits.TextEdit;
22
23 public class TextEditUtilities {
24
25     public static TextEdit getInsertOperation(IDocumentNode node, IDocument doc) {
26         node = getHighestNodeToBeWritten(node, doc);
27         if (node.getParentNode() == null)
28             return new InsertEdit(0, node.write(true));
29
30         if (node.getOffset() > -1) {
31             // this is an element that was of the form <element/>
32
// it now needs to be broken up into <element><new/></element>
33
return new ReplaceEdit(node.getOffset(), node.getLength(), node.write(false));
34         }
35         // try to insert after last sibling that has an offset
36
TextEdit op = insertAfterSibling(node, doc);
37         
38         // insert as first child of its parent if op is null
39
return (op != null) ? op : insertAsFirstChild(node, doc);
40     }
41     
42     public static TextEdit addAttributeOperation(IDocumentAttribute attr, String JavaDoc newValue, IDocument doc) {
43         int offset = attr.getValueOffset();
44         if (offset > -1)
45             return new ReplaceEdit(offset, attr.getValueLength(), PDEXMLHelper.getWritableString(newValue));
46
47         IDocumentNode node = attr.getEnclosingElement();
48         if (node.getOffset() > -1) {
49             int len = getNextPosition(doc, node.getOffset(), '>');
50             return new ReplaceEdit(node.getOffset(), len + 1, node.writeShallow(shouldTerminateElement(doc, node.getOffset()+ len)));
51         }
52         return getInsertOperation(node, doc);
53     }
54     
55     private static boolean shouldTerminateElement(IDocument doc, int offset) {
56         try {
57             return doc.get(offset-1, 1).toCharArray()[0] == '/';
58         } catch (BadLocationException e) {
59         }
60         return false;
61     }
62
63     private static IDocumentNode getHighestNodeToBeWritten(IDocumentNode node, IDocument doc) {
64         IDocumentNode parent = node.getParentNode();
65         if (parent == null)
66             return node;
67         if (parent.getOffset() > -1) {
68             try {
69                 String JavaDoc endChars = doc.get(parent.getOffset() + parent.getLength() - 2, 2);
70                 return ("/>".equals(endChars)) ? parent : node; //$NON-NLS-1$
71
} catch (BadLocationException e) {
72                 return node;
73             }
74             
75         }
76         return getHighestNodeToBeWritten(parent, doc);
77     }
78
79     private static InsertEdit insertAfterSibling(IDocumentNode node, IDocument doc) {
80         IDocumentNode sibling = node.getPreviousSibling();
81         for (;;) {
82             if (sibling == null)
83                 break;
84             if (sibling.getOffset() > -1) {
85                 node.setLineIndent(sibling.getLineIndent());
86                 String JavaDoc sep = TextUtilities.getDefaultLineDelimiter(doc);
87                 return new InsertEdit(sibling.getOffset() + sibling.getLength(), sep + node.write(true));
88             }
89             sibling = sibling.getPreviousSibling();
90         }
91         return null;
92     }
93     
94     private static InsertEdit insertAsFirstChild(IDocumentNode node, IDocument doc) {
95         int offset = node.getParentNode().getOffset();
96         int length = getNextPosition(doc, offset, '>');
97         node.setLineIndent(node.getParentNode().getLineIndent() + 3);
98         String JavaDoc sep = TextUtilities.getDefaultLineDelimiter(doc);
99         return new InsertEdit(offset+ length + 1, sep + node.write(true));
100     }
101     
102     private static int getNextPosition(IDocument doc, int offset, char ch) {
103         int i = 0;
104         try {
105             for (i = 0; i + offset < doc.getLength() ;i++) {
106                 if (ch == doc.get(offset + i, 1).toCharArray()[0])
107                     break;
108             }
109         } catch (BadLocationException e) {
110         }
111         return i;
112     }
113
114 }
115
Popular Tags