01: /*******************************************************************************
02: * Copyright (c) 2005, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.core.text;
11:
12: import java.util.HashMap;
13:
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
22: AbstractTextChangeListener {
23:
24: protected HashMap fReadableNames = null;
25:
26: public AbstractKeyValueTextChangeListener(IDocument document,
27: boolean generateReadableNames) {
28: super (document);
29: if (generateReadableNames)
30: fReadableNames = new HashMap();
31: }
32:
33: public TextEdit[] getTextOperations() {
34: if (fOperationTable.size() == 0)
35: return new TextEdit[0];
36: return (TextEdit[]) fOperationTable.values().toArray(
37: new TextEdit[fOperationTable.size()]);
38: }
39:
40: protected void insertKey(IDocumentKey key, String name) {
41: int offset = PropertiesUtil.getInsertOffset(fDocument);
42: InsertEdit edit = new InsertEdit(offset, key.write());
43: fOperationTable.put(key, edit);
44: if (fReadableNames != null)
45: fReadableNames.put(edit, name);
46: }
47:
48: protected void deleteKey(IDocumentKey key, String name) {
49: if (key.getOffset() >= 0) {
50: DeleteEdit edit = new DeleteEdit(key.getOffset(), key
51: .getLength());
52: fOperationTable.put(key, edit);
53: if (fReadableNames != null)
54: fReadableNames.put(edit, name);
55: }
56: }
57:
58: protected void modifyKey(IDocumentKey key, String name) {
59: if (key.getOffset() == -1)
60: insertKey(key, name);
61: else {
62: ReplaceEdit edit = new ReplaceEdit(key.getOffset(), key
63: .getLength(), key.write());
64: fOperationTable.put(key, edit);
65: if (fReadableNames != null)
66: fReadableNames.put(edit, name);
67: }
68: }
69:
70: public String getReadableName(TextEdit edit) {
71: if (fReadableNames != null && fReadableNames.containsKey(edit))
72: return (String) fReadableNames.get(edit);
73: return null;
74: }
75: }
|