use of org.exbin.deltahex.capability.EditationModeCapable in project deltahex-java by exbin.
the class DeltaHexEditorBasic method editationModeLabelMouseClicked.
// GEN-LAST:event_aboutMenuItemActionPerformed
private void editationModeLabelMouseClicked(java.awt.event.MouseEvent evt) {
// GEN-FIRST:event_editationModeLabelMouseClicked
if (evt.getButton() == MouseEvent.BUTTON1) {
EditationMode editationMode;
if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.INSERT) {
editationMode = EditationMode.OVERWRITE;
} else {
editationMode = EditationMode.INSERT;
}
((EditationModeCapable) codeArea.getWorker()).setEditationMode(editationMode);
}
}
use of org.exbin.deltahex.capability.EditationModeCapable in project deltahex-java by exbin.
the class ExtendedCodeAreaTest method testCopyPasteInInsertMode.
@Test
public void testCopyPasteInInsertMode() {
CodeArea codeArea = new CodeArea();
((EditationModeCapable) codeArea.getWorker()).setEditationMode(EditationMode.INSERT);
EditableBinaryData sampleData = getSampleData(SAMPLE_ALLBYTES);
codeArea.setData(sampleData);
long dataSize = sampleData.getDataSize();
codeArea.selectAll();
codeArea.copy();
codeArea.clearSelection();
codeArea.paste();
Assert.assertTrue(codeArea.getDataSize() == dataSize * 2);
}
use of org.exbin.deltahex.capability.EditationModeCapable in project deltahex-java by exbin.
the class DeltaHexEditorBasic method postInit.
private void postInit() {
codeArea.setComponentPopupMenu(mainPopupMenu);
setIconImage(new javax.swing.ImageIcon(getClass().getResource("/org/exbin/deltahex/editor/basic/resources/icons/icon.png")).getImage());
undoHandler.addUndoUpdateListener(new BinaryDataUndoUpdateListener() {
@Override
public void undoCommandPositionChanged() {
updateUndoState();
codeArea.repaint();
}
@Override
public void undoCommandAdded(@Nonnull BinaryDataCommand command) {
updateUndoState();
codeArea.repaint();
}
});
((EditationModeCapable) codeArea.getWorker()).addEditationModeChangedListener(new EditationModeChangedListener() {
@Override
public void editationModeChanged(EditationMode editationMode) {
switch(editationMode) {
case INSERT:
{
editationModeLabel.setText("INS");
break;
}
case OVERWRITE:
{
editationModeLabel.setText("OVR");
break;
}
default:
{
throw new IllegalStateException("Unexpected editation mode " + editationMode.name());
}
}
}
});
((CaretCapable) codeArea.getWorker()).addCaretMovedListener(new CaretMovedListener() {
@Override
public void caretMoved(CaretPosition caretPosition) {
positionLabel.setText(caretPosition.getDataPosition() + ":" + caretPosition.getCodeOffset());
}
});
((SelectionCapable) codeArea.getWorker()).addSelectionChangedListener(new SelectionChangedListener() {
@Override
public void selectionChanged(SelectionRange selection) {
updateClipboardState();
}
});
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.addFlavorListener(new FlavorListener() {
@Override
public void flavorsChanged(FlavorEvent e) {
updateClipboardState();
}
});
updateUndoState();
updateClipboardState();
openFileButton.setText("Open");
}
use of org.exbin.deltahex.capability.EditationModeCapable in project deltahex-java by exbin.
the class CodeAreaTest method testCopyPasteInInsertMode.
@Test
public void testCopyPasteInInsertMode() {
CodeArea codeArea = new CodeArea();
((EditationModeCapable) codeArea.getWorker()).setEditationMode(EditationMode.INSERT);
EditableBinaryData sampleData = getSampleData(SAMPLE_ALLBYTES);
codeArea.setData(sampleData);
long dataSize = sampleData.getDataSize();
codeArea.selectAll();
codeArea.copy();
codeArea.clearSelection();
codeArea.paste();
Assert.assertTrue(codeArea.getDataSize() == dataSize * 2);
}
use of org.exbin.deltahex.capability.EditationModeCapable in project deltahex-java by exbin.
the class DefaultCodeAreaCommandHandler method paste.
@Override
public void paste() {
if (!((EditationModeCapable) codeArea.getWorker()).isEditable()) {
return;
}
try {
if (clipboard.isDataFlavorAvailable(binaryDataFlavor)) {
if (codeArea.hasSelection()) {
deleteSelection();
codeArea.notifyDataChanged();
}
try {
Object object = clipboard.getData(binaryDataFlavor);
if (object instanceof BinaryData) {
DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
long dataPosition = caret.getDataPosition();
BinaryData clipboardData = (BinaryData) object;
long dataSize = clipboardData.getDataSize();
if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
long toRemove = dataSize;
if (dataPosition + toRemove > codeArea.getDataSize()) {
toRemove = codeArea.getDataSize() - dataPosition;
}
((EditableBinaryData) codeArea.getData()).remove(dataPosition, toRemove);
}
((EditableBinaryData) codeArea.getData()).insert(dataPosition, clipboardData);
codeArea.notifyDataChanged();
caret.setCaretPosition(caret.getDataPosition() + dataSize);
caret.setCodeOffset(0);
updateScrollBars();
notifyCaretMoved();
revealCursor();
}
} catch (UnsupportedFlavorException | IOException ex) {
Logger.getLogger(DefaultCodeAreaCommandHandler.class.getName()).log(Level.SEVERE, null, ex);
}
} else if (clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor)) {
if (codeArea.hasSelection()) {
deleteSelection();
codeArea.notifyDataChanged();
}
Object insertedData;
try {
insertedData = clipboard.getData(DataFlavor.stringFlavor);
if (insertedData instanceof String) {
DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) codeArea.getWorker()).getCaret();
long dataPosition = caret.getDataPosition();
byte[] bytes = ((String) insertedData).getBytes(Charset.forName(CodeAreaUtils.DEFAULT_ENCODING));
int length = bytes.length;
if (((EditationModeCapable) codeArea.getWorker()).getEditationMode() == EditationMode.OVERWRITE) {
long toRemove = length;
if (dataPosition + toRemove > codeArea.getDataSize()) {
toRemove = codeArea.getDataSize() - dataPosition;
}
((EditableBinaryData) codeArea.getData()).remove(dataPosition, toRemove);
}
((EditableBinaryData) codeArea.getData()).insert(dataPosition, bytes);
codeArea.notifyDataChanged();
caret.setCaretPosition(caret.getDataPosition() + length);
caret.setCodeOffset(0);
updateScrollBars();
notifyCaretMoved();
revealCursor();
}
} catch (UnsupportedFlavorException | IOException ex) {
Logger.getLogger(DefaultCodeAreaCommandHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (IllegalStateException ex) {
// Clipboard not available - ignore
}
}
Aggregations