Menu

[r11]: / trunk / SWFInvestigator / src / hexEditor / gridEventHandlers.as  Maximize  Restore  History

Download this file

211 lines (173 with data), 6.1 kB

  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
/****************************************************************************
* ADOBE SYSTEMS INCORPORATED
* Copyright 2012 Adobe Systems Incorporated and it’s licensors
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file
* in accordance with the terms of the license agreement accompanying it.
* ****************************************************************************/
/**
* This is currently designed to be an include file for files that have a hex grid
* It assumes the relevant data grid is called hvDataGrid
* It assumes the HexViewer object is called HV
* Ideally I would make the data grid and these functions a component
* In the meantime, I am just making it an include file for the ABCEditor, Editor Tab and Binary Editor.
*/
import flash.desktop.Clipboard;
import flash.events.Event;
import flash.utils.ByteArray;
import osUtils.DragAndDropManager;
import spark.components.gridClasses.CellPosition;
import spark.events.GridItemEditorEvent;
private var dragManager:DragAndDropManager;
/**
* @private
* Updates the string column in the grid after an edit
*/
private function updateString(e:GridItemEditorEvent):void {
this.HV.resetString(e.rowIndex);
}
/**
* @private
* This is registered by initializeDG() to handle the Event.COPY event
* It will copy the hex from the DataGrid to the clipboard as a comma-delimited string.
* The string and offset data is not copied.
*
* @param e An ActionScript Event.COPY call
*/
private function handleCopy(e:Event):void {
if (e != null) {
e.preventDefault();
}
// Using a Vector for performance
var dataVector:Vector.<String> = new Vector.<String>;
var startCell:CellPosition = this.hvDataGrid.selectedCells[0];
var endCell:CellPosition = this.hvDataGrid.selectedCells[this.hvDataGrid.selectedCells.length - 1];
var row:uint = startCell.rowIndex;
var colIndex:uint = startCell.columnIndex;
var data:Object;
var i:uint;
if (colIndex < 2) {colIndex=2;}
while (row < endCell.rowIndex) {
data = this.hvDataGrid.dataProvider[row];
while (colIndex < 18) {
dataVector.push (data["col" + (colIndex - 2)]);
colIndex++;
}
colIndex = 2;
row++;
}
if (startCell.rowIndex == endCell.rowIndex) {
i = startCell.columnIndex - 2;
} else {
i = 0;
}
data = this.hvDataGrid.dataProvider[row];
colIndex = endCell.columnIndex;
if (colIndex > 17) {
colIndex = 17;
while (data["col" + (colIndex-2)] == null) {
colIndex--;
}
}
while (i + 2 <= colIndex) {
dataVector.push(data["col" + i]);
i++;
}
// Write dataString to the clipboard.
Clipboard.generalClipboard.clear();
System.setClipboard(dataVector.toString());
dataVector = null;
}
/**
* @private
* This is registered by initializeDG() to handle the Event.CUT event
* It will copy the hex from the DataGrid to the clipboard as a comma-delimited string.
* It will also remove the relevant bytes from the data grid.
* The string and offset data is not copied.
*
* @param e An ActionScript Event.CUT call
*/
private function handleCut(e:Event):void {
e.preventDefault();
handleCopy(null);
var startCell:CellPosition = this.hvDataGrid.selectedCells[0];
var endCell:CellPosition = this.hvDataGrid.selectedCells[this.hvDataGrid.selectedCells.length - 1];
var n:int = this.hvDataGrid.selectedCells.length;
if (n <= 0) {return;}
var start:uint;
if (startCell.columnIndex > 1) {
start = (startCell.rowIndex * 16) + startCell.columnIndex - 2;
} else {
start = startCell.rowIndex * 16;
}
var end:uint = (endCell.rowIndex * 16) + endCell.columnIndex - 2;
if (end > this.HV.swfBytes.length) {
end = this.HV.swfBytes.length - 1;
}
this.HV.deleteData(start,end);
this.HV.updateHexArray();
this.hvDataGrid.ensureCellIsVisible(startCell.rowIndex,startCell.columnIndex);
this.hvDataGrid.setSelectedCell(startCell.rowIndex, startCell.columnIndex);
}
/**
* @private
* This is registered by initializeDG() to handle the Event.PASTE event
* This will copy the comma-delimited hex from the clipboard to the DataGrid.
* The string and offset data should not be included in the paste.
*
* @param e An ActionScript Event.COPY call
*/
private function handlePaste(e:Event):void {
e.preventDefault();
if (this.hvDataGrid.selectedCells.length != 1) {
if (this.HV.swfBytes != null && this.HV.swfBytes.length > 0) {
return;
} else if (this.HV.swfBytes == null) {
this.HV.initViewer(new ByteArray());
}
}
var pasteData:String = Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT) as String;
var bytes:Array = pasteData.split(",");
var pasteArray:ByteArray = new ByteArray();
for (var i:int = 0; i < bytes.length; i++) {
pasteArray.writeByte(int("0x" + bytes[i]));
}
pasteArray.position = 0;
var cell:CellPosition = this.hvDataGrid.selectedCell;
var pos:uint;
if (cell != null) {
pos = (cell.rowIndex * 16) + cell.columnIndex - 2;
} else {
cell = new CellPosition(0,0);
pos = 0;
}
this.HV.insertData(pos,pasteArray);
this.HV.updateHexArray();
this.hvDataGrid.ensureCellIsVisible(cell.rowIndex,cell.columnIndex);
this.hvDataGrid.setSelectedCell(cell.rowIndex,cell.columnIndex);
}
/**
* @private
* Check the bytes of the file
*
* @param data The ByteArray from the drag
*/
private function dndValidation(data:ByteArray, fName:String, url:String):void {
if (data.length > 0) {
this.HV.initViewer(data);
this.HV.updateHexArray();
}
}
/**
* @private
* Registers the handlers for cut, copy and paste commands
*/
private function initializeDG():void {
this.hvDataGrid.addEventListener(Event.COPY, handleCopy);
this.hvDataGrid.addEventListener(Event.CUT, handleCut);
this.hvDataGrid.addEventListener(Event.PASTE, handlePaste);
if (DragAndDropManager.isSupported() && this.HV.dndSupported) {
this.dragManager = new DragAndDropManager(this.hvDataGrid, dndValidation);
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.