rainmeter-studio/RainmeterStudio/UI/Panels/ProjectPanel.xaml.cs

185 lines
5.3 KiB
C#
Raw Normal View History

using System;
using System.Windows;
using System.Windows.Controls;
using RainmeterStudio.Core.Model;
using RainmeterStudio.Core.Utils;
using RainmeterStudio.UI.Controller;
using RainmeterStudio.UI.ViewModel;
2014-08-16 12:40:08 +00:00
namespace RainmeterStudio.UI.Panels
{
/// <summary>
/// Interaction logic for SkinsPanel.xaml
/// </summary>
public partial class ProjectPanel : UserControl
{
private ProjectController _controller;
public ProjectController Controller
{
get
{
return _controller;
}
set
{
2014-08-16 12:40:08 +00:00
// Unsubscribe from old controller
if (_controller != null)
{
Controller.ActiveProjectChanged -= Controller_ActiveProjectChanged;
}
// Set new project
_controller = value;
_controller.ActiveProjectChanged += Controller_ActiveProjectChanged;
Refresh();
}
}
#region Commands
public Command SyncWithActiveViewCommand { get; private set; }
public Command RefreshCommand { get; private set; }
public Command ExpandAllCommand { get; private set; }
public Command CollapseAllCommand { get; private set; }
public Command ShowAllFilesCommand { get; private set; }
#endregion
2014-08-16 12:40:08 +00:00
/// <summary>
/// Gets the selected tree view item
/// </summary>
2014-08-31 11:41:24 +00:00
public Reference ActiveItem
2014-08-16 12:40:08 +00:00
{
get
{
var selected = treeProjectItems.SelectedItem as Tree<ReferenceViewModel>;
if (selected == null)
{
return Controller.ActiveProject.Root;
}
else
{
return selected.Data.Reference;
}
}
}
private bool _canExpand = false;
private bool CanExpand
{
get
{
return _canExpand;
}
set
{
_canExpand = value;
2014-08-16 12:40:08 +00:00
ExpandAllCommand.NotifyCanExecuteChanged();
CollapseAllCommand.NotifyCanExecuteChanged();
}
}
public ProjectPanel()
{
InitializeComponent();
SyncWithActiveViewCommand = new Command("ProjectPanel_SyncWithActiveViewCommand", SyncWithActiveView);
RefreshCommand = new Command("ProjectPanel_RefreshCommand", Refresh);
ExpandAllCommand = new Command("ProjectPanel_ExpandAllCommand", ExpandAll, () => _canExpand);
CollapseAllCommand = new Command("ProjectPanel_CollapseAllCommand", CollapseAll, () => !_canExpand);
ShowAllFilesCommand = new Command("ProjectPanel_ShowAllFilesCommand", Refresh);
this.DataContext = this;
Refresh();
}
void Controller_ActiveProjectChanged(object sender, EventArgs e)
{
Refresh();
}
private void SyncWithActiveView()
{
// TODO: implement
}
private void Refresh()
{
if (Controller == null || Controller.ActiveProject == null)
{
this.IsEnabled = false;
}
else
{
this.IsEnabled = true;
// Get tree
2014-08-31 11:41:24 +00:00
Reference refTree;
if (toggleShowAllFiles.IsChecked.HasValue && toggleShowAllFiles.IsChecked.Value)
{
2014-08-31 11:41:24 +00:00
// Get directory name
string projectFolder = System.IO.Path.GetDirectoryName(Controller.ActiveProjectPath);
// Get folder tree
refTree = DirectoryHelper.GetFolderTree(projectFolder);
}
else
{
2014-08-31 11:41:24 +00:00
refTree = Controller.ActiveProject.Root;
}
// Add tree to tree view
treeProjectItems.Items.Clear();
2014-08-31 11:41:24 +00:00
treeProjectItems.Items.Add(new ReferenceViewModel(refTree));
}
}
private void ExpandAll()
{
// Get tree
2014-08-31 11:41:24 +00:00
var tree = treeProjectItems.Items[0] as ReferenceViewModel;
if (tree == null)
return;
// Expand all
2014-08-31 11:41:24 +00:00
tree.TreeExpand(true);
// Set can expand property
CanExpand = false;
}
private void CollapseAll()
{
// Get tree
2014-08-31 11:41:24 +00:00
var tree = treeProjectItems.Items[0] as ReferenceViewModel;
if (tree == null)
return;
// Expand all
2014-08-31 11:41:24 +00:00
tree.TreeExpand(false);
// Set can expand property
CanExpand = true;
}
void TreeViewItem_ExpandedOrCollapsed(object sender, RoutedEventArgs e)
{
// Get tree
2014-08-31 11:41:24 +00:00
var tree = treeProjectItems.Items[0] as ReferenceViewModel;
if (tree == null)
return;
// We can expand if the root is not expanded
2014-08-31 11:41:24 +00:00
CanExpand = (!tree.IsExpanded);
}
}
}