001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.views.bookmarkexplorer;
011:
012: import java.util.List;
013:
014: import org.eclipse.core.resources.IMarker;
015: import org.eclipse.jface.dialogs.MessageDialog;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.jface.viewers.StructuredViewer;
018: import org.eclipse.osgi.util.NLS;
019: import org.eclipse.swt.SWTError;
020: import org.eclipse.swt.dnd.DND;
021: import org.eclipse.swt.dnd.TextTransfer;
022: import org.eclipse.swt.dnd.Transfer;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.internal.views.bookmarkexplorer.BookmarkMessages;
025: import org.eclipse.ui.part.MarkerTransfer;
026:
027: /**
028: * Copies one or more bookmark(s) to the clipboard.
029: */
030: class CopyBookmarkAction extends BookmarkAction {
031:
032: /**
033: * Creates the action.
034: *
035: * @param bookmarkNavigator the view
036: */
037: public CopyBookmarkAction(BookmarkNavigator bookmarkNavigator) {
038: super (bookmarkNavigator, BookmarkMessages.CopyBookmark_text);
039: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
040: IBookmarkHelpContextIds.COPY_BOOKMARK_ACTION);
041: setEnabled(false);
042: }
043:
044: /**
045: * Performs this action.
046: */
047: public void run() {
048: // Get the selected markers
049: BookmarkNavigator bookmarkNavigator = getView();
050: StructuredViewer viewer = bookmarkNavigator.getViewer();
051: IStructuredSelection selection = (IStructuredSelection) viewer
052: .getSelection();
053: if (selection.isEmpty()) {
054: return;
055: }
056: List list = selection.toList();
057: IMarker[] markers = new IMarker[list.size()];
058: list.toArray(markers);
059:
060: setClipboard(markers, createBookmarkReport(markers));
061: }
062:
063: /**
064: * Updates enablement based on the current selection
065: */
066: public void selectionChanged(IStructuredSelection sel) {
067: setEnabled(!sel.isEmpty());
068: }
069:
070: private void setClipboard(IMarker[] markers, String markerReport) {
071: try {
072: // Place the markers on the clipboard
073: Object[] data = new Object[] { markers, markerReport };
074: Transfer[] transferTypes = new Transfer[] {
075: MarkerTransfer.getInstance(),
076: TextTransfer.getInstance() };
077:
078: // set the clipboard contents
079: getView().getClipboard().setContents(data, transferTypes);
080: } catch (SWTError e) {
081: if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
082: throw e;
083: }
084: if (MessageDialog
085: .openQuestion(
086: getView().getShell(),
087: BookmarkMessages.CopyToClipboardProblemDialog_title,
088: BookmarkMessages.CopyToClipboardProblemDialog_message)) {
089: setClipboard(markers, markerReport);
090: }
091: }
092: }
093:
094: private String createBookmarkReport(IMarker[] markers) {
095: String report = ""; //$NON-NLS-1$
096:
097: //write header
098: report += BookmarkMessages.ColumnDescription_header + '\t';
099: 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,
111: String.valueOf(line));
112: report += System.getProperty("line.separator"); //$NON-NLS-1$
113: }
114:
115: return report;
116: }
117: }
|