public void run() {
// Get the editor, and the various bits we need such as the document,
// the selection and the shell
IEditorPart activeEditor = Workbench.getInstance().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
CFMLEditor thisEdit = (CFMLEditor) activeEditor;
IDocument doc = thisEdit.getDocumentProvider().getDocument(thisEdit.getEditorInput());
ISelection sel = thisEdit.getSelectionProvider().getSelection();
DocItem docItem = thisEdit.getSelectionCursorListener().getSelectedTag();
if (docItem != null && docItem instanceof CfmlTagItem) {
CfmlTagItem tagItem = (CfmlTagItem) docItem;
int tagStart = tagItem.getStartPosition();
int tagEnd = tagItem.getEndPosition();
TextSelection selection;
int startPos = tagItem.getStartPosition();
int endPos = tagItem.getEndPosition();
int start = 0;
int length = 0;
if(tagItem.matchingItem != null) {
if (tagItem.matchingItem.getStartPosition() < tagItem.getStartPosition()) {
start = tagItem.matchingItem.getStartPosition();
length = tagItem.getEndPosition() - tagItem.matchingItem.getStartPosition() + 1;
} else {
start = tagItem.getStartPosition();
length = tagItem.matchingItem.getEndPosition() - tagItem.getStartPosition() + 1;
}
} else {
if (tagItem.matchingItem != null && tagItem.matchingItem.getStartPosition() <= startPos
&& tagItem.matchingItem.getEndPosition() >= startPos) {
start = tagItem.matchingItem.getStartPosition();
length = tagItem.matchingItem.getEndPosition() - tagItem.matchingItem.getStartPosition() + 1;
} else {
start = tagItem.getStartPosition();
length = tagItem.getEndPosition() - tagItem.getStartPosition() + 1;
}
}
// thisEdit.selectAndReveal(tagStart,tagEnd-tagStart+1);
selection = new TextSelection(start, length);
ISelectionProvider selectionProvider = thisEdit.getSelectionProvider();
selectionProvider.setSelection(selection);
/*
* Get the actual textual content of the tag
*/
String tagText = "";
try {
tagText = doc.get(selection.getOffset(), selection.getLength());
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
/*
* Get the attributes as a map TODO: I am not sure if map's keep the
* ordering... need to check this
*/
Map attributeMap = CFDocUtils.parseStartTag(tagItem.getName(), tagText);
/*
* Now open the TagEditDialog, we know the tag name (as a string)
* and the tag attributes (as a map) So, we go and get the tag from
* the dictionary
*/
Tag tagToEdit = DictionaryManager.getDictionary("CF_DICTIONARY").getTag(tagItem.getName());
/*
* Setup the tageditor dialog
*/
TagEditDialog tagview = new TagEditDialog(shell, tagToEdit);
tagview.setSelectedattributes(attributeMap);
/*
* Once the editor closes, we do this
*/
if (tagview.open() == IDialogConstants.OK_ID) {
Properties fieldStore = tagview.getFieldStore(); // The new
// items
ArrayList propOrder = new ArrayList(); // The order of the itmes
Properties attributesToRender = new Properties(); // The
// attributes
// that we
// are going
// to be
// formatting
// Put the original attributes in
Set oldFieldSet = attributeMap.keySet();
for (Iterator iter = oldFieldSet.iterator(); iter.hasNext();) {
String oldElement = (String) iter.next();
propOrder.add(oldElement);
}
attributesToRender.putAll(attributeMap);
// Loop through the new ones
Set newFieldsSet = fieldStore.keySet();
for (Iterator iter = newFieldsSet.iterator(); iter.hasNext();) {
String element = (String) iter.next();
if (attributesToRender.containsKey(element)) {
attributesToRender.setProperty(element, fieldStore.getProperty(element));
} else {
propOrder.add(element);
attributesToRender.put(element, fieldStore.getProperty(element));
}
}
/*
* Pass in the attributes into a Tag Formatter
*/
TagFormatter tf = new TagFormatter(tagToEdit, attributesToRender, propOrder);
// Here is where we actually do the insertion
if (thisEdit instanceof ITextEditor) {
try {
ICFDocument cfd = (ICFDocument) doc;
cfd.replace(selection.getOffset(), selection.getLength(), tf.getTagStart());
} catch (BadLocationException e) {
e.printStackTrace();
}
thisEdit.setFocus();
}
}
}
}