KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > StructuredViewerSection


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.pde.internal.ui.editor;
12
13 import org.eclipse.jface.action.IMenuListener;
14 import org.eclipse.jface.action.IMenuManager;
15 import org.eclipse.jface.action.MenuManager;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.pde.internal.ui.parts.StructuredViewerPart;
19 import org.eclipse.swt.dnd.Clipboard;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Menu;
23 import org.eclipse.ui.forms.widgets.FormToolkit;
24
25 public abstract class StructuredViewerSection extends PDESection {
26     protected StructuredViewerPart fViewerPart;
27
28     /**
29      * Constructor for StructuredViewerSection.
30      * @param formPage
31      */

32     public StructuredViewerSection(PDEFormPage formPage, Composite parent, int style, String JavaDoc [] buttonLabels) {
33         this(formPage, parent, style, true, buttonLabels);
34     }
35
36     /**
37      * Constructor for StructuredViewerSection.
38      * @param formPage
39      */

40     public StructuredViewerSection(PDEFormPage formPage, Composite parent, int style, boolean titleBar, String JavaDoc [] buttonLabels) {
41         super(formPage, parent, style, titleBar);
42         fViewerPart = createViewerPart(buttonLabels);
43         fViewerPart.setMinimumSize(50, 50);
44         FormToolkit toolkit = formPage.getManagedForm().getToolkit();
45         createClient(getSection(), toolkit);
46     }
47
48     protected void createViewerPartControl(Composite parent, int style, int span, FormToolkit toolkit) {
49         fViewerPart.createControl(parent, style, span, toolkit);
50         MenuManager popupMenuManager = new MenuManager();
51         IMenuListener listener = new IMenuListener() {
52             public void menuAboutToShow(IMenuManager mng) {
53                 fillContextMenu(mng);
54             }
55         };
56         popupMenuManager.addMenuListener(listener);
57         popupMenuManager.setRemoveAllWhenShown(true);
58         Control control = fViewerPart.getControl();
59         Menu menu = popupMenuManager.createContextMenu(control);
60         control.setMenu(menu);
61     }
62     
63     protected Composite createClientContainer(Composite parent, int span, FormToolkit toolkit) {
64         Composite container = toolkit.createComposite(parent);
65         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, span));
66         return container;
67     }
68     
69     protected abstract StructuredViewerPart createViewerPart(String JavaDoc [] buttonLabels);
70     
71     protected void fillContextMenu(IMenuManager manager) {
72     }
73     
74     protected void buttonSelected(int index) {
75     }
76     
77     protected void doPaste() {
78         ISelection selection = getViewerSelection();
79         IStructuredSelection ssel = (IStructuredSelection)selection;
80         if (ssel.size()>1) return;
81         
82         Object JavaDoc target = ssel.getFirstElement();
83         
84         Clipboard clipboard = getPage().getPDEEditor().getClipboard();
85         ModelDataTransfer modelTransfer = ModelDataTransfer.getInstance();
86         Object JavaDoc [] objects = (Object JavaDoc[])clipboard.getContents(modelTransfer);
87         if (objects!=null) {
88             doPaste(target, objects);
89         }
90     }
91     
92     /* (non-Javadoc)
93      * @see org.eclipse.pde.internal.ui.editor.PDESection#canPaste(org.eclipse.swt.dnd.Clipboard)
94      */

95     public boolean canPaste(Clipboard clipboard) {
96         // TODO: MP: CCP: Checking clipboard data done incorrectly. See Bug 37223
97
ISelection selection = getViewerSelection();
98         IStructuredSelection ssel = (IStructuredSelection)selection;
99         if (ssel.size()>1) return false;
100             
101         Object JavaDoc target = ssel.getFirstElement();
102         ModelDataTransfer modelTransfer = ModelDataTransfer.getInstance();
103         Object JavaDoc [] objects = (Object JavaDoc[])clipboard.getContents(modelTransfer);
104         if (objects!=null && objects.length>0) {
105             return canPaste(target, objects);
106         }
107         return false;
108     }
109     
110     protected ISelection getViewerSelection() {
111         return fViewerPart.getViewer().getSelection();
112     }
113     
114     /**
115      * @param targetObject
116      * @param sourceObjects
117      */

118     protected void doPaste(Object JavaDoc targetObject, Object JavaDoc[] sourceObjects) {
119         // NO-OP
120
// Children will override to provide fuctionality
121
}
122     
123     /**
124      * @param targetObject
125      * @param sourceObjects
126      * @return
127      */

128     protected boolean canPaste(Object JavaDoc targetObject, Object JavaDoc[] sourceObjects) {
129         return false;
130     }
131     
132     public void setFocus() {
133         fViewerPart.getControl().setFocus();
134     }
135     
136     public StructuredViewerPart getStructuredViewerPart() {
137         return this.fViewerPart;
138     }
139 }
140
Popular Tags