Browse Source

Fixed forum-9028: orphan resx file if form file is renamed

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1733 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 20 years ago
parent
commit
4584a6fa5b
  1. 17
      src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerViewContent.cs
  2. 112
      src/AddIns/DisplayBindings/FormsDesigner/Project/Src/Services/DesignerResourceService.cs
  3. 4
      src/Main/Base/Project/Src/Gui/AbstractSecondaryViewContent.cs
  4. 3
      src/Main/Base/Project/Src/Gui/AbstractViewContent.cs
  5. 2
      src/Main/Base/Project/Src/Gui/ISecondaryViewContent.cs
  6. 3
      src/Main/Base/Project/Src/Gui/IViewContent.cs

17
src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerViewContent.cs

@ -1,7 +1,7 @@
// <file> // <file>
// <copyright see="prj:///doc/copyright.txt"/> // <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/> // <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/> // <owner name="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
// <version>$Revision$</version> // <version>$Revision$</version>
// </file> // </file>
@ -30,7 +30,6 @@ namespace ICSharpCode.FormsDesigner
protected bool failedDesignerInitialize; protected bool failedDesignerInitialize;
protected IViewContent viewContent; protected IViewContent viewContent;
protected Dictionary<string, DesignerResourceService.ResourceStorage> resources = new Dictionary<string, DesignerResourceService.ResourceStorage>();
protected ITextEditorControlProvider textAreaControlProvider; protected ITextEditorControlProvider textAreaControlProvider;
Panel p = new Panel(); Panel p = new Panel();
@ -92,7 +91,6 @@ namespace ICSharpCode.FormsDesigner
this.viewContent = viewContent; this.viewContent = viewContent;
this.textAreaControlProvider = viewContent as ITextEditorControlProvider; this.textAreaControlProvider = viewContent as ITextEditorControlProvider;
} }
public override void SwitchedTo() public override void SwitchedTo()
@ -113,7 +111,7 @@ namespace ICSharpCode.FormsDesigner
serviceContainer.AddService(typeof(IHelpService), new HelpService()); serviceContainer.AddService(typeof(IHelpService), new HelpService());
serviceContainer.AddService(typeof(System.Drawing.Design.IPropertyValueUIService), new PropertyValueUIService()); serviceContainer.AddService(typeof(System.Drawing.Design.IPropertyValueUIService), new PropertyValueUIService());
designerResourceService = new DesignerResourceService(viewContent.FileName, this.resources); designerResourceService = new DesignerResourceService(viewContent.FileName);
serviceContainer.AddService(typeof(System.ComponentModel.Design.IResourceService), designerResourceService); serviceContainer.AddService(typeof(System.ComponentModel.Design.IResourceService), designerResourceService);
AmbientProperties ambientProperties = new AmbientProperties(); AmbientProperties ambientProperties = new AmbientProperties();
serviceContainer.AddService(typeof(AmbientProperties), ambientProperties); serviceContainer.AddService(typeof(AmbientProperties), ambientProperties);
@ -349,13 +347,22 @@ namespace ICSharpCode.FormsDesigner
public override void NotifyAfterSave(bool successful) public override void NotifyAfterSave(bool successful)
{ {
base.NotifyAfterSave(successful);
if (successful) { if (successful) {
if (designerResourceService != null) { if (designerResourceService != null) {
designerResourceService.Save(); designerResourceService.Save(viewContent.FileName);
} }
} }
} }
public override void NotifyFileNameChanged()
{
base.NotifyFileNameChanged();
if (designerResourceService != null) {
designerResourceService.FormFileName = viewContent.FileName;
}
}
void SelectionChangedHandler(object sender, EventArgs args) void SelectionChangedHandler(object sender, EventArgs args)
{ {
UpdatePropertyPadSelection((ISelectionService)sender); UpdatePropertyPadSelection((ISelectionService)sender);

112
src/AddIns/DisplayBindings/FormsDesigner/Project/Src/Services/DesignerResourceService.cs

@ -26,18 +26,27 @@ namespace ICSharpCode.FormsDesigner.Services
public class DesignerResourceService : System.ComponentModel.Design.IResourceService , IDisposable public class DesignerResourceService : System.ComponentModel.Design.IResourceService , IDisposable
{ {
IDesignerHost host; IDesignerHost host;
string formFileName;
string FileName = String.Empty; public string FormFileName {
get {
return formFileName;
}
set {
formFileName = value;
}
}
// Culture name (or empty string) => Resources
Dictionary<string, DesignerResourceService.ResourceStorage> resources = new Dictionary<string, DesignerResourceService.ResourceStorage>();
#region ResourceStorage #region ResourceStorage
public class ResourceStorage public class ResourceStorage
{ {
MemoryStream stream; MemoryStream stream;
IResourceWriter writer; IResourceWriter writer;
public IProject project = null;
string fileName;
byte[] buffer; byte[] buffer;
ResourceType type = ResourceType.Resx;
/// <summary> /// <summary>
/// true, if the currently stored resource is not empty. /// true, if the currently stored resource is not empty.
@ -50,12 +59,6 @@ namespace ICSharpCode.FormsDesigner.Services
} }
} }
public ResourceStorage(string fileName, IProject project)
{
this.project = project;
this.fileName = fileName;
}
public void Dispose() public void Dispose()
{ {
if (this.stream != null) { if (this.stream != null) {
@ -90,16 +93,17 @@ namespace ICSharpCode.FormsDesigner.Services
/// Returns a new resource reader for this resource based on the most recent /// Returns a new resource reader for this resource based on the most recent
/// version available (either in memory or on disk). /// version available (either in memory or on disk).
/// </summary> /// </summary>
public IResourceReader GetReader() public IResourceReader GetReader(string resourceFileName)
{ {
if (this.GetBuffer() == null) { if (this.GetBuffer() == null) {
if (File.Exists(this.fileName)) { if (File.Exists(resourceFileName)) {
return CreateResourceReader(this.fileName, GetResourceType(this.fileName)); type = GetResourceType(resourceFileName);
return CreateResourceReader(resourceFileName, type);
} else { } else {
return null; return null;
} }
} else { } else {
return CreateResourceReader(new MemoryStream(this.buffer, false), GetResourceType(this.fileName)); return CreateResourceReader(new MemoryStream(this.buffer, false), type);
} }
} }
@ -112,7 +116,7 @@ namespace ICSharpCode.FormsDesigner.Services
public IResourceWriter GetWriter() public IResourceWriter GetWriter()
{ {
this.stream = new MemoryStream(); this.stream = new MemoryStream();
this.writer = CreateResourceWriter(this.stream, GetResourceType(this.fileName)); this.writer = CreateResourceWriter(this.stream, type);
return this.writer; return this.writer;
} }
@ -131,15 +135,11 @@ namespace ICSharpCode.FormsDesigner.Services
}; };
// In ResourceMemoryStreams are stored: // In ResourceMemoryStreams are stored:
// Key: "true" file names from the project // Key: Culture name (empty string for invariant culture)
// Value: ResourceStorage, where the resources are stored // Value: ResourceStorage, where the resources are stored
// If the file is read, after
// calculating of the "true" file name, it looks for MemoryStream
// uses it if it exists.
// Memory streams are cleared, when WriteSerialization will start // Memory streams are cleared, when WriteSerialization will start
// or File in the editor will be reloaded from the disc and of // or File in the editor will be reloaded from the disc and of
// course in Dispose of the service // course in Dispose of the service
protected Dictionary<string, ResourceStorage> resources = null;
public Dictionary<string, ResourceStorage> Resources public Dictionary<string, ResourceStorage> Resources
{ {
get { get {
@ -158,33 +158,30 @@ namespace ICSharpCode.FormsDesigner.Services
} }
} }
public DesignerResourceService(string fileName, Dictionary<string, ResourceStorage> resources) public DesignerResourceService(string formFileName)
{ {
this.FileName = fileName; this.formFileName = formFileName;
this.resources = resources;
} }
IProject _project; static IProject GetProject(string formFileName)
IProject GetProject()
{ {
if (_project == null && ProjectService.OpenSolution != null && FileName != null) if (ProjectService.OpenSolution != null && formFileName != null)
_project = ProjectService.OpenSolution.FindProjectContainingFile(FileName); return ProjectService.OpenSolution.FindProjectContainingFile(formFileName);
return _project; else
return null;
} }
#region System.ComponentModel.Design.IResourceService interface implementation #region System.ComponentModel.Design.IResourceService interface implementation
public System.Resources.IResourceWriter GetResourceWriter(System.Globalization.CultureInfo info) public System.Resources.IResourceWriter GetResourceWriter(CultureInfo info)
{ {
try { try {
LoggingService.Debug("ResourceWriter requested for culture: "+info.ToString()); LoggingService.Debug("ResourceWriter requested for culture: " + info.ToString());
string fileName = CalcResourceFileName(info);
ResourceStorage resourceStorage; ResourceStorage resourceStorage;
if (resources.ContainsKey(fileName)) { if (resources.ContainsKey(info.Name)) {
resourceStorage = resources[fileName]; resourceStorage = resources[info.Name];
} else { } else {
resourceStorage = new ResourceStorage(fileName, GetProject()); resourceStorage = new ResourceStorage();
resources[fileName] = resourceStorage; resources[info.Name] = resourceStorage;
} }
return resourceStorage.GetWriter(); return resourceStorage.GetWriter();
} catch (Exception e) { } catch (Exception e) {
@ -197,15 +194,14 @@ namespace ICSharpCode.FormsDesigner.Services
{ {
try { try {
LoggingService.Debug("ResourceReader requested for culture: "+info.ToString()); LoggingService.Debug("ResourceReader requested for culture: "+info.ToString());
string fileName = CalcResourceFileName(info);
ResourceStorage resourceStorage; ResourceStorage resourceStorage;
if (resources != null && resources.ContainsKey(fileName)) { if (resources != null && resources.ContainsKey(info.Name)) {
resourceStorage = resources[fileName]; resourceStorage = resources[info.Name];
} else { } else {
resourceStorage = new ResourceStorage(fileName, GetProject()); resourceStorage = new ResourceStorage();
resources[fileName] = resourceStorage; resources[info.Name] = resourceStorage;
} }
return resourceStorage.GetReader(); return resourceStorage.GetReader(CalcResourceFileName(formFileName, info.Name));
} catch (Exception e) { } catch (Exception e) {
MessageService.ShowError(e); MessageService.ShowError(e);
return null; return null;
@ -213,24 +209,26 @@ namespace ICSharpCode.FormsDesigner.Services
} }
#endregion #endregion
public void Save() public void Save(string formFileName)
{ {
this.formFileName = formFileName;
if (resources != null) { if (resources != null) {
foreach (KeyValuePair<string, ResourceStorage> entry in resources) { foreach (KeyValuePair<string, ResourceStorage> entry in resources) {
string resourceFileName = entry.Key; string cultureName = entry.Key;
string resourceFileName = CalcResourceFileName(formFileName, cultureName);
FileUtility.ObservedSave(new NamedFileOperationDelegate(entry.Value.Save), resourceFileName, FileErrorPolicy.Inform); FileUtility.ObservedSave(new NamedFileOperationDelegate(entry.Value.Save), resourceFileName, FileErrorPolicy.Inform);
IProject project = GetProject(); IProject project = GetProject(formFileName);
// Add this resource file to the project // Add this resource file to the project
if (entry.Value.ContainsData && project != null && !project.IsFileInProject(resourceFileName)) { if (entry.Value.ContainsData && project != null && !project.IsFileInProject(resourceFileName)) {
FileProjectItem newFileProjectItem = new FileProjectItem(project, ItemType.EmbeddedResource); FileProjectItem newFileProjectItem = new FileProjectItem(project, ItemType.EmbeddedResource);
newFileProjectItem.DependentUpon = Path.GetFileName(FileName); newFileProjectItem.DependentUpon = Path.GetFileName(formFileName);
newFileProjectItem.Include = FileUtility.GetRelativePath(project.Directory, resourceFileName); newFileProjectItem.Include = FileUtility.GetRelativePath(project.Directory, resourceFileName);
ProjectService.AddProjectItem(project, newFileProjectItem); ProjectService.AddProjectItem(project, newFileProjectItem);
PadDescriptor pd = WorkbenchSingleton.Workbench.GetPad(typeof(ProjectBrowserPad)); PadDescriptor pd = WorkbenchSingleton.Workbench.GetPad(typeof(ProjectBrowserPad));
FileNode formFileNode = ((ProjectBrowserPad)pd.PadContent).ProjectBrowserControl.FindFileNode(FileName); FileNode formFileNode = ((ProjectBrowserPad)pd.PadContent).ProjectBrowserControl.FindFileNode(formFileName);
if (formFileNode != null) { if (formFileNode != null) {
LoggingService.Info("FormFileNode found, adding subitem"); LoggingService.Info("FormFileNode found, adding subitem");
FileNode fileNode = new FileNode(resourceFileName, FileNodeStatus.BehindFile); FileNode fileNode = new FileNode(resourceFileName, FileNodeStatus.BehindFile);
@ -243,13 +241,13 @@ namespace ICSharpCode.FormsDesigner.Services
} }
} }
protected string CalcResourceFileName(System.Globalization.CultureInfo info) protected static string CalcResourceFileName(string formFileName, string cultureName)
{ {
StringBuilder resourceFileName = null; StringBuilder resourceFileName = null;
IProject project = GetProject(); IProject project = GetProject(formFileName);
if (FileName != null && FileName != String.Empty) { if (formFileName != null && formFileName != String.Empty) {
resourceFileName = new StringBuilder(Path.GetDirectoryName(FileName)); resourceFileName = new StringBuilder(Path.GetDirectoryName(formFileName));
} else if (project != null) { } else if (project != null) {
resourceFileName = new StringBuilder(project.Directory); resourceFileName = new StringBuilder(project.Directory);
} else { } else {
@ -258,11 +256,11 @@ namespace ICSharpCode.FormsDesigner.Services
} }
resourceFileName.Append(Path.DirectorySeparatorChar); resourceFileName.Append(Path.DirectorySeparatorChar);
string sourceFileName = null; string sourceFileName = null;
if (project != null && this.FileName != null) { if (project != null && formFileName != null) {
// Try to find the source file name by using the project dependencies first. // Try to find the source file name by using the project dependencies first.
FileProjectItem sourceItem = project.Items.Find(delegate(ProjectItem item) { FileProjectItem sourceItem = project.Items.Find(delegate(ProjectItem item) {
FileProjectItem fpi = item as FileProjectItem; FileProjectItem fpi = item as FileProjectItem;
return fpi != null && fpi.FileName != null && FileUtility.IsEqualFileName(fpi.FileName, this.FileName); return fpi != null && fpi.FileName != null && FileUtility.IsEqualFileName(fpi.FileName, formFileName);
}) as FileProjectItem; }) as FileProjectItem;
if (sourceItem != null && sourceItem.DependentUpon != null && sourceItem.DependentUpon.Length > 0) { if (sourceItem != null && sourceItem.DependentUpon != null && sourceItem.DependentUpon.Length > 0) {
sourceFileName = Path.GetFileNameWithoutExtension(sourceItem.DependentUpon); sourceFileName = Path.GetFileNameWithoutExtension(sourceItem.DependentUpon);
@ -272,20 +270,20 @@ namespace ICSharpCode.FormsDesigner.Services
// If the source file name cannot be found using the project dependencies, // If the source file name cannot be found using the project dependencies,
// assume the resource file name to be equal to the current source file name. // assume the resource file name to be equal to the current source file name.
// Remove the ".Designer" part if present. // Remove the ".Designer" part if present.
sourceFileName = Path.GetFileNameWithoutExtension(this.FileName); sourceFileName = Path.GetFileNameWithoutExtension(formFileName);
if (sourceFileName != null && sourceFileName.ToLowerInvariant().EndsWith(".designer")) { if (sourceFileName != null && sourceFileName.ToLowerInvariant().EndsWith(".designer")) {
sourceFileName = sourceFileName.Substring(0, sourceFileName.Length - 9); sourceFileName = sourceFileName.Substring(0, sourceFileName.Length - 9);
} }
} }
resourceFileName.Append(sourceFileName); resourceFileName.Append(sourceFileName);
if (info != null && info.Name.Length > 0) { if (!string.IsNullOrEmpty(cultureName)) {
resourceFileName.Append('.'); resourceFileName.Append('.');
resourceFileName.Append(info.Name); resourceFileName.Append(cultureName);
} }
// Use .resources filename if file exists. // Use .resources filename if file exists.
if (File.Exists(String.Concat(resourceFileName.ToString(), ".resources"))) { if (File.Exists(resourceFileName.ToString() + ".resources")) {
resourceFileName.Append(".resources"); resourceFileName.Append(".resources");
} else { } else {
resourceFileName.Append(".resx"); resourceFileName.Append(".resx");

4
src/Main/Base/Project/Src/Gui/AbstractSecondaryViewContent.cs

@ -19,5 +19,9 @@ namespace ICSharpCode.SharpDevelop.Gui
public virtual void NotifyAfterSave(bool successful) public virtual void NotifyAfterSave(bool successful)
{ {
} }
public virtual void NotifyFileNameChanged()
{
}
} }
} }

3
src/Main/Base/Project/Src/Gui/AbstractViewContent.cs

@ -54,6 +54,9 @@ namespace ICSharpCode.SharpDevelop.Gui
protected virtual void OnFileNameChanged(EventArgs e) protected virtual void OnFileNameChanged(EventArgs e)
{ {
foreach (ISecondaryViewContent svc in SecondaryViewContents) {
svc.NotifyFileNameChanged();
}
if (FileNameChanged != null) { if (FileNameChanged != null) {
FileNameChanged(this, e); FileNameChanged(this, e);
} }

2
src/Main/Base/Project/Src/Gui/ISecondaryViewContent.cs

@ -22,5 +22,7 @@ namespace ICSharpCode.SharpDevelop.Gui
void NotifyBeforeSave(); void NotifyBeforeSave();
void NotifyAfterSave(bool successful); void NotifyAfterSave(bool successful);
void NotifyFileNameChanged();
} }
} }

3
src/Main/Base/Project/Src/Gui/IViewContent.cs

@ -30,6 +30,7 @@ namespace ICSharpCode.SharpDevelop.Gui
this.successful = successful; this.successful = successful;
} }
} }
/// <summary> /// <summary>
/// IViewContent is the base interface for all editable data /// IViewContent is the base interface for all editable data
/// inside SharpDevelop. /// inside SharpDevelop.
@ -121,6 +122,8 @@ namespace ICSharpCode.SharpDevelop.Gui
/// </summary> /// </summary>
event EventHandler TitleNameChanged; event EventHandler TitleNameChanged;
event EventHandler FileNameChanged;
event EventHandler Saving; event EventHandler Saving;
event SaveEventHandler Saved; event SaveEventHandler Saved;
} }

Loading…
Cancel
Save