1 11 package org.eclipse.debug.internal.ui.actions; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 import org.eclipse.debug.internal.ui.DebugUIPlugin; 22 import org.eclipse.debug.internal.ui.viewers.AsynchronousTreeViewer; 23 import org.eclipse.debug.internal.ui.viewers.ILabelResult; 24 import org.eclipse.debug.ui.IDebugView; 25 import org.eclipse.jface.action.IAction; 26 import org.eclipse.jface.dialogs.MessageDialog; 27 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 28 import org.eclipse.jface.operation.IRunnableWithProgress; 29 import org.eclipse.jface.viewers.ContentViewer; 30 import org.eclipse.jface.viewers.ISelection; 31 import org.eclipse.swt.SWTError; 32 import org.eclipse.swt.dnd.Clipboard; 33 import org.eclipse.swt.dnd.DND; 34 import org.eclipse.swt.dnd.TextTransfer; 35 import org.eclipse.swt.dnd.Transfer; 36 import org.eclipse.swt.widgets.Control; 37 import org.eclipse.swt.widgets.Tree; 38 import org.eclipse.swt.widgets.TreeItem; 39 40 public class CopyToClipboardActionDelegate extends AbstractDebugActionDelegate { 41 42 private ContentViewer fViewer; 43 private static final String TAB = "\t"; private static final String EMPTY_STRING = ""; private static final String SEPARATOR = "line.separator"; 47 50 protected boolean initialize(IAction action, ISelection selection) { 51 if (!isInitialized()) { 52 IDebugView adapter= (IDebugView)getView().getAdapter(IDebugView.class); 53 if (adapter != null) { 54 if (adapter.getViewer() instanceof ContentViewer) { 55 setViewer((ContentViewer) adapter.getViewer()); 56 } 57 adapter.setAction(getActionId(), action); 58 } 59 return super.initialize(action, selection); 60 } 61 return false; 62 } 63 64 protected String getActionId() { 65 return IDebugView.COPY_ACTION; 66 } 67 68 73 protected void append(ILabelResult item, StringBuffer buffer, int indent) { 74 for (int i= 0; i < indent; i++) { 75 buffer.append(TAB); 76 } 77 String [] labels = item.getLabels(); 78 int count = labels.length; 79 if(count > 0) { 80 for (int i = 0; i < count; i++) { 81 String text = labels[i]; 82 if(text != null && !text.trim().equals(EMPTY_STRING)) { 83 buffer.append(text+TAB); 84 } 85 } 86 buffer.append(System.getProperty(SEPARATOR)); 87 } 88 } 89 90 93 public void run(final IAction action) { 94 if (fViewer instanceof AsynchronousTreeViewer) { 95 final AsynchronousTreeViewer atv = (AsynchronousTreeViewer)fViewer; 97 List selectedItems = getPrunedSelection(); 98 TreeItem[]items = (TreeItem[]) selectedItems.toArray(new TreeItem[selectedItems.size()]); 99 final Object [] elements = new Object [selectedItems.size()]; 100 for (int i = 0; i < elements.length; i++) { 101 elements[i] = items[i].getData(); 102 } 103 if (elements.length > 0) { 104 final StringBuffer buffer = new StringBuffer (); 105 IRunnableWithProgress runnable = new IRunnableWithProgress() { 106 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 107 monitor.beginTask(DebugUIPlugin.removeAccelerators(action.getText()), elements.length * 2); 108 for (int i = 0; i < elements.length; i++) { 109 Object element = elements[i]; 110 if (element != null) { 111 SubProgressMonitor sub = new SubProgressMonitor(monitor, 1); 112 List results = atv.buildLabels(sub, element, ""); if (monitor.isCanceled()) { 114 throw new InterruptedException (); 115 } 116 performCopy(results, buffer); 118 monitor.worked(1); 119 } else { 120 monitor.worked(2); 121 } 122 } 123 monitor.done(); 124 } 125 }; 126 ProgressMonitorDialog dialog = new ProgressMonitorDialog(fViewer.getControl().getShell()); 127 dialog.setCancelable(true); 128 try { 129 dialog.run(true, true, runnable); 130 } catch (InvocationTargetException e) { 131 DebugUIPlugin.log(e); 132 return; 133 } catch (InterruptedException e) { 134 return; 135 } 136 TextTransfer plainTextTransfer = TextTransfer.getInstance(); 137 Clipboard clipboard= new Clipboard(fViewer.getControl().getDisplay()); 138 try { 139 doCopy(clipboard, plainTextTransfer, buffer); 140 } finally { 141 clipboard.dispose(); 142 } 143 } 144 145 } 146 } 147 148 protected void performCopy(List results, StringBuffer buffer) { 149 final Iterator iter= results.iterator(); 150 if (results.size() > 0) { 151 int topLevel = ((ILabelResult)results.get(0)).getDepth(); 152 while (iter.hasNext()) { 153 ILabelResult result = (ILabelResult) iter.next(); 154 append(result, buffer, result.getDepth() - topLevel); 155 } 156 } 157 } 158 159 protected void doCopy(Clipboard clipboard, TextTransfer plainTextTransfer, StringBuffer buffer) { 160 try { 161 clipboard.setContents( 162 new String []{buffer.toString()}, 163 new Transfer[]{plainTextTransfer}); 164 } catch (SWTError e){ 165 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) { 166 throw e; 167 } 168 if (MessageDialog.openQuestion(fViewer.getControl().getShell(), ActionMessages.CopyToClipboardActionDelegate_Problem_Copying_to_Clipboard_1, ActionMessages.CopyToClipboardActionDelegate_There_was_a_problem_when_accessing_the_system_clipboard__Retry__2)) { doCopy(clipboard, plainTextTransfer, buffer); 170 } 171 } 172 } 173 174 178 protected List getPrunedSelection() { 179 Control control = fViewer.getControl(); 180 List items = new ArrayList (); 181 if (control instanceof Tree) { 182 Tree tree = (Tree) control; 183 TreeItem[] selection = tree.getSelection(); 184 185 for (int i = 0; i < selection.length; i++) { 186 TreeItem item = selection[i]; 187 if (isEnabledFor(item.getData())) { 188 if (walkHierarchy(item, items)) { 189 items.add(item); 190 } 191 } 192 } 193 } 194 return items; 195 } 196 197 201 protected boolean walkHierarchy(TreeItem item, List elements) { 202 TreeItem parent= item.getParentItem(); 203 if (parent == null) { 204 return true; 205 } 206 if (elements.contains(parent)) { 207 return false; 208 } 209 return walkHierarchy(parent, elements); 210 } 211 212 215 protected boolean shouldAppendChildren(TreeItem item) { 216 return item.getExpanded(); 217 } 218 219 protected ContentViewer getViewer() { 220 return fViewer; 221 } 222 223 protected void setViewer(ContentViewer viewer) { 224 fViewer = viewer; 225 } 226 229 protected void doAction(Object element) { 230 } 232 } 233 | Popular Tags |