Skip to main content
summaryrefslogtreecommitdiffstats
blob: c612aac4f60c0a65cf969494880bbe0330df9660 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.commands.range;

import org.eclipse.jst.pagedesigner.dom.DOMPosition;
import org.eclipse.jst.pagedesigner.dom.DOMPositionHelper;
import org.eclipse.jst.pagedesigner.dom.DOMRange;
import org.eclipse.jst.pagedesigner.dom.DOMRefPosition;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.w3c.dom.Text;

/**
 * This command can used to handle things like "paste". Or keyboard printable
 * ascii key. Note: ENTER key is not handled here.
 * 
 * @author mengbo
 */
// FIXME: \r \n in the content string is not handled.
public class ContentCommand extends RangeModeCommand {

	private String _content;

	/**
	 * @param viewer
	 * @param content 
	 */
	public ContentCommand(IHTMLGraphicalViewer viewer, String content) {
		super("", viewer);
		_content = content;
	}

	/**
	 * @param viewer
	 * @param c
	 */
	public ContentCommand(IHTMLGraphicalViewer viewer, char c) {
		super("", viewer);
		_content = String.valueOf(c);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.commands.DesignerCommand#doExecute()
	 */
	protected DOMRange doRangeExecute(DOMRange range) {
		if (range == null)
			return null;

		IDOMPosition position = DOMPositionHelper.removeRange(range);
		position = doContent(position);
		return new DOMRange(position, position);

	}

	/**
	 * @param position
	 * @return ??
	 */
	protected IDOMPosition doContent(IDOMPosition position) {
		position = DOMPositionHelper.mergeIntoText(position);

		if (position.getContainerNode() instanceof Text) {
			Text text = (Text) position.getContainerNode();
			String data = text.getData();
			int offset = position.getOffset();
			String newData = data.substring(0, offset) + _content
					+ data.substring(offset);
			text.setData(newData);
			return new DOMPosition(text, offset + _content.length());
		}
        // we need to create a text node.
        Text text = getDocument().createTextNode(_content);
        position.getContainerNode().insertBefore(text,
        		position.getNextSiblingNode());
        return new DOMRefPosition(text, true);
	}

	// protected DesignPosition doContent()
	// {
	// // DesignPosition position = removeRange();
	// DesignPosition position = this.getSelectionRange().getEndPosition();
	// if ('\r' == _content || '\n' == _content)
	// {
	// Element br = getModel().getDocument().createElement("BR");
	// Node node = RangeUtil.insertElement(position, br);
	//
	// // we need set the new range to the node.
	// // FIXME: temp code, need to reconsider how to do refresh, when those
	// editpart
	// // are recreated.
	// IDOMNode parent = (IDOMNode) node.getParentNode();
	// EditPart parentPart = (EditPart) parent.getAdapterFor(EditPart.class);
	// List childParts = parentPart.getChildren();
	// for (int i=0; i<childParts.size(); i++)
	// {
	// if (node == ((EditPart)childParts.get(i)).getModel())
	// {
	// return new DesignPosition(parentPart, i+1);
	// }
	// }
	// return new DesignPosition(parentPart, childParts.size());
	// }
	// else
	// {
	// TextPosition textPosition = RangeUtil.insertText(position,
	// String.valueOf(_content));
	// IDOMText text = textPosition.getTextNode();
	// EditPart part = (EditPart) text.getAdapterFor(EditPart.class);
	// return new DesignPosition(part, textPosition.getOffset());
	// }
	// }
	//
}

Back to the top