Skip to content

Commit

Permalink
Made FileQuery and FileChooser system generic over IFileQueryables
Browse files Browse the repository at this point in the history
  • Loading branch information
ktvoelker committed Mar 17, 2011
1 parent 9383292 commit 3ddebc0
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 56 deletions.
10 changes: 5 additions & 5 deletions Di/Controller/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,18 @@ public class CreateAndFocusWindow : LoneCommand
{
public override void Execute(Window b)
{
FileChooser chooser = null;
FileChooser<Model.ProjectFile> chooser = null;
Action<Di.Model.ProjectFile> handler = file =>
{
b.Controller.EndFileChooser.Handler(chooser);
b.Controller.FileChooserEvents.End.Handler(chooser);
b.Controller.FocusedWindow.Value = b.Controller.FindOrCreateWindow(file);
};
Action cancel = () =>
{
b.Controller.CancelFileChooser.Handler();
b.Controller.FileChooserEvents.Cancel.Handler();
};
chooser = new FileChooser(b.Controller.Model.CurrentProject, "Choose a file", handler, cancel);
b.Controller.BeginFileChooser.Handler(chooser);
chooser = new FileChooser<Model.ProjectFile>(() => b.Controller.Model.CurrentProject.Files, "Choose a file", handler, cancel);
b.Controller.FileChooserEvents.Begin.Handler(chooser);
}
}
}
Expand Down
33 changes: 21 additions & 12 deletions Di/Controller/FileChooser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,51 @@
using System.Collections.Generic;
namespace Di.Controller
{
public class FileChooser
public class FileChooserEvents<T> where T : Model.IFileQueryable
{
private Di.Model.Project project;
public readonly Event1<FileChooser<T>> Begin = new Event1<FileChooser<T>>();

public readonly Event1<FileChooser<T>> End = new Event1<FileChooser<T>>();

public readonly Event0 Cancel = new Event0();
}

public class FileChooser<T> where T : Model.IFileQueryable
{
private Func<IEnumerable<T>> getCandidates;

public readonly string Message;

private Action<Di.Model.ProjectFile> handler;
private Action<T> handler;

private Action cancelHandler;

private Di.Model.FileQuery query;
private Di.Model.FileQuery<T> query;

public string Query
{
set
{
query = new Di.Model.FileQuery(value);
query = new Di.Model.FileQuery<T>(value);
Files.Clear();
Update();
}
}

public BindList<Di.Model.ProjectFile> Files;
public BindList<T> Files;

public FileChooser(Di.Model.Project _project, string _message, Action<Di.Model.ProjectFile> _handler, Action _cancelHandler)
public FileChooser(Func<IEnumerable<T>> _getCandidates, string _message, Action<T> _handler, Action _cancelHandler)
{
project = _project;
getCandidates = _getCandidates;
handler = _handler;
Message = _message;
cancelHandler = _cancelHandler;
query = new Di.Model.FileQuery("");
Files = new BindList<Di.Model.ProjectFile>();
query = new Di.Model.FileQuery<T>("");
Files = new BindList<T>();
Update();
}

public void Choose(Di.Model.ProjectFile file)
public void Choose(T file)
{
handler(file);
}
Expand All @@ -69,7 +78,7 @@ public void Cancel()

private void Update()
{
query.Evaluate(project.Files).ForEach(f => Files.Add(f));
query.Evaluate(getCandidates()).ForEach(f => Files.Add(f));
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions Di/Controller/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ public partial class Main

public readonly ReadOnlyCollection<WindowMode> WindowModes;

public readonly Event1<FileChooser> BeginFileChooser = new Event1<FileChooser>();
public readonly FileChooserEvents<Model.ProjectFile> FileChooserEvents = new FileChooserEvents<Model.ProjectFile>();

public readonly Event1<FileChooser> EndFileChooser = new Event1<FileChooser>();

public readonly Event0 CancelFileChooser = new Event0();
public readonly FileChooserEvents<Model.ProjectDirectory> DirectoryChooserEvents = new FileChooserEvents<Model.ProjectDirectory>();

public Main(Model.Main m)
{
Expand Down
2 changes: 2 additions & 0 deletions Di/Di.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
<Compile Include="Controller\FileChooser.cs" />
<Compile Include="Event.cs" />
<Compile Include="Font.cs" />
<Compile Include="Model\ProjectDirectory.cs" />
<Compile Include="Model\Language\Directory.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="gtk-sharp, Version=2.12.0.0, Culture=neutral, PublicKeyToken=35e10195dab3c99f" />
Expand Down
5 changes: 5 additions & 0 deletions Di/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,10 @@ public static bool GetBoolWithDefault<K>(this IDictionary<K, string> dict, K key
return fallback;
}
}

public static string ProjectRelativeFullName(this Model.IFileQueryable node)
{
return node.FullName.Substring(node.Root.Root.FullName.Length + 1);
}
}
}
34 changes: 31 additions & 3 deletions Di/Model/FileQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,35 @@
using System.Linq;
namespace Di.Model
{
public class FileQuery
public interface IFileQueryable
{
Project Root
{
get;
}

ProjectDirectory Parent
{
get;
}

Language.Base Lang
{
get;
}

string Name
{
get;
}

string FullName
{
get;
}
}

public class FileQuery<T> where T : IFileQueryable
{
private string query;

Expand All @@ -32,9 +60,9 @@ public FileQuery(string _query)
query = _query;
}

public IQueryable<ProjectFile> Evaluate(IEnumerable<ProjectFile> files)
public IQueryable<T> Evaluate(IEnumerable<T> files)
{
return files.AsQueryable().Where(f => f.File.Name == query);
return files.AsQueryable().Where(f => f.Name == query);
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions Di/Model/Language/Directory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Directory.cs
//
// Author:
// Karl Voelker <[email protected]>
//
// Copyright (c) 2011 Karl Voelker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
namespace Di.Model.Language
{
public class Directory : Base
{
public Directory()
{
Name = "Directory";
}
}
}

3 changes: 2 additions & 1 deletion Di/Model/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public Project(DirectoryInfo _dir)
m.ExcludeGlob(e);
}
}
files = m.MatchAll(Root).Select(f => new ProjectFile(this, f)).ToList();
// TODO pass a non-null Parent
files = m.MatchAll(Root).Select(f => new ProjectFile(this, null, f)).ToList();
Files = new ReadOnlyCollection<ProjectFile>(files);
}

Expand Down
84 changes: 84 additions & 0 deletions Di/Model/ProjectDirectory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// ProjectDirectory.cs
//
// Author:
// Karl Voelker <[email protected]>
//
// Copyright (c) 2011 Karl Voelker
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.IO;
namespace Di.Model
{
public class ProjectDirectory : IFileQueryable
{
private static readonly Language.Base LangInstance = new Language.Directory();

public Project Root
{
get;
private set;
}

public ProjectDirectory Parent
{
get;
private set;
}

public DirectoryInfo Directory
{
get;
private set;
}

public Language.Base Lang
{
get
{
return LangInstance;
}
}

public string Name
{
get
{
return Directory.Name;
}
}

public string FullName
{
get
{
return Directory.FullName;
}
}

public string ProjectRelativeFullName
{
get { return Directory.FullName.Substring(Root.Root.FullName.Length + 1); }
}

public ProjectDirectory(Project root, ProjectDirectory parent, DirectoryInfo dir)
{
Root = root;
Parent = parent;
Directory = dir;
}
}
}

47 changes: 31 additions & 16 deletions Di/Model/ProjectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,24 @@
using System.IO;
namespace Di.Model
{
public class ProjectFile
public class ProjectFile : IFileQueryable
{
public Project Parent
public Project Root
{
get;
private set;
}

private FileInfo file;
public ProjectDirectory Parent
{
get;
private set;
}

public FileInfo File
{
get
{
return file;
}

private set
{
file = value;
ProjectRelativeFullName = file.FullName.Substring(Parent.Root.FullName.Length + 1);
}
get;
private set;
}

public Language.Base Lang
Expand All @@ -52,14 +48,33 @@ public Language.Base Lang
private set;
}

public string Name
{
get
{
return File.Name;
}
}

public string FullName
{
get
{
return File.FullName;
}
}

public string ProjectRelativeFullName
{
get;
private set;
get
{
return File.FullName.Substring(Root.Root.FullName.Length + 1);
}
}

public ProjectFile(Project parent, FileInfo file)
public ProjectFile(Project root, ProjectDirectory parent, FileInfo file)
{
Root = root;
Parent = parent;
File = file;
Lang = new Language.Plain();
Expand Down
Loading

0 comments on commit 3ddebc0

Please sign in to comment.