Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to view selected node in Binary Converter window #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Asn1Editor/API/Interfaces/ITreeCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace SysadminsLV.Asn1Editor.API.Interfaces;

public interface ITreeCommands {
ICommand ShowNodeTextViewer { get; }
ICommand ShowNodeInConverter { get; }
ICommand SaveNodeCommand { get; }
ICommand EditNodeCommand { get; }
ICommand RegisterOidCommand { get; }
Expand Down
2 changes: 1 addition & 1 deletion Asn1Editor/API/Utils/WPF/WindowFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void ShowNodeTextViewer() {
hwnd = App.Container.Resolve<TextViewer>();
ShowAsWindow(true);
}
public void ShowConverterWindow(IEnumerable<Byte> data, Func<Byte[], Task> action) {
public void ShowConverterWindow(IEnumerable<Byte> data, Func<Byte[], Task>? action) {
if (!binConverterWindowClosed) {
binConverterWindow.Focus();
return;
Expand Down
19 changes: 12 additions & 7 deletions Asn1Editor/API/ViewModel/BinaryConverterVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
Expand All @@ -17,20 +18,20 @@
namespace SysadminsLV.Asn1Editor.API.ViewModel;

class BinaryConverterVM : AsyncViewModel {
readonly String master = new String('0', 78);
readonly String master = new('0', 78);
readonly Func<Byte[], Task>? _action;
Double width;
String text, path;
EncodingTypeEntry? selectedEncoding;
Boolean canCheck;
readonly Func<Byte[], Task> _action;

public BinaryConverterVM(Func<Byte[], Task> action) {
public BinaryConverterVM(Func<Byte[], Task>? action) {
_action = action;
OpenCommand = new RelayCommand(openFile);
SaveCommand = new RelayCommand(saveFile, canPrintSave);
PrintCommand = new RelayCommand(print, canPrintSave);
ClearCommand = new RelayCommand(clearText);
ValidateCommand = new RelayCommand(validateInput);
ValidateCommand = new AsyncCommand(validateInput, canValidateInput);
//TextBoxWidth = TextUtility.MeasureStringWidth(master, Settings.Default.FontSize, true);
initialize();
EncodingTypesView = CollectionViewSource.GetDefaultView(EncodingTypes);
Expand All @@ -40,7 +41,7 @@ public BinaryConverterVM(Func<Byte[], Task> action) {
public ICommand OpenCommand { get; }
public ICommand SaveCommand { get; }
public ICommand ClearCommand { get; }
public ICommand ValidateCommand { get; }
public IAsyncCommand ValidateCommand { get; }
public ICommand PrintCommand { get; }

public String Text {
Expand Down Expand Up @@ -166,7 +167,7 @@ void saveFile(Object obj) {
break;
}
}
async void validateInput(Object obj) {
async Task validateInput(Object obj, CancellationToken token) {
if (String.IsNullOrEmpty(Text)) {
Tools.MsgBox("Warning", "There is nothing to validate.", MessageBoxImage.Warning);
return;
Expand All @@ -179,10 +180,14 @@ async void validateInput(Object obj) {
Tools.MsgBox("Error", "Input text cannot be validated.");
}
RawData.AddRange(HexUtility.AnyToBinary(Text));
if (obj != null && obj.ToString() == "Decode") {
if (obj is not null && _action is not null && obj.ToString() == "Decode") {
await _action(RawData.ToArray());
}
}
Boolean canValidateInput(Object o) {
return _action is not null;
}

void clearText(Object obj) {
Text = String.Empty;
RawData.Clear();
Expand Down
10 changes: 10 additions & 0 deletions Asn1Editor/API/ViewModel/TreeViewCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public TreeViewCommands(IWindowFactory windowFactory, IHasAsnDocumentTabs appTab
_tabs = appTabs;
SaveNodeCommand = new RelayCommand(saveBinaryNode, ensureNodeSelected);
ShowNodeTextViewer = new RelayCommand(showNodeTextViewer, ensureNodeSelected);
ShowNodeInConverter = new RelayCommand(showNodeInConverter, ensureNodeSelected);
EditNodeCommand = new RelayCommand(editNodeContent, ensureNodeSelected);
RegisterOidCommand = new RelayCommand(registerOid, ensureNodeSelected);
AddNewNodeCommand = new RelayCommand(addNewNode, canAddNewNode);
Expand All @@ -38,6 +39,7 @@ public TreeViewCommands(IWindowFactory windowFactory, IHasAsnDocumentTabs appTab
}

public ICommand ShowNodeTextViewer { get; }
public ICommand ShowNodeInConverter { get; }
public ICommand EditNodeCommand { get; }
public ICommand RegisterOidCommand { get; set; }
public ICommand SaveNodeCommand { get; }
Expand Down Expand Up @@ -71,6 +73,14 @@ void saveBinaryNode(Object o) {
void showNodeTextViewer(Object o) {
_windowFactory.ShowNodeTextViewer();
}
void showNodeInConverter(Object o) {
isTabSelected(out IDataSource data); // granted to be non-null
if (data.SelectedNode != null) {
IEnumerable<Byte> nodeData = data.RawData.Skip(data.SelectedNode.Offset).Take(data.SelectedNode.TagLength);

_windowFactory.ShowConverterWindow(nodeData, null);
}
}
void editNodeContent(Object o) {
isTabSelected(out IDataSource data); // granted to be non-null
if (data.SelectedNode != null) {
Expand Down
12 changes: 12 additions & 0 deletions Asn1Editor/Views/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
<Image Source="/Views/Images/Menu/viewtext_16x16.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="View encoded node..."
Command="{Binding TreeCommands.ShowNodeInConverter}">
<MenuItem.Icon>
<Image Source="/Views/Images/Menu/converter_16x16.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Edit Object Identifier mapping"
Command="{Binding TreeCommands.RegisterOidCommand}">
<MenuItem.Style>
Expand Down Expand Up @@ -406,6 +412,12 @@
<Image Source="/Views/Images/Menu/viewtext_16x16.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="View encoded node..."
Command="{Binding TreeCommands.ShowNodeInConverter}">
<MenuItem.Icon>
<Image Source="/Views/Images/Menu/converter_16x16.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Edit Object Identifier mapping"
Command="{Binding TreeCommands.RegisterOidCommand}">
<MenuItem.Style>
Expand Down
Loading