KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > bookmarkexplorer > CopyBookmarkAction


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
12 package org.eclipse.ui.views.bookmarkexplorer;
13
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.StructuredViewer;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.swt.SWTError;
22 import org.eclipse.swt.dnd.DND;
23 import org.eclipse.swt.dnd.TextTransfer;
24 import org.eclipse.swt.dnd.Transfer;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.internal.views.bookmarkexplorer.BookmarkMessages;
27 import org.eclipse.ui.part.MarkerTransfer;
28
29 /**
30  * Copies one or more bookmark(s) to the clipboard.
31  */

32 class CopyBookmarkAction extends BookmarkAction {
33
34     /**
35      * Creates the action.
36      *
37      * @param bookmarkNavigator the view
38      */

39     public CopyBookmarkAction(BookmarkNavigator bookmarkNavigator) {
40         super(bookmarkNavigator, BookmarkMessages.CopyBookmark_text);
41         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
42                 IBookmarkHelpContextIds.COPY_BOOKMARK_ACTION);
43         setEnabled(false);
44     }
45
46     /**
47      * Performs this action.
48      */

49     public void run() {
50         // Get the selected markers
51
BookmarkNavigator bookmarkNavigator = getView();
52         StructuredViewer viewer = bookmarkNavigator.getViewer();
53         IStructuredSelection selection = (IStructuredSelection) viewer
54                 .getSelection();
55         if (selection.isEmpty()) {
56             return;
57         }
58         List JavaDoc list = selection.toList();
59         IMarker[] markers = new IMarker[list.size()];
60         list.toArray(markers);
61
62         setClipboard(markers, createBookmarkReport(markers));
63     }
64
65     /**
66      * Updates enablement based on the current selection
67      */

68     public void selectionChanged(IStructuredSelection sel) {
69         setEnabled(!sel.isEmpty());
70     }
71
72     private void setClipboard(IMarker[] markers, String JavaDoc markerReport) {
73         try {
74             // Place the markers on the clipboard
75
Object JavaDoc[] data = new Object JavaDoc[] { markers, markerReport };
76             Transfer[] transferTypes = new Transfer[] {
77                     MarkerTransfer.getInstance(), TextTransfer.getInstance() };
78
79             // set the clipboard contents
80
getView().getClipboard().setContents(data, transferTypes);
81         } catch (SWTError e) {
82             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
83                 throw e;
84             }
85             if (MessageDialog
86                     .openQuestion(
87                             getView().getShell(),
88                             BookmarkMessages.CopyToClipboardProblemDialog_title, BookmarkMessages.CopyToClipboardProblemDialog_message)) {
89                 setClipboard(markers, markerReport);
90             }
91         }
92     }
93
94     private String JavaDoc createBookmarkReport(IMarker[] markers) {
95         String JavaDoc report = ""; //$NON-NLS-1$
96

97         //write header
98
report += BookmarkMessages.ColumnDescription_header + '\t';
99         report += BookmarkMessages.ColumnResource_header + '\t';
100         report += BookmarkMessages.ColumnFolder_header + '\t';
101         report += BookmarkMessages.ColumnLocation_header;
102         report += System.getProperty("line.separator"); //$NON-NLS-1$
103

104         //write markers
105
for (int i = 0; i < markers.length; i++) {
106             report += MarkerUtil.getMessage(markers[i]) + '\t';
107             report += MarkerUtil.getResourceName(markers[i]) + '\t';
108             report += MarkerUtil.getContainerName(markers[i]) + '\t';
109             int line = MarkerUtil.getLineNumber(markers[i]);
110             report += NLS.bind(BookmarkMessages.LineIndicator_text, String.valueOf(line));
111             report += System.getProperty("line.separator"); //$NON-NLS-1$
112
}
113
114         return report;
115     }
116 }
117
118
Popular Tags