KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > repo > CopyRepositoryNameAction


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 package org.eclipse.team.internal.ccvs.ui.repo;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.swt.dnd.*;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
21 import org.eclipse.team.internal.ccvs.ui.actions.CVSAction;
22
23 public class CopyRepositoryNameAction extends CVSAction {
24     public boolean isEnabled() {
25         return true;
26     }
27     public void execute(IAction action) {
28         ICVSRepositoryLocation[] locations = getSelectedRepositories();
29         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
30         for (int i = 0; i < locations.length; i++) {
31             buffer.append(locations[i].getLocation(true));
32             if (i < locations.length - 1) buffer.append("\n"); //$NON-NLS-1$
33
}
34         copyToClipbard(Display.getDefault(), buffer.toString());
35     }
36     protected ICVSRepositoryLocation[] getSelectedRepositories() {
37         ArrayList JavaDoc repositories = null;
38         IStructuredSelection selection = getSelection();
39         if (!selection.isEmpty()) {
40             repositories = new ArrayList JavaDoc();
41             Iterator JavaDoc elements = selection.iterator();
42             while (elements.hasNext()) {
43                 Object JavaDoc next = getAdapter(elements.next(), ICVSRepositoryLocation.class);
44                 if (next instanceof ICVSRepositoryLocation) {
45                     repositories.add(next);
46                     continue;
47                 }
48             }
49         }
50         if (repositories != null && !repositories.isEmpty()) {
51             ICVSRepositoryLocation[] result = new ICVSRepositoryLocation[repositories.size()];
52             repositories.toArray(result);
53             return result;
54         }
55         return new ICVSRepositoryLocation[0];
56     }
57     private void copyToClipbard(Display display, String JavaDoc str) {
58         Clipboard clipboard = new Clipboard(display);
59         clipboard.setContents(new String JavaDoc[] { str }, new Transfer[] { TextTransfer.getInstance()});
60         clipboard.dispose();
61     }
62 }
63
Popular Tags