KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.resources.IResource;
15 import org.eclipse.core.resources.IWorkspaceRoot;
16 import org.eclipse.core.runtime.Path;
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.window.Window;
19 import org.eclipse.pde.core.IModelChangedEvent;
20 import org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo;
21 import org.eclipse.pde.internal.core.iproduct.IProduct;
22 import org.eclipse.pde.internal.core.iproduct.IProductModel;
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.pde.internal.ui.util.FileNameFilter;
31 import org.eclipse.pde.internal.ui.util.FileValidator;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.dnd.Clipboard;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.IActionBars;
43 import org.eclipse.ui.PartInitException;
44 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
45 import org.eclipse.ui.forms.events.HyperlinkEvent;
46 import org.eclipse.ui.forms.widgets.FormToolkit;
47 import org.eclipse.ui.forms.widgets.Section;
48 import org.eclipse.ui.ide.IDE;
49 import org.eclipse.ui.model.WorkbenchContentProvider;
50 import org.eclipse.ui.model.WorkbenchLabelProvider;
51
52
53 public class ConfigurationSection extends PDESection {
54
55     private Button fDefault;
56     private Button fCustom;
57     private FormEntry fCustomEntry;
58     
59     public ConfigurationSection(PDEFormPage page, Composite parent) {
60         super(page, parent, Section.DESCRIPTION);
61         createClient(getSection(), page.getEditor().getToolkit());
62     }
63
64     /* (non-Javadoc)
65      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
66      */

67     protected void createClient(Section section, FormToolkit toolkit) {
68         
69         section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
70         GridData sectionData = new GridData(GridData.FILL_HORIZONTAL);
71         section.setLayoutData(sectionData);
72         
73         section.setText(PDEUIMessages.ConfigurationSection_title);
74         section.setDescription(PDEUIMessages.ConfigurationSection_desc);
75
76         Composite client = toolkit.createComposite(section);
77         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 3));
78         client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
79         
80         fDefault = toolkit.createButton(client, PDEUIMessages.ConfigurationSection_default, SWT.RADIO);
81         GridData gd = new GridData();
82         gd.horizontalSpan = 3;
83         fDefault.setLayoutData(gd);
84         fDefault.setEnabled(isEditable());
85         fDefault.addSelectionListener(new SelectionAdapter() {
86             public void widgetSelected(SelectionEvent e) {
87                 boolean selected = fDefault.getSelection();
88                 getConfigurationFileInfo().setUse(selected ? "default" : "custom"); //$NON-NLS-1$ //$NON-NLS-2$
89
fCustomEntry.setEditable(!selected);
90             }
91         });
92         
93         fCustom = toolkit.createButton(client, PDEUIMessages.ConfigurationSection_existing, SWT.RADIO);
94         gd = new GridData();
95         gd.horizontalSpan = 3;
96         fCustom.setLayoutData(gd);
97         fCustom.setEnabled(isEditable());
98         
99         IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars();
100         fCustomEntry = new FormEntry(client, toolkit, PDEUIMessages.ConfigurationSection_file, PDEUIMessages.ConfigurationSection_browse, isEditable(), 35); //
101
fCustomEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
102             public void textValueChanged(FormEntry entry) {
103                 getConfigurationFileInfo().setPath(entry.getValue());
104             }
105             public void browseButtonSelected(FormEntry entry) {
106                 handleBrowse();
107             }
108             public void linkActivated(HyperlinkEvent e) {
109                 handleOpen();
110             }
111         });
112         fCustomEntry.setEditable(isEditable());
113         
114         toolkit.paintBordersFor(client);
115         section.setClient(client);
116         // Register to be notified when the model changes
117
getModel().addModelChangedListener(this);
118     }
119     
120     /* (non-Javadoc)
121      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
122      */

123     public void dispose() {
124         IProductModel model = getModel();
125         if (model != null) {
126             model.removeModelChangedListener(this);
127         }
128         super.dispose();
129     }
130     
131     private void handleBrowse() {
132         ElementTreeSelectionDialog dialog =
133             new ElementTreeSelectionDialog(
134                 getSection().getShell(),
135                 new WorkbenchLabelProvider(),
136                 new WorkbenchContentProvider());
137                 
138         dialog.setValidator(new FileValidator());
139         dialog.setAllowMultiple(false);
140         dialog.setTitle(PDEUIMessages.ConfigurationSection_selection);
141         dialog.setMessage(PDEUIMessages.ConfigurationSection_message);
142         dialog.addFilter(new FileNameFilter("config.ini")); //$NON-NLS-1$
143
dialog.setInput(PDEPlugin.getWorkspace().getRoot());
144
145         if (dialog.open() == Window.OK) {
146             IFile file = (IFile)dialog.getFirstResult();
147             fCustomEntry.setValue(file.getFullPath().toString());
148         }
149     }
150     
151     public void refresh() {
152         IConfigurationFileInfo info = getConfigurationFileInfo();
153         if (info == null) {
154             fDefault.setSelection(true);
155             fCustomEntry.setEditable(false);
156         } else {
157             boolean custom = "custom".equals(info.getUse()); //$NON-NLS-1$
158
fDefault.setSelection(!custom);
159             fCustom.setSelection(custom);
160             fCustomEntry.setValue(info.getPath(), true);
161             fCustomEntry.setEditable(custom);
162         }
163         super.refresh();
164     }
165     
166     private IConfigurationFileInfo getConfigurationFileInfo() {
167         IConfigurationFileInfo info = getProduct().getConfigurationFileInfo();
168         if (info == null) {
169             info = getModel().getFactory().createConfigFileInfo();
170             getProduct().setConfigurationFileInfo(info);
171         }
172         return info;
173     }
174     
175     private IProduct getProduct() {
176         return getModel().getProduct();
177     }
178     
179     private IProductModel getModel() {
180         return (IProductModel)getPage().getPDEEditor().getAggregateModel();
181     }
182     
183     public void commit(boolean onSave) {
184         fCustomEntry.commit();
185         super.commit(onSave);
186     }
187     
188     public void cancelEdit() {
189         fCustomEntry.cancelEdit();
190         super.cancelEdit();
191     }
192     
193     public boolean canPaste(Clipboard clipboard) {
194         Display d = getSection().getDisplay();
195         Control c = d.getFocusControl();
196         if (c instanceof Text)
197             return true;
198         return false;
199     }
200
201     private void handleOpen() {
202         IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
203         Path path = new Path(fCustomEntry.getValue());
204         if(path.isEmpty()){
205             MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.WindowImagesSection_open, PDEUIMessages.WindowImagesSection_emptyPath); //
206
return;
207         }
208         IResource resource = root.findMember(path);
209         try {
210             if (resource != null && resource instanceof IFile)
211                 IDE.openEditor(PDEPlugin.getActivePage(), (IFile)resource, true);
212             else
213                 MessageDialog.openWarning(PDEPlugin.getActiveWorkbenchShell(), PDEUIMessages.WindowImagesSection_open, PDEUIMessages.WindowImagesSection_warning); //
214
} catch (PartInitException e) {
215         }
216     }
217
218     /* (non-Javadoc)
219      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
220      */

221     public void modelChanged(IModelChangedEvent e) {
222         // No need to call super, handling world changed event here
223
if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
224             handleModelEventWorldChanged(e);
225         }
226     }
227     
228     /**
229      * @param event
230      */

231     private void handleModelEventWorldChanged(IModelChangedEvent event) {
232         // This section can get disposed if the configuration is changed from
233
// plugins to features or vice versa. Subsequently, the configuration
234
// page is removed and readded. In this circumstance, abort the
235
// refresh
236
if (fCustomEntry.getText().isDisposed()) {
237             return;
238         }
239         // Perform the refresh
240
refresh();
241         // Note: A deferred selection event is fired from radio buttons when
242
// their value is toggled, the user switches to another page, and the
243
// user switches back to the same page containing the radio buttons
244
// This appears to be a result of a SWT bug.
245
// If the radio button is the last widget to have focus when leaving
246
// the page, an event will be fired when entering the page again.
247
// An event is not fired if the radio button does not have focus.
248
// The solution is to redirect focus to a stable widget.
249
getPage().setLastFocusControl(fCustomEntry.getText());
250     }
251 }
252
Popular Tags