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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* ClipboardModel.cs
* Copyright © 2011 kbinani
*
* This file is part of org.kbinani.cadencii.
*
* org.kbinani.cadencii is free software; you can redistribute it and/or
* modify it under the terms of the GPLv3 License.
*
* org.kbinani.cadencii 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.
*/
#if JAVA
package org.kbinani.cadencii;
import java.io.*;
import java.util.*;
import org.kbinani.*;
import org.kbinani.vsq.*;
#else
namespace org
{
namespace kbinani
{
namespace cadencii
{
#if __cplusplus
#else
using System;
using org.kbinani.vsq;
using org.kbinani.java.util;
using System.Windows.Forms;
#endif
#endif
/// <summary>
/// クリップボードを管理するクラスです.
/// </summary>
public class ClipboardModel
{
#if CLIPBOARD_AS_TEXT
/// <summary>
/// OSのクリップボードに貼り付ける文字列の接頭辞.
/// これがついていた場合,クリップボードの文字列をCadenciiが使用できると判断する.
/// </summary>
public const String CLIP_PREFIX = "CADENCIIOBJ";
#endif
#if CLIPBOARD_AS_TEXT
/// <summary>
/// オブジェクトをシリアライズし,クリップボードに格納するための文字列を作成します
/// </summary>
/// <param name="obj">シリアライズするオブジェクト</param>
/// <returns>シリアライズされた文字列</returns>
private String getSerializedText( Object obj )
#if JAVA
throws IOException
#endif
{
String str = "";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream( outputStream );
objectOutputStream.writeObject( obj );
byte[] arr = outputStream.toByteArray();
#if JAVA
str = CLIP_PREFIX + ":" + obj.getClass().getName() + ":" + org.kbinani.Base64.encode( arr );
#else
str = CLIP_PREFIX + ":" + obj.GetType().FullName + ":" + Base64.encode( arr );
#endif
return str;
}
/// <summary>
/// クリップボードに格納された文字列を元に,デシリアライズされたオブジェクトを取得します
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private Object getDeserializedObjectFromText( String s )
{
if ( s.StartsWith( CLIP_PREFIX ) )
{
int index = s.IndexOf( ":" );
index = s.IndexOf( ":", index + 1 );
Object ret = null;
try
{
ByteArrayInputStream bais = new ByteArrayInputStream( org.kbinani.Base64.decode( str.sub( s, index + 1 ) ) );
ObjectInputStream ois = new ObjectInputStream( bais );
ret = ois.readObject();
}
catch ( Exception ex )
{
ret = null;
Logger.write( typeof( ClipboardModel ) + ".getDeserializedObjectFromText; ex=" + ex + "\n" );
}
return ret;
}
else
{
return null;
}
}
#endif
/// <summary>
/// クリップボードにオブジェクトを貼り付けます.
/// </summary>
/// <param name="item">貼り付けるオブジェクトを格納したClipboardEntryのインスタンス</param>
public void setClipboard( ClipboardEntry item )
{
#if CLIPBOARD_AS_TEXT
String clip = "";
try
{
clip = getSerializedText( item );
#if DEBUG
sout.println( "ClipboardModel#setClipboard; clip=" + clip );
#endif
}
catch ( Exception ex )
{
serr.println( "ClipboardModel#setClipboard; ex=" + ex );
Logger.write( typeof( ClipboardModel ) + ".setClipboard; ex=" + ex + "\n" );
return;
}
PortUtil.setClipboardText( clip );
#else
Clipboard.SetDataObject( item, false );
#endif
}
/// <summary>
/// クリップボードにオブジェクトを貼り付けるためのユーティリティ.
/// </summary>
/// <param name="events"></param>
/// <param name="tempo"></param>
/// <param name="timesig"></param>
/// <param name="curve"></param>
/// <param name="bezier"></param>
/// <param name="copy_started_clock"></param>
private void setClipboard(
Vector<VsqEvent> events,
Vector<TempoTableEntry> tempo,
Vector<TimeSigTableEntry> timesig,
TreeMap<CurveType, VsqBPList> curve,
TreeMap<CurveType, Vector<BezierChain>> bezier,
int copy_started_clock )
{
ClipboardEntry ce = new ClipboardEntry();
ce.events = events;
ce.tempo = tempo;
ce.timesig = timesig;
ce.points = curve;
ce.beziers = bezier;
ce.copyStartedClock = copy_started_clock;
#if CLIPBOARD_AS_TEXT
String clip = "";
try
{
clip = getSerializedText( ce );
#if DEBUG
sout.println( "ClipboardModel#setClipboard; clip=" + clip );
#endif
}
catch ( Exception ex )
{
serr.println( "ClipboardModel#setClipboard; ex=" + ex );
Logger.write( typeof( ClipboardModel ) + ".setClipboard; ex=" + ex + "\n" );
return;
}
PortUtil.setClipboardText( clip );
#else // CLIPBOARD_AS_TEXT
#if DEBUG
// ClipboardEntryがシリアライズ可能かどうかを試すため,
// この部分のコードは残しておくこと
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = null;
System.IO.MemoryStream ms = null;
try {
bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
ms = new System.IO.MemoryStream();
bf.Serialize( ms, ce );
} catch ( Exception ex ) {
sout.println( "ClipboardModel#setClipboard; ex=" + ex );
}
#endif // DEBUG
Clipboard.SetDataObject( ce, false );
#endif // CLIPBOARD_AS_TEXT
}
/// <summary>
/// クリップボードに貼り付けられたアイテムを取得します.
/// </summary>
/// <returns>クリップボードに貼り付けられたアイテムを格納したClipboardEntryのインスタンス</returns>
public ClipboardEntry getCopiedItems()
{
ClipboardEntry ce = null;
#if CLIPBOARD_AS_TEXT
String clip = PortUtil.getClipboardText();
if ( clip != null && str.startsWith( clip, CLIP_PREFIX ) ) {
int index1 = clip.IndexOf( ":" );
int index2 = clip.IndexOf( ":", index1 + 1 );
String typename = str.sub( clip, index1 + 1, index2 - index1 - 1 );
#if DEBUG
sout.println( "ClipboardModel#getCopiedItems; typename=" + typename );
#endif
#if JAVA
if ( typename.Equals( ClipboardEntry.class.getName() ) ) {
#else
if ( typename.Equals( typeof( ClipboardEntry ).FullName ) ) {
#endif
try {
ce = (ClipboardEntry)getDeserializedObjectFromText( clip );
} catch ( Exception ex ) {
Logger.write( typeof( ClipboardModel ) + ".getCopiedItems; ex=" + ex + "\n" );
}
}
}
#else
IDataObject dobj = Clipboard.GetDataObject();
if ( dobj != null )
{
Object obj = dobj.GetData( typeof( ClipboardEntry ) );
if ( obj != null && obj is ClipboardEntry )
{
ce = (ClipboardEntry)obj;
}
}
#endif
if ( ce == null )
{
ce = new ClipboardEntry();
}
if ( ce.beziers == null )
{
ce.beziers = new TreeMap<CurveType, Vector<BezierChain>>();
}
if ( ce.events == null )
{
ce.events = new Vector<VsqEvent>();
}
if ( ce.points == null )
{
ce.points = new TreeMap<CurveType, VsqBPList>();
}
if ( ce.tempo == null )
{
ce.tempo = new Vector<TempoTableEntry>();
}
if ( ce.timesig == null )
{
ce.timesig = new Vector<TimeSigTableEntry>();
}
return ce;
}
/// <summary>
/// VsqEventのリストをクリップボードにセットします.
/// </summary>
/// <param name="item">セットするVsqEventのリスト</param>
/// <param name="copy_started_clock"></param>
public void setCopiedEvent( Vector<VsqEvent> item, int copy_started_clock )
{
setClipboard( item, null, null, null, null, copy_started_clock );
}
/// <summary>
/// テンポ変更イベント(TempoTableEntry)のリストをクリップボードにセットします.
/// </summary>
/// <param name="item">セットするTempoTableEntryのリスト</param>
/// <param name="copy_started_clock"></param>
public void setCopiedTempo( Vector<TempoTableEntry> item, int copy_started_clock )
{
setClipboard( null, item, null, null, null, copy_started_clock );
}
/// <summary>
/// 拍子変更イベント(TimeSigTableEntry)のリストをクリップボードにセットします.
/// </summary>
/// <param name="item">セットする拍子変更イベントのリスト</param>
/// <param name="copy_started_clock"></param>
public void setCopiedTimesig( Vector<TimeSigTableEntry> item, int copy_started_clock )
{
setClipboard( null, null, item, null, null, copy_started_clock );
}
/// <summary>
/// コントロールカーブをクリップボードにセットします.
/// </summary>
/// <param name="item">セットするコントロールカーブ</param>
/// <param name="copy_started_clock"></param>
public void setCopiedCurve( TreeMap<CurveType, VsqBPList> item, int copy_started_clock )
{
setClipboard( null, null, null, item, null, copy_started_clock );
}
/// <summary>
/// ベジエ曲線をクリップボードにセットします.
/// </summary>
/// <param name="item">セットするベジエ曲線</param>
/// <param name="copy_started_clock"></param>
public void setCopiedBezier( TreeMap<CurveType, Vector<BezierChain>> item, int copy_started_clock )
{
setClipboard( null, null, null, null, item, copy_started_clock );
}
}
#if !JAVA
}
}
}
#endif
|