001: /*******************************************************************************
002: * Copyright (c) 2007 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.internal.provisional.views.markers;
011:
012: import org.eclipse.core.commands.ExecutionEvent;
013: import org.eclipse.core.resources.IMarker;
014: import org.eclipse.swt.dnd.TextTransfer;
015: import org.eclipse.swt.dnd.Transfer;
016: import org.eclipse.ui.internal.provisional.views.markers.api.MarkerField;
017: import org.eclipse.ui.part.MarkerTransfer;
018:
019: /**
020: * MarkerCopyHandler is the handler for the copy action when the markers view is
021: * selected.
022: *
023: * @since 3.4
024: *
025: */
026: public class MarkerCopyHandler extends MarkerViewHandler {
027:
028: /*
029: * (non-Javadoc)
030: *
031: * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
032: */
033: public Object execute(ExecutionEvent event) {
034: ExtendedMarkersView view = getView(event);
035: if (view == null)
036: return null;
037:
038: setClipboard(view);
039: return this ;
040: }
041:
042: /**
043: * Set the workbench clipboard for the markers.
044: *
045: * @param markers
046: * @param markerReport
047: */
048: private void setClipboard(ExtendedMarkersView view) {
049:
050: IMarker[] markers = view.getSelectedMarkers();
051:
052: String markerReport = createMarkerReport(view, markers);
053:
054: // Place the markers on the clipboard
055: Object[] data;
056: Transfer[] transferTypes;
057: if (markerReport == null) {
058: data = new Object[] { markers };
059: transferTypes = new Transfer[] { MarkerTransfer
060: .getInstance() };
061: } else {
062: data = new Object[] { markers, markerReport };
063: transferTypes = new Transfer[] {
064: MarkerTransfer.getInstance(),
065: TextTransfer.getInstance() };
066: }
067:
068: view.getClipboard().setContents(data, transferTypes);
069:
070: }
071:
072: /**
073: * Creates a plain-text report of the selected markers based on predefined
074: * properties.
075: *
076: * @param view the view being copied
077: * @param markers
078: * @return the marker report
079: */
080: String createMarkerReport(ExtendedMarkersView view,
081: IMarker[] markers) {
082: StringBuffer report = new StringBuffer();
083:
084: MarkerField[] fields = view.getVisibleFields();
085:
086: // create header
087: for (int i = 0; i < fields.length; i++) {
088: report.append(fields[i].getColumnHeaderText());
089: if (i == fields.length - 1) {
090: report.append(Character.LINE_SEPARATOR);
091: } else {
092: report.append('\t');
093: }
094: }
095:
096: for (int i = 0; i < markers.length; i++) {
097:
098: for (int j = 0; j < fields.length; j++) {
099: report.append(fields[j].getMarkerValue(markers[i]));
100: if (j == fields.length - 1) {
101: report.append(Character.LINE_SEPARATOR);
102: } else {
103: report.append('\t');
104: }
105: }
106: }
107:
108: return report.toString();
109: }
110:
111: }
|