Skip to content

Commit

Permalink
Closes #424
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed May 13, 2016
1 parent 711c8b7 commit b981387
Showing 1 changed file with 126 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,154 @@
import org.protege.editor.owl.ui.view.ViewClipboard;
import org.semanticweb.owlapi.model.OWLObject;

import javax.swing.FocusManager;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.Optional;


/**
* Author: Matthew Horridge<br>
* The University Of Manchester<br>
* Medical Informatics Group<br>
* Date: 07-Oct-2006<br><br>
* <p/>
* matthew.horridge@cs.man.ac.uk<br>
* www.cs.man.ac.uk/~horridgm<br><br>
*/
public class CopyAction extends FocusedComponentAction<Copyable> {
public class CopyAction extends ProtegeOWLAction {

private final PropertyChangeListener propertyChangeListener = event -> {
if ("focusOwner".equals(event.getPropertyName())) {
updateState();
}
};

private final ChangeListener changeListener = event -> {
getCopyable().ifPresent(c -> setEnabled(c.canCopy()));
};

private Optional<Copyable> currentCopyable = Optional.empty();

protected boolean canPerform() {
return getCurrentTarget().canCopy();
@Override
public void initialise() throws Exception {
FocusManager.getCurrentManager().addPropertyChangeListener(propertyChangeListener);
updateState();
}

@Override
public void dispose() throws Exception {
FocusManager.getCurrentManager().removePropertyChangeListener(propertyChangeListener);
}

private void updateState() {
currentCopyable.ifPresent(c -> c.removeChangeListener(changeListener));
Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
if (getCopyable().isPresent()) {
currentCopyable = getCopyable();
currentCopyable.ifPresent(c -> {
c.addChangeListener(changeListener);
setEnabled(c.canCopy());
});
}
else if (focusOwner instanceof JTextComponent) {
setEnabled(true);
}
else if (focusOwner instanceof JTable) {
setEnabled(true);
}
else if (focusOwner instanceof JList) {
setEnabled(true);
}
else {
setEnabled(false);
}

protected Class<Copyable> initialiseAction() {
return Copyable.class;
}

private Optional<Copyable> getCopyable() {
Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
if (focusOwner instanceof Copyable) {
return Optional.of((Copyable) focusOwner);
}
else {
return Optional.ofNullable((Copyable) SwingUtilities.getAncestorOfClass(Copyable.class, focusOwner));
}
}

public void actionPerformed(ActionEvent e) {
List<OWLObject> objects = getCurrentTarget().getObjectsToCopy();
Component focusOwner = FocusManager.getCurrentManager().getFocusOwner();
if (getCopyable().isPresent()) {
getCopyable().ifPresent(this::handleCopy);
}
else if (focusOwner instanceof JTextComponent) {
handleCopy((JTextComponent) focusOwner);
}
else if (focusOwner instanceof JTable) {
handleCopy((JTable) focusOwner);
}
else if (focusOwner instanceof JList) {
handleCopy((JList) focusOwner);
}


// Actually, we could put text on to the system clipboard
// OWLObject should be serializable!!!
}

private void handleCopy(JTextComponent textComponent) {
textComponent.copy();
}

private void handleCopy(JTable table) {
StringBuilder sb = new StringBuilder();
for (int row : table.getSelectedRows()) {
for (int col = 0; col < table.getColumnCount(); col++) {
Object value = table.getValueAt(row, col);
TableCellRenderer cellRenderer = table.getCellRenderer(row, col);
Component renderer = cellRenderer.getTableCellRendererComponent(table, value, false, false, row, col);
String stringValue;
if (renderer instanceof JLabel) {
stringValue = ((JLabel) renderer).getText();
}
else {
stringValue = value.toString();
}
sb.append(stringValue);
sb.append("\t");
}
sb.append("\n");
}
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(sb.toString().trim()), null);
}

@SuppressWarnings("unchecked")
private void handleCopy(JList list) {
StringBuilder sb = new StringBuilder();
for (int i : list.getSelectedIndices()) {
Object value = list.getModel().getElementAt(i);
String stringValue;
if (value instanceof OWLObject) {
stringValue = getOWLModelManager().getRendering((OWLObject) value);
}
else {
stringValue = value.toString();
}
sb.append(stringValue);
sb.append("\n");
}
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(sb.toString().trim()), null);
}

private void handleCopy(Copyable copyable) {
List<OWLObject> objects = copyable.getObjectsToCopy();
if (objects.isEmpty()) {
// Shouldn't really happen, but just in case
return;
Expand All @@ -51,8 +170,6 @@ public void actionPerformed(ActionEvent e) {
}
StringSelection stringSelection = new StringSelection(buffer.toString().trim());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
// Actually, we could put text on to the system clipboard
// OWLObject should be serializable!!!
}

//
Expand Down

0 comments on commit b981387

Please sign in to comment.