1 11 package org.eclipse.debug.internal.ui.actions.breakpointGroups; 12 13 import java.util.Iterator ; 14 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.debug.core.model.IBreakpoint; 17 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 18 import org.eclipse.debug.internal.ui.views.breakpoints.BreakpointsView; 19 import org.eclipse.jface.dialogs.MessageDialog; 20 import org.eclipse.jface.viewers.ILabelProvider; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.jface.viewers.IStructuredSelection; 23 import org.eclipse.jface.viewers.StructuredViewer; 24 import org.eclipse.swt.SWTError; 25 import org.eclipse.swt.dnd.Clipboard; 26 import org.eclipse.swt.dnd.DND; 27 import org.eclipse.swt.dnd.TextTransfer; 28 import org.eclipse.swt.dnd.Transfer; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.views.navigator.LocalSelectionTransfer; 31 32 35 public class CopyBreakpointsAction extends BreakpointSelectionAction { 36 37 40 private Clipboard clipboard; 41 42 45 private PasteBreakpointsAction pasteAction; 46 47 53 public CopyBreakpointsAction(BreakpointsView view, Clipboard clipboard) { 54 super(BreakpointGroupMessages.CopyBreakpointsAction_0, view); 55 Assert.isNotNull(clipboard); 56 this.clipboard = clipboard; 57 setToolTipText(BreakpointGroupMessages.CopyBreakpointsAction_1); 58 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugHelpContextIds.COPY_BREAKPOINTS_ACTION); 59 } 60 61 68 public CopyBreakpointsAction(BreakpointsView view, Clipboard clipboard, PasteBreakpointsAction pasteAction) { 69 this(view, clipboard); 70 this.pasteAction = pasteAction; 71 } 72 73 78 public void run() { 79 IStructuredSelection selection = getStructuredSelection(); 80 Object [] objects = selection.toArray(); 81 StringBuffer buffer = new StringBuffer (); 82 ILabelProvider labelProvider = (ILabelProvider) ((StructuredViewer)getBreakpointsView().getViewer()).getLabelProvider(); 83 for (int i = 0; i < objects.length; i++) { 84 Object object = objects[i]; 85 if (i > 0) { 86 buffer.append("\n"); } 88 buffer.append(labelProvider.getText(object)); 89 } 90 setClipboard(selection, buffer.toString()); 91 92 if (pasteAction != null && pasteAction.getStructuredSelection() != null) 95 pasteAction.selectionChanged(pasteAction.getStructuredSelection()); 96 } 97 98 103 private void setClipboard(ISelection selection, String text) { 104 try { 105 LocalSelectionTransfer.getInstance().setSelection(selection); 106 LocalSelectionTransfer.getInstance().setSelectionSetTime(System.currentTimeMillis()); 107 clipboard.setContents(new Object [] {selection, text}, new Transfer[] {LocalSelectionTransfer.getInstance(), TextTransfer.getInstance()}); 108 } catch (SWTError e) { 109 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) 110 throw e; 111 if (MessageDialog.openQuestion( 112 getBreakpointsView().getSite().getShell(), BreakpointGroupMessages.CopyBreakpointsAction_2, 113 BreakpointGroupMessages.CopyBreakpointsAction_3)) { 114 setClipboard(selection, text); 115 } 116 } 117 } 118 119 122 protected boolean updateSelection(IStructuredSelection selection) { 123 if (selection.isEmpty()) { 124 return false; 125 } 126 Iterator iterator = selection.iterator(); 127 while (iterator.hasNext()) { 128 if (!(iterator.next() instanceof IBreakpoint)) { 129 return false; 130 } 131 } 132 return true; 133 } 134 135 } 136 137 | Popular Tags |