KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > HandlerSection


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.feature;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.pde.core.IModelChangedEvent;
15 import org.eclipse.pde.internal.core.ifeature.IFeature;
16 import org.eclipse.pde.internal.core.ifeature.IFeatureInstallHandler;
17 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
18 import org.eclipse.pde.internal.ui.PDEPlugin;
19 import org.eclipse.pde.internal.ui.PDEUIMessages;
20 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
21 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
22 import org.eclipse.pde.internal.ui.editor.PDESection;
23 import org.eclipse.pde.internal.ui.parts.FormEntry;
24 import org.eclipse.swt.dnd.Clipboard;
25 import org.eclipse.swt.dnd.RTFTransfer;
26 import org.eclipse.swt.dnd.TextTransfer;
27 import org.eclipse.swt.dnd.Transfer;
28 import org.eclipse.swt.dnd.TransferData;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.ui.forms.widgets.FormToolkit;
32 import org.eclipse.ui.forms.widgets.Section;
33
34 public class HandlerSection extends PDESection {
35     private FormEntry fLibraryText;
36
37     private FormEntry fHandlerText;
38
39     public HandlerSection(FeatureAdvancedPage page, Composite parent) {
40         super(page, parent, Section.DESCRIPTION);
41         getSection().setText(PDEUIMessages.FeatureEditor_HandlerSection_title);
42         getSection().setDescription(PDEUIMessages.FeatureEditor_HandlerSection_desc);
43         createClient(getSection(), page.getManagedForm().getToolkit());
44     }
45
46     public boolean canPaste(Clipboard clipboard) {
47         TransferData[] types = clipboard.getAvailableTypes();
48         Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(),
49                 RTFTransfer.getInstance() };
50         for (int i = 0; i < types.length; i++) {
51             for (int j = 0; j < transfers.length; j++) {
52                 if (transfers[j].isSupportedType(types[i]))
53                     return true;
54             }
55         }
56         return false;
57     }
58
59     public void commit(boolean onSave) {
60         fLibraryText.commit();
61         fHandlerText.commit();
62         super.commit(onSave);
63     }
64
65     public void createClient(Section section, FormToolkit toolkit) {
66         
67         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
68         GridData data = new GridData(GridData.FILL_HORIZONTAL);
69         section.setLayoutData(data);
70         
71         Composite container = toolkit.createComposite(section);
72         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
73         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
74         
75         IFeatureModel model = (IFeatureModel) getPage().getModel();
76         final IFeature feature = model.getFeature();
77
78         fLibraryText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_HandlerSection_library, null, false);
79         fLibraryText.setFormEntryListener(new FormEntryAdapter(this) {
80             public void textValueChanged(FormEntry text) {
81                 try {
82                     setLibrary(feature, text.getValue());
83                 } catch (CoreException e) {
84                     PDEPlugin.logException(e);
85                 }
86             }
87         });
88         fHandlerText = new FormEntry(container, toolkit, PDEUIMessages.FeatureEditor_HandlerSection_handler, null, false);
89         fHandlerText.setFormEntryListener(new FormEntryAdapter(this) {
90             public void textValueChanged(FormEntry text) {
91                 try {
92                     setHandler(feature, text.getValue());
93                 } catch (CoreException e) {
94                     PDEPlugin.logException(e);
95                 }
96             }
97         });
98
99         toolkit.paintBordersFor(container);
100         section.setClient(container);
101         initialize();
102     }
103
104     private void setLibrary(IFeature feature, String JavaDoc value)
105             throws CoreException {
106         IFeatureInstallHandler handler = getHandler(feature);
107         handler.setLibrary(value);
108     }
109
110     private void setHandler(IFeature feature, String JavaDoc value)
111             throws CoreException {
112         IFeatureInstallHandler handler = getHandler(feature);
113         handler.setHandlerName(value);
114     }
115
116     private IFeatureInstallHandler getHandler(IFeature feature)
117             throws CoreException {
118         IFeatureInstallHandler handler = feature.getInstallHandler();
119         if (handler == null) {
120             handler = feature.getModel().getFactory().createInstallHandler();
121             feature.setInstallHandler(handler);
122         }
123         return handler;
124     }
125
126     public void dispose() {
127         IFeatureModel model = (IFeatureModel) getPage().getModel();
128         if (model != null)
129             model.removeModelChangedListener(this);
130         super.dispose();
131     }
132
133     public void initialize() {
134         IFeatureModel model = (IFeatureModel) getPage().getModel();
135         refresh();
136         if (model.isEditable() == false) {
137             fLibraryText.getText().setEditable(false);
138             fHandlerText.getText().setEditable(false);
139         }
140         model.addModelChangedListener(this);
141     }
142
143     public void modelChanged(IModelChangedEvent e) {
144         if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
145             markStale();
146         }
147     }
148
149     public void setFocus() {
150         if (fLibraryText != null)
151             fLibraryText.getText().setFocus();
152     }
153
154     private void setIfDefined(FormEntry formText, Object JavaDoc value) {
155         if (value != null)
156             formText.setValue(value.toString(), true);
157         else
158             formText.setValue(null, true);
159     }
160
161     public void refresh() {
162         IFeatureModel model = (IFeatureModel) getPage().getModel();
163         IFeature feature = model.getFeature();
164         IFeatureInstallHandler handler = feature.getInstallHandler();
165         if (handler != null) {
166             setIfDefined(fLibraryText, handler.getLibrary());
167             setIfDefined(fHandlerText, handler.getHandlerName());
168         }
169         super.refresh();
170     }
171
172     public void cancelEdit() {
173         fLibraryText.cancelEdit();
174         fHandlerText.cancelEdit();
175         super.cancelEdit();
176     }
177 }
178
Popular Tags