FileExplorerPanel.cpp

#include "StdAfx.h"
#include "FileExplorerPanel.h"


#include "DirectoryTreeView.h"
#include "FileListView.h"

#include "Settings.h"


using namespace Core;

using namespace System;
using namespace System::Diagnostics;


/// [ Constructor ]
FileExplorerPanel::FileExplorerPanel()
{
    // コンポーネントを初期化する
    InitializeComponent();

    m_fileListView->Extension = "ity"; // 拡張子
}

/// [ Destructor ]
FileExplorerPanel::~FileExplorerPanel()
{
    if( components != nullptr )
    {
        delete components;
    }
}


#pragma region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
void FileExplorerPanel::InitializeComponent(void)
{
    this->components = (gcnew System::ComponentModel::Container());
    System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(FileExplorerPanel::typeid));
    this->m_splitContainer = (gcnew System::Windows::Forms::SplitContainer());
    this->m_driveImageList = (gcnew System::Windows::Forms::ImageList(this->components));
    this->m_directoryTreeView = (gcnew Core::DirectoryTreeView());
    this->m_fileListView = (gcnew Core::FileListView());
    this->m_fileImageList = (gcnew System::Windows::Forms::ImageList(this->components));
    this->m_splitContainer->Panel1->SuspendLayout();
    this->m_splitContainer->Panel2->SuspendLayout();
    this->m_splitContainer->SuspendLayout();
    this->SuspendLayout();
    //
    // splitContainer
    //
    this->m_splitContainer->Dock = System::Windows::Forms::DockStyle::Fill;
    this->m_splitContainer->Location = System::Drawing::Point(0, 0);
    this->m_splitContainer->Name = L"splitContainer";
    this->m_splitContainer->Orientation = System::Windows::Forms::Orientation::Horizontal;
    //
    // splitContainer.Panel1
    //
    this->m_splitContainer->Panel1->Controls->Add(this->m_directoryTreeView);
    //
    // splitContainer.Panel2
    //
    this->m_splitContainer->Panel2->Controls->Add(this->m_fileListView);
    this->m_splitContainer->Size = System::Drawing::Size(180, 350);
    this->m_splitContainer->SplitterDistance = 123;
    this->m_splitContainer->TabIndex = 0;
    this->m_splitContainer->TabStop = false;
    //
    // directory_TreeView
    //
    this->m_directoryTreeView->Dock = System::Windows::Forms::DockStyle::Fill;
    this->m_directoryTreeView->ImageIndex = 0;
    this->m_directoryTreeView->ImageList = this->m_driveImageList;
    this->m_directoryTreeView->Location = System::Drawing::Point(0, 0);
    this->m_directoryTreeView->Name = L"directory_TreeView";
    this->m_directoryTreeView->SelectedImageIndex = 0;
    this->m_directoryTreeView->Size = System::Drawing::Size(180, 123);
    this->m_directoryTreeView->HideSelection = false;
    this->m_directoryTreeView->TabIndex = 1;
    this->m_directoryTreeView->AfterSelect += gcnew System::Windows::Forms::TreeViewEventHandler(this, &FileExplorerPanel::DirectoryTreeViewAfterSelect);
    //
    // drive_ImageList
    //
    this->m_driveImageList->ImageStream = (cli::safe_cast<System::Windows::Forms::ImageListStreamer^ >(resources->GetObject(L"drive_ImageList.ImageStream")));
    this->m_driveImageList->TransparentColor = System::Drawing::Color::Fuchsia;
    this->m_driveImageList->Images->SetKeyName(0, L"drive.bmp");
    this->m_driveImageList->Images->SetKeyName(1, L"XPfolder_closed.bmp");
    this->m_driveImageList->Images->SetKeyName(2, L"XPfolder_open.bmp");
    //
    // file_ListView
    //
    this->m_fileListView->Dock = System::Windows::Forms::DockStyle::Fill;
    this->m_fileListView->Location = System::Drawing::Point(0, 0);
    this->m_fileListView->Name = L"file_ListView";
    this->m_fileListView->Size = System::Drawing::Size(180, 223);
    this->m_fileListView->SmallImageList = this->m_fileImageList;
    this->m_fileListView->HideSelection = false;
    this->m_fileListView->TabIndex = 0;
    this->m_fileListView->UseCompatibleStateImageBehavior = false;
    this->m_fileListView->View = System::Windows::Forms::View::Details;
    this->m_fileListView->ItemActivate += gcnew System::EventHandler(this, &FileExplorerPanel::FileListViewItemActivate);
    //
    // file_ImageList
    //
    this->m_fileImageList->ImageStream = (cli::safe_cast<System::Windows::Forms::ImageListStreamer^ >(resources->GetObject(L"file_ImageList.ImageStream")));
    this->m_fileImageList->TransparentColor = System::Drawing::Color::Fuchsia;
    this->m_fileImageList->Images->SetKeyName(0, L"document.bmp");
    //
    // FileExplorerPanel
    //
    this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    this->Controls->Add(this->m_splitContainer);
    this->CloseButton = false;
    this->DockAreas = static_cast<WeifenLuo::WinFormsUI::Docking::DockAreas>(((WeifenLuo::WinFormsUI::Docking::DockAreas::Float | WeifenLuo::WinFormsUI::Docking::DockAreas::DockLeft)
        | WeifenLuo::WinFormsUI::Docking::DockAreas::DockRight));
    this->HideOnClose = true;

    this->Name = L"FileExplorerPanel";
    this->ShowHint = WeifenLuo::WinFormsUI::Docking::DockState::DockLeftAutoHide;
    this->TabText = L"File";
    this->Text = L"File";
    this->Size = System::Drawing::Size(180, 350);
    this->m_splitContainer->Panel1->ResumeLayout(false);
    this->m_splitContainer->Panel2->ResumeLayout(false);
    this->m_splitContainer->ResumeLayout(false);
    this->ResumeLayout(false);

}
#pragma endregion


/// 作業フォルダを設定する
void FileExplorerPanel::SetWorkingFolder()
{
    String^ folderName = "Robotity";  // フォルダ名


    // 基準パスを取得する (マイドキュメント)
    String^ basePath = Environment::GetFolderPath( Environment::SpecialFolder::Personal );

    // ... それとフォルダ名を連結して、作業フォルダのパスを取得する
    String^ workingFolder = IO::Path::Combine( basePath, folderName );


    /// @todo
    /// 作業フォルダは設定ファイルから読み込むようにする。


    // 作業フォルダを設定する
    Settings::WorkingFolder = workingFolder;
}


/// フォームが初めて表示される
void FileExplorerPanel::OnLoad( System::EventArgs^ e )
{
    // 基本クラスの同一関数を呼び出す
    WeifenLuo::WinFormsUI::Docking::DockContent::OnLoad( e );


    /// @todo
    /// 事前に 作業フォルダの設定がされているかを確認する。


    // ディレクトリ ツリーを更新する
    m_directoryTreeView->RefreshTree();
}


/// 設定を書き込む
System::Xml::XmlElement^ FileExplorerPanel::SaveSettings( System::Xml::XmlDocument^ document )
{
    // 作業フォルダの要素を生成する
    Xml::XmlElement^ node = document->CreateElement( "workingDirectory" );

    // ... そこに作業フォルダ を追加する
    node->InnerText = Environment::CurrentDirectory;

    return node;
}


/// ディレクトリが選択された
System::Void FileExplorerPanel::DirectoryTreeViewAfterSelect( System::Object^, System::Windows::Forms::TreeViewEventArgs^ e )
{
    // ツリーから絶対パスを取得する
    String^ absolutePath = e->Node->FullPath;

    // ... それをリストビューに与える
    m_fileListView->DirectoryName = absolutePath;


    // ファイルを表示する
    m_fileListView->ShowFiles();

    // ... イベントを発生する
    DirectorySelected( this, gcnew FileEventArgs( absolutePath ) );
}

/// リストビューの項目がアクティブになった
System::Void FileExplorerPanel::FileListViewItemActivate( System::Object^, System::EventArgs^ )
{
    // 選択されたファイルを取得する
    Windows::Forms::ListView::SelectedListViewItemCollection^ selectedFies
        = m_fileListView->SelectedItems;

    if( selectedFies->Count < 2 )
    {
        // 絶対パスを取得する
        String^ absolutePath = m_fileListView->GetAbsolutePath( selectedFies[ 0 ]->Text );

        // ... イベントを発生する
        FileSelected( this, gcnew FileEventArgs( absolutePath ) );
    }
    else
    {
        Debug::WriteLine( "複数のファイルを同時に開くことは出来ない" );
    }
}


/// ファイルを保存する
void FileExplorerPanel::SaveFile( System::String^ fileName, System::Xml::XmlDocument^ document )
{
    // 絶対パスを取得する
    System::String^ absolutePath = m_fileListView->GetAbsolutePath( fileName );

    // ... XML ドキュメントを保存する
    document->Save( absolutePath );


    // ファイル リストを更新する
    m_fileListView->ShowFiles();
}

/// ファイルから読み込む
System::Xml::XmlDocument^ FileExplorerPanel::LoadFile( System::String^ name )
{
    Debug::Assert( IO::File::Exists( name ), "存在するファイルである" );


    Xml::XmlDocument^ result = gcnew Xml::XmlDocument();

    try
    {
        // XMLドキュメントを読み込む
        result->Load( name );
    }
    catch( Xml::XmlException^ e )
    {
        Debug::WriteLine( "読み込みエラー : " + e->Message );
        result = nullptr;
    }
    return result;
}


/// 指定されたディレクトリを選択する [ Delegate ]
void FileExplorerPanel::SelectSpecifiedDirectory( System::String^ absolutePath )
{
    // 絶対パスから ディレクトリ名を取得する
    String^ directoryName = IO::Path::GetDirectoryName( absolutePath );

    m_directoryTreeView->SelectSpecifiedDirectory( directoryName );


    /// @todo
    /// リストビューのファイルも 選択されたことがわかるようにする。
    /// (ただし ListViewのメンバには 選択状態を設定するものはない)
}