KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > FeatureDetailsSection


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.site;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.pde.core.IModelChangedEvent;
18 import org.eclipse.pde.internal.core.isite.ISiteFeature;
19 import org.eclipse.pde.internal.core.isite.ISiteModel;
20 import org.eclipse.pde.internal.ui.PDEPlugin;
21 import org.eclipse.pde.internal.ui.PDEUIMessages;
22 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
23 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
24 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
25 import org.eclipse.pde.internal.ui.editor.PDESection;
26 import org.eclipse.pde.internal.ui.parts.FormEntry;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.dnd.Clipboard;
29 import org.eclipse.swt.dnd.RTFTransfer;
30 import org.eclipse.swt.dnd.TextTransfer;
31 import org.eclipse.swt.dnd.Transfer;
32 import org.eclipse.swt.dnd.TransferData;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.widgets.Button;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.ui.forms.IFormPart;
39 import org.eclipse.ui.forms.IPartSelectionListener;
40 import org.eclipse.ui.forms.widgets.FormToolkit;
41 import org.eclipse.ui.forms.widgets.Section;
42
43 public class FeatureDetailsSection extends PDESection implements IFormPart,
44         IPartSelectionListener {
45
46     private static final String JavaDoc PROPERTY_TYPE = "type"; //$NON-NLS-1$
47

48     private static final String JavaDoc PROPERTY_URL = "url"; //$NON-NLS-1$
49

50     private ISiteFeature fCurrentSiteFeature;
51
52     private Button fPatchCheckBox;
53
54     private FormEntry fUrlText;
55
56     public FeatureDetailsSection(PDEFormPage page, Composite parent) {
57         this(page, parent, PDEUIMessages.FeatureDetailsSection_title,
58                 PDEUIMessages.FeatureDetailsSection_desc, SWT.NULL);
59     }
60
61     public FeatureDetailsSection(PDEFormPage page, Composite parent,
62             String JavaDoc title, String JavaDoc desc, int toggleStyle) {
63         super(page, parent, Section.DESCRIPTION | toggleStyle);
64         getSection().setText(title);
65         getSection().setDescription(desc);
66         createClient(getSection(), page.getManagedForm().getToolkit());
67     }
68
69     private void applyIsPatch(boolean patch) throws CoreException {
70         if (fCurrentSiteFeature == null)
71             return;
72         fCurrentSiteFeature.setIsPatch(patch);
73     }
74
75     private void applyValue(String JavaDoc property, String JavaDoc value) throws CoreException {
76         if (fCurrentSiteFeature == null)
77             return;
78         if (property.equals(PROPERTY_URL))
79             fCurrentSiteFeature.setURL(value);
80         else if (property.equals(PROPERTY_TYPE))
81             fCurrentSiteFeature.setType(value);
82     }
83
84     public void cancelEdit() {
85         fUrlText.cancelEdit();
86         super.cancelEdit();
87     }
88
89     public boolean canPaste(Clipboard clipboard) {
90         TransferData[] types = clipboard.getAvailableTypes();
91         Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(),
92                 RTFTransfer.getInstance() };
93         for (int i = 0; i < types.length; i++) {
94             for (int j = 0; j < transfers.length; j++) {
95                 if (transfers[j].isSupportedType(types[i]))
96                     return true;
97             }
98         }
99         return false;
100     }
101
102     private void clearField(String JavaDoc property) {
103         if (property.equals(PROPERTY_URL))
104             fUrlText.setValue(null, true);
105     }
106
107     private void clearFields() {
108         fUrlText.setValue(null, true);
109         fPatchCheckBox.setSelection(false);
110     }
111
112     public void commit(boolean onSave) {
113         try {
114             applyIsPatch(fPatchCheckBox.getSelection());
115         } catch (CoreException e) {
116             PDEPlugin.logException(e);
117         }
118
119         super.commit(onSave);
120     }
121
122     public void createClient(Section section, FormToolkit toolkit) {
123         
124         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
125         Composite container = toolkit.createComposite(section);
126         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
127         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
128
129         GridData data = new GridData(GridData.FILL_HORIZONTAL);
130         section.setLayoutData(data);
131         
132         fUrlText = new FormEntry(container, toolkit, PDEUIMessages.FeatureDetailsSection_url, null, false);
133         fUrlText.setFormEntryListener(new FormEntryAdapter(this) {
134             public void textValueChanged(FormEntry text) {
135                 try {
136                     if (text.getValue().length() <= 0) {
137                         setValue(PROPERTY_URL);
138                         MessageDialog
139                                 .openError(
140                                         PDEPlugin.getActiveWorkbenchShell(),
141                                         PDEUIMessages.FeatureDetailsSection_requiredURL_title,
142                                                 PDEUIMessages.FeatureDetailsSection_requiredURL);
143                     } else {
144                         applyValue(PROPERTY_URL, text.getValue());
145                     }
146                 } catch (CoreException e) {
147                     PDEPlugin.logException(e);
148                 }
149             }
150         });
151         limitTextWidth(fUrlText);
152         fUrlText.getText().setEnabled(false);
153         
154         createPatchButton(toolkit, container);
155
156         toolkit.paintBordersFor(container);
157         section.setClient(container);
158
159         ISiteModel model = (ISiteModel) getPage().getModel();
160         if (model != null)
161             model.addModelChangedListener(this);
162     }
163
164     private void createPatchButton(FormToolkit toolkit, Composite container) {
165         fPatchCheckBox = toolkit.createButton(container, PDEUIMessages.FeatureDetailsSection_patch, SWT.CHECK);
166         fPatchCheckBox.addSelectionListener(new SelectionAdapter() {
167             public void widgetSelected(SelectionEvent e) {
168                 try {
169                     applyIsPatch(fPatchCheckBox.getSelection());
170                 } catch (CoreException ce) {
171                     PDEPlugin.logException(ce);
172                 }
173             }
174         });
175         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
176         gd.horizontalSpan = 2;
177         fPatchCheckBox.setLayoutData(gd);
178         fPatchCheckBox.setEnabled(isEditable());
179     }
180
181     public void dispose() {
182         ISiteModel model = (ISiteModel) getPage().getModel();
183         if (model != null)
184             model.removeModelChangedListener(this);
185         super.dispose();
186     }
187
188     private void limitTextWidth(FormEntry entry) {
189         GridData gd = (GridData) entry.getText().getLayoutData();
190         gd.widthHint = 30;
191     }
192
193     public void modelChanged(IModelChangedEvent e) {
194         markStale();
195     }
196
197     public void refresh() {
198         if (fCurrentSiteFeature == null) {
199             clearFields();
200             super.refresh();
201             return;
202         }
203         setValue(PROPERTY_URL);
204         setValue(PROPERTY_TYPE);
205         fPatchCheckBox.setSelection(fCurrentSiteFeature.isPatch());
206         super.refresh();
207     }
208
209     public void selectionChanged(IFormPart part, ISelection selection) {
210         if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
211             Object JavaDoc o = ((IStructuredSelection) selection).getFirstElement();
212             if (o instanceof SiteFeatureAdapter) {
213                 fCurrentSiteFeature = ((SiteFeatureAdapter) o).feature;
214             } else {
215                 fCurrentSiteFeature = null;
216             }
217         } else
218             fCurrentSiteFeature = null;
219         refresh();
220     }
221
222     public void setFocus() {
223         if (fUrlText != null)
224             fUrlText.getText().setFocus();
225     }
226
227     private void setValue(String JavaDoc property) {
228         if (fCurrentSiteFeature == null) {
229             clearField(property);
230         } else {
231             if (property.equals(PROPERTY_URL))
232                 fUrlText.setValue(fCurrentSiteFeature.getURL(), true);
233         }
234     }
235 }
236
Popular Tags