KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > breakpointGroups > CopyBreakpointsAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.actions.breakpointGroups;
12
13 import java.util.Iterator JavaDoc;
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 /**
33  * Action for copying the currently selected breakpoints to the clipboard.
34  */

35 public class CopyBreakpointsAction extends BreakpointSelectionAction {
36
37     /**
38      * System clipboard
39      */

40     private Clipboard clipboard;
41
42     /**
43      * Associated paste action. May be <code>null</code>
44      */

45     private PasteBreakpointsAction pasteAction;
46     
47     /**
48      * Creates a new action.
49      *
50      * @param shell the shell for any dialogs
51      * @param clipboard a platform clipboard
52      */

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     /**
62      * Creates a new action.
63      *
64      * @param shell the shell for any dialogs
65      * @param clipboard a platform clipboard
66      * @param pasteAction a paste action
67      */

68     public CopyBreakpointsAction(BreakpointsView view, Clipboard clipboard, PasteBreakpointsAction pasteAction) {
69         this(view, clipboard);
70         this.pasteAction = pasteAction;
71     }
72
73     /**
74      * The <code>CopyAction</code> implementation of this method defined
75      * on <code>IAction</code> copies the selected resources to the
76      * clipboard.
77      */

78     public void run() {
79         IStructuredSelection selection = getStructuredSelection();
80         Object JavaDoc[] objects = selection.toArray();
81         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
82         ILabelProvider labelProvider = (ILabelProvider) ((StructuredViewer)getBreakpointsView().getViewer()).getLabelProvider();
83         for (int i = 0; i < objects.length; i++) {
84             Object JavaDoc object = objects[i];
85             if (i > 0) {
86                 buffer.append("\n"); //$NON-NLS-1$
87
}
88             buffer.append(labelProvider.getText(object));
89         }
90         setClipboard(selection, buffer.toString());
91
92         // update the enablement of the paste action
93
// workaround since the clipboard does not suppot callbacks
94
if (pasteAction != null && pasteAction.getStructuredSelection() != null)
95             pasteAction.selectionChanged(pasteAction.getStructuredSelection());
96     }
97
98     /**
99      * Set the clipboard contents. Prompt to retry if clipboard is busy.
100      *
101      * @param selection the selection to copy to the clipboard
102      */

103     private void setClipboard(ISelection selection, String JavaDoc text) {
104         try {
105             LocalSelectionTransfer.getInstance().setSelection(selection);
106             LocalSelectionTransfer.getInstance().setSelectionSetTime(System.currentTimeMillis());
107             clipboard.setContents(new Object JavaDoc[] {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     /**
120      * Enables if one or more breakpoints are selected.
121      */

122     protected boolean updateSelection(IStructuredSelection selection) {
123         if (selection.isEmpty()) {
124             return false;
125         }
126         Iterator JavaDoc 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