View Javadoc

1   package gov.nasa.pds.ltdt.gui.util;
2   
3   import gov.nasa.pds.ltdt.gui.MainWindow;
4   
5   import java.awt.datatransfer.DataFlavor;
6   import java.awt.datatransfer.StringSelection;
7   import java.awt.datatransfer.Transferable;
8   import java.util.ArrayList;
9   import java.util.List;
10  
11  import javax.swing.JComponent;
12  import javax.swing.JList;
13  import javax.swing.JOptionPane;
14  import javax.swing.TransferHandler;
15  
16  /***
17   * Handles data reformatting when a keyword got drag  and drop from the
18   * Dictionary List panel to Template Editing panel.
19   * @author jwang
20   *
21   */
22  public class ListTransferHandler extends TransferHandler{
23  	
24  	private int[] indices = null;
25  	private MainWindow window;
26      
27  	public ListTransferHandler (MainWindow window) {
28  		this.window = window;
29  	}
30  
31  	/***
32  	 * construct the string to be copied over to the target panel. 
33  	 * The object type will be added with END OBJECT
34  	 * The element type will be copied over straight
35  	 * @param component
36  	 * @return reformatted output string
37  	 */
38      protected String exportString(JComponent component) {
39          JList list = (JList)component;
40          
41          Object[] values = list.getSelectedValues();
42          
43          StringBuffer buf = new StringBuffer();
44          StringBuffer message = new StringBuffer();
45  
46          for (int i=0; i<values.length; i++) {       
47          	String val = (String)values[i];
48          	buf.append(val == null ? "" : DictionaryUtility.list2editblock(window, val, message));
49          	
50   
51          }
52     
53    /*      
54         	if (message.length()>0) {
55      		int response =
56      		JOptionPane.showConfirmDialog(window, message.toString(), "Undefined keyword found", 
57      				JOptionPane.YES_NO_OPTION);
58      		
59      		switch (response) {
60      		case JOptionPane.YES_OPTION:  			
61      			break;
62      		case JOptionPane.NO_OPTION:  			
63      			return "";
64      		case JOptionPane.CLOSED_OPTION:   			
65      			return "";
66      		
67      		}
68      	}
69    */     
70          
71          window.editorPane.jta.grabFocus();
72  
73         	return buf.toString();
74          
75      }
76  
77  
78      /***
79       * get a Transferable object
80       */
81      protected Transferable createTransferable(JComponent component) {
82      	
83      	// StringSelection creates a Transferable 
84          return new StringSelection(exportString(component));
85      }
86     
87      /***
88       * returns the type of action performed by this transfer
89       */
90      public int getSourceActions(JComponent component) {
91      	return COPY;
92      }
93      
94      /***
95       * flags true if the transferred data is indeed a string
96       */
97      public boolean canImport(JComponent component, DataFlavor[] flavors) {
98          for (int i = 0; i < flavors.length; i++) {
99              if (DataFlavor.stringFlavor.equals(flavors[i])) {
100                 return true;
101             }
102         }
103         return false;
104     }
105 
106 }