001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.product;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.resources.IResource;
014: import org.eclipse.core.resources.IWorkspaceRoot;
015: import org.eclipse.core.runtime.Path;
016: import org.eclipse.jface.dialogs.MessageDialog;
017: import org.eclipse.jface.window.Window;
018: import org.eclipse.pde.core.IModelChangedEvent;
019: import org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo;
020: import org.eclipse.pde.internal.core.iproduct.IProduct;
021: import org.eclipse.pde.internal.core.iproduct.IProductModel;
022: import org.eclipse.pde.internal.ui.PDEPlugin;
023: import org.eclipse.pde.internal.ui.PDEUIMessages;
024: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
025: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
026: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
027: import org.eclipse.pde.internal.ui.editor.PDESection;
028: import org.eclipse.pde.internal.ui.parts.FormEntry;
029: import org.eclipse.pde.internal.ui.util.FileNameFilter;
030: import org.eclipse.pde.internal.ui.util.FileValidator;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.dnd.Clipboard;
033: import org.eclipse.swt.events.SelectionAdapter;
034: import org.eclipse.swt.events.SelectionEvent;
035: import org.eclipse.swt.layout.GridData;
036: import org.eclipse.swt.widgets.Button;
037: import org.eclipse.swt.widgets.Composite;
038: import org.eclipse.swt.widgets.Control;
039: import org.eclipse.swt.widgets.Display;
040: import org.eclipse.swt.widgets.Text;
041: import org.eclipse.ui.IActionBars;
042: import org.eclipse.ui.PartInitException;
043: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
044: import org.eclipse.ui.forms.events.HyperlinkEvent;
045: import org.eclipse.ui.forms.widgets.FormToolkit;
046: import org.eclipse.ui.forms.widgets.Section;
047: import org.eclipse.ui.ide.IDE;
048: import org.eclipse.ui.model.WorkbenchContentProvider;
049: import org.eclipse.ui.model.WorkbenchLabelProvider;
050:
051: public class ConfigurationSection extends PDESection {
052:
053: private Button fDefault;
054: private Button fCustom;
055: private FormEntry fCustomEntry;
056:
057: public ConfigurationSection(PDEFormPage page, Composite parent) {
058: super (page, parent, Section.DESCRIPTION);
059: createClient(getSection(), page.getEditor().getToolkit());
060: }
061:
062: /* (non-Javadoc)
063: * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
064: */
065: protected void createClient(Section section, FormToolkit toolkit) {
066:
067: section.setLayout(FormLayoutFactory.createClearGridLayout(
068: false, 1));
069: GridData sectionData = new GridData(GridData.FILL_HORIZONTAL);
070: section.setLayoutData(sectionData);
071:
072: section.setText(PDEUIMessages.ConfigurationSection_title);
073: section.setDescription(PDEUIMessages.ConfigurationSection_desc);
074:
075: Composite client = toolkit.createComposite(section);
076: client.setLayout(FormLayoutFactory
077: .createSectionClientGridLayout(false, 3));
078: client.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
079:
080: fDefault = toolkit.createButton(client,
081: PDEUIMessages.ConfigurationSection_default, SWT.RADIO);
082: GridData gd = new GridData();
083: gd.horizontalSpan = 3;
084: fDefault.setLayoutData(gd);
085: fDefault.setEnabled(isEditable());
086: fDefault.addSelectionListener(new SelectionAdapter() {
087: public void widgetSelected(SelectionEvent e) {
088: boolean selected = fDefault.getSelection();
089: getConfigurationFileInfo().setUse(
090: selected ? "default" : "custom"); //$NON-NLS-1$ //$NON-NLS-2$
091: fCustomEntry.setEditable(!selected);
092: }
093: });
094:
095: fCustom = toolkit.createButton(client,
096: PDEUIMessages.ConfigurationSection_existing, SWT.RADIO);
097: gd = new GridData();
098: gd.horizontalSpan = 3;
099: fCustom.setLayoutData(gd);
100: fCustom.setEnabled(isEditable());
101:
102: IActionBars actionBars = getPage().getPDEEditor()
103: .getEditorSite().getActionBars();
104: fCustomEntry = new FormEntry(client, toolkit,
105: PDEUIMessages.ConfigurationSection_file,
106: PDEUIMessages.ConfigurationSection_browse,
107: isEditable(), 35); //
108: fCustomEntry.setFormEntryListener(new FormEntryAdapter(this ,
109: actionBars) {
110: public void textValueChanged(FormEntry entry) {
111: getConfigurationFileInfo().setPath(entry.getValue());
112: }
113:
114: public void browseButtonSelected(FormEntry entry) {
115: handleBrowse();
116: }
117:
118: public void linkActivated(HyperlinkEvent e) {
119: handleOpen();
120: }
121: });
122: fCustomEntry.setEditable(isEditable());
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: /* (non-Javadoc)
131: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
132: */
133: public void dispose() {
134: IProductModel model = getModel();
135: if (model != null) {
136: model.removeModelChangedListener(this );
137: }
138: super .dispose();
139: }
140:
141: private void handleBrowse() {
142: ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(
143: getSection().getShell(), new WorkbenchLabelProvider(),
144: new WorkbenchContentProvider());
145:
146: dialog.setValidator(new FileValidator());
147: dialog.setAllowMultiple(false);
148: dialog.setTitle(PDEUIMessages.ConfigurationSection_selection);
149: dialog.setMessage(PDEUIMessages.ConfigurationSection_message);
150: dialog.addFilter(new FileNameFilter("config.ini")); //$NON-NLS-1$
151: dialog.setInput(PDEPlugin.getWorkspace().getRoot());
152:
153: if (dialog.open() == Window.OK) {
154: IFile file = (IFile) dialog.getFirstResult();
155: fCustomEntry.setValue(file.getFullPath().toString());
156: }
157: }
158:
159: public void refresh() {
160: IConfigurationFileInfo info = getConfigurationFileInfo();
161: if (info == null) {
162: fDefault.setSelection(true);
163: fCustomEntry.setEditable(false);
164: } else {
165: boolean custom = "custom".equals(info.getUse()); //$NON-NLS-1$
166: fDefault.setSelection(!custom);
167: fCustom.setSelection(custom);
168: fCustomEntry.setValue(info.getPath(), true);
169: fCustomEntry.setEditable(custom);
170: }
171: super .refresh();
172: }
173:
174: private IConfigurationFileInfo getConfigurationFileInfo() {
175: IConfigurationFileInfo info = getProduct()
176: .getConfigurationFileInfo();
177: if (info == null) {
178: info = getModel().getFactory().createConfigFileInfo();
179: getProduct().setConfigurationFileInfo(info);
180: }
181: return info;
182: }
183:
184: private IProduct getProduct() {
185: return getModel().getProduct();
186: }
187:
188: private IProductModel getModel() {
189: return (IProductModel) getPage().getPDEEditor()
190: .getAggregateModel();
191: }
192:
193: public void commit(boolean onSave) {
194: fCustomEntry.commit();
195: super .commit(onSave);
196: }
197:
198: public void cancelEdit() {
199: fCustomEntry.cancelEdit();
200: super .cancelEdit();
201: }
202:
203: public boolean canPaste(Clipboard clipboard) {
204: Display d = getSection().getDisplay();
205: Control c = d.getFocusControl();
206: if (c instanceof Text)
207: return true;
208: return false;
209: }
210:
211: private void handleOpen() {
212: IWorkspaceRoot root = PDEPlugin.getWorkspace().getRoot();
213: Path path = new Path(fCustomEntry.getValue());
214: if (path.isEmpty()) {
215: MessageDialog.openWarning(PDEPlugin
216: .getActiveWorkbenchShell(),
217: PDEUIMessages.WindowImagesSection_open,
218: PDEUIMessages.WindowImagesSection_emptyPath); //
219: return;
220: }
221: IResource resource = root.findMember(path);
222: try {
223: if (resource != null && resource instanceof IFile)
224: IDE.openEditor(PDEPlugin.getActivePage(),
225: (IFile) resource, true);
226: else
227: MessageDialog.openWarning(PDEPlugin
228: .getActiveWorkbenchShell(),
229: PDEUIMessages.WindowImagesSection_open,
230: PDEUIMessages.WindowImagesSection_warning); //
231: } catch (PartInitException e) {
232: }
233: }
234:
235: /* (non-Javadoc)
236: * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
237: */
238: public void modelChanged(IModelChangedEvent e) {
239: // No need to call super, handling world changed event here
240: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
241: handleModelEventWorldChanged(e);
242: }
243: }
244:
245: /**
246: * @param event
247: */
248: private void handleModelEventWorldChanged(IModelChangedEvent event) {
249: // This section can get disposed if the configuration is changed from
250: // plugins to features or vice versa. Subsequently, the configuration
251: // page is removed and readded. In this circumstance, abort the
252: // refresh
253: if (fCustomEntry.getText().isDisposed()) {
254: return;
255: }
256: // Perform the refresh
257: refresh();
258: // Note: A deferred selection event is fired from radio buttons when
259: // their value is toggled, the user switches to another page, and the
260: // user switches back to the same page containing the radio buttons
261: // This appears to be a result of a SWT bug.
262: // If the radio button is the last widget to have focus when leaving
263: // the page, an event will be fired when entering the page again.
264: // An event is not fired if the radio button does not have focus.
265: // The solution is to redirect focus to a stable widget.
266: getPage().setLastFocusControl(fCustomEntry.getText());
267: }
268: }
|