KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > model > VirtualCopyToClipboardActionDelegate


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.viewers.model;
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.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 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
private boolean fDone = false;
47     
48     /**
49      * @see AbstractDebugActionDelegate#initialize(IAction, ISelection)
50      */

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

74     protected void append(TreeItem item, StringBuffer JavaDoc buffer, int indent) {
75         for (int i= 0; i < indent; i++) {
76             buffer.append(TAB);
77         }
78         String JavaDoc[] labels = null;
79         if (((InternalTreeModelViewer)fViewer).isShowColumns()) {
80             labels = new String JavaDoc[((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 JavaDoc[]{item.getText()};
86         }
87         int count = labels.length;
88         if(count > 0) {
89             for (int i = 0; i < count; i++) {
90                 String JavaDoc 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     /**
100      * Do the specific action using the current selection.
101      */

102     public void run(final IAction action) {
103         if (fViewer instanceof InternalTreeModelViewer) {
104             InternalTreeModelViewer viewer = (InternalTreeModelViewer) fViewer;
105             fDone = false;
106             final Object JavaDoc lock = new Object JavaDoc();
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 JavaDoc, InterruptedException JavaDoc {
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 JavaDoc e) {
145                     DebugUIPlugin.log(e);
146                     return;
147                 } catch (InterruptedException JavaDoc e) {
148                     return;
149                 }
150             }
151             
152             viewer.removeLabelUpdateListener(listener);
153             if (!monitor.isCanceled()) {
154                 List JavaDoc roots = getPrunedSelection();
155                 Iterator JavaDoc iterator = roots.iterator();
156                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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     /**
173      * @param item
174      * @param buffer
175      */

176     protected void copy(TreeItem item, StringBuffer JavaDoc 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 JavaDoc buffer) {
187         try {
188             clipboard.setContents(
189                     new String JavaDoc[]{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)) { //
196
doCopy(clipboard, plainTextTransfer, buffer);
197             }
198         }
199     }
200     
201     /**
202      * Returns the selected items in the tree, pruning children
203      * if from selected parents.
204      */

205     protected List JavaDoc getPrunedSelection() {
206         Control control = fViewer.getControl();
207         List JavaDoc items = new ArrayList JavaDoc();
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     /**
225      * Returns whether the parent of the specified
226      * element is already contained in the collection.
227      */

228     protected boolean walkHierarchy(TreeItem item, List JavaDoc 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     /**
247      * @see AbstractDebugActionDelegate#doAction(Object)
248      */

249     protected void doAction(Object JavaDoc element) {
250         //not used
251
}
252 }
253
Popular Tags