use of java.awt.datatransfer.UnsupportedFlavorException in project airavata by apache.
the class GraphCanvas method drop.
private void drop(final DropTargetDropEvent event) {
logger.debug("Event:" + event);
Transferable transferable = event.getTransferable();
try {
// Cannot cast transferable.
final ComponentReference componentReference = (ComponentReference) transferable.getTransferData(ComponentSourceTransferable.FLAVOR);
final Point location = event.getLocation();
// The component might not have loaded if the network is slow.
new Thread() {
@Override
public void run() {
try {
Component component = componentReference.getComponent();
addNode(component, location);
// To be able to delete the added node by the keyboard.
GraphCanvas.this.panel.requestFocusInWindow();
// XXX this sometimes throws exception.
event.dropComplete(true);
} catch (ComponentException e) {
// If there is any error, the component tree viewer
// shows the error dialog.
logger.error(e.getMessage(), e);
event.dropComplete(false);
} catch (ComponentRegistryException e) {
logger.error(e.getMessage(), e);
event.dropComplete(false);
}
}
}.start();
} catch (UnsupportedFlavorException e) {
// Should not happen.
logger.error(e.getMessage(), e);
} catch (IOException e) {
// Should not happen.
logger.error(e.getMessage(), e);
}
}
use of java.awt.datatransfer.UnsupportedFlavorException in project tetrad by cmu-phil.
the class SessionFileTransferHandler method importData.
@Override
public boolean importData(TransferSupport support) {
try {
List<File> files = (List<File>) support.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
for (File file : files) {
Preferences.userRoot().put("sessionSaveLocation", file.getParent());
Session session = DesktopController.getInstance().getSessionByName(file.getName());
if (session != null) {
if (session.isEmpty()) {
DesktopController.getInstance().closeSessionByName(file.getName());
} else {
int ret = JOptionPane.showConfirmDialog(JOptionUtils.centeringComp(), "Replace existing session by that name?.", "Confirm", JOptionPane.YES_NO_OPTION);
if (ret == JOptionPane.YES_OPTION) {
DesktopController.getInstance().closeSessionByName(file.getName());
} else {
return false;
}
}
}
try (InputStream in = Files.newInputStream(file.toPath())) {
DecompressibleInputStream objIn = new DecompressibleInputStream(in);
Object o = objIn.readObject();
TetradMetadata metadata = null;
SessionWrapper sessionWrapper = null;
if (o instanceof TetradMetadata) {
metadata = (TetradMetadata) o;
sessionWrapper = (SessionWrapper) objIn.readObject();
} else if (o instanceof SessionWrapper) {
metadata = null;
sessionWrapper = (SessionWrapper) o;
}
in.close();
if (metadata == null) {
throw new NullPointerException("Could not read metadata.");
}
if (sessionWrapper == null) {
Version version = metadata.getVersion();
Date date = metadata.getDate();
SimpleDateFormat df = new SimpleDateFormat("MMM dd, yyyy");
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "Could not load session. The version of the session was \n" + version + "; it was saved on " + df.format(date) + ". You " + "\nmight try loading it with that version instead.");
return false;
}
SessionEditorWorkbench graph = new SessionEditorWorkbench(sessionWrapper);
String name = file.getName();
sessionWrapper.setName(name);
SessionEditor editor = new SessionEditor(name, graph);
DesktopController.getInstance().addSessionEditor(editor);
DesktopController.getInstance().closeEmptySessions();
DesktopController.getInstance().putMetadata(sessionWrapper, metadata);
} catch (FileNotFoundException exception) {
LOGGER.error("", exception);
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "That wasn't a TETRAD session file: " + file);
} catch (Exception exception) {
LOGGER.error("", exception);
JOptionPane.showMessageDialog(JOptionUtils.centeringComp(), "An error occurred attempting to load the session.");
}
}
} catch (UnsupportedFlavorException | IOException exception) {
LOGGER.error("", exception);
}
return super.importData(support);
}
use of java.awt.datatransfer.UnsupportedFlavorException in project tetrad by cmu-phil.
the class TabularDataTransferHandler method importData.
public boolean importData(JComponent c, Transferable t) {
if (c instanceof TabularDataJTable) {
try {
TabularDataJTable tabularData = (TabularDataJTable) c;
String s = (String) t.getTransferData(DataFlavor.stringFlavor);
int startRow = tabularData.getSelectedRow();
int startCol = tabularData.getSelectedColumn();
if (startRow == 0) {
startRow = 1;
}
if (startCol < getNumLeadingCols()) {
startCol = getNumLeadingCols();
}
if (!checkRanges(s, startCol, tabularData)) {
return false;
}
boolean shouldAsk = false;
boolean shiftDown = true;
BufferedReader preReader = new BufferedReader(new CharArrayReader(s.toCharArray()));
String preLine = preReader.readLine();
StringTokenizer preTokenizer = new StringTokenizer(preLine, "\t");
int numTokens = preTokenizer.countTokens();
for (int col = startCol; col < startCol + numTokens; col++) {
Object value = tabularData.getValueAt(startRow, col);
if (!"".equals(value) && !(null == value)) {
shouldAsk = true;
}
if (startRow - getNumLeadingRows() >= tabularData.getDataSet().getNumRows() || startCol - getNumLeadingCols() >= tabularData.getDataSet().getNumColumns()) {
shouldAsk = false;
shiftDown = false;
}
}
if (shouldAsk) {
String[] choices = new String[] { "Shift corresponding cells down to make room", "Replace corresponding cells" };
Object choice = JOptionPane.showInputDialog(JOptionUtils.centeringComp(), "How should the clipboard contents be pasted?", "Paste Contents", JOptionPane.INFORMATION_MESSAGE, null, choices, choices[0]);
// Null means the user cancelled the input.
if (choice == null) {
return false;
}
shiftDown = choice.equals(choices[0]);
}
doPaste(s, startRow, startCol, shiftDown, tabularData);
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
return false;
}
use of java.awt.datatransfer.UnsupportedFlavorException in project tetrad by cmu-phil.
the class SubgraphSelection method getTransferData.
/**
* @return an object which represents the data to be transferred. The class
* of the object returned is defined by the representation class of the
* flavor.
*
* @param flavor the requested flavor for the data
* @throws IOException if the data is no longer available in
* the requested flavor.
* @throws UnsupportedFlavorException if the requested data flavor is not
* supported.
* @see DataFlavor#getRepresentationClass
*/
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (!isDataFlavorSupported(flavor)) {
throw new UnsupportedFlavorException(flavor);
}
List returnList = this.graphElements;
this.graphElements = null;
return returnList;
}
use of java.awt.datatransfer.UnsupportedFlavorException in project RFToolsControl by McJty.
the class GuiProgrammer method handleClipboard.
private boolean handleClipboard(int keyCode) {
if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL)) {
if (keyCode == Keyboard.KEY_A) {
selectAll();
} else if (keyCode == Keyboard.KEY_C) {
if (!checkSelection()) {
GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Nothing is selected!");
} else {
ProgramCardInstance instance = makeGridInstance(true);
String json = instance.writeToJson();
try {
StringSelection selection = new StringSelection(json);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
} catch (Exception e) {
GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Error copying to clipboard!");
}
}
return true;
} else if (keyCode == Keyboard.KEY_Z) {
if (undoProgram != null) {
ProgramCardInstance curProgram = makeGridInstance(false);
clearGrid(false);
loadProgram(undoProgram);
undoProgram = curProgram;
}
return true;
} else if (keyCode == Keyboard.KEY_X) {
if (!checkSelection()) {
GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Nothing is selected!");
} else {
ProgramCardInstance instance = makeGridInstance(true);
String json = instance.writeToJson();
try {
StringSelection selection = new StringSelection(json);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
undoProgram = makeGridInstance(false);
clearGrid(checkSelection());
} catch (Exception e) {
GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Error copying to clipboard!");
}
}
return true;
} else if (keyCode == Keyboard.KEY_V) {
try {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String data = (String) clipboard.getData(DataFlavor.stringFlavor);
ProgramCardInstance program = ProgramCardInstance.readFromJson(data);
undoProgram = makeGridInstance(false);
mergeProgram(program, getSelectedGridHolder());
} catch (UnsupportedFlavorException e) {
GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Clipboard does not contain program!");
} catch (Exception e) {
GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "Error reading from clipboard!");
}
}
}
return false;
}
Aggregations