using EPocalipse.Json.Viewer;
namespace YLErp.Forms
{
public partial class FormJsonView : Form
{
public FormJsonView()
{
InitializeComponent();
}
private void MainForm_Shown(object sender, EventArgs e)
{
var args = Environment.GetCommandLineArgs();
for (var i = 1; i < args.Length; i++)
{
var arg = args[i];
if (arg.Equals("/c", StringComparison.OrdinalIgnoreCase))
{
LoadFromClipboard();
}
else if (File.Exists(arg))
{
LoadFromFile(arg);
}
}
}
public FormJsonView SetJson(string json)
{
JsonViewer.ShowTab(Tabs.Viewer);
JsonViewer.Json = json;
return this;
}
private void LoadFromFile(string fileName)
{
var json = File.ReadAllText(fileName);
JsonViewer.ShowTab(Tabs.Viewer);
JsonViewer.Json = json;
}
private void LoadFromClipboard()
{
var json = Clipboard.GetText();
if (!string.IsNullOrEmpty(json))
{
JsonViewer.ShowTab(Tabs.Viewer);
JsonViewer.Json = json;
}
}
private void MainForm_Load(object sender, EventArgs e)
{
JsonViewer.ShowTab(Tabs.Viewer);
}
///
/// Closes the program
///
///
///
/// Menu item File > Exit
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
///
/// Open File Dialog for JSON files or Yahoo! Pipe files
///
///
///
/// Menu item File > Open
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
var dialog = new OpenFileDialog
{
Filter =
"json files (*.json)|*.json|Yahoo! Pipe files (*.run)|*.run|All files (*.*)|*.*",
InitialDirectory = Application.StartupPath,
Title = "Select a JSON file"
};
if (dialog.ShowDialog() == DialogResult.OK)
{
this.LoadFromFile(dialog.FileName);
}
}
///
/// Selects all text in the textbox
///
///
///
/// Menu item Edit > Select All
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("txtJson", true)[0];
((RichTextBox)c).SelectAll();
}
///
/// Deletes selected text in the textbox
///
///
///
/// Menu item Edit > Delete
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("txtJson", true)[0];
string text;
if (((RichTextBox)c).SelectionLength > 0)
{
text = ((RichTextBox)c).SelectedText;
}
else
{
text = ((RichTextBox)c).Text;
} ((RichTextBox)c).SelectedText = "";
}
///
/// Pastes text in the clipboard into the textbox
///
///
///
/// Menu item Edit > Paste
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("txtJson", true)[0];
((RichTextBox)c).Paste();
}
///
/// Copies text in the textbox into the clipboard
///
///
///
/// Menu item Edit > Copy
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("txtJson", true)[0];
((RichTextBox)c).Copy();
}
///
/// Cuts selected text from the textbox
///
///
///
/// Menu item Edit > Cut
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("txtJson", true)[0];
((RichTextBox)c).Cut();
}
///
/// Undo the last action
///
///
///
/// Menu item Edit > Undo
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("txtJson", true)[0];
((RichTextBox)c).Undo();
}
///
/// Displays the find prompt
///
///
///
/// Menu item Viewer > Find
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("pnlFind", true)[0];
((Panel)c).Visible = true;
Control t;
t = this.JsonViewer.Controls.Find("txtFind", true)[0];
((TextBox)t).Focus();
}
///
/// Expands all the nodes
///
///
///
/// Menu item Viewer > Expand All
///
private void expandAllToolStripMenuItem_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("tvJson", true)[0];
((TreeView)c).BeginUpdate();
try
{
if (((TreeView)c).SelectedNode != null)
{
var topNode = ((TreeView)c).TopNode;
((TreeView)c).SelectedNode.ExpandAll();
((TreeView)c).TopNode = topNode;
}
}
finally
{
((TreeView)c).EndUpdate();
}
}
///
/// Copies a node
///
///
///
/// Menu item Viewer > Copy
private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("tvJson", true)[0];
var node = ((TreeView)c).SelectedNode;
if (node != null)
{
Clipboard.SetText(node.Text);
}
}
///
/// Copies just the node's value
///
///
///
/// Menu item Viewer > Copy Value
///
private void copyValueToolStripMenuItem_Click(object sender, EventArgs e)
{
Control c;
c = this.JsonViewer.Controls.Find("tvJson", true)[0];
var node = (JsonViewerTreeNode)((TreeView)c).SelectedNode;
if (node != null && node.JsonObject.Value != null)
{
Clipboard.SetText(node.JsonObject.Value.ToString());
}
}
}
}