KickJava   Java API By Example, From Geeks To Geeks.

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


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.ISiteCategory;
19 import org.eclipse.pde.internal.core.isite.ISiteCategoryDefinition;
20 import org.eclipse.pde.internal.core.isite.ISiteDescription;
21 import org.eclipse.pde.internal.core.isite.ISiteFeature;
22 import org.eclipse.pde.internal.core.isite.ISiteModel;
23 import org.eclipse.pde.internal.ui.PDEPlugin;
24 import org.eclipse.pde.internal.ui.PDEUIMessages;
25 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
26 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
27 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
28 import org.eclipse.pde.internal.ui.editor.PDESection;
29 import org.eclipse.pde.internal.ui.parts.FormEntry;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.dnd.Clipboard;
32 import org.eclipse.swt.dnd.RTFTransfer;
33 import org.eclipse.swt.dnd.TextTransfer;
34 import org.eclipse.swt.dnd.Transfer;
35 import org.eclipse.swt.dnd.TransferData;
36 import org.eclipse.swt.layout.GridData;
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 CategoryDetailsSection extends PDESection implements IFormPart,
44         IPartSelectionListener {
45
46     private static final String JavaDoc PROPERTY_DESC = "desc"; //$NON-NLS-1$
47

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

50     private static final String JavaDoc PROPERTY_TYPE = "type"; //$NON-NLS-1$
51

52     private ISiteCategoryDefinition fCurrentCategoryDefinition;
53
54     private FormEntry fDescriptionText;
55
56     private FormEntry fLabelText;
57
58     private FormEntry fNameText;
59
60     public CategoryDetailsSection(PDEFormPage page, Composite parent) {
61         this(page, parent, PDEUIMessages.CategoryDetails_title,
62                 PDEUIMessages.CategoryDetails_sectionDescription, SWT.NULL);
63
64     }
65
66     public CategoryDetailsSection(PDEFormPage page, Composite parent,
67             String JavaDoc title, String JavaDoc desc, int toggleStyle) {
68         super(page, parent, Section.DESCRIPTION | toggleStyle);
69         getSection().setText(title);
70         getSection().setDescription(desc);
71         createClient(getSection(), page.getManagedForm().getToolkit());
72     }
73
74     private boolean alreadyExists(String JavaDoc name) {
75         ISiteCategoryDefinition[] defs = fCurrentCategoryDefinition.getModel()
76                 .getSite().getCategoryDefinitions();
77         for (int i = 0; i < defs.length; i++) {
78             ISiteCategoryDefinition def = defs[i];
79             if (def == fCurrentCategoryDefinition)
80                 continue;
81             String JavaDoc dname = def.getName();
82             if (dname != null && dname.equals(name))
83                 return true;
84         }
85         return false;
86     }
87
88     private void applyValue(String JavaDoc property, String JavaDoc value) throws CoreException {
89         if (fCurrentCategoryDefinition == null)
90             return;
91         if (property.equals(PROPERTY_NAME)){
92             String JavaDoc oldName = fCurrentCategoryDefinition.getName();
93             fCurrentCategoryDefinition.setName(value);
94             bringFeatures(oldName);
95         } else if (property.equals(PROPERTY_TYPE))
96             fCurrentCategoryDefinition.setLabel(value);
97         else if (property.equals(PROPERTY_DESC)) {
98             if (value == null || value.length() == 0) {
99                 fCurrentCategoryDefinition.setDescription(null);
100             } else {
101                 ISiteDescription siteDesc = fCurrentCategoryDefinition
102                         .getDescription();
103                 if (siteDesc == null) {
104                     siteDesc = fCurrentCategoryDefinition.getModel()
105                             .getFactory().createDescription(
106                                     fCurrentCategoryDefinition);
107                     siteDesc.setText(value);
108                     fCurrentCategoryDefinition.setDescription(siteDesc);
109                 } else {
110                     siteDesc.setText(value);
111                 }
112             }
113         }
114     }
115
116     public void cancelEdit() {
117         fNameText.cancelEdit();
118         fLabelText.cancelEdit();
119         fDescriptionText.cancelEdit();
120         super.cancelEdit();
121     }
122
123     public boolean canPaste(Clipboard clipboard) {
124         TransferData[] types = clipboard.getAvailableTypes();
125         Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(),
126                 RTFTransfer.getInstance() };
127         for (int i = 0; i < types.length; i++) {
128             for (int j = 0; j < transfers.length; j++) {
129                 if (transfers[j].isSupportedType(types[i]))
130                     return true;
131             }
132         }
133         return false;
134     }
135
136     private void clearField(String JavaDoc property) {
137         if (property.equals(PROPERTY_NAME))
138             fNameText.setValue(null, true);
139         else if (property.equals(PROPERTY_TYPE))
140             fLabelText.setValue(null, true);
141         else if (property.equals(PROPERTY_DESC))
142             fDescriptionText.setValue(null, true);
143     }
144
145     private void clearFields() {
146         fNameText.setValue(null, true);
147         fLabelText.setValue(null, true);
148         fDescriptionText.setValue(null, true);
149     }
150
151     public void commit(boolean onSave) {
152         fNameText.commit();
153         fLabelText.commit();
154         fDescriptionText.commit();
155
156         super.commit(onSave);
157     }
158
159     public void createClient(Section section, FormToolkit toolkit) {
160
161         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
162         Composite container = toolkit.createComposite(section);
163         container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
164         container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
165
166         GridData data = new GridData(GridData.FILL_BOTH);
167         section.setLayoutData(data);
168         
169         fNameText = new FormEntry(container, toolkit, PDEUIMessages.CategoryDetails_name, null, false);
170         fNameText.setFormEntryListener(new FormEntryAdapter(this) {
171             public void textValueChanged(FormEntry text) {
172                 try {
173                     if (text.getValue().length() <= 0
174                             || alreadyExists(text.getValue())) {
175                         setValue(PROPERTY_NAME);
176                         String JavaDoc message = PDEUIMessages.CategoryDetails_alreadyExists;
177                         MessageDialog
178                                 .openError(
179                                         PDEPlugin.getActiveWorkbenchShell(),
180                                         PDEUIMessages.CategoryDetails_alreadyExists_title,
181                                         message);
182                     } else {
183                         applyValue(PROPERTY_NAME, text.getValue());
184                     }
185                 } catch (CoreException e) {
186                     PDEPlugin.logException(e);
187                 }
188             }
189         });
190         limitTextWidth(fNameText);
191         fNameText.setEditable(isEditable());
192
193         fLabelText = new FormEntry(container, toolkit, PDEUIMessages.CategoryDetails_label, null, false);
194         fLabelText.setFormEntryListener(new FormEntryAdapter(this) {
195             public void textValueChanged(FormEntry text) {
196                 try {
197                     applyValue(PROPERTY_TYPE, text.getValue());
198                 } catch (CoreException e) {
199                     PDEPlugin.logException(e);
200                 }
201             }
202         });
203         limitTextWidth(fLabelText);
204         fLabelText.setEditable(isEditable());
205
206         fDescriptionText = new FormEntry(container, toolkit, PDEUIMessages.CategoryDetails_desc, SWT.WRAP | SWT.MULTI);
207         fDescriptionText.getText().setLayoutData(
208                 new GridData(GridData.FILL_BOTH));
209
210         fDescriptionText.getLabel().setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
211         
212         fDescriptionText.setFormEntryListener(new FormEntryAdapter(this) {
213             public void textValueChanged(FormEntry text) {
214                 try {
215                     applyValue(PROPERTY_DESC, text.getValue());
216                 } catch (CoreException e) {
217                     PDEPlugin.logException(e);
218                 }
219             }
220         });
221         limitTextWidth(fDescriptionText);
222         fDescriptionText.setEditable(isEditable());
223         toolkit.paintBordersFor(container);
224         section.setClient(container);
225
226         ISiteModel model = (ISiteModel) getPage().getModel();
227         if (model != null)
228             model.addModelChangedListener(this);
229     }
230
231     public void dispose() {
232         ISiteModel model = (ISiteModel) getPage().getModel();
233         if (model != null)
234             model.removeModelChangedListener(this);
235         super.dispose();
236     }
237
238     private void limitTextWidth(FormEntry entry) {
239         GridData gd = (GridData) entry.getText().getLayoutData();
240         gd.widthHint = 30;
241     }
242
243     public void modelChanged(IModelChangedEvent e) {
244         markStale();
245     }
246
247     private void bringFeatures(String JavaDoc oldCategory){
248         ISiteFeature[] siteFeatures = fCurrentCategoryDefinition.getModel()
249                 .getSite().getFeatures();
250         for (int i = 0; i < siteFeatures.length; i++) {
251             ISiteCategory[] categories = siteFeatures[i].getCategories();
252             for (int c = 0; c < categories.length; c++) {
253                 if (oldCategory.equals(categories[c].getName())) {
254                     try {
255                         categories[c].setName(fCurrentCategoryDefinition
256                                 .getName());
257                     } catch (CoreException ce) {
258                     }
259                 }
260             }
261         }
262     }
263     public void refresh() {
264         if (fCurrentCategoryDefinition == null) {
265             clearFields();
266             super.refresh();
267             return;
268         }
269         setValue(PROPERTY_NAME);
270         setValue(PROPERTY_TYPE);
271         setValue(PROPERTY_DESC);
272         super.refresh();
273     }
274
275     public void selectionChanged(IFormPart part, ISelection selection) {
276         if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
277             Object JavaDoc o = ((IStructuredSelection) selection).getFirstElement();
278             if (o instanceof ISiteCategoryDefinition) {
279                 fCurrentCategoryDefinition = (ISiteCategoryDefinition) o;
280             } else {
281                 fCurrentCategoryDefinition = null;
282             }
283         } else
284             fCurrentCategoryDefinition = null;
285         refresh();
286     }
287
288     public void setFocus() {
289         if (fNameText != null)
290             fNameText.getText().setFocus();
291     }
292
293     private void setValue(String JavaDoc property) {
294         if (fCurrentCategoryDefinition == null) {
295             clearField(property);
296         } else {
297             if (property.equals(PROPERTY_NAME))
298                 fNameText.setValue(fCurrentCategoryDefinition.getName(), true);
299             else if (property.equals(PROPERTY_TYPE))
300                 fLabelText.setValue(fCurrentCategoryDefinition.getLabel(), true);
301             else if (property.equals(PROPERTY_DESC)) {
302                 ISiteDescription siteDesc = fCurrentCategoryDefinition
303                         .getDescription();
304                 if (siteDesc == null) {
305                     clearField(property);
306                 } else {
307                     fDescriptionText.setValue(siteDesc.getText(), true);
308                 }
309
310             }
311         }
312     }
313 }
314
Popular Tags