Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions jhotdraw-actions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,24 @@
<artifactId>jhotdraw-datatransfer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.5.1</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,25 @@ public CopyAction(JComponent target) {

@Override
public void actionPerformed(ActionEvent evt) {
JComponent c = target;
if (c == null && (KeyboardFocusManager.getCurrentKeyboardFocusManager().
getPermanentFocusOwner() instanceof JComponent)) {
c = (JComponent) KeyboardFocusManager.getCurrentKeyboardFocusManager().
getPermanentFocusOwner();
JComponent component = getTargetComponent();
if (component != null) {
copyToClipboard(component);
}
// Note: copying is allowed for disabled components
if (c != null) {
c.getTransferHandler().exportToClipboard(
c,
ClipboardUtil.getClipboard(),
TransferHandler.COPY);
}

private JComponent getTargetComponent() {
if (target != null) {
return target;
}
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
return (focusOwner instanceof JComponent) ? (JComponent) focusOwner : null;
}

private void copyToClipboard(JComponent component) {
component.getTransferHandler().exportToClipboard(
component,
ClipboardUtil.getClipboard(),
TransferHandler.COPY
);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
/*
* @(#)CutAction.java
*
* Copyright (c) 1996-2010 The authors and contributors of JHotDraw.
* You may not use, copy or modify this file, except in compliance with the
* accompanying license terms.
*/
package org.jhotdraw.action.edit;

import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import org.jhotdraw.datatransfer.ClipboardUtil;
import org.jhotdraw.util.*;
import org.jhotdraw.util.ResourceBundleUtil;

/**
* Cuts the selected region and places its contents into the system clipboard.
Expand All @@ -26,9 +19,7 @@
* If you want this behavior in your application, you have to create an action
* with this ID and put it in your {@code ApplicationModel} in method
* {@link org.jhotdraw.app.ApplicationModel#initApplication}.
*
* @author Werner Randelshofer
* @version $Id$
* </p>
*/
public class CutAction extends AbstractSelectionAction {

Expand All @@ -50,23 +41,56 @@ public CutAction() {
*/
public CutAction(JComponent target) {
super(target);
initializeAction();
}

private void initializeAction() {
ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.action.Labels");
labels.configureAction(this, ID);
}

@Override
public void actionPerformed(ActionEvent evt) {
JComponent c = target;
if (c == null && (KeyboardFocusManager.getCurrentKeyboardFocusManager().
getPermanentFocusOwner() instanceof JComponent)) {
c = (JComponent) KeyboardFocusManager.getCurrentKeyboardFocusManager().
getPermanentFocusOwner();
JComponent component = getActiveComponent();
if (isComponentEligibleForCut(component)) {
performCutToClipboard(component);
}
if (c != null && c.isEnabled()) {
c.getTransferHandler().exportToClipboard(
c,
ClipboardUtil.getClipboard(),
TransferHandler.MOVE);
}

/**
* Retrieves the target component, defaulting to the currently focused component if target is null.
*
* @return the active JComponent or null if none is focused.
*/
private JComponent getActiveComponent() {
if (target != null) {
return target;
}
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
return (focusOwner instanceof JComponent) ? (JComponent) focusOwner : null;
}

/**
* Checks if the component is eligible for a cut operation.
*
* @param component the component to check
* @return true if component is not null and is enabled
*/
private boolean isComponentEligibleForCut(JComponent component) {
return component != null && component.isEnabled();
}

/**
* Cuts the contents of the component to the system clipboard.
*
* @param component the component whose contents are to be cut
*/
private void performCutToClipboard(JComponent component) {
component.getTransferHandler().exportToClipboard(
component,
ClipboardUtil.getClipboard(),
TransferHandler.MOVE
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public DeleteAction() {
* Creates a new instance which acts on the specified component.
*
* @param target The target of the action. Specify null for the currently
* focused component.
* focused component.
*/
public DeleteAction(JComponent target) {
this(target, ID);
Expand All @@ -87,7 +87,7 @@ public DeleteAction(JComponent target) {
* Creates a new instance which acts on the specified component.
*
* @param target The target of the action. Specify null for the currently
* focused component.
* focused component.
*/
protected DeleteAction(JComponent target, String id) {
super(id);
Expand All @@ -110,15 +110,15 @@ public void propertyChange(PropertyChangeEvent evt) {

@Override
public void actionPerformed(ActionEvent evt) {
JComponent c = target;
if (c == null && (KeyboardFocusManager.getCurrentKeyboardFocusManager().
JComponent component = target;
if (component == null && (KeyboardFocusManager.getCurrentKeyboardFocusManager().
getPermanentFocusOwner() instanceof JComponent)) {
c = (JComponent) KeyboardFocusManager.getCurrentKeyboardFocusManager().
component = (JComponent) KeyboardFocusManager.getCurrentKeyboardFocusManager().
getPermanentFocusOwner();
}
if (c != null && c.isEnabled()) {
if (c instanceof EditableComponent) {
((EditableComponent) c).delete();
if (component != null && component.isEnabled()) {
if (component instanceof EditableComponent) {
((EditableComponent) component).delete();
} else {
deleteNextChar(evt);
}
Expand All @@ -130,27 +130,51 @@ public void actionPerformed(ActionEvent evt) {
* DefaultEditorKit.DeleteNextCharAction.actionPerformed(ActionEvent).
*/
public void deleteNextChar(ActionEvent e) {
JTextComponent c = getTextComponent(e);
boolean beep = true;
if ((c != null) && (c.isEditable())) {
try {
javax.swing.text.Document doc = c.getDocument();
Caret caret = c.getCaret();
int dot = caret.getDot();
int mark = caret.getMark();
if (dot != mark) {
doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
beep = false;
} else if (dot < doc.getLength()) {
doc.remove(dot, 1);
beep = false;
}
} catch (BadLocationException bl) {
// allowed empty
}
JTextComponent textComponent = getTextComponent(e);

if (shouldBeepInsteadOfDelete(textComponent)) {
Toolkit.getDefaultToolkit().beep();
return;
}

try {
deleteTextOrNextCharacter(textComponent);
} catch (BadLocationException ignored) {
// Exception ignored as it's unlikely to occur in practice
}
if (beep) {
}
// Refactored helper methods
private boolean shouldBeepInsteadOfDelete(JTextComponent textComponent) {
return textComponent == null || !textComponent.isEditable();
}

private void deleteTextOrNextCharacter(JTextComponent textComponent) throws BadLocationException {
Document doc = textComponent.getDocument();
Caret caret = textComponent.getCaret();

if (hasSelection(caret)) {
deleteSelectedText(doc, caret);
} else if (canDeleteNextCharacter(caret, doc)) {
deleteNextCharacter(doc, caret.getDot());
} else {
Toolkit.getDefaultToolkit().beep();
}
}
}

private boolean hasSelection(Caret caret) {
return caret.getDot() != caret.getMark();
}

private void deleteSelectedText(Document doc, Caret caret) throws BadLocationException {
int dot = caret.getDot();
int mark = caret.getMark();
doc.remove(Math.min(dot, mark), Math.abs(dot - mark));
}

private boolean canDeleteNextCharacter(Caret caret, Document doc) {
return caret.getDot() < doc.getLength();
}

private void deleteNextCharacter(Document doc, int dot) throws BadLocationException {
doc.remove(dot, 1);
}}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jhotdraw.api.gui.EditableComponent;
import org.jhotdraw.util.*;


/**
* Duplicates the selected region.
* <p>
Expand Down Expand Up @@ -61,28 +62,45 @@ public DuplicateAction() {
* Creates a new instance which acts on the specified component.
*
* @param target The target of the action. Specify null for the currently
* focused component.
* focused component.
*/
public DuplicateAction(JComponent target) {
super(target);
ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.action.Labels");
labels.configureAction(this, ID);
}


@Override
public void actionPerformed(ActionEvent evt) {
JComponent c = target;
if (c == null && (KeyboardFocusManager.getCurrentKeyboardFocusManager().
getPermanentFocusOwner() instanceof JComponent)) {
c = (JComponent) KeyboardFocusManager.getCurrentKeyboardFocusManager().
getPermanentFocusOwner();
// Change Variable name
JComponent targetComponent = getTargetComponent();

// Return early if there is no valid or enabled component
if (targetComponent == null || !targetComponent.isEnabled()) {
return;
}
if (c != null && c.isEnabled()) {
if (c instanceof EditableComponent) {
((EditableComponent) c).duplicate();
} else {
c.getToolkit().beep();
}

// Perform duplication if the component is editable
if (targetComponent instanceof EditableComponent) {
((EditableComponent) targetComponent).duplicate();
} else {
targetComponent.getToolkit().beep();
}
}
private JComponent getTargetComponent() {
// Check if 'target' is available
if (target != null) {
return target;
}

// Get the component that is currently in focus
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
if (focusOwner instanceof JComponent) {
return (JComponent) focusOwner;
}

// Returnér null, if no valid component is found
return null;
}
}
Loading