KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > properties > CopyPropertyAction


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.properties;
13
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.swt.SWTError;
17 import org.eclipse.swt.dnd.Clipboard;
18 import org.eclipse.swt.dnd.DND;
19 import org.eclipse.swt.dnd.TextTransfer;
20 import org.eclipse.swt.dnd.Transfer;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.views.properties.PropertiesMessages;
23
24 /**
25  * Copies a property to the clipboard.
26  */

27 /*package*/class CopyPropertyAction extends PropertySheetAction {
28     /**
29      * System clipboard
30      */

31     private Clipboard clipboard;
32
33     /**
34      * Creates the action.
35      *
36      * @param viewer the viewer
37      * @param name the name
38      * @param clipboard the clipboard
39      */

40     public CopyPropertyAction(PropertySheetViewer viewer, String JavaDoc name,
41             Clipboard clipboard) {
42         super(viewer, name);
43         PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
44                 IPropertiesHelpContextIds.COPY_PROPERTY_ACTION);
45         this.clipboard = clipboard;
46     }
47
48     /**
49      * Performs this action.
50      */

51     public void run() {
52         // Get the selected property
53
IStructuredSelection selection = (IStructuredSelection) getPropertySheet()
54                 .getSelection();
55         if (selection.isEmpty()) {
56             return;
57         }
58         // Assume single selection
59
IPropertySheetEntry entry = (IPropertySheetEntry) selection
60                 .getFirstElement();
61
62         // Place text on the clipboard
63
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
64         buffer.append(entry.getDisplayName());
65         buffer.append("\t"); //$NON-NLS-1$
66
buffer.append(entry.getValueAsString());
67
68         setClipboard(buffer.toString());
69     }
70
71     /**
72      * Updates enablement based on the current selection.
73      *
74      * @param sel the selection
75      */

76     public void selectionChanged(IStructuredSelection sel) {
77         setEnabled(!sel.isEmpty());
78     }
79
80     private void setClipboard(String JavaDoc text) {
81         try {
82             Object JavaDoc[] data = new Object JavaDoc[] { text };
83             Transfer[] transferTypes = new Transfer[] { TextTransfer
84                     .getInstance() };
85             clipboard.setContents(data, transferTypes);
86         } catch (SWTError e) {
87             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
88                 throw e;
89             }
90             if (MessageDialog.openQuestion(getPropertySheet().getControl()
91                     .getShell(), PropertiesMessages.CopyToClipboardProblemDialog_title,
92                     PropertiesMessages.CopyToClipboardProblemDialog_message)) {
93                 setClipboard(text);
94             }
95         }
96     }
97 }
98
99
Popular Tags