1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// Copyright © 2008, 2010, Oracle and/or its affiliates. All rights reserved.
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
// MySQL Connectors. There are special exceptions to the terms and
// conditions of the GPLv2 as it is applied to this software, see the
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// 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; version 2 of the License.
//
// 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, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using System.IO;
using System.Globalization;
namespace MySql.Data.VisualStudio.Editors
{
public class BaseEditorControl : UserControl, IVsPersistDocData, IPersistFileFormat
{
private bool savingFile;
private bool loadingFile;
protected ServiceProvider serviceProvider;
protected string fileName;
#region IVsPersistDocData Members
int IVsPersistDocData.Close()
{
return VSConstants.S_OK;
}
int IVsPersistDocData.GetGuidEditorType(out Guid pClassID)
{
throw new NotImplementedException();
}
int IVsPersistDocData.IsDocDataDirty(out int pfDirty)
{
return ((IPersistFileFormat)this).IsDirty(out pfDirty);
}
int IVsPersistDocData.IsDocDataReloadable(out int pfReloadable)
{
pfReloadable = 1;
return VSConstants.S_OK;
}
int IVsPersistDocData.LoadDocData(string pszMkDocument)
{
return ((IPersistFileFormat)this).Load(pszMkDocument, 0, 0);
}
int IVsPersistDocData.OnRegisterDocData(uint docCookie, IVsHierarchy pHierNew, uint itemidNew)
{
return VSConstants.S_OK;
}
int IVsPersistDocData.ReloadDocData(uint grfFlags)
{
return ((IPersistFileFormat)this).Load(null, grfFlags, 0);
}
int IVsPersistDocData.RenameDocData(uint grfAttribs, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)
{
fileName = pszMkDocumentNew;
return VSConstants.S_OK;
}
private int QuerySave(out tagVSQuerySaveResult qsResult)
{
uint result;
IVsQueryEditQuerySave2 querySave =
(IVsQueryEditQuerySave2)serviceProvider.GetService(typeof(SVsQueryEditQuerySave));
int hr = querySave.QuerySaveFile(fileName, 0, null, out result);
qsResult = (tagVSQuerySaveResult)result;
return hr;
}
int IVsPersistDocData.SaveDocData(VSSAVEFLAGS dwSave, out string pbstrMkDocumentNew, out int pfSaveCanceled)
{
pbstrMkDocumentNew = null;
pfSaveCanceled = 0;
int hr;
IVsUIShell uiShell = (IVsUIShell)serviceProvider.GetService(typeof(IVsUIShell));
switch (dwSave)
{
case VSSAVEFLAGS.VSSAVE_Save:
case VSSAVEFLAGS.VSSAVE_SilentSave:
{
tagVSQuerySaveResult qsResult;
hr = QuerySave(out qsResult);
if (ErrorHandler.Failed(hr)) return hr;
if (qsResult == tagVSQuerySaveResult.QSR_NoSave_Cancel)
pfSaveCanceled = ~0;
else if (qsResult == tagVSQuerySaveResult.QSR_SaveOK)
{
hr = uiShell.SaveDocDataToFile(dwSave, this, fileName,
out pbstrMkDocumentNew, out pfSaveCanceled);
if (ErrorHandler.Failed(hr)) return hr;
}
else if (qsResult == tagVSQuerySaveResult.QSR_ForceSaveAs)
{
hr = uiShell.SaveDocDataToFile(VSSAVEFLAGS.VSSAVE_SaveAs, this, fileName,
out pbstrMkDocumentNew, out pfSaveCanceled);
if (ErrorHandler.Failed(hr)) return hr;
}
break;
}
case VSSAVEFLAGS.VSSAVE_SaveAs:
case VSSAVEFLAGS.VSSAVE_SaveCopyAs:
{
// --- Make sure the file name as the right extension
if (String.Compare(".mysql", Path.GetExtension(fileName), true,
CultureInfo.CurrentCulture) != 0)
fileName += ".mysql";
// --- Call the shell to do the save for us
hr = uiShell.SaveDocDataToFile(dwSave, this, fileName,
out pbstrMkDocumentNew, out pfSaveCanceled);
if (ErrorHandler.Failed(hr)) return hr;
break;
}
default:
throw new ArgumentException("Unable to save file");
}
return VSConstants.S_OK;
}
int IVsPersistDocData.SetUntitledDocPath(string pszDocDataPath)
{
fileName = pszDocDataPath;
return VSConstants.S_OK;
}
#endregion
#region IPersistFileFormat Members
int IPersistFileFormat.GetClassID(out Guid pClassID)
{
throw new NotImplementedException();
}
int IPersistFileFormat.GetCurFile(out string ppszFilename, out uint pnFormatIndex)
{
ppszFilename = fileName;
pnFormatIndex = 0;
return VSConstants.S_OK;
}
int IPersistFileFormat.GetFormatList(out string ppszFormatList)
{
ppszFormatList = GetFileFormatList();
return VSConstants.S_OK;
}
int IPersistFileFormat.InitNew(uint nFormatIndex)
{
return VSConstants.S_OK;
}
int IPersistFileFormat.IsDirty(out int pfIsDirty)
{
pfIsDirty = IsDirty ? 1 : 0;
return VSConstants.S_OK;
}
int IPersistFileFormat.Load(string pszFilename, uint grfMode, int fReadOnly)
{
// --- A valid file name is required.
if ((pszFilename == null) && ((fileName == null) || (fileName.Length == 0)))
throw new ArgumentNullException("pszFilename");
loadingFile = true;
int hr = VSConstants.S_OK;
try
{
// --- If the new file name is null, then this operation is a reload
bool isReload = false;
if (pszFilename == null)
{
isReload = true;
}
// --- Set the new file name
if (!isReload)
{
fileName = pszFilename;
}
// --- Load the file
LoadFile(fileName);
IsDirty = false;
// --- Notify the load or reload
//NotifyDocChanged();
}
finally
{
loadingFile = false;
}
return hr;
}
int IPersistFileFormat.Save(string pszFilename, int fRemember, uint nFormatIndex)
{
// --- switch into the NoScribble mode
savingFile = true;
try
{
// --- If file is null or same --> SAVE
if (pszFilename == null || pszFilename == fileName)
{
SaveFile(fileName);
IsDirty = false;
}
else
{
// --- If remember --> SaveAs
if (fRemember != 0)
{
fileName = pszFilename;
SaveFile(fileName);
IsDirty = false;
}
else // --- Else, Save a Copy As
{
SaveFile(pszFilename);
}
}
}
finally
{
// --- Switch into the Normal mode
savingFile = false;
}
return VSConstants.S_OK;
}
int IPersistFileFormat.SaveCompleted(string pszFilename)
{
if (savingFile)
return VSConstants.S_FALSE;
return VSConstants.S_OK;
}
#endregion
#region IPersist Members
public int GetClassID(out Guid pClassID)
{
throw new NotImplementedException();
}
#endregion
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
#region Virtuals
protected virtual string GetFileFormatList() { return null; }
protected virtual void SaveFile(string fileName) { }
protected virtual void LoadFile(string fileName) { }
protected virtual bool IsDirty { get { return true; } set {} }
#endregion
#region IPersist Members
int IPersist.GetClassID(out Guid pClassID)
{
throw new NotImplementedException();
}
#endregion
}
}
|