KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > markers > internal > ActionCopyMarker


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.markers.internal;
13
14 import org.eclipse.core.resources.IMarker;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jface.dialogs.ErrorDialog;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.swt.SWTError;
21 import org.eclipse.swt.dnd.Clipboard;
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.ISharedImages;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.PlatformUI;
28 import org.eclipse.ui.part.MarkerTransfer;
29
30 /**
31  * Copies one or more marker to the clipboard.
32  */

33 public class ActionCopyMarker extends MarkerSelectionProviderAction {
34
35     private IWorkbenchPart part;
36
37     private Clipboard clipboard;
38
39     private IField[] properties;
40
41     /**
42      * Creates the action.
43      *
44      * @param part
45      * @param provider
46      */

47     public ActionCopyMarker(IWorkbenchPart part, ISelectionProvider provider) {
48         super(provider, MarkerMessages.copyAction_title);
49         this.part = part;
50         setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
51                 .getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
52         setEnabled(false);
53     }
54
55     /**
56      * Sets the clipboard that the marker(s) will be copied to.
57      *
58      * @param clipboard
59      * the clipboard
60      */

61     void setClipboard(Clipboard clipboard) {
62         this.clipboard = clipboard;
63     }
64
65     /**
66      * Sets the properties to be added to the plain-text marker report that will
67      * be copied to the clipboard.
68      *
69      * @param properties
70      */

71     void setProperties(IField[] properties) {
72         this.properties = properties;
73     }
74
75     /**
76      * Copies the selected IMarker objects to the clipboard. If properties have
77      * been set, also copies a plain-text report of the selected markers to the
78      * clipboard.
79      */

80     public void run() {
81         IMarker[] markers = getSelectedMarkers();
82         setClipboard(markers, createMarkerReport(markers));
83     }
84
85     /*
86      * (non-Javadoc)
87      *
88      * @see org.eclipse.ui.actions.SelectionProviderAction#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
89      */

90     public void selectionChanged(IStructuredSelection selection) {
91         setEnabled(Util.allConcreteSelection(selection));
92     }
93
94     private void setClipboard(IMarker[] markers, String JavaDoc markerReport) {
95         try {
96             // Place the markers on the clipboard
97
Object JavaDoc[] data;
98             Transfer[] transferTypes;
99             if (markerReport == null) {
100                 data = new Object JavaDoc[] { markers };
101                 transferTypes = new Transfer[] { MarkerTransfer.getInstance() };
102             } else {
103                 data = new Object JavaDoc[] { markers, markerReport };
104                 transferTypes = new Transfer[] { MarkerTransfer.getInstance(),
105                         TextTransfer.getInstance() };
106             }
107
108             clipboard.setContents(data, transferTypes);
109         } catch (SWTError e) {
110             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
111                 throw e;
112             }
113             if (MessageDialog.openQuestion(part.getSite().getShell(),
114                     MarkerMessages.CopyToClipboardProblemDialog_title,
115                     MarkerMessages.CopyToClipboardProblemDialog_message)) {
116                 setClipboard(markers, markerReport);
117             }
118         }
119     }
120
121     /**
122      * Creates a plain-text report of the selected markers based on predefined
123      * properties.
124      *
125      * @param rawMarkers
126      * @return the marker report
127      */

128     String JavaDoc createMarkerReport(IMarker[] rawMarkers) {
129         ConcreteMarker[] markers;
130         try {
131             markers = MarkerList.createMarkers(rawMarkers);
132         } catch (CoreException e) {
133             ErrorDialog.openError(part.getSite().getShell(),
134                     MarkerMessages.Error, null, e.getStatus());
135             return ""; //$NON-NLS-1$
136
}
137
138         StringBuffer JavaDoc report = new StringBuffer JavaDoc();
139
140         final String JavaDoc NEWLINE = System.getProperty("line.separator"); //$NON-NLS-1$
141
final char DELIMITER = '\t';
142
143         if (properties == null) {
144             return null;
145         }
146
147         // create header
148
for (int i = 0; i < properties.length; i++) {
149             report.append(properties[i].getDescription());
150             if (i == properties.length - 1) {
151                 report.append(NEWLINE);
152             } else {
153                 report.append(DELIMITER);
154             }
155         }
156
157         for (int i = 0; i < markers.length; i++) {
158             ConcreteMarker marker = markers[i];
159             for (int j = 0; j < properties.length; j++) {
160                 report.append(properties[j].getValue(marker));
161                 if (j == properties.length - 1) {
162                     report.append(NEWLINE);
163                 } else {
164                     report.append(DELIMITER);
165                 }
166             }
167         }
168
169         return report.toString();
170     }
171 }
172
Popular Tags