2
* Created by SharpDevelop.
3
* User: Daniel Grunwald
8
using ICSharpCode.SharpDevelop.Editor;
10
using System.Collections;
11
using System.Diagnostics;
13
using System.Resources;
15
using ICSharpCode.Core;
16
using ICSharpCode.SharpDevelop;
17
using ICSharpCode.SharpDevelop.Gui;
19
namespace StringResourceToolAddIn
21
public class ToolCommand1 : AbstractMenuCommand
23
public override void Run()
25
// Here an example that shows how to access the current text document:
27
ITextEditorProvider tecp = WorkbenchSingleton.Workbench.ActiveContent as ITextEditorProvider;
29
// active content is not a text editor control
33
// Get the active text area from the control:
34
ITextEditor textEditor = tecp.TextEditor;
35
if (textEditor.SelectionLength == 0)
37
// get the selected text:
38
string text = textEditor.SelectedText;
40
string sdSrcPath = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location),
42
string resxFile = Path.Combine(sdSrcPath, "../data/Resources/StringResources.resx");
44
using (ResXResourceReader r = new ResXResourceReader(resxFile)) {
45
IDictionaryEnumerator en = r.GetEnumerator();
46
// Goes through the enumerator, printing out the key and value pairs.
47
while (en.MoveNext()) {
48
if (object.Equals(en.Value, text)) {
49
SetText(textEditor, en.Key.ToString(), text);
55
string resourceName = MessageService.ShowInputBox("Add Resource", "Please enter the name for the new resource.\n" +
56
"This should be a namespace-like construct, please see what the names of resources in the same component are.", PropertyService.Get("ResourceToolLastResourceName"));
57
if (resourceName == null || resourceName.Length == 0) return;
58
PropertyService.Set("ResourceToolLastResourceName", resourceName);
60
string purpose = MessageService.ShowInputBox("Add Resource", "Enter resource purpose (may be empty)", "");
61
if (purpose == null) return;
63
SetText(textEditor, resourceName, text);
65
string path = Path.GetFullPath(Path.Combine(sdSrcPath, "Tools/StringResourceTool/bin/Debug"));
66
ProcessStartInfo info = new ProcessStartInfo(path + "\\StringResourceTool.exe",
67
"\"" + resourceName + "\" "
69
+ "\"" + purpose + "\"");
70
info.WorkingDirectory = path;
73
} catch (Exception ex) {
74
MessageService.ShowException(ex, "Error starting " + info.FileName);
78
void SetText(ITextEditor textEditor, string resourceName, string oldText)
80
// ensure caret is at start of selection / deselect text
81
textEditor.Select(textEditor.SelectionStart, 0);
82
// replace the selected text with the new text:
84
if (Path.GetExtension(textEditor.FileName) == ".xaml")
85
newText = "{core:Localize " + resourceName + "}";
87
newText = "$" + "{res:" + resourceName + "}";
88
// Replace() takes the arguments: start offset to replace, length of the text to remove, new text
89
textEditor.Document.Replace(textEditor.Caret.Offset, oldText.Length, newText);