/// <summary>Presents a File Open Dialog to the user and either loads the selected source or does nothing if nothing was selected or the FOD was cancelled.</summary> private void SourceLoadDialog() { IList <ResourceSourceFactory> factories = ResourceSourceFactory.GetFactories(); var filter = String.Empty; foreach (var factory in factories) { filter += factory.OpenFileFilter + '|'; } filter += "All Files (*.*)|*.*"; __ofd.Filter = filter; if (__ofd.ShowDialog(this) != DialogResult.OK) { return; } // filterIndex is from 1 var selectedFactory = __ofd.FilterIndex > factories.Count ? null : factories[__ofd.FilterIndex - 1]; SourceLoad(__ofd.FileName, selectedFactory, false); }
/// <summary>Prompts the user to save the current source (if open) then opens a Save File dialog for the new empty ResourceSource then loads it.</summary> private void SourceNew() { if (!SourceUnload()) { return; } IList <ResourceSourceFactory> factories = ResourceSourceFactory.GetFactories(); var filter = String.Empty; foreach (var factory in factories) { filter += factory.NewFileFilter + '|'; } if (filter.EndsWith("|")) { filter = filter.Substring(0, filter.Length - 1); } __sfd.Filter = filter; if (__sfd.ShowDialog(this) != DialogResult.OK) { return; } var selectedFactory = factories[__sfd.FilterIndex - 1]; var source = selectedFactory.CreateNew(__sfd.FileName, false, ResourceSourceLoadMode.LazyLoadData); SourceLoad(source, __sfd.FileName); }
////////////////////// public static ResourceSource Open(string fileName, bool readOnly, ResourceSourceLoadMode mode) { var ext = Path.GetExtension(fileName).ToUpperInvariant(); if (ext.StartsWith(".", StringComparison.Ordinal)) { ext = ext.Substring(1); } var factory = ResourceSourceFactory.GetFactoryForExtension(ext); return(factory.Create(fileName, readOnly, mode)); }
/// <summary>Calls SourceUnload (which prompts the user) if there's a source currently loaded, then loads the specified source.</summary> private void SourceLoad(String path, ResourceSourceFactory selectedFactory, Boolean removeFromMruOnError) { if (CurrentSource != null) { if (!SourceUnload()) { return; } } if (!File.Exists(path)) { var notFoundMessage = "Resourcer could not locate the file \"{0}\""; if (removeFromMruOnError) { notFoundMessage += "\r\n\r\n Would you like to remove it from the Most Recently Used menu?"; } notFoundMessage = String.Format(Cult.InvariantCulture, notFoundMessage, path); var result = MessageBox.Show(this, notFoundMessage, "Anolis Resourcer", removeFromMruOnError ? MessageBoxButtons.YesNo : MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) { MruRemove(path); } return; } try { ResourceSource source; if (selectedFactory != null) { source = selectedFactory.Create(path, false, ResourceSourceLoadMode.LazyLoadData); } else { source = ResourceSource.Open(path, false, ResourceSourceLoadMode.LazyLoadData); } SourceLoad(source, path); } catch (AnolisException aex) { SourceLoadCatch(aex, path, removeFromMruOnError); } }
private void __sourceFileBrowse_Click(object sender, EventArgs e) { IList <ResourceSourceFactory> factories = ResourceSourceFactory.GetFactories(); String filter = String.Empty; foreach (ResourceSourceFactory factory in factories) { filter += factory.OpenFileFilter + '|'; } filter += "All Files (*.*)|*.*"; __ofd.Filter = filter; if (__ofd.ShowDialog(this) == DialogResult.OK) { __sourceFile.Text = __ofd.FileName; } }
public static ResourceSourceFactory GetFactoryForExtension(String extension) { // return the last match, since that will be the most specific ResourceSourceFactory yesMatch = null; ResourceSourceFactory maybeMatch = null; ResourceSourceFactory allMatch = null; IList <ResourceSourceFactory> factories = GetFactories(); foreach (ResourceSourceFactory factory in factories) { Compatibility compat = factory.HandlesExtension(extension); switch (compat) { case Compatibility.Yes: yesMatch = factory; break; case Compatibility.Maybe: maybeMatch = factory; break; case Compatibility.All: allMatch = factory; break; } } if (yesMatch != null) { return(yesMatch); } if (maybeMatch != null) { return(maybeMatch); } return(allMatch); }