KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.jface.preference.ColorSelector;
14 import org.eclipse.jface.util.IPropertyChangeListener;
15 import org.eclipse.jface.util.PropertyChangeEvent;
16 import org.eclipse.jface.window.Window;
17 import org.eclipse.pde.core.plugin.IPluginModelBase;
18 import org.eclipse.pde.internal.core.PDECore;
19 import org.eclipse.pde.internal.core.iproduct.IProduct;
20 import org.eclipse.pde.internal.core.iproduct.IProductModel;
21 import org.eclipse.pde.internal.core.iproduct.ISplashInfo;
22 import org.eclipse.pde.internal.ui.PDEPlugin;
23 import org.eclipse.pde.internal.ui.PDEUIMessages;
24 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
25 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
26 import org.eclipse.pde.internal.ui.editor.PDESection;
27 import org.eclipse.pde.internal.ui.parts.FormEntry;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.dnd.Clipboard;
30 import org.eclipse.swt.events.ModifyEvent;
31 import org.eclipse.swt.events.ModifyListener;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.graphics.Color;
35 import org.eclipse.swt.graphics.RGB;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Button;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Spinner;
44 import org.eclipse.swt.widgets.Text;
45 import org.eclipse.ui.IActionBars;
46 import org.eclipse.ui.dialogs.ElementListSelectionDialog;
47 import org.eclipse.ui.forms.FormColors;
48 import org.eclipse.ui.forms.widgets.FormToolkit;
49 import org.eclipse.ui.forms.widgets.Section;
50
51
52 public class SplashSection extends PDESection {
53
54     private FormEntry fPluginEntry;
55     private ColorSelector fColorSelector;
56     private boolean fBlockNotification;
57     
58     private Button fAddBarButton;
59     // spinners controlling the progress bar geometry
60
private Spinner[] fBarSpinners = new Spinner[4];
61     // all swt controls under the progress bar checkbox
62
private Control[] fBarControls = new Control[8];
63     
64     private Button fAddMessageButton;
65     // spinners controlling the progress message geometry
66
private Spinner[] fMessageSpinners = new Spinner[4];
67     // all swt controls under the progress message checkbox
68
private Control[] fMessageControls = new Control[10];
69     
70     public SplashSection(PDEFormPage page, Composite parent) {
71         super(page, parent, Section.DESCRIPTION);
72         createClient(getSection(), page.getEditor().getToolkit());
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
77      */

78     protected void createClient(Section section, FormToolkit toolkit) {
79         section.setText(PDEUIMessages.SplashSection_title);
80         section.setDescription(PDEUIMessages.SplashSection_desc);
81
82         Composite client = toolkit.createComposite(section);
83         GridLayout layout = new GridLayout();
84         layout.numColumns = 10;
85         layout.marginHeight = 5;
86         client.setLayout(layout);
87         
88         Label label = toolkit.createLabel(client, PDEUIMessages.SplashSection_label, SWT.WRAP);
89         GridData gd = new GridData();
90         gd.horizontalSpan = 10;
91         label.setLayoutData(gd);
92         
93         IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars();
94         fPluginEntry = new FormEntry(client, toolkit, PDEUIMessages.SplashSection_plugin, PDEUIMessages.SplashSection_browse, false); //
95
fPluginEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
96             public void textValueChanged(FormEntry entry) {
97                 getSplashInfo().setLocation(entry.getValue(), false);
98             }
99             public void browseButtonSelected(FormEntry entry) {
100                 handleBrowse();
101             }
102         });
103         fPluginEntry.setEditable(isEditable());
104         
105         createProgressBarConfig(client, toolkit);
106         createProgressMessageConfig(client, toolkit);
107         
108         toolkit.paintBordersFor(client);
109         section.setClient(client);
110         gd = new GridData(GridData.FILL_HORIZONTAL);
111         gd.horizontalSpan = 2;
112         section.setLayoutData(gd);
113     }
114     
115     private void createProgressBarConfig(Composite parent, FormToolkit toolkit) {
116         fAddBarButton = createButton(parent, toolkit, PDEUIMessages.SplashSection_progressBar);
117         fAddBarButton.addSelectionListener(new SelectionAdapter() {
118             public void widgetSelected(SelectionEvent e) {
119                 boolean enable = fAddBarButton.getSelection();
120                 getSplashInfo().addProgressBar(enable, false);
121                 for (int i = 0; i < fBarControls.length; i++)
122                     fBarControls[i].setEnabled(enable);
123             }
124         });
125         
126         Color foreground = toolkit.getColors().getColor(FormColors.TITLE);
127         
128         fBarControls[0] = createLabel(parent, toolkit, foreground, PDEUIMessages.SplashSection_progressX);
129         fBarControls[1] = fBarSpinners[0] = createSpinner(parent, toolkit);
130         fBarControls[2] = createLabel(parent, toolkit, foreground, PDEUIMessages.SplashSection_progressY);
131         fBarControls[3] = fBarSpinners[1] = createSpinner(parent, toolkit);
132         fBarControls[4] = createLabel(parent, toolkit, foreground, PDEUIMessages.SplashSection_progressWidth);
133         fBarControls[5] = fBarSpinners[2] = createSpinner(parent, toolkit);
134         fBarControls[6] = createLabel(parent, toolkit, foreground, PDEUIMessages.SplashSection_progressHeight);
135         fBarControls[7] = fBarSpinners[3] = createSpinner(parent, toolkit);
136         
137         for (int i = 0; i < fBarSpinners.length; i++) {
138             fBarSpinners[i].addModifyListener(new ModifyListener() {
139                 public void modifyText(ModifyEvent e) {
140                     applySpinners(true);
141                 }
142             });
143         }
144         
145         Composite filler = toolkit.createComposite(parent);
146         filler.setLayout(new GridLayout());
147         GridData gd = new GridData();
148         gd.horizontalSpan = 2;
149         filler.setLayoutData(gd);
150     }
151     
152     private void createProgressMessageConfig(Composite parent, FormToolkit toolkit) {
153         fAddMessageButton = createButton(parent, toolkit, PDEUIMessages.SplashSection_progressMessage);
154         fAddMessageButton.addSelectionListener(new SelectionAdapter() {
155             public void widgetSelected(SelectionEvent e) {
156                 boolean enable = fAddMessageButton.getSelection();
157                 getSplashInfo().addProgressMessage(enable, false);
158                 for (int i = 0; i < fMessageControls.length; i++)
159                     fMessageControls[i].setEnabled(enable);
160             }
161         });
162         
163         Color foreground = toolkit.getColors().getColor(FormColors.TITLE);
164         
165         fMessageControls[0] = createLabel(parent, toolkit, foreground, PDEUIMessages.SplashSection_messageX);
166         fMessageControls[1] = fMessageSpinners[0] = createSpinner(parent, toolkit);
167         fMessageControls[2] = createLabel(parent, toolkit, foreground, PDEUIMessages.SplashSection_messageY);
168         fMessageControls[3] = fMessageSpinners[1] = createSpinner(parent, toolkit);
169         
170         fMessageControls[4] = createLabel(parent, toolkit, foreground, PDEUIMessages.SplashSection_messageWidth);
171         fMessageControls[5] = fMessageSpinners[2] = createSpinner(parent, toolkit);
172         fMessageControls[6] = createLabel(parent, toolkit, foreground, PDEUIMessages.SplashSection_messageHeight);
173         fMessageControls[7] = fMessageSpinners[3] = createSpinner(parent, toolkit);
174
175         fMessageControls[8] = createLabel(parent, toolkit, foreground, PDEUIMessages.SplashSection_messageColor);
176         fColorSelector = new ColorSelector(parent);
177         fColorSelector.addListener(new IPropertyChangeListener() {
178             public void propertyChange(PropertyChangeEvent event) {
179                 if (!event.getNewValue().equals(event.getOldValue()))
180                     applyColor();
181             }
182         });
183         toolkit.adapt(fColorSelector.getButton(), true, true);
184         fMessageControls[9] = fColorSelector.getButton();
185
186         for (int i = 0; i < fMessageSpinners.length; i++) {
187             fMessageSpinners[i].addModifyListener(new ModifyListener() {
188                 public void modifyText(ModifyEvent e) {
189                     applySpinners(false);
190                 }
191             });
192         }
193     }
194     
195     private void applyColor() {
196         if (fBlockNotification)
197             return;
198         RGB rgb = fColorSelector.getColorValue();
199         getSplashInfo().setForegroundColor(rgbToHEX(rgb), false);
200     }
201     
202     private void applySpinners(boolean bar) {
203         if (fBlockNotification)
204             return;
205         Spinner[] spinners = bar ? fBarSpinners : fMessageSpinners;
206         int[] geo = new int[] {
207                 spinners[0].getSelection(),
208                 spinners[1].getSelection(),
209                 spinners[2].getSelection(),
210                 spinners[3].getSelection()
211         };
212         if (bar)
213             getSplashInfo().setProgressGeometry(geo, false);
214         else
215             getSplashInfo().setMessageGeometry(geo, false);
216     }
217     
218     private Label createLabel(Composite parent, FormToolkit toolkit, Color color, String JavaDoc labelName) {
219         Label label = toolkit.createLabel(parent, labelName);
220         label.setForeground(color);
221         GridData gd = new GridData();
222         gd.horizontalIndent = 10;
223         label.setLayoutData(gd);
224         return label;
225     }
226     
227     private Button createButton(Composite parent, FormToolkit toolkit, String JavaDoc label) {
228         Button button = toolkit.createButton(parent, label, SWT.CHECK);
229         GridData gd = new GridData();
230         gd.horizontalSpan = 10;
231         button.setLayoutData(gd);
232         return button;
233     }
234     
235     private Spinner createSpinner(Composite parent, FormToolkit toolkit) {
236         Spinner spinner = new Spinner(parent, SWT.BORDER);
237         spinner.setMinimum(0);
238         spinner.setMaximum(9999);
239         toolkit.adapt(spinner, false, false);
240         return spinner;
241     }
242     
243     public void refresh() {
244         ISplashInfo info = getSplashInfo();
245         fBlockNotification = true;
246         
247         fPluginEntry.setValue(info.getLocation(), true);
248         fColorSelector.setColorValue(hexToRGB(info.getForegroundColor()));
249         
250         int[] pgeo = info.getProgressGeometry();
251         boolean addProgress = pgeo != null;
252         info.addProgressBar(addProgress, fBlockNotification);
253         if (addProgress)
254             for (int i = 0; i < pgeo.length; i++)
255                 fBarSpinners[i].setSelection(pgeo[i]);
256         
257         fAddBarButton.setSelection(addProgress);
258         for (int i = 0; i < fBarControls.length; i++)
259             fBarControls[i].setEnabled(addProgress);
260         
261         int[] mgeo = info.getMessageGeometry();
262         boolean addMessage = mgeo != null;
263         info.addProgressMessage(addMessage, fBlockNotification);
264         if (addMessage)
265             for (int i = 0; i < mgeo.length; i++)
266                 fMessageSpinners[i].setSelection(mgeo[i]);
267         fColorSelector.setColorValue(
268                 addMessage ?
269                         hexToRGB(info.getForegroundColor()) :
270                         new RGB(0,0,0));
271         
272         fAddMessageButton.setSelection(addMessage);
273         for (int i = 0; i < fMessageControls.length; i++)
274             fMessageControls[i].setEnabled(addMessage);
275         
276         fBlockNotification = false;
277         super.refresh();
278     }
279     
280     public void commit(boolean onSave) {
281         fPluginEntry.commit();
282         super.commit(onSave);
283     }
284     
285     public void cancelEdit() {
286         fPluginEntry.cancelEdit();
287         super.cancelEdit();
288     }
289     
290     private ISplashInfo getSplashInfo() {
291         ISplashInfo info = getProduct().getSplashInfo();
292         if (info == null) {
293             info = getModel().getFactory().createSplashInfo();
294             getProduct().setSplashInfo(info);
295         }
296         return info;
297     }
298     
299     private IProduct getProduct() {
300         return getModel().getProduct();
301     }
302     
303     private IProductModel getModel() {
304         return (IProductModel)getPage().getPDEEditor().getAggregateModel();
305     }
306     
307     private void handleBrowse() {
308         ElementListSelectionDialog dialog = new ElementListSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), PDEPlugin.getDefault().getLabelProvider());
309         dialog.setElements(PDECore.getDefault().getModelManager().getAllPlugins());
310         dialog.setMultipleSelection(false);
311         dialog.setTitle(PDEUIMessages.SplashSection_selection);
312         dialog.setMessage(PDEUIMessages.SplashSection_message);
313         if (dialog.open() == Window.OK) {
314             IPluginModelBase model = (IPluginModelBase)dialog.getFirstResult();
315             fPluginEntry.setValue(model.getPluginBase().getId());
316         }
317     }
318
319     public boolean canPaste(Clipboard clipboard) {
320         Display d = getSection().getDisplay();
321         Control c = d.getFocusControl();
322         if (c instanceof Text)
323             return true;
324         return false;
325     }
326
327     private String JavaDoc rgbToHEX(RGB rgb) {
328         if (rgb == null)
329             return null;
330         return rgbToHex(rgb.red) + rgbToHex(rgb.green) + rgbToHex(rgb.blue);
331     }
332     
333     private String JavaDoc rgbToHex(int value) {
334         value = Math.max(0, value);
335         value = Math.min(value, 255);
336         String JavaDoc hex = Integer.toHexString(value).toUpperCase();
337         if (hex.length() == 1)
338             hex = '0' + hex;
339         return hex;
340     }
341     
342     private RGB hexToRGB(String JavaDoc hexValue) {
343         if (hexValue == null || hexValue.length() < 6)
344             return new RGB(0,0,0);
345         return new RGB(
346                 Integer.parseInt(hexValue.substring(0,2),16),
347                 Integer.parseInt(hexValue.substring(2,4),16),
348                 Integer.parseInt(hexValue.substring(4,6),16)
349             );
350     }
351 }
352
Popular Tags