KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > CopyToClipboardActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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;
12
13  
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
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 JavaDoc TAB = "\t"; //$NON-NLS-1$
44
private static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
45
private static final String JavaDoc SEPARATOR = "line.separator"; //$NON-NLS-1$
46

47     /**
48      * @see AbstractDebugActionDelegate#initialize(IAction, ISelection)
49      */

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 JavaDoc getActionId() {
65         return IDebugView.COPY_ACTION;
66     }
67
68     /**
69      * Appends the representation of the specified element (using the label provider and indent)
70      * to the buffer. For elements down to stack frames, children representations
71      * are append to the buffer as well.
72      */

73     protected void append(ILabelResult item, StringBuffer JavaDoc buffer, int indent) {
74         for (int i= 0; i < indent; i++) {
75             buffer.append(TAB);
76         }
77         String JavaDoc[] labels = item.getLabels();
78         int count = labels.length;
79         if(count > 0) {
80             for (int i = 0; i < count; i++) {
81                 String JavaDoc 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     /**
91      * Do the specific action using the current selection.
92      */

93     public void run(final IAction action) {
94         if (fViewer instanceof AsynchronousTreeViewer) {
95             // force labels to be created
96
final AsynchronousTreeViewer atv = (AsynchronousTreeViewer)fViewer;
97             List JavaDoc selectedItems = getPrunedSelection();
98             TreeItem[]items = (TreeItem[]) selectedItems.toArray(new TreeItem[selectedItems.size()]);
99             final Object JavaDoc[] elements = new Object JavaDoc[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 JavaDoc buffer = new StringBuffer JavaDoc();
105                 IRunnableWithProgress runnable = new IRunnableWithProgress() {
106                     public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
107                         monitor.beginTask(DebugUIPlugin.removeAccelerators(action.getText()), elements.length * 2);
108                         for (int i = 0; i < elements.length; i++) {
109                             Object JavaDoc element = elements[i];
110                             if (element != null) {
111                                 SubProgressMonitor sub = new SubProgressMonitor(monitor, 1);
112                                 List JavaDoc results = atv.buildLabels(sub, element, ""); //$NON-NLS-1$
113
if (monitor.isCanceled()) {
114                                     throw new InterruptedException JavaDoc();
115                                 }
116                                 // now copy to buffer
117
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 JavaDoc e) {
131                     DebugUIPlugin.log(e);
132                     return;
133                 } catch (InterruptedException JavaDoc 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 JavaDoc results, StringBuffer JavaDoc buffer) {
149         final Iterator JavaDoc 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 JavaDoc buffer) {
160         try {
161             clipboard.setContents(
162                     new String JavaDoc[]{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)) { //
169
doCopy(clipboard, plainTextTransfer, buffer);
170             }
171         }
172     }
173     
174     /**
175      * Returns the selected items in the tree, pruning children
176      * if from selected parents.
177      */

178     protected List JavaDoc getPrunedSelection() {
179         Control control = fViewer.getControl();
180         List JavaDoc items = new ArrayList JavaDoc();
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     /**
198      * Returns whether the parent of the specified
199      * element is already contained in the collection.
200      */

201     protected boolean walkHierarchy(TreeItem item, List JavaDoc 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     /**
213      * Only append children that are expanded in the tree viewer
214      */

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     /**
227      * @see AbstractDebugActionDelegate#doAction(Object)
228      */

229     protected void doAction(Object JavaDoc element) {
230         //not used
231
}
232 }
233
Popular Tags