Browse Source

Add AbstractViewContentSD1234 for backward compatiblity with existing viewcontents.

filemodels
Daniel Grunwald 12 years ago
parent
commit
525f2a243d
  1. 4
      src/AddIns/DisplayBindings/HexEditor/Project/Src/View/HexEditView.cs
  2. 1
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj
  3. 6
      src/Main/Base/Project/Workbench/AbstractViewContent.cs
  4. 29
      src/Main/Base/Project/Workbench/AbstractViewContentHandlingLoadErrors.cs
  5. 104
      src/Main/Base/Project/Workbench/AbstractViewContentSD1234.cs
  6. 14
      src/Main/Base/Project/Workbench/File/OpenedFile.cs

4
src/AddIns/DisplayBindings/HexEditor/Project/Src/View/HexEditView.cs

@ -27,7 +27,7 @@ using ICSharpCode.SharpDevelop.Workbench; @@ -27,7 +27,7 @@ using ICSharpCode.SharpDevelop.Workbench;
namespace HexEditor.View
{
public class HexEditView : AbstractViewContent, IClipboardHandler, IUndoHandler
public class HexEditView : AbstractViewContentSD1234, IClipboardHandler, IUndoHandler
{
HexEditContainer hexEditContainer;
@ -38,7 +38,7 @@ namespace HexEditor.View @@ -38,7 +38,7 @@ namespace HexEditor.View
this.Files.Add(file);
file.ForceInitializeView(this);
LoadModel();
SD.AnalyticsMonitor.TrackFeature(typeof(HexEditView));
}

1
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -352,6 +352,7 @@ @@ -352,6 +352,7 @@
<Compile Include="Workbench\AbstractPadContent.cs" />
<Compile Include="Workbench\AbstractViewContent.cs" />
<Compile Include="Workbench\AbstractViewContentHandlingLoadErrors.cs" />
<Compile Include="Workbench\AbstractViewContentSD1234.cs" />
<Compile Include="Workbench\DisplayBinding\DisplayBindingDescriptor.cs" />
<Compile Include="Workbench\DisplayBinding\ExternalProcessDisplayBinding.cs" />
<Compile Include="Workbench\DisplayBinding\IDisplayBinding.cs" />

6
src/Main/Base/Project/Workbench/AbstractViewContent.cs

@ -208,7 +208,7 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -208,7 +208,7 @@ namespace ICSharpCode.SharpDevelop.Workbench
void InitFiles()
{
files = new ObserveAddRemoveCollection<OpenedFile>(RegisterFileEventHandlers, UnregisterFileEventHandlers);
files = new ObserveAddRemoveCollection<OpenedFile>(OnFileAdded, OnFileRemoved);
filesReadonly = new ReadOnlyCollection<OpenedFile>(files);
}
@ -254,7 +254,7 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -254,7 +254,7 @@ namespace ICSharpCode.SharpDevelop.Workbench
}
}
void RegisterFileEventHandlers(OpenedFile newItem)
internal virtual void OnFileAdded(OpenedFile newItem)
{
newItem.FileNameChanged += OnFileNameChanged;
newItem.IsDirtyChanged += OnIsDirtyChanged;
@ -262,7 +262,7 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -262,7 +262,7 @@ namespace ICSharpCode.SharpDevelop.Workbench
OnIsDirtyChanged(null, EventArgs.Empty); // re-evaluate this.IsDirty after changing the file collection
}
void UnregisterFileEventHandlers(OpenedFile oldItem)
internal virtual void OnFileRemoved(OpenedFile oldItem)
{
oldItem.FileNameChanged -= OnFileNameChanged;
oldItem.IsDirtyChanged -= OnIsDirtyChanged;

29
src/Main/Base/Project/Workbench/AbstractViewContentHandlingLoadErrors.cs

@ -26,6 +26,7 @@ using System.Windows.Media; @@ -26,6 +26,7 @@ using System.Windows.Media;
namespace ICSharpCode.SharpDevelop.Workbench
{
/*
/// <summary>
/// This class handles errors in the Load method and prevents destroying invalid files.
/// Scenario:
@ -67,31 +68,6 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -67,31 +68,6 @@ namespace ICSharpCode.SharpDevelop.Workbench
}
}
public bool HasLoadError {
get {
return errorList.Count > 0;
}
}
class LoadError
{
internal Exception exception;
internal byte[] fileData;
public LoadError(Exception exception, Stream stream)
{
this.exception = exception;
stream.Position = 0;
this.fileData = new byte[(int)stream.Length];
int pos = 0;
while (pos < fileData.Length) {
int c = stream.Read(fileData, pos, fileData.Length - pos);
if (c == 0) break;
pos += c;
}
}
}
TextBox errorTextBox;
protected void ShowError(Exception ex)
@ -105,8 +81,6 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -105,8 +81,6 @@ namespace ICSharpCode.SharpDevelop.Workbench
SD.WinForms.SetContent(contentControl, errorTextBox, this);
}
Dictionary<OpenedFile, LoadError> errorList = new Dictionary<OpenedFile, LoadError>();
/// <summary>
/// Gets a text to be shown above the exception when a load error occurs.
/// The default is an empty string.
@ -115,4 +89,5 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -115,4 +89,5 @@ namespace ICSharpCode.SharpDevelop.Workbench
get { return String.Empty; }
}
}
*/
}

104
src/Main/Base/Project/Workbench/AbstractViewContentSD1234.cs

@ -0,0 +1,104 @@ @@ -0,0 +1,104 @@
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using ICSharpCode.Core;
namespace ICSharpCode.SharpDevelop.Workbench
{
/// <summary>
/// View content base class that is compatible with the old SD-1234 OpenedFile model.
/// </summary>
public abstract class AbstractViewContentSD1234 : AbstractViewContent
{
// The old behavior where view contents are loaded/saved without the use of a model instance is achieved by
// making the IViewContent itself the model.
class Provider : IFileModelProvider<AbstractViewContentSD1234>
{
AbstractViewContentSD1234 IFileModelProvider<AbstractViewContentSD1234>.Load(OpenedFile file)
{
throw new NotSupportedException();
}
void IFileModelProvider<AbstractViewContentSD1234>.Save(OpenedFile file, AbstractViewContentSD1234 model, FileSaveOptions options)
{
MemoryStream ms = new MemoryStream();
model.Save(file, ms);
file.ReplaceModel(FileModels.Binary, new BinaryFileModel(ms.ToArray()), ReplaceModelMode.TransferDirty);
}
void IFileModelProvider<AbstractViewContentSD1234>.SaveCopyAs(OpenedFile file, AbstractViewContentSD1234 model, FileName outputFileName, FileSaveOptions options)
{
using (Stream s = SD.FileSystem.OpenWrite(outputFileName)) {
model.Save(file, s);
}
}
bool IFileModelProvider<AbstractViewContentSD1234>.CanLoadFrom<U>(IFileModelProvider<U> otherProvider)
{
return false;
}
void IFileModelProvider<AbstractViewContentSD1234>.NotifyRename(OpenedFile file, AbstractViewContentSD1234 model, FileName oldName, FileName newName)
{
}
void IFileModelProvider<AbstractViewContentSD1234>.NotifyStale(OpenedFile file, AbstractViewContentSD1234 model)
{
model.isStale = true;
}
void IFileModelProvider<AbstractViewContentSD1234>.NotifyLoaded(OpenedFile file, AbstractViewContentSD1234 model)
{
}
void IFileModelProvider<AbstractViewContentSD1234>.NotifyUnloaded(OpenedFile file, AbstractViewContentSD1234 model)
{
}
}
Provider provider = new Provider(); // each AbstractViewContent gets its own provider instance
bool isStale = true;
public abstract void Save(OpenedFile file, Stream stream);
public abstract void Load(OpenedFile file, Stream stream);
public override void LoadModel()
{
base.LoadModel();
if (isStale) {
foreach (var file in this.Files) {
using (Stream s = file.GetModel(FileModels.Binary).OpenRead()) {
Load(file, s);
}
file.ReplaceModel(provider, this, ReplaceModelMode.TransferDirty);
}
isStale = false;
}
}
internal override void OnFileRemoved(OpenedFile oldItem)
{
// When a file is removed from the Files collection, unload our 'model' to remove the reference to the view content.
oldItem.UnloadModel(provider);
// This method also gets called for each OpenedFile when the view content is disposed; so we don't need to override Dispose for cleanup.
base.OnFileRemoved(oldItem);
}
}
}

14
src/Main/Base/Project/Workbench/File/OpenedFile.cs

@ -61,7 +61,8 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -61,7 +61,8 @@ namespace ICSharpCode.SharpDevelop.Workbench
/// </summary>
SetAsValid,
/// <summary>
/// The new model is marked as dirty, the previously dirty model is marked as stale, and any other models are unchanged.
/// The new model is marked as dirty or valid (depending on whether the OpenedFile was previously dirty),
/// the previously dirty model (if any) is marked as stale, and any other models are unchanged.
/// This mode is intended for use in <see cref="IFileModelProvider{T}.Save"/> implementations.
/// </summary>
TransferDirty
@ -89,6 +90,7 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -89,6 +90,7 @@ namespace ICSharpCode.SharpDevelop.Workbench
public abstract void SaveCopyAs(OpenedFile file, FileName outputFileName, FileSaveOptions options);
public abstract void NotifyRename(OpenedFile file, FileName oldName, FileName newName);
public abstract void NotifyStale(OpenedFile file);
public abstract void NotifyLoaded(OpenedFile file);
public abstract void NotifyUnloaded(OpenedFile file);
public abstract bool NeedsSaveForLoadInto<T>(IFileModelProvider<T> modelProvider) where T : class;
}
@ -127,6 +129,11 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -127,6 +129,11 @@ namespace ICSharpCode.SharpDevelop.Workbench
provider.NotifyStale(file, Model);
}
public override void NotifyLoaded(OpenedFile file)
{
provider.NotifyLoaded(file, Model);
}
public override void NotifyUnloaded(OpenedFile file)
{
provider.NotifyUnloaded(file, Model);
@ -475,6 +482,7 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -475,6 +482,7 @@ namespace ICSharpCode.SharpDevelop.Workbench
if (entry.Model != model) {
entry.NotifyUnloaded(this);
entry.Model = model;
entry.NotifyLoaded(this);
}
entry.IsStale = false;
}
@ -491,8 +499,8 @@ namespace ICSharpCode.SharpDevelop.Workbench @@ -491,8 +499,8 @@ namespace ICSharpCode.SharpDevelop.Workbench
}
break;
case ReplaceModelMode.TransferDirty:
dirtyEntry = entry;
this.IsDirty = true;
if (dirtyEntry != null)
dirtyEntry = entry;
break;
default:
throw new ArgumentOutOfRangeException();

Loading…
Cancel
Save