Skip to content

Commit 8c9ac7e

Browse files
committed
Save network properties to Data. Add Properties network context menu.
1 parent 930bce3 commit 8c9ac7e

File tree

5 files changed

+77
-18
lines changed

5 files changed

+77
-18
lines changed

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainExplorerToolWindowControl.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<UserControl.CommandBindings>
4444
<!-- bindings for context menu commands -->
4545
<CommandBinding Command="{x:Static stratisui:BlockchainExplorerTree.NewNetworkCmd}" Executed="NewNetworkCmdExecuted" />
46+
<CommandBinding Command="{x:Static stratisui:BlockchainExplorerTree.PropertiesCmd}" Executed="PropertiesCmd_Executed" />
4647
<CommandBinding Command="{x:Static stratisui:BlockchainExplorerTree.NewEndpointCmd}" Executed="NewEndpointCmd_Executed" />
4748
<CommandBinding Command="{x:Static stratisui:BlockchainExplorerTree.DeleteEndpointCmd}" Executed="DeleteEndpointCmd_Executed" CanExecute="DeleteEndpointCmd_CanExecute" />
4849
</UserControl.CommandBindings>

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainExplorerToolWindowControl.xaml.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,17 @@ private async void NewNetworkCmdExecuted(object sender, ExecutedRoutedEventArgs
8181
};
8282
dw.ButtonClicked += (cd, args) =>
8383
{
84+
validForClose = false;
8485
errors.Visibility = Visibility.Hidden;
8586
if (args.Button == ContentDialogButton.Primary)
8687
{
8788
if (!string.IsNullOrEmpty(name.Text) && !string.IsNullOrEmpty(rpcurl.Text) && Uri.TryCreate(rpcurl.Text, UriKind.Absolute, out var _))
8889
{
90+
if (tree.RootItem.HasChild(name.Text, BlockchainInfoKind.Network))
91+
{
92+
ShowValidationErrors(errors, "Enter a unique name for the network name.");
93+
return;
94+
}
8995
ShowProgressRing(progressring);
9096
var text = rpcurl.Text;
9197
#if IS_VSIX
@@ -108,17 +114,19 @@ private async void NewNetworkCmdExecuted(object sender, ExecutedRoutedEventArgs
108114
else
109115
{
110116
ShowValidationErrors(errors, string.Format("The specified chain id {0} does not match the chain id returned by the network endpoint: {1}.", chainid.Text, cid.Value));
117+
return;
111118
}
112119
}
113120
else
114121
{
115122
ShowValidationErrors(errors, "Error connecting to JSON-RPC URL: " + cid.Exception.Message + " " + cid.Exception.InnerException?.Message);
123+
return;
116124
}
117125
}
118126
else
119127
{
120-
validForClose = false;
121128
ShowValidationErrors(errors, "Enter a network name and a valid JSON-RPC endpoint URL.");
129+
return;
122130
}
123131
}
124132
else
@@ -130,9 +138,12 @@ private async void NewNetworkCmdExecuted(object sender, ExecutedRoutedEventArgs
130138
var r = await dw.ShowAsync();
131139
if (r != ContentDialogResult.Primary)
132140
{
141+
name.Text = "";
142+
rpcurl.Text = "";
143+
chainid.Text = "";
133144
return;
134145
}
135-
var t = tree.SelectedItem.AddChild(BlockchainInfoKind.Network, name.Text);
146+
var t = tree.RootItem.AddNetwork(name.Text, BigInteger.Parse(chainid.Text), rpcurl.Text);
136147
var endpoints = t.AddChild(BlockchainInfoKind.Folder, "Endpoints");
137148
endpoints.AddChild(BlockchainInfoKind.Endpoint, rpcurl.Text);
138149
if (!tree.RootItem.Save("BlockchainExplorerTree", out var ex))
@@ -166,15 +177,23 @@ private async void NewEndpointCmd_Executed(object sender, ExecutedRoutedEventArg
166177
var rpcurl = (Wpc.TextBox)((StackPanel)sp.Children[0]).Children[1];
167178
var errors = (Wpc.TextBlock)((StackPanel)sp.Children[1]).Children[0];
168179
var validForClose = false;
169-
170180
dw.ButtonClicked += (cd, args) =>
171181
{
182+
validForClose = false;
172183
errors.Visibility = Visibility.Hidden;
173184
if (args.Button == ContentDialogButton.Primary)
174185
{
175186
if (!string.IsNullOrEmpty(rpcurl.Text) && Uri.TryCreate(rpcurl.Text, UriKind.Absolute, out var _))
176187
{
177-
validForClose = true;
188+
if (tree.SelectedItem.HasChild(rpcurl.Text, BlockchainInfoKind.Endpoint))
189+
{
190+
ShowValidationErrors(errors, "Enter a unique network endpoint URL.");
191+
return;
192+
}
193+
else
194+
{
195+
validForClose = true;
196+
}
178197
}
179198
else
180199
{
@@ -200,7 +219,7 @@ private async void NewEndpointCmd_Executed(object sender, ExecutedRoutedEventArg
200219

201220
var uri = new Uri(rpcurl.Text);
202221
var endpoints = tree.SelectedItem.GetChild("Endpoints", BlockchainInfoKind.Folder);
203-
endpoints.AddChild(BlockchainInfoKind.Endpoint, uri.ToString(), uri);
222+
endpoints.AddChild(BlockchainInfoKind.Endpoint, uri.ToString());
204223
if (!tree.RootItem.Save("BlockchainExplorerTree", out var ex))
205224
{
206225
System.Windows.MessageBox.Show("Error saving tree data: " + ex?.Message);
@@ -261,5 +280,10 @@ private void DeleteEndpointCmd_CanExecute(object sender, CanExecuteRoutedEventAr
261280
}
262281
e.CanExecute = (item.Parent.Name == "Stratis MainNet");
263282
}
283+
284+
private void PropertiesCmd_Executed(object sender, ExecutedRoutedEventArgs e)
285+
{
286+
var item = GetSelectedItem(sender);
287+
}
264288
}
265289
}

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainExplorerTree.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class BlockchainExplorerTree : TreeViewBase<BlockchainInfo>
1717
#region Properties
1818
public static RoutedCommand NewNetworkCmd { get; } = new RoutedCommand();
1919

20+
public static RoutedCommand PropertiesCmd { get; } = new RoutedCommand();
21+
2022
public static RoutedCommand NewEndpointCmd { get; } = new RoutedCommand();
2123

2224
public static RoutedCommand DeleteEndpointCmd { get; } = new RoutedCommand();

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainExplorerTreeResources.xaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@
120120
</DrawingImage.Drawing>
121121
</DrawingImage>
122122

123+
<DrawingImage x:Key="PropertiesDrawingImage">
124+
<DrawingImage.Drawing>
125+
<DrawingGroup ClipGeometry="M0,0 V512 H512 V0 H0 Z">
126+
<DrawingGroup.Transform>
127+
<TranslateTransform X="0" Y="2.1316282072803006E-14" />
128+
</DrawingGroup.Transform>
129+
<DrawingGroup Opacity="1">
130+
<DrawingGroup Opacity="1" Transform="1,0,0,1,85.333333,42.666667">
131+
<GeometryDrawing Brush="{DynamicResource TextFillColorPrimaryBrush}" Geometry="F0 M512,512z M0,0z M222.169889,-2.13162821E-14L341.333333,119.163444 341.333732,209.124352C328.37638,201.628866,313.98315,196.339524,298.667665,193.769948L298.666667,136.853333 204.48,42.6666667 42.6666667,42.6666667 42.6666667,384 166.457109,383.999404C176.363874,401.125458,190.124655,415.743148,206.553524,426.666547L1.42108547E-14,426.666667 1.42108547E-14,-2.13162821E-14 222.169889,-2.13162821E-14z M306.847154,213.333333L306.84783,236.169297C317.251058,239.846405,326.765408,245.405183,334.98194,252.43669L354.778753,241.006655 384.408382,292.326679 364.624464,303.749991C365.597738,309.018468 366.106413,314.44983 366.106413,320 366.106413,325.550277 365.597718,330.981742 364.624408,336.250315L384.408382,347.673321 354.778753,398.993345 334.98194,387.56331C326.765408,394.594817,317.251058,400.153595,306.84783,403.830703L306.847154,426.666667 247.587895,426.666667 247.587219,403.830703C237.184377,400.153731,227.670347,394.595229,219.454023,387.564092L199.656296,398.993345 170.026667,347.673321 189.810642,336.250315C188.837331,330.981742 188.328636,325.550277 188.328636,320 188.328636,314.449479 188.837376,309.017781 189.81077,303.74899L170.026667,292.326679 199.656296,241.006655 219.453029,252.436758C227.66958,245.405219,237.183957,239.846417,247.587219,236.169297L247.587895,213.333333 306.847154,213.333333z M277.217525,284.444444C257.580733,284.444444 241.661969,300.363209 241.661969,320 241.661969,339.636791 257.580733,355.555556 277.217525,355.555556 296.854316,355.555556 312.77308,339.636791 312.77308,320 312.77308,300.363209 296.854316,284.444444 277.217525,284.444444z" />
132+
</DrawingGroup>
133+
</DrawingGroup>
134+
</DrawingGroup>
135+
</DrawingImage.Drawing>
136+
</DrawingImage>
137+
123138
<!-- TreeView items data template-->
124139
<DataTemplate x:Key="BlockchainInfoTemplate" DataType="{x:Type vm:BlockchainInfo}">
125140
<StackPanel x:Name="GroupPanel" Orientation="Horizontal" Margin="0,2,0,2">
@@ -199,6 +214,11 @@
199214
<Image Source="{StaticResource GlobeDrawingImage}" />
200215
</MenuItem.Icon>
201216
</MenuItem>
217+
<MenuItem Header="Properties..." Command="{x:Static stratisui:BlockchainExplorerTree.PropertiesCmd}">
218+
<MenuItem.Icon>
219+
<Image Source="{StaticResource PropertiesDrawingImage}" />
220+
</MenuItem.Icon>
221+
</MenuItem>
202222
</ContextMenu>
203223

204224
<ContextMenu x:Key="EndpointContextMenu" Style="{DynamicResource ContextMenuStyle}">

tests/Stratis.DevEx.Wpf.Test/BlockchainExplorer/BlockchainViewModel.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-

2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Collections.ObjectModel;
54
using System.Collections.Specialized;
65
using System.ComponentModel;
76
using System.IO;
87
using System.Linq;
8+
using System.Numerics;
99
using System.Net.Http;
10-
using System.Runtime.CompilerServices;
11-
using System.Text;
1210

1311
using Newtonsoft.Json;
1412
using Newtonsoft.Json.Serialization;
13+
1514
using Stratis.DevEx;
1615
using Stratis.DevEx.Ethereum.Explorers;
1716

@@ -30,7 +29,7 @@ public enum BlockchainInfoKind
3029
public class BlockchainInfo
3130
{
3231
#region Constructors
33-
public BlockchainInfo(BlockchainInfoKind kind, string name, BlockchainInfo parent = null, object data = null)
32+
public BlockchainInfo(BlockchainInfoKind kind, string name, BlockchainInfo parent = null, Dictionary<string,object> data = null)
3433
{
3534
Kind = kind;
3635
Name = name;
@@ -41,42 +40,55 @@ public BlockchainInfo(BlockchainInfoKind kind, string name, BlockchainInfo paren
4140

4241
#region Properties
4342
public BlockchainInfoKind Kind { get; set; }
43+
4444
public string Name { get; set; }
45+
46+
public Dictionary<string, object> Data { get; } = new Dictionary<string, object>();
47+
4548
[JsonProperty(ItemIsReference = true)]
4649
public BlockchainInfo Parent { get; set; }
47-
48-
public object Data { get; set; }
50+
4951
[JsonProperty(ItemReferenceLoopHandling = ReferenceLoopHandling.Serialize)]
5052
public ObservableCollection<BlockchainInfo> Children = new ObservableCollection<BlockchainInfo>();
5153

5254

53-
54-
public string Key => ((this.Parent?.Name) ?? "Root") + "_" + this.Kind + "_" + this.Name;
55+
public string Key => ((this.Parent?.Key) ?? "Root") + "_" + this.Kind + "_" + this.Name;
5556
#endregion
5657

5758
#region Methods
58-
public BlockchainInfo AddChild(BlockchainInfoKind kind, string name, object data = null)
59+
public BlockchainInfo AddChild(BlockchainInfoKind kind, string name, Dictionary<string, object> data = null)
5960
{
6061
var info = new BlockchainInfo(kind, name, this, data);
6162
Children.Add(info);
6263
return info;
6364
}
6465

66+
public BlockchainInfo AddNetwork(string name, BigInteger chainid, string uri)
67+
{
68+
var data = new Dictionary<string, object>()
69+
{
70+
{"ChainId", chainid },
71+
{"EndpointUri", uri }
72+
};
73+
return AddChild(BlockchainInfoKind.Network, name, data);
74+
}
75+
6576
public override int GetHashCode() => Key.GetHashCode();
6677

6778
public override bool Equals(object obj) => obj is BlockchainInfo bi ? Key == bi.Key : false;
6879

6980
public void DeleteChild(BlockchainInfo child) => Children.Remove(child);
7081

71-
public void DeleteChild(string name, BlockchainInfoKind kind) => Children.Remove(GetChild(name, kind));
82+
public void DeleteChild(string name, BlockchainInfoKind kind) => Children.Remove(GetChild(name, kind));
7283

84+
public bool HasChild(string name, BlockchainInfoKind kind) => Children.Count(bi => bi.Name == name && bi.Kind == kind) > 0;
85+
7386
public BlockchainInfo GetChild(string name, BlockchainInfoKind kind) => Children.Single(c => c.Name == name && c.Kind == kind);
7487

7588
public IEnumerable<BlockchainInfo> GetChildren(BlockchainInfoKind kind) => Children.Where(c => c.Kind == kind);
7689

7790
public IEnumerable<BlockchainInfo> GetEndPoints() => GetChildren(BlockchainInfoKind.Endpoint);
7891

79-
8092
public bool Save(string path, out Exception e )
8193
{
8294
try
@@ -222,7 +234,7 @@ public static ObservableCollection<BlockchainInfo> CreateInitialTreeData()
222234
var root = new BlockchainInfo(BlockchainInfoKind.Folder, "EVM Networks");
223235
var mainnet = root.AddChild(BlockchainInfoKind.Network, "Stratis Mainnet");
224236
var endpoints = mainnet.AddChild(BlockchainInfoKind.Folder, "Endpoints");
225-
endpoints.AddChild(BlockchainInfoKind.Endpoint, "rpc.stratisevm.com", new Uri("https://rpc.stratisevm.com:8545"));
237+
endpoints.AddChild(BlockchainInfoKind.Endpoint, "https://rpc.stratisevm.com:8545");
226238
data.Add(root);
227239
//var testnet = new BlockchainInfo(BlockchainInfoKind.Network, "Stratis Testnet");
228240
//mainnet.AddChild(BlockchainInfoKind.Endpoint, "auroria.stratisevm.com", new Uri("https://auroria.rpc.stratisevm.com"));

0 commit comments

Comments
 (0)