use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class XMLHyperlinkDetector method getBaseLocation.
/**
* Get the base location from the current model (local file system)
*/
private String getBaseLocation(IDocument document) {
String baseLoc = null;
// get the base location from the current model
IStructuredModel sModel = null;
try {
sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (sModel != null) {
IPath location = new Path(sModel.getBaseLocation());
if (location.toFile().exists()) {
baseLoc = location.toString();
} else {
if (location.segmentCount() > 1) {
baseLoc = ResourcesPlugin.getWorkspace().getRoot().getFile(location).getLocation().toString();
} else {
baseLoc = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(location).toString();
}
}
}
} finally {
if (sModel != null) {
sModel.releaseFromRead();
}
}
return baseLoc;
}
use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class XMLHyperlinkDetector method getCurrentNode.
/**
* Returns the node the cursor is currently on in the document. null if no
* node is selected
*
* @param offset
* @return Node either element, doctype, text, or null
*/
private Node getCurrentNode(IDocument document, int offset) {
// get the current node at the offset (returns either: element,
// doctype, text)
IndexedRegion inode = null;
IStructuredModel sModel = null;
try {
sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
inode = sModel.getIndexedRegion(offset);
if (inode == null) {
inode = sModel.getIndexedRegion(offset - 1);
}
} finally {
if (sModel != null) {
sModel.releaseFromRead();
}
}
if (inode instanceof Node) {
return (Node) inode;
}
return null;
}
use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class JSPJavaSelectionProvider method getSelection.
static IJavaScriptElement[] getSelection(ITextEditor textEditor) {
IJavaScriptElement[] elements = null;
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
ISelection selection = textEditor.getSelectionProvider().getSelection();
if (selection instanceof ITextSelection) {
ITextSelection textSelection = (ITextSelection) selection;
// get the JSP translation object for this editor's document
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model instanceof IDOMModel) {
IDOMModel xmlModel = (IDOMModel) model;
IDOMDocument xmlDoc = xmlModel.getDocument();
JsTranslationAdapter adapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class);
if (adapter != null) {
IJsTranslation translation = adapter.getJsTranslation(true);
elements = translation.getElementsFromJsRange(translation.getJavaScriptOffset(textSelection.getOffset()), translation.getJavaScriptOffset(textSelection.getOffset() + textSelection.getLength()));
}
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
if (elements == null) {
elements = new IJavaScriptElement[0];
}
return elements;
}
use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class StandardEditorActionsAction method delete.
private void delete(IAction action) {
JsJfaceNode[] nodes = parseSelection();
if (nodes == null || nodes.length == 0) {
return;
}
IStructuredDocument lastDoc = null;
IModelManager modelManager = StructuredModelManager.getModelManager();
IStructuredModel model = null;
try {
int start;
int length;
for (int i = 0; i < nodes.length; i++) {
JsJfaceNode currentNode = nodes[i];
start = currentNode.getStartOffset();
length = currentNode.getLength();
IStructuredDocument doc = currentNode.getStructuredDocument();
if (doc != lastDoc) {
lastDoc = doc;
if (model != null) {
model.endRecording(action);
model.changedModel();
model.releaseFromEdit();
}
if (modelManager != null) {
model = modelManager.getExistingModelForEdit(doc);
model.aboutToChangeModel();
model.beginRecording(action, "Delete JavaScript Element", "Delete JavaScript Element");
}
}
// $NON-NLS-1$
doc.replaceText(action, start, length, "");
}
model.endRecording(action);
} catch (Exception e) {
// $NON-NLS-1$
System.out.println(Messages.getString("StandardEditorActionsAction.8") + e);
} finally {
if (model != null) {
model.changedModel();
model.releaseFromEdit();
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.IStructuredModel in project webtools.sourceediting by eclipse.
the class StandardEditorActionsAction method paste.
private void paste(IAction action, boolean atEnd) {
JsJfaceNode[] nodes = parseSelection();
if (nodes == null || nodes.length == 0) {
return;
}
int startOfPaste = -1;
IStructuredDocument doc = null;
/* Figure out where to paste the content */
if (atEnd) {
for (int i = 0; i < nodes.length; i++) {
if ((nodes[i].getStartOffset() + nodes[i].getLength()) > startOfPaste) {
startOfPaste = (nodes[i].getStartOffset() + nodes[i].getLength());
doc = nodes[i].getStructuredDocument();
}
}
} else {
for (int i = 0; i < nodes.length; i++) {
if ((nodes[i].getStartOffset() < startOfPaste || startOfPaste < 0)) {
startOfPaste = nodes[i].getStartOffset();
doc = nodes[i].getStructuredDocument();
}
}
}
Clipboard clipboard = null;
IModelManager modelManager = StructuredModelManager.getModelManager();
IStructuredModel model = null;
try {
clipboard = new Clipboard(Display.getCurrent());
String pasteString = (String) clipboard.getContents(TextTransfer.getInstance());
model = modelManager.getExistingModelForEdit(doc);
model.aboutToChangeModel();
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
model.beginRecording(action, Messages.getString("StandardEditorActionsAction.9") + (atEnd ? Messages.getString("StandardEditorActionsAction.10") : Messages.getString("StandardEditorActionsAction.11")) + Messages.getString("StandardEditorActionsAction.12"), Messages.getString("StandardEditorActionsAction.13") + (atEnd ? Messages.getString("StandardEditorActionsAction.14") : Messages.getString("StandardEditorActionsAction.15")) + Messages.getString("StandardEditorActionsAction.16"));
doc.replaceText(action, startOfPaste, 0, pasteString);
} finally {
if (clipboard != null) {
clipboard.dispose();
}
if (model != null) {
model.endRecording(action);
model.changedModel();
model.releaseFromEdit();
}
}
}
Aggregations