Browse Source

Add context menu command and option page to the test plugin.

pull/129/head
Daniel Grunwald 14 years ago
parent
commit
aaaf350bc0
  1. 2
      ILSpy/ILSpySettings.cs
  2. 41
      TestPlugin/ContextMenuCommand.cs
  3. 8
      TestPlugin/CustomOptionPage.xaml
  4. 94
      TestPlugin/CustomOptionPage.xaml.cs
  5. 46
      TestPlugin/Readme.txt
  6. 12
      TestPlugin/TestPlugin.csproj

2
ILSpy/ILSpySettings.cs

@ -42,7 +42,7 @@ namespace ICSharpCode.ILSpy @@ -42,7 +42,7 @@ namespace ICSharpCode.ILSpy
this.root = root;
}
public XElement this[string section] {
public XElement this[XName section] {
get {
return root.Element(section) ?? new XElement(section);
}

41
TestPlugin/ContextMenuCommand.cs

@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Linq;
using ICSharpCode.ILSpy;
using ICSharpCode.ILSpy.TreeNodes;
using ICSharpCode.TreeView;
using Microsoft.Win32;
using Mono.Cecil;
namespace TestPlugin
{
[ExportContextMenuEntry(Header = "_Save Assembly")]
public class SaveAssembly : IContextMenuEntry
{
public bool IsVisible(SharpTreeNode[] selectedNodes)
{
return selectedNodes.All(n => n is AssemblyTreeNode);
}
public bool IsEnabled(SharpTreeNode[] selectedNodes)
{
return selectedNodes.Length == 1;
}
public void Execute(SharpTreeNode[] selectedNodes)
{
AssemblyTreeNode node = (AssemblyTreeNode)selectedNodes[0];
AssemblyDefinition asm = node.LoadedAssembly.AssemblyDefinition as AssemblyDefinition;
if (asm != null) {
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = node.LoadedAssembly.FileName;
dlg.Filter = "Assembly|*.dll;*.exe";
if (dlg.ShowDialog(MainWindow.Instance) == true) {
asm.MainModule.Write(dlg.FileName);
}
}
}
}
}

8
TestPlugin/CustomOptionPage.xaml

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
<UserControl x:Class="TestPlugin.CustomOptionPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<CheckBox IsChecked="{Binding UselessOption1}">Useless option 1</CheckBox>
<Slider Minimum="0" Maximum="100" Value="{Binding UselessOption2}"/>
</StackPanel>
</UserControl>

94
TestPlugin/CustomOptionPage.xaml.cs

@ -0,0 +1,94 @@ @@ -0,0 +1,94 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Linq;
using ICSharpCode.ILSpy;
namespace TestPlugin
{
[ExportOptionPage("TestPlugin")]
partial class CustomOptionPage : UserControl, IOptionPage
{
static readonly XNamespace ns = "http://www.ilspy.net/testplugin";
public CustomOptionPage()
{
InitializeComponent();
}
public void Load(ILSpySettings settings)
{
// For loading options, use ILSpySetting's indexer.
// If the specified section does exist, the indexer will return a new empty element.
XElement e = settings[ns + "CustomOptions"];
// Now load the options from the XML document:
Options s = new Options();
s.UselessOption1 = (bool?)e.Attribute("useless1") ?? s.UselessOption1;
s.UselessOption2 = (double?)e.Attribute("useless2") ?? s.UselessOption2;
this.DataContext = s;
}
public void Save(XElement root)
{
Options s = (Options)this.DataContext;
// Save the options back into XML:
XElement section = new XElement(ns + "CustomOptions");
section.SetAttributeValue("useless1", s.UselessOption1);
section.SetAttributeValue("useless2", s.UselessOption2);
// Replace the existing section in the settings file, or add a new section,
// if required.
XElement existingElement = root.Element(ns + "CustomOptions");
if (existingElement != null)
existingElement.ReplaceWith(section);
else
root.Add(section);
}
}
class Options : INotifyPropertyChanged
{
bool uselessOption1;
public bool UselessOption1 {
get { return uselessOption1; }
set {
if (uselessOption1 != value) {
uselessOption1 = value;
OnPropertyChanged("UselessOption1");
}
}
}
double uselessOption2;
public double UselessOption2 {
get { return uselessOption2; }
set {
if (uselessOption2 != value) {
uselessOption2 = value;
OnPropertyChanged("UselessOption2");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}

46
TestPlugin/Readme.txt

@ -20,6 +20,50 @@ Adding another language: @@ -20,6 +20,50 @@ Adding another language:
Adding an entry to the main menu:
[ExportMainMenuCommand(Menu = "_File", Header = "_Clear List", MenuCategory = "Open", MenuOrder = 1.5)]
[ExportMainMenuCommand(Menu = "_File", MenuIcon = "Clear.png", Header = "_Clear List", MenuCategory = "Open", MenuOrder = 1.5)]
public class UnloadAllAssembliesCommand : SimpleCommand
Menu: menu into which the item is added
MenuIcon: optional, icon to use for the menu item. Must be embedded as "Resource" (WPF-style resource) in the same assembly as the command type.
Header: text on the menu item
MenuCategory: optional, used for grouping related menu items together. A separator is added between different groups.
MenuOrder: controls the order in which the items appear (items are sorted by this value)
The command class must implement WPF's ICommand interface.
---
Adding an entry to the tool bar:
[ExportToolbarCommand(ToolTip = "Clears the current assembly list", ToolbarIcon = "Clear.png", ToolbarCategory = "Open", ToolbarOrder = 1.5)]
public class UnloadAllAssembliesCommand : SimpleCommand
ToolTip: the tool tip
ToolbarIcon: The icon. Must be embedded as "Resource" (WPF-style resource) in the same assembly as the command type.
ToolbarCategory: optional, used for grouping related toolbar items together. A separator is added between different groups.
ToolbarOrder: controls the order in which the items appear (items are sorted by this value)
The command class must implement WPF's ICommand interface.
---
Adding an entry to the context menu:
[ExportContextMenuEntry(Header = "_Save Assembly")]
public class SaveAssembly : IContextMenuEntry
Icon: optional, icon to use for the menu item. Must be embedded as "Resource" (WPF-style resource) in the same assembly as the command type.
Header: text on the menu item
Category: optional, used for grouping related menu items together. A separator is added between different groups.
Order: controls the order in which the items appear (items are sorted by this value)
Context menu entries must implement IContextMenuEntry, which defines 3 methods:
bool IsVisible, bool IsEnabled, and void Execute.
---
Adding an option page:
[ExportOptionPage("TestPlugin")]
partial class CustomOptionPage : UserControl, IOptionPage

12
TestPlugin/TestPlugin.csproj

@ -68,7 +68,12 @@ @@ -68,7 +68,12 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ContextMenuCommand.cs" />
<Compile Include="CustomLanguage.cs" />
<Compile Include="CustomOptionPage.xaml.cs">
<DependentUpon>CustomOptionPage.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="MainMenuCommand.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@ -93,6 +98,13 @@ @@ -93,6 +98,13 @@
<Project>{3B2A5653-EC97-4001-BB9B-D90F1AF2C371}</Project>
<Name>ICSharpCode.NRefactory</Name>
</ProjectReference>
<ProjectReference Include="..\SharpTreeView\ICSharpCode.TreeView.csproj">
<Project>{DDE2A481-8271-4EAC-A330-8FA6A38D13D1}</Project>
<Name>ICSharpCode.TreeView</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Page Include="CustomOptionPage.xaml" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
</Project>
Loading…
Cancel
Save