[MOBY-guts] biomoby commit

Paul Gordon gordonp at dev.open-bio.org
Thu May 13 16:04:00 UTC 2010


gordonp
Thu May 13 12:03:59 EDT 2010
Update of /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui
In directory dev.open-bio.org:/tmp/cvs-serv28759/src/main/ca/ucalgary/seahawk/gui

Modified Files:
	FileAndTextTransferHandler.java MobyObjectTransferHandler.java 
	SeahawkTransferable.java 
Log Message:
Update to properly handle drag of objects within JVM
moby-live/Java/src/main/ca/ucalgary/seahawk/gui FileAndTextTransferHandler.java,1.10,1.11 MobyObjectTransferHandler.java,1.4,1.5 SeahawkTransferable.java,1.2,1.3
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/FileAndTextTransferHandler.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/FileAndTextTransferHandler.java	2010/03/29 19:46:18	1.10
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/FileAndTextTransferHandler.java	2010/05/13 16:03:59	1.11
@@ -199,6 +199,7 @@
     }
 
     public String convertToString(Transferable t, boolean get_plain_text) throws Exception{
+	System.err.println("Converting transferable " + t + "  to text");
 	StringBuffer textBuffer = new StringBuffer();
 	Reader reader = get_plain_text ? DataFlavor.getTextPlainUnicodeFlavor().getReaderForText(t) :
 	    DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors()).getReaderForText(t);

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/MobyObjectTransferHandler.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/MobyObjectTransferHandler.java	2010/04/11 02:00:02	1.4
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/MobyObjectTransferHandler.java	2010/05/13 16:03:59	1.5
@@ -49,7 +49,7 @@
 	if(!handlers.containsKey(cl)){
 	    handlers.put(cl, new MobyObjectTransferHandler(cl));
 	}
-	System.err.println("Returning handler " + handlers.get(cl));
+	//System.err.println("Returning handler " + handlers.get(cl));
 	return handlers.get(cl);
     }
 
@@ -65,7 +65,7 @@
 	}
 	receiver = (MobyObjectReceiver) c;
 
-	System.err.println("Received drop event");
+	//System.err.println("Received drop event");
 	Map<String, MobyPrimaryData> acceptableData = receiver.getAcceptableData();
 	// Does the component even want data?
 	if(acceptableData == null || acceptableData.isEmpty()){
@@ -82,10 +82,17 @@
 
         // Should be done in another thread to not block UI
         try {
-            if (hasFileFlavor(t.getTransferDataFlavors())) {
+	    DataFlavor mobyFlavor = getMobyFlavor(t.getTransferDataFlavors());
+	    if(mobyFlavor != null){
+		MobyDataInstance mobyData = (MobyDataInstance) t.getTransferData(mobyFlavor);
+		dataFound = new HashMap<String,MobyDataInstance>();
+		dataFound.put(mobyData.getName(), mobyData);
+	    }
+            else if (hasFileFlavor(t.getTransferDataFlavors())) {
 		dataFound = getDataFromFiles(acceptableData, 
 					     (java.util.List<File>) t.getTransferData(DataFlavor.javaFileListFlavor));
-            } else if (hasTextFlavor(t.getTransferDataFlavors())) {
+            }
+	    else if (hasTextFlavor(t.getTransferDataFlavors())) {
                 // Make a string out of the text
 		boolean PLAIN_TEXT = true;
 		String text = convertToString(t, PLAIN_TEXT);
@@ -94,7 +101,7 @@
 		// the proper object to load
 		MobyContentInstance content = HTMLUtils.checkForMobyXML(text);
 		if(content != null){
-		    System.err.println("Data appears to be moby XML...");
+		    //System.err.println("Data appears to be moby XML...");
 		    dataFound = getDataFromMobyContents(acceptableData, content);
 		    return sendDataToReceiver(receiver, acceptableData, dataFound);
 		}
@@ -111,7 +118,7 @@
                 }
                 if(u != null){
 		    // Does the URL itself encode any objects (as opposed to its contents)?
-		    System.err.println("Data appears to be a URL...");
+		    //System.err.println("Data appears to be a URL...");
                     dataFound = getDataFromURL(acceptableData, u);
 		    // Does the contents of the URL?
 		    mergeDataMaps(dataFound, getDataFromURLContents(acceptableData, u));
@@ -120,11 +127,11 @@
 
                 // Otherwise take the data as-is and see if it contains anything that can
 		// be turned into a MOBY object by some MOB rule
-		System.err.println("Data appears to be text..."+text);
+		//System.err.println("Data appears to be text..."+text);
 		dataFound = getDataFromText(acceptableData, text);
             }  //end else if(text)
 	    else{
-		System.err.println("Cannot drop data into Seahawk targeted type, not a file or text");
+		System.err.println("Cannot drop data into Seahawk targeted type, not a Moby Java object, a file or convertible to text");
 	    }
         } catch (UnsupportedFlavorException ufe) {
             System.out.println("importMobyData: unsupported data flavor: "+ufe);
@@ -142,7 +149,7 @@
 				      Map<String,MobyPrimaryData> acceptableData,
 				      Map<String,MobyDataInstance> dataFound){
 	if(dataFound == null || dataFound.isEmpty()){
-	    System.err.println("Received data was blank, not sending to consumer");
+	    //System.err.println("Received data was blank, not sending to consumer");
 	    return false;
 	}
 	// Unambiguously found a MOBY object or object collection mapping for the pasted data 
@@ -151,7 +158,7 @@
 	    MobyPrimaryData dataTemplate = acceptableData.values().iterator().next();
 	    String dataName = acceptableData.keySet().iterator().next();
 
-	    System.err.println("Received data match 1 to 1, forwarding to " + receiver + " the data" +newData.toString());
+	    //System.err.println("Received data match 1 to 1, forwarding to " + receiver + " the data" +newData.toString());
 	    if(newData instanceof MobyDataObject){
 		if(dataTemplate instanceof MobyPrimaryDataSimple){
 		    receiver.consumeMobyObject(dataName, newData);
@@ -180,7 +187,7 @@
 	    
 	    return true;
 	}
-	System.err.println("Received data does not match 1 to 1, forwarding not yet implemented");
+	//System.err.println("Received data does not match 1 to 1, forwarding not yet implemented");
 	// Otherwise the user will have to choose the object to paste
 	// from the list of Moby Objects created from the pasted data
 	// TODO
@@ -219,7 +226,7 @@
 	    int itemCount = 1;
 	    MobyDataType desiredDataType = acceptableData.get(fieldName).getDataType();
 	    String dataTypeName = desiredDataType.getName();
-	    System.err.println("Checking text for field " + fieldName+ ", data type " + desiredDataType.getName());
+	    //System.err.println("Checking text for field " + fieldName+ ", data type " + desiredDataType.getName());
 
 	    // trivial data creations first ...
 	    if(dataTypeName.equals(MobyTags.MOBYSTRING)){
@@ -238,13 +245,13 @@
 		    text.matches("\\d+(?:\\.\\d*)?(?:[eE][\\-\\+]?\\d+)?")){
 		foundData.put(fieldName+"#"+itemCount++, new MobyDataFloat(text));
 	    }
-	    else if(dataTypeName.equals(MobyTags.MOBYFLOAT) && 
+	    else if(dataTypeName.equals(MobyTags.MOBYDATETIME) && 
 		    text.matches("(?:19|20)\\d\\d-(?:0[1-9]|1[012])-([012][1-9]|3[01])")){
 		foundData.put(fieldName+"#"+itemCount++, new MobyDataDateTime(text));
 	    }
 	    MobyDataObject[] textData = client.getMobyObjects(text, desiredDataType);
 	    if(textData != null){
-		System.err.println("Found " + textData.length + " matches");
+		//System.err.println("Found " + textData.length + " matches");
 		for(MobyDataObject object: textData){
 		    foundData.put(fieldName+"#"+itemCount++, object);
 		}
@@ -364,4 +371,13 @@
     public void lostOwnership(Clipboard clipboard, Transferable contents){
 	
     }
+
+    protected DataFlavor getMobyFlavor(DataFlavor[] flavors) {
+        for (DataFlavor flavor: flavors) {
+            if (flavor.getRepresentationClass().getName().equals(MobyDataInstance.class.getName())) {
+                return flavor;
+            }
+        }
+        return null;
+    }
 }

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/SeahawkTransferable.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/SeahawkTransferable.java	2010/04/23 05:17:28	1.2
+++ /home/repository/moby/moby-live/Java/src/main/ca/ucalgary/seahawk/gui/SeahawkTransferable.java	2010/05/13 16:03:59	1.3
@@ -6,7 +6,11 @@
 import org.biomoby.shared.MobyPrimaryData;
 import org.biomoby.shared.data.*;
 
+import foxtrot.Task; // for fancy synchronous GUI handling on data paste to external apps
+import foxtrot.Worker;
+
 import java.awt.datatransfer.*;
+import javax.swing.JDialog;
 import javax.swing.JOptionPane;
 import java.util.*;
 import java.util.logging.*;
@@ -42,14 +46,20 @@
 	if(!isDataFlavorSupported(flavor)){
 	    return null;
 	}
+	MobyDataInstance field = pane.getDraggedData();
+
 	Class clazz = flavor.getRepresentationClass();
-	
+	// Data drag within the JVM, pass the object as-is
+	if(clazz.getName().equals(MobyDataInstance.class.getName())){
+	    //System.err.println("Drag producing "+field);
+	    return field;
+	}
+
 	// Essentially, we let the user drag Moby objects or submembers rather than substrings of members' text
 	// so that we can make simple rules for decomposition in programming-by-example.  OR an XSLT "DEM" rules
 	// could be applied to provide a particular format e.g. DNASequence -> FastA
 	String fieldString = null;
 	String transformRuleURI = null;
-	MobyDataInstance field = pane.getDraggedData();
 	// If primitive, do straight toString();
 	if(field instanceof MobyDataString){
 	    fieldString = ((MobyDataObject) field).getValue().toString();
@@ -78,23 +88,44 @@
 	    try{
 		// Is there a DEM rule we can use?
 		boolean ONLY_IF_HAS_URN = true;
-		String[] formatChoices = getTextClient().getPossibleTextTypes((MobyPrimaryData) field, ONLY_IF_HAS_URN);
+		final String[] formatChoices = getTextClient().getPossibleTextTypes((MobyPrimaryData) field, ONLY_IF_HAS_URN);
 		String targetTextType = null;
 		if(formatChoices != null && formatChoices.length != 0){
 		    if(formatChoices.length == 1){
 			targetTextType = formatChoices[0];
 		    }
+		    else if(pane != null){
+			// We can't show a Java dialog to get the format choice, because this
+			// code is being launched from the event queue thread (would cause deadlock, 
+			// even using foxtrot due to JDialog GUI needing painting, which also happens on this thread).
+			// Instead, the MobyContentPane will offload the selection GUI to the Web browser
+			// in some way and wait synchronously for the user's choice.
+			try{
+			    targetTextType = (String) Worker.post(new Task(){
+				    public Object run() throws Exception{
+					int chosenIndex = pane.getExportOption(formatChoices); 
+					if(chosenIndex < 0 || chosenIndex >= formatChoices.length){
+					    return null;
+					}
+					return formatChoices[chosenIndex];
+				    }});
+			    logger.log(Level.INFO, "Chose "+formatChoices[0]+" instead of "+formatChoices[1]);
+			} catch (Exception e){
+			    logger.log(Level.WARNING, "Failed to get user's format choice from data paste dialog, "+
+				       "using default choice "+formatChoices[0], e);
+			    // fallback on failure is to take first type
+			    targetTextType = formatChoices[0];  
+			}
+			if(targetTextType == null){
+			    // user cancel
+			    return null;
+			}
+		    }
 		    else{
-			// Give the user a choice
+			logger.log(Level.INFO, 
+				   "No content pane to coordinate paste format choice, choosing default of " + 
+				   formatChoices[0]);
 			targetTextType = formatChoices[0];
-			logger.log(Level.INFO, "Chose "+formatChoices[0]+" instead of "+formatChoices[1]);
-// 			targetTextType = (String) JOptionPane.showInputDialog(pane
-// 									      "Please select a text format to convert to",
-// 									      "Format choices available",
-// 									      JOptionPane.QUESTION_MESSAGE,
-// 									      null,
-// 									      formatChoices,
-// 									      formatChoices[0]);
 		    }
 		}
 		fieldString = getTextClient().getText(field, targetTextType);
@@ -110,8 +141,8 @@
 	    transformRuleURI = "urn:lsid:bioxml.info:mobyLoweringSchemaMapping:ID2String";
 	}
 
-	System.err.println("\""+fieldString+"\": Requested transfer class was " + clazz.getName() + 
-			   ", MIME type was " + flavor.getPrimaryType() + "/" + flavor.getSubType());
+	//System.err.println("\""+fieldString+"\": Requested transfer class was " + clazz.getName() + 
+	//		   ", MIME type was " + flavor.getPrimaryType() + "/" + flavor.getSubType());
 	lastPastedValue = fieldString;
 	lastTransformRuleURI = transformRuleURI;
 	return (new StringSelection(fieldString)).getTransferData(flavor);
@@ -123,6 +154,8 @@
 	for(DataFlavor stringFlavor: (new StringSelection("boo")).getTransferDataFlavors()){
 	    flavors.add(stringFlavor);
 	}
+	DataFlavor mobyJavaObjectFlavor = new DataFlavor(MobyDataInstance.class, MobyDataInstance.class.getName());
+	flavors.add(mobyJavaObjectFlavor);
 	// We can create a file or text out of just about anything
 	//if(flavor.equals(DataFlavor.javaFileListFlavor) ||
 	//   flavor.isFlavorTextType()){
@@ -147,6 +180,6 @@
     public void exportDone(){
 	//let the PBE system record that the particular value is being copied
 	pane.exportDone(lastPastedValue, lastTransformRuleURI);
-	System.err.println("Export complete");
+	//System.err.println("Export complete");
     }
 }




More information about the MOBY-guts mailing list