1 11 package org.eclipse.debug.internal.ui.viewers.model; 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.debug.internal.ui.DebugUIPlugin; 21 import org.eclipse.debug.internal.ui.actions.AbstractDebugActionDelegate; 22 import org.eclipse.debug.internal.ui.actions.ActionMessages; 23 import org.eclipse.debug.internal.ui.viewers.model.provisional.ILabelUpdate; 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 VirtualCopyToClipboardActionDelegate 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"; private boolean fDone = false; 47 48 51 protected boolean initialize(IAction action, ISelection selection) { 52 if (!isInitialized()) { 53 IDebugView adapter= (IDebugView)getView().getAdapter(IDebugView.class); 54 if (adapter != null) { 55 if (adapter.getViewer() instanceof ContentViewer) { 56 setViewer((ContentViewer) adapter.getViewer()); 57 } 58 adapter.setAction(getActionId(), action); 59 } 60 return super.initialize(action, selection); 61 } 62 return false; 63 } 64 65 protected String getActionId() { 66 return IDebugView.COPY_ACTION; 67 } 68 69 74 protected void append(TreeItem item, StringBuffer buffer, int indent) { 75 for (int i= 0; i < indent; i++) { 76 buffer.append(TAB); 77 } 78 String [] labels = null; 79 if (((InternalTreeModelViewer)fViewer).isShowColumns()) { 80 labels = new String [((InternalTreeModelViewer)fViewer).getPresentationContext().getColumns().length]; 81 for (int i = 0; i < labels.length; i++) { 82 labels[i] = item.getText(i); 83 } 84 } else { 85 labels = new String []{item.getText()}; 86 } 87 int count = labels.length; 88 if(count > 0) { 89 for (int i = 0; i < count; i++) { 90 String text = labels[i]; 91 if(text != null && !text.trim().equals(EMPTY_STRING)) { 92 buffer.append(text+TAB); 93 } 94 } 95 buffer.append(System.getProperty(SEPARATOR)); 96 } 97 } 98 99 102 public void run(final IAction action) { 103 if (fViewer instanceof InternalTreeModelViewer) { 104 InternalTreeModelViewer viewer = (InternalTreeModelViewer) fViewer; 105 fDone = false; 106 final Object lock = new Object (); 107 ProgressMonitorDialog dialog = new ProgressMonitorDialog(fViewer.getControl().getShell()); 108 final IProgressMonitor monitor = dialog.getProgressMonitor(); 109 dialog.setCancelable(true); 110 111 boolean queued = false; 112 ILabelUpdateListener listener = new ILabelUpdateListener() { 113 public void labelUpdatesComplete() { 114 synchronized (lock) { 115 fDone = true; 116 lock.notifyAll(); 117 } 118 } 119 public void labelUpdatesBegin() { 120 } 121 public void labelUpdateStarted(ILabelUpdate update) { 122 } 123 public void labelUpdateComplete(ILabelUpdate update) { 124 monitor.worked(1); 125 } 126 }; 127 viewer.addLabelUpdateListener(listener); 128 queued = viewer.populateVitrualItems(); 129 130 if (queued) { 131 IRunnableWithProgress runnable = new IRunnableWithProgress() { 132 public void run(final IProgressMonitor m) throws InvocationTargetException , InterruptedException { 133 m.beginTask(DebugUIPlugin.removeAccelerators(getAction().getText()), IProgressMonitor.UNKNOWN); 134 synchronized (lock) { 135 if (!fDone) { 136 lock.wait(); 137 } 138 } 139 m.done(); 140 } 141 }; 142 try { 143 dialog.run(true, true, runnable); 144 } catch (InvocationTargetException e) { 145 DebugUIPlugin.log(e); 146 return; 147 } catch (InterruptedException e) { 148 return; 149 } 150 } 151 152 viewer.removeLabelUpdateListener(listener); 153 if (!monitor.isCanceled()) { 154 List roots = getPrunedSelection(); 155 Iterator iterator = roots.iterator(); 156 StringBuffer buffer = new StringBuffer (); 157 while (iterator.hasNext()) { 158 TreeItem item = (TreeItem) iterator.next(); 159 copy(item, buffer, 0); 160 } 161 TextTransfer plainTextTransfer = TextTransfer.getInstance(); 162 Clipboard clipboard= new Clipboard(fViewer.getControl().getDisplay()); 163 try { 164 doCopy(clipboard, plainTextTransfer, buffer); 165 } finally { 166 clipboard.dispose(); 167 } 168 } 169 } 170 } 171 172 176 protected void copy(TreeItem item, StringBuffer buffer, int indent) { 177 append(item, buffer, indent); 178 if (item.getExpanded()) { 179 TreeItem[] items = item.getItems(); 180 for (int i = 0; i < items.length; i++) { 181 copy(items[i], buffer, indent + 1); 182 } 183 } 184 } 185 186 protected void doCopy(Clipboard clipboard, TextTransfer plainTextTransfer, StringBuffer buffer) { 187 try { 188 clipboard.setContents( 189 new String []{buffer.toString()}, 190 new Transfer[]{plainTextTransfer}); 191 } catch (SWTError e){ 192 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) { 193 throw e; 194 } 195 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); 197 } 198 } 199 } 200 201 205 protected List getPrunedSelection() { 206 Control control = fViewer.getControl(); 207 List items = new ArrayList (); 208 if (control instanceof Tree) { 209 Tree tree = (Tree) control; 210 TreeItem[] selection = tree.getSelection(); 211 212 for (int i = 0; i < selection.length; i++) { 213 TreeItem item = selection[i]; 214 if (isEnabledFor(item.getData())) { 215 if (walkHierarchy(item, items)) { 216 items.add(item); 217 } 218 } 219 } 220 } 221 return items; 222 } 223 224 228 protected boolean walkHierarchy(TreeItem item, List elements) { 229 TreeItem parent= item.getParentItem(); 230 if (parent == null) { 231 return true; 232 } 233 if (elements.contains(parent)) { 234 return false; 235 } 236 return walkHierarchy(parent, elements); 237 } 238 239 protected ContentViewer getViewer() { 240 return fViewer; 241 } 242 243 protected void setViewer(ContentViewer viewer) { 244 fViewer = viewer; 245 } 246 249 protected void doAction(Object element) { 250 } 252 } 253 | Popular Tags |