KickJava   Java API By Example, From Geeks To Geeks.

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


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.IProduct;
17 import org.eclipse.pde.internal.core.iproduct.IProductModel;
18 import org.eclipse.pde.internal.core.iproduct.IWindowImages;
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.dnd.Clipboard;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.swt.widgets.Text;
36 import org.eclipse.ui.IActionBars;
37 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
38 import org.eclipse.ui.forms.events.HyperlinkEvent;
39 import org.eclipse.ui.forms.widgets.FormToolkit;
40 import org.eclipse.ui.forms.widgets.Section;
41 import org.eclipse.ui.model.WorkbenchContentProvider;
42 import org.eclipse.ui.model.WorkbenchLabelProvider;
43
44
45 public class WindowImagesSection extends PDESection {
46
47     private TextValidator[] fWinImageEntryValidator;
48     
49     private static final int[][] F_ICON_DIMENSIONS = new int[][] {
50         {16, 16}, {32, 32}, {48, 48}, {64, 64}, {128, 128}
51     };
52     private static final String JavaDoc[] F_ICON_LABELS = new String JavaDoc[] {
53         PDEUIMessages.WindowImagesSection_16,
54         PDEUIMessages.WindowImagesSection_32,
55         PDEUIMessages.WindowImagesSection_48,
56         PDEUIMessages.WindowImagesSection_64,
57         PDEUIMessages.WindowImagesSection_128
58     };
59     private FormEntry[] fImages = new FormEntry[F_ICON_LABELS.length];
60     
61     public WindowImagesSection(PDEFormPage page, Composite parent) {
62         super(page, parent, Section.DESCRIPTION);
63         createClient(getSection(), page.getEditor().getToolkit());
64     }
65
66     /* (non-Javadoc)
67      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
68      */

69     protected void createClient(Section section, FormToolkit toolkit) {
70         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
71         GridData data = new GridData(GridData.FILL_HORIZONTAL);
72         section.setLayoutData(data);
73         
74         section.setText(PDEUIMessages.WindowImagesSection_title);
75         section.setDescription(PDEUIMessages.WindowImagesSection_desc);
76
77         Composite client = toolkit.createComposite(section);
78         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
79         client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
80         
81         IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars();
82         // Store all image entry validators
83
fWinImageEntryValidator = new TextValidator[F_ICON_LABELS.length];
84         for (int i = 0; i < fImages.length; i++) {
85             final int index = i;
86             fImages[index] = new FormEntry(client,
87                     toolkit, F_ICON_LABELS[index],
88                     PDEUIMessages.WindowImagesSection_browse,
89                     isEditable());
90             fImages[index].setEditable(isEditable());
91             // Create validator
92
fWinImageEntryValidator[index] = new TextValidator(
93                     getManagedForm(), fImages[index].getText(), getProject(), true) {
94                 protected boolean validateControl() {
95                     return validateWinImageEntry(index);
96                 }
97             };
98             fImages[index].setFormEntryListener(new FormEntryAdapter(this, actionBars) {
99                 public void textValueChanged(FormEntry entry) {
100                     getWindowImages().setImagePath(entry.getValue(), index);
101                 }
102                 public void browseButtonSelected(FormEntry entry) {
103                     handleBrowse(entry);
104                 }
105                 public void linkActivated(HyperlinkEvent e) {
106                     EditorUtilities.openImage(fImages[index].getValue(), getProduct().getDefiningPluginId());
107                 }
108             });
109         }
110         
111         toolkit.paintBordersFor(client);
112         section.setClient(client);
113         // Register to be notified when the model changes
114
getModel().addModelChangedListener(this);
115     }
116     
117     public void refresh() {
118         IWindowImages images = getWindowImages();
119         // Turn off auto message update until after values are set
120
fWinImageEntryValidator[0].setRefresh(false);
121         for (int i = 0; i < F_ICON_LABELS.length; i++) {
122             fImages[i].setValue(images.getImagePath(i), true);
123         }
124         // Turn back on auto message update
125
fWinImageEntryValidator[0].setRefresh(true);
126         super.refresh();
127     }
128
129     /**
130      * @return
131      */

132     private boolean validateWinImageEntry(int index) {
133         return EditorUtilities.imageEntryHasExactSize(
134                 fWinImageEntryValidator[index],
135                 fImages[index],
136                 getProduct(),
137                 F_ICON_DIMENSIONS[index][0],
138                 F_ICON_DIMENSIONS[index][1]);
139     }
140
141     private IWindowImages getWindowImages() {
142         IWindowImages images = getProduct().getWindowImages();
143         if (images == null) {
144             images = getModel().getFactory().createWindowImages();
145             getProduct().setWindowImages(images);
146         }
147         return images;
148     }
149     
150     private IProduct getProduct() {
151         return getModel().getProduct();
152     }
153     
154     private IProductModel getModel() {
155         return (IProductModel)getPage().getPDEEditor().getAggregateModel();
156     }
157
158     public void commit(boolean onSave) {
159         for (int i = 0; i < F_ICON_LABELS.length; i++) {
160             fImages[i].commit();
161         }
162         super.commit(onSave);
163     }
164     
165     public void cancelEdit() {
166         for (int i = 0; i < F_ICON_LABELS.length; i++) {
167             fImages[i].cancelEdit();
168         }
169         super.cancelEdit();
170     }
171     
172     private void handleBrowse(FormEntry entry) {
173         ElementTreeSelectionDialog dialog =
174             new ElementTreeSelectionDialog(
175                 getSection().getShell(),
176                 new WorkbenchLabelProvider(),
177                 new WorkbenchContentProvider());
178                 
179         dialog.setValidator(new FileValidator());
180         dialog.setAllowMultiple(false);
181         dialog.setTitle(PDEUIMessages.WindowImagesSection_dialogTitle);
182         dialog.setMessage(PDEUIMessages.WindowImagesSection_dialogMessage);
183         dialog.addFilter(new FileExtensionFilter("gif")); //$NON-NLS-1$
184
dialog.setInput(PDEPlugin.getWorkspace().getRoot());
185
186         if (dialog.open() == Window.OK) {
187             IFile file = (IFile)dialog.getFirstResult();
188             entry.setValue(file.getFullPath().toString());
189         }
190     }
191
192     public boolean canPaste(Clipboard clipboard) {
193         Display d = getSection().getDisplay();
194         Control c = d.getFocusControl();
195         if (c instanceof Text)
196             return true;
197         return false;
198     }
199     
200     /* (non-Javadoc)
201      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
202      */

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

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

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