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.markers.internal;
011:
012: import org.eclipse.core.resources.IMarker;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.jface.dialogs.ErrorDialog;
015: import org.eclipse.jface.dialogs.MessageDialog;
016: import org.eclipse.jface.viewers.ISelectionProvider;
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.swt.SWTError;
019: import org.eclipse.swt.dnd.Clipboard;
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.ISharedImages;
024: import org.eclipse.ui.IWorkbenchPart;
025: import org.eclipse.ui.PlatformUI;
026: import org.eclipse.ui.part.MarkerTransfer;
027:
028: /**
029: * Copies one or more marker to the clipboard.
030: */
031: public class ActionCopyMarker extends MarkerSelectionProviderAction {
032:
033: private IWorkbenchPart part;
034:
035: private Clipboard clipboard;
036:
037: private IField[] properties;
038:
039: /**
040: * Creates the action.
041: *
042: * @param part
043: * @param provider
044: */
045: public ActionCopyMarker(IWorkbenchPart part,
046: ISelectionProvider provider) {
047: super (provider, MarkerMessages.copyAction_title);
048: this .part = part;
049: setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
050: .getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
051: setEnabled(false);
052: }
053:
054: /**
055: * Sets the clipboard that the marker(s) will be copied to.
056: *
057: * @param clipboard
058: * the clipboard
059: */
060: void setClipboard(Clipboard clipboard) {
061: this .clipboard = clipboard;
062: }
063:
064: /**
065: * Sets the properties to be added to the plain-text marker report that will
066: * be copied to the clipboard.
067: *
068: * @param properties
069: */
070: void setProperties(IField[] properties) {
071: this .properties = properties;
072: }
073:
074: /**
075: * Copies the selected IMarker objects to the clipboard. If properties have
076: * been set, also copies a plain-text report of the selected markers to the
077: * clipboard.
078: */
079: public void run() {
080: IMarker[] markers = getSelectedMarkers();
081: setClipboard(markers, createMarkerReport(markers));
082: }
083:
084: /*
085: * (non-Javadoc)
086: *
087: * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
088: */
089: public void selectionChanged(IStructuredSelection selection) {
090: setEnabled(Util.allConcreteSelection(selection));
091: }
092:
093: private void setClipboard(IMarker[] markers, String markerReport) {
094: try {
095: // Place the markers on the clipboard
096: Object[] data;
097: Transfer[] transferTypes;
098: if (markerReport == null) {
099: data = new Object[] { markers };
100: transferTypes = new Transfer[] { MarkerTransfer
101: .getInstance() };
102: } else {
103: data = new Object[] { markers, markerReport };
104: transferTypes = new Transfer[] {
105: MarkerTransfer.getInstance(),
106: TextTransfer.getInstance() };
107: }
108:
109: clipboard.setContents(data, transferTypes);
110: } catch (SWTError e) {
111: if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
112: throw e;
113: }
114: if (MessageDialog
115: .openQuestion(
116: part.getSite().getShell(),
117: MarkerMessages.CopyToClipboardProblemDialog_title,
118: MarkerMessages.CopyToClipboardProblemDialog_message)) {
119: setClipboard(markers, markerReport);
120: }
121: }
122: }
123:
124: /**
125: * Creates a plain-text report of the selected markers based on predefined
126: * properties.
127: *
128: * @param rawMarkers
129: * @return the marker report
130: */
131: String createMarkerReport(IMarker[] rawMarkers) {
132: ConcreteMarker[] markers;
133: try {
134: markers = MarkerList.createMarkers(rawMarkers);
135: } catch (CoreException e) {
136: ErrorDialog.openError(part.getSite().getShell(),
137: MarkerMessages.Error, null, e.getStatus());
138: return ""; //$NON-NLS-1$
139: }
140:
141: StringBuffer report = new StringBuffer();
142:
143: final String NEWLINE = System.getProperty("line.separator"); //$NON-NLS-1$
144: final char DELIMITER = '\t';
145:
146: if (properties == null) {
147: return null;
148: }
149:
150: // create header
151: for (int i = 0; i < properties.length; i++) {
152: report.append(properties[i].getDescription());
153: if (i == properties.length - 1) {
154: report.append(NEWLINE);
155: } else {
156: report.append(DELIMITER);
157: }
158: }
159:
160: for (int i = 0; i < markers.length; i++) {
161: ConcreteMarker marker = markers[i];
162: for (int j = 0; j < properties.length; j++) {
163: report.append(properties[j].getValue(marker));
164: if (j == properties.length - 1) {
165: report.append(NEWLINE);
166: } else {
167: report.append(DELIMITER);
168: }
169: }
170: }
171:
172: return report.toString();
173: }
174: }
|