KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > AbstractKeyValueTextChangeListener


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.core.text;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.pde.internal.core.util.PropertiesUtil;
16 import org.eclipse.text.edits.DeleteEdit;
17 import org.eclipse.text.edits.InsertEdit;
18 import org.eclipse.text.edits.ReplaceEdit;
19 import org.eclipse.text.edits.TextEdit;
20
21 public abstract class AbstractKeyValueTextChangeListener extends AbstractTextChangeListener {
22
23     public AbstractKeyValueTextChangeListener(IDocument document) {
24         super(document);
25     }
26     
27     public TextEdit[] getTextOperations() {
28         if (fOperationTable.size() == 0)
29             return new TextEdit[0];
30         
31         TextEdit[] ops = (TextEdit[])fOperationTable.values().toArray(new TextEdit[fOperationTable.size()]);
32         try {
33             if (!PropertiesUtil.isNewlineNeeded(fDocument))
34                 return ops;
35         } catch (BadLocationException e) {
36         }
37         
38         TextEdit[] result = new TextEdit[fOperationTable.size() + 1];
39         result[0] = new InsertEdit(PropertiesUtil.getInsertOffset(fDocument), fSep);
40         System.arraycopy(ops, 0, result, 1, ops.length);
41         return result;
42     }
43
44     protected void insertKey(IDocumentKey key) {
45         int offset = PropertiesUtil.getInsertOffset(fDocument);
46         fOperationTable.put(key, new InsertEdit(offset, key.write()));
47     }
48     
49     protected void deleteKey(IDocumentKey key) {
50         if (key.getOffset() >= 0)
51             fOperationTable.put(key, new DeleteEdit(key.getOffset(), key.getLength()));
52     }
53     
54     protected void modifyKey(IDocumentKey key) {
55         if (key.getOffset() == -1)
56             insertKey(key);
57         else
58             fOperationTable.put(key, new ReplaceEdit(key.getOffset(), key.getLength(), key.write()));
59     }
60
61 }
62
Popular Tags