~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Tools/StringResourceToolAddIn/Src/Command.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created by SharpDevelop.
 
3
 * User: Daniel Grunwald
 
4
 * Date: 19.01.2006
 
5
 * Time: 16:34
 
6
 */
 
7
 
 
8
using ICSharpCode.SharpDevelop.Editor;
 
9
using System;
 
10
using System.Collections;
 
11
using System.Diagnostics;
 
12
using System.IO;
 
13
using System.Resources;
 
14
using System.Text;
 
15
using ICSharpCode.Core;
 
16
using ICSharpCode.SharpDevelop;
 
17
using ICSharpCode.SharpDevelop.Gui;
 
18
 
 
19
namespace StringResourceToolAddIn
 
20
{
 
21
        public class ToolCommand1 : AbstractMenuCommand
 
22
        {
 
23
                public override void Run()
 
24
                {
 
25
                        // Here an example that shows how to access the current text document:
 
26
                        
 
27
                        ITextEditorProvider tecp = WorkbenchSingleton.Workbench.ActiveContent as ITextEditorProvider;
 
28
                        if (tecp == null) {
 
29
                                // active content is not a text editor control
 
30
                                return;
 
31
                        }
 
32
                        
 
33
                        // Get the active text area from the control:
 
34
                        ITextEditor textEditor = tecp.TextEditor;
 
35
                        if (textEditor.SelectionLength == 0)
 
36
                                return;
 
37
                        // get the selected text:
 
38
                        string text = textEditor.SelectedText;
 
39
                        
 
40
                        string sdSrcPath = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location),
 
41
                                                        "../../../..");
 
42
                        string resxFile = Path.Combine(sdSrcPath, "../data/Resources/StringResources.resx");
 
43
                        
 
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);
 
50
                                                return;
 
51
                                        }
 
52
                                }
 
53
                        }
 
54
                        
 
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);
 
59
                        
 
60
                        string purpose = MessageService.ShowInputBox("Add Resource", "Enter resource purpose (may be empty)", "");
 
61
                        if (purpose == null) return;
 
62
                        
 
63
                        SetText(textEditor, resourceName, text);
 
64
                        
 
65
                        string path = Path.GetFullPath(Path.Combine(sdSrcPath, "Tools/StringResourceTool/bin/Debug"));
 
66
                        ProcessStartInfo info = new ProcessStartInfo(path + "\\StringResourceTool.exe",
 
67
                                                                     "\"" + resourceName + "\" "
 
68
                                                                     + "\"" + text + "\" "
 
69
                                                                     + "\"" + purpose + "\"");
 
70
                        info.WorkingDirectory = path;
 
71
                        try {
 
72
                                Process.Start(info);
 
73
                        } catch (Exception ex) {
 
74
                                MessageService.ShowException(ex, "Error starting " + info.FileName);
 
75
                        }
 
76
                }
 
77
                
 
78
                void SetText(ITextEditor textEditor, string resourceName, string oldText)
 
79
                {
 
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:
 
83
                        string newText;
 
84
                        if (Path.GetExtension(textEditor.FileName) == ".xaml")
 
85
                                newText = "{core:Localize " + resourceName + "}";
 
86
                        else
 
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);
 
90
                }
 
91
        }
 
92
}