KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > product > AboutSection


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.product;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.jface.window.Window;
15 import org.eclipse.pde.core.IModelChangedEvent;
16 import org.eclipse.pde.internal.core.iproduct.IAboutInfo;
17 import org.eclipse.pde.internal.core.iproduct.IProduct;
18 import org.eclipse.pde.internal.core.iproduct.IProductModel;
19 import org.eclipse.pde.internal.ui.PDEPlugin;
20 import org.eclipse.pde.internal.ui.PDEUIMessages;
21 import org.eclipse.pde.internal.ui.editor.EditorUtilities;
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.editor.validation.TextValidator;
27 import org.eclipse.pde.internal.ui.parts.FormEntry;
28 import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
29 import org.eclipse.pde.internal.ui.util.FileValidator;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.dnd.Clipboard;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.swt.widgets.Text;
37 import org.eclipse.ui.IActionBars;
38 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
39 import org.eclipse.ui.forms.events.HyperlinkEvent;
40 import org.eclipse.ui.forms.widgets.FormToolkit;
41 import org.eclipse.ui.forms.widgets.Section;
42 import org.eclipse.ui.model.WorkbenchContentProvider;
43 import org.eclipse.ui.model.WorkbenchLabelProvider;
44
45
46 public class AboutSection extends PDESection {
47
48     private FormEntry fImageEntry;
49     private FormEntry fTextEntry;
50     
51     private TextValidator fImageEntryValidator;
52
53     public AboutSection(PDEFormPage page, Composite parent) {
54         super(page, parent, Section.DESCRIPTION);
55         createClient(getSection(), page.getEditor().getToolkit());
56     }
57
58     /* (non-Javadoc)
59      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
60      */

61     protected void createClient(Section section, FormToolkit toolkit) {
62         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
63         GridData data = new GridData(GridData.FILL_BOTH);
64         data.widthHint = 300;
65         data.heightHint = 70;
66         section.setLayoutData(data);
67         
68         section.setText(PDEUIMessages.AboutSection_title);
69         section.setDescription(PDEUIMessages.AboutSection_desc);
70
71         Composite client = toolkit.createComposite(section);
72         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
73         client.setLayoutData(new GridData(GridData.FILL_BOTH));
74         
75         IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars();
76         fImageEntry = new FormEntry(client, toolkit, PDEUIMessages.AboutSection_image, PDEUIMessages.AboutSection_browse, isEditable());
77         fImageEntry.setEditable(isEditable());
78         // Create validator
79
fImageEntryValidator = new TextValidator(
80                 getManagedForm(), fImageEntry.getText(), getProject(), true) {
81             protected boolean validateControl() {
82                 return validateImageEntry();
83             }
84         };
85         fImageEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
86             public void textValueChanged(FormEntry entry) {
87                 getAboutInfo().setImagePath(entry.getValue());
88             }
89             public void browseButtonSelected(FormEntry entry) {
90                 handleBrowse();
91             }
92             public void linkActivated(HyperlinkEvent e) {
93                 EditorUtilities.openImage(fImageEntry.getValue(), getProduct().getDefiningPluginId());
94             }
95         });
96         
97         // Text field
98
// Create Text field UI
99
int style = SWT.MULTI | SWT.WRAP | SWT.V_SCROLL;
100         fTextEntry = new FormEntry(
101                 client,
102                 toolkit,
103                 PDEUIMessages.AboutSection_text,
104                 style);
105         // Configure Text widget
106
data = new GridData(GridData.FILL_BOTH);
107         data.horizontalSpan = 2;
108         // Needed to align vertically with form entry field and allow space
109
// for a possible field decoration
110
data.horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT;
111         fTextEntry.getText().setLayoutData(data);
112         // Configure Label widget to be aligned to the top-left
113
data = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
114         fTextEntry.getLabel().setLayoutData(data);
115         // Configure editability
116
fTextEntry.setEditable(isEditable());
117         // Create Text field listener
118
fTextEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
119             public void textValueChanged(FormEntry entry) {
120                 getAboutInfo().setText(entry.getValue());
121             }
122         });
123         
124         toolkit.paintBordersFor(client);
125         section.setClient(client);
126         // Register to be notified when the model changes
127
getModel().addModelChangedListener(this);
128     }
129     
130     /**
131      * @return
132      */

133     private boolean validateImageEntry() {
134         return EditorUtilities.imageEntrySizeDoesNotExceed(
135                 fImageEntryValidator, fImageEntry, getProduct(),
136                 500, 330, 250, 330);
137     }
138     
139     private void handleBrowse() {
140         ElementTreeSelectionDialog dialog =
141             new ElementTreeSelectionDialog(
142                 getSection().getShell(),
143                 new WorkbenchLabelProvider(),
144                 new WorkbenchContentProvider());
145                 
146         dialog.setValidator(new FileValidator());
147         dialog.setAllowMultiple(false);
148         dialog.setTitle(PDEUIMessages.AboutSection_imgTitle);
149         dialog.setMessage(PDEUIMessages.AboutSection_imgMessage);
150         dialog.addFilter(new FileExtensionFilter("gif")); //$NON-NLS-1$
151
dialog.setInput(PDEPlugin.getWorkspace().getRoot());
152
153         if (dialog.open() == Window.OK) {
154             IFile file = (IFile)dialog.getFirstResult();
155             fImageEntry.setValue(file.getFullPath().toString());
156         }
157     }
158     
159     public void refresh() {
160         fImageEntry.setValue(getAboutInfo().getImagePath(), true);
161         fTextEntry.setValue(getAboutInfo().getText(), true);
162         super.refresh();
163     }
164     
165     public void commit(boolean onSave) {
166         fImageEntry.commit();
167         fTextEntry.commit();
168         super.commit(onSave);
169     }
170     
171     public void cancelEdit() {
172         fImageEntry.cancelEdit();
173         fTextEntry.cancelEdit();
174         super.cancelEdit();
175     }
176     
177     private IAboutInfo getAboutInfo() {
178         IAboutInfo info = getProduct().getAboutInfo();
179         if (info == null) {
180             info = getModel().getFactory().createAboutInfo();
181             getProduct().setAboutInfo(info);
182         }
183         return info;
184     }
185     
186     private IProduct getProduct() {
187         return getModel().getProduct();
188     }
189     
190     private IProductModel getModel() {
191         return (IProductModel)getPage().getPDEEditor().getAggregateModel();
192     }
193     
194     public boolean canPaste(Clipboard clipboard) {
195         Display d = getSection().getDisplay();
196         Control c = d.getFocusControl();
197         if (c instanceof Text)
198             return true;
199         return false;
200     }
201     
202     /* (non-Javadoc)
203      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
204      */

205     public void modelChanged(IModelChangedEvent e) {
206         // No need to call super, handling world changed event here
207
if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
208             handleModelEventWorldChanged(e);
209         }
210     }
211
212     /**
213      * @param event
214      */

215     private void handleModelEventWorldChanged(IModelChangedEvent event) {
216         refresh();
217     }
218     
219     /* (non-Javadoc)
220      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
221      */

222     public void dispose() {
223         IProductModel model = getModel();
224         if (model != null) {
225             model.removeModelChangedListener(this);
226         }
227         super.dispose();
228     }
229 }
230
Popular Tags