NBTExplorer/NBTExplorerMac/Mac/MainWindow.cs
Justin Aquadro 5849c30f2e Mac UI
2012-11-04 23:18:25 -05:00

318 lines
8.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using MonoMac.Foundation;
using MonoMac.AppKit;
using NBTExplorer.Mac;
using System.IO;
using NBTExplorer.Model;
namespace NBTExplorer
{
public partial class MainWindow : MonoMac.AppKit.NSWindow
{
#region Constructors
// Called when created from unmanaged code
public MainWindow (IntPtr handle) : base (handle)
{
Initialize ();
}
// Called when created directly from a XIB file
[Export ("initWithCoder:")]
public MainWindow (NSCoder coder) : base (coder)
{
Initialize ();
}
// Shared initialization code
void Initialize ()
{
InitializeIconRegistry();
}
private NBTExplorer.Mac.IconRegistry _iconRegistry;
private void InitializeIconRegistry ()
{
_iconRegistry = new NBTExplorer.Mac.IconRegistry();
_iconRegistry.DefaultIcon = NSImage.ImageNamed("question-white.png");
_iconRegistry.Register(typeof(TagByteDataNode), NSImage.ImageNamed("document-attribute-b.png"));
_iconRegistry.Register(typeof(TagShortDataNode), NSImage.ImageNamed("document-attribute-s.png"));
_iconRegistry.Register(typeof(TagIntDataNode), NSImage.ImageNamed("document-attribute-i.png"));
_iconRegistry.Register(typeof(TagLongDataNode), NSImage.ImageNamed("document-attribute-l.png"));
_iconRegistry.Register(typeof(TagFloatDataNode), NSImage.ImageNamed("document-attribute-f.png"));
_iconRegistry.Register(typeof(TagDoubleDataNode), NSImage.ImageNamed("document-attribute-d.png"));
_iconRegistry.Register(typeof(TagByteArrayDataNode), NSImage.ImageNamed("edit-code.png"));
_iconRegistry.Register(typeof(TagStringDataNode), NSImage.ImageNamed("edit-small-caps.png"));
_iconRegistry.Register(typeof(TagListDataNode), NSImage.ImageNamed("edit-list.png"));
_iconRegistry.Register(typeof(TagCompoundDataNode), NSImage.ImageNamed("box.png"));
_iconRegistry.Register(typeof(RegionChunkDataNode), NSImage.ImageNamed("wooden-box.png"));
_iconRegistry.Register(typeof(DirectoryDataNode), NSImage.ImageNamed("folder-open"));
_iconRegistry.Register(typeof(RegionFileDataNode), NSImage.ImageNamed("block.png"));
_iconRegistry.Register(typeof(CubicRegionDataNode), NSImage.ImageNamed("block.png"));
_iconRegistry.Register(typeof(NbtFileDataNode), NSImage.ImageNamed("wooden-box.png"));
_iconRegistry.Register(typeof(TagIntArrayDataNode), NSImage.ImageNamed("edit-code-i.png"));
}
#endregion
private TreeDataSource _dataSource;
public override void AwakeFromNib ()
{
base.AwakeFromNib ();
_dataSource = new TreeDataSource();
_mainOutlineView.DataSource = _dataSource;
_mainOutlineView.Delegate = new MyDelegate(this);
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 2) {
string[] paths = new string[args.Length - 1];
Array.Copy(args, 1, paths, 0, paths.Length);
OpenPaths(paths);
}
else {
OpenMinecraftDirectory();
}
}
public class MyDelegate : NSOutlineViewDelegate
{
private MainWindow _main;
public MyDelegate (MainWindow main)
{
_main = main;
}
public override void ItemWillExpand (NSNotification notification)
{
TreeDataNode node = notification.UserInfo ["NSObject"] as TreeDataNode;
if (node != null) {
Console.WriteLine ("Preparing to expand: " + node.Data.NodeDisplay);
_main.ExpandNode(node);
}
}
public override void ItemDidExpand (NSNotification notification)
{
TreeDataNode node = notification.UserInfo ["NSObject"] as TreeDataNode;
if (node != null) {
Console.WriteLine("Finished Expanding: " + node.Data.NodeDisplay);
}
}
public override void ItemWillCollapse (NSNotification notification)
{
TreeDataNode node = notification.UserInfo ["NSObject"] as TreeDataNode;
if (node != null) {
if (node.Data.NodeDisplay == "saves") // The root node
Console.WriteLine ("Uh-oh...");
Console.WriteLine("Preparing to collapse: " + node.Data.NodeDisplay);
}
}
public override void ItemDidCollapse (NSNotification notification)
{
TreeDataNode node = notification.UserInfo ["NSObject"] as TreeDataNode;
if (node != null) {
_main.CollapseNode(node);
}
}
public override void WillDisplayCell (NSOutlineView outlineView, NSObject cell, NSTableColumn tableColumn, NSObject item)
{
ImageAndTextCell c = cell as ImageAndTextCell;
TreeDataNode node = item as TreeDataNode;
c.Title = node.CombinedName;
c.Image = _main._iconRegistry.Lookup(node.Data.GetType());
//c.StringValue = node.Name;
//throw new System.NotImplementedException ();
}
}
#region Actions
partial void ActionOpenFolder (MonoMac.Foundation.NSObject sender)
{
OpenFolder ();
}
#endregion
private string _openFolderPath = null;
private void OpenFolder ()
{
NSOpenPanel opanel = new NSOpenPanel ();
opanel.CanChooseDirectories = true;
opanel.CanChooseFiles = false;
if (_openFolderPath != null)
opanel.DirectoryUrl = new NSUrl (_openFolderPath, true);
if (opanel.RunModal () == (int)NSPanelButtonType.Ok) {
_openFolderPath = opanel.DirectoryUrl.AbsoluteString;
OpenPaths(new string[] { opanel.DirectoryUrl.Path });
}
//UpdateUI();
}
private void OpenMinecraftDirectory ()
{
try {
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
path = Path.Combine(path, "Library", "Application Support");
path = Path.Combine(path, "minecraft", "saves");
if (!Directory.Exists(path)) {
path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
}
OpenPaths(new string[] { path });
}
catch (Exception e) {
//NSAlert.WithMessage("Could not open default Minecraft save directory", "OK", null, null, null).RunModal();
Console.WriteLine(e.Message);
try {
OpenPaths(new string[] { Directory.GetCurrentDirectory() });
}
catch (Exception) {
//MessageBox.Show("Could not open current directory, this tool is probably not compatible with your platform.");
Console.WriteLine(e.Message);
NSApplication.SharedApplication.Terminate(this);
}
}
//UpdateUI();
}
private void OpenPaths (string[] paths)
{
_dataSource.Nodes.Clear ();
_mainOutlineView.ReloadData ();
foreach (string path in paths) {
if (Directory.Exists (path)) {
DirectoryDataNode node = new DirectoryDataNode (path);
_dataSource.Nodes.Add (new TreeDataNode (node));
// AddPathToHistory(Settings.Default.RecentDirectories, path);
} else if (File.Exists (path)) {
DataNode node = null;
}
}
if (_dataSource.Nodes.Count > 0) {
_mainOutlineView.ExpandItem(_dataSource.Nodes[0]);
}
_mainOutlineView.ReloadData();
// UpdateUI();
// UpdateOpenMenu();
/*_nodeTree.Nodes.Clear();
foreach (string path in paths) {
if (Directory.Exists(path)) {
DirectoryDataNode node = new DirectoryDataNode(path);
_nodeTree.Nodes.Add(CreateUnexpandedNode(node));
AddPathToHistory(Settings.Default.RecentDirectories, path);
}
else if (File.Exists(path)) {
DataNode node = null;
foreach (var item in FileTypeRegistry.RegisteredTypes) {
if (item.Value.NamePatternTest(path))
node = item.Value.NodeCreate(path);
}
if (node != null) {
_nodeTree.Nodes.Add(CreateUnexpandedNode(node));
AddPathToHistory(Settings.Default.RecentFiles, path);
}
}
}
if (_nodeTree.Nodes.Count > 0) {
_nodeTree.Nodes[0].Expand();
}
UpdateUI();
UpdateOpenMenu();*/
}
private void ExpandNode (TreeDataNode node)
{
if (node == null || node.IsExpanded)
return;
Console.WriteLine ("Expand Node: " + node.Data.NodeDisplay);
node.IsExpanded = true;
node.Nodes.Clear ();
DataNode backNode = node.Data;
if (!backNode.IsExpanded) {
backNode.Expand ();
}
foreach (DataNode child in backNode.Nodes) {
if (child != null) {
node.Nodes.Add (new TreeDataNode (child));
}
}
}
private void CollapseNode (TreeDataNode node)
{
if (node == null || !node.IsExpanded)
return;
/*Console.WriteLine("Collapse Node: " + node.Data.NodeDisplay);
DataNode backNode = node.Data;
if (backNode.IsModified)
return;
backNode.Collapse();
node.IsExpanded = false;
node.Nodes.Clear();*/
}
public void ActionEditValue ()
{
TreeDataNode node = _mainOutlineView.ItemAtRow(_mainOutlineView.SelectedRow) as TreeDataNode;
if (node != null)
EditNode(node);
}
private void EditNode (TreeDataNode node)
{
if (node == null)
return;
if (!node.Data.CanEditNode)
return;
//NBTExplorer.Mac.EditValue form = new NBTExplorer.Mac.EditValue(node.Data);
//if (node.Data.EditNode()) {
//node.Text = dataNode.NodeDisplay;
//UpdateUI(dataNode);
//}
}
}
}