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.target;
011:
012: import org.eclipse.debug.ui.StringVariableSelectionDialog;
013: import org.eclipse.jface.window.Window;
014: import org.eclipse.pde.core.IModelChangedEvent;
015: import org.eclipse.pde.internal.core.itarget.ILocationInfo;
016: import org.eclipse.pde.internal.core.itarget.ITarget;
017: import org.eclipse.pde.internal.core.itarget.ITargetModel;
018: import org.eclipse.pde.internal.ui.PDEPlugin;
019: import org.eclipse.pde.internal.ui.PDEUIMessages;
020: import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
021: import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
022: import org.eclipse.pde.internal.ui.editor.PDEFormPage;
023: import org.eclipse.pde.internal.ui.editor.PDESection;
024: import org.eclipse.pde.internal.ui.parts.FormEntry;
025: import org.eclipse.swt.SWT;
026: import org.eclipse.swt.dnd.Clipboard;
027: import org.eclipse.swt.events.SelectionAdapter;
028: import org.eclipse.swt.events.SelectionEvent;
029: import org.eclipse.swt.layout.GridData;
030: import org.eclipse.swt.widgets.Button;
031: import org.eclipse.swt.widgets.Composite;
032: import org.eclipse.swt.widgets.Control;
033: import org.eclipse.swt.widgets.DirectoryDialog;
034: import org.eclipse.swt.widgets.Display;
035: import org.eclipse.swt.widgets.Label;
036: import org.eclipse.swt.widgets.Text;
037: import org.eclipse.ui.IActionBars;
038: import org.eclipse.ui.forms.IFormColors;
039: import org.eclipse.ui.forms.widgets.ExpandableComposite;
040: import org.eclipse.ui.forms.widgets.FormToolkit;
041: import org.eclipse.ui.forms.widgets.Section;
042:
043: public class TargetDefinitionSection extends PDESection {
044:
045: private FormEntry fNameEntry;
046: private FormEntry fPath;
047: private Button fUseDefault;
048: private Button fCustomPath;
049: private Button fFileSystem;
050: private Button fVariable;
051: private static int NUM_COLUMNS = 5;
052:
053: public TargetDefinitionSection(PDEFormPage page, Composite parent) {
054: super (page, parent, ExpandableComposite.TITLE_BAR);
055: createClient(getSection(), page.getEditor().getToolkit());
056: }
057:
058: /* (non-Javadoc)
059: * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
060: */
061: protected void createClient(Section section, FormToolkit toolkit) {
062: section.setLayout(FormLayoutFactory.createClearTableWrapLayout(
063: false, 1));
064: Composite client = toolkit.createComposite(section);
065: client.setLayout(FormLayoutFactory
066: .createSectionClientGridLayout(false, NUM_COLUMNS));
067:
068: IActionBars actionBars = getPage().getPDEEditor()
069: .getEditorSite().getActionBars();
070:
071: createNameEntry(client, toolkit, actionBars);
072:
073: createLocation(client, toolkit, actionBars);
074:
075: toolkit.paintBordersFor(client);
076: section.setClient(client);
077: section.setText(PDEUIMessages.TargetDefinitionSection_title);
078: GridData data = new GridData();
079: data.horizontalSpan = 2;
080: data.grabExcessHorizontalSpace = true;
081: data.horizontalAlignment = SWT.FILL;
082: section.setLayoutData(data);
083: // Register to be notified when the model changes
084: getModel().addModelChangedListener(this );
085: }
086:
087: /* (non-Javadoc)
088: * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
089: */
090: public void dispose() {
091: ITargetModel model = getModel();
092: if (model != null) {
093: model.removeModelChangedListener(this );
094: }
095: super .dispose();
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
100: */
101: public void modelChanged(IModelChangedEvent e) {
102: // No need to call super, handling world changed event here
103: if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
104: handleModelEventWorldChanged(e);
105: }
106: }
107:
108: /**
109: * @param event
110: */
111: private void handleModelEventWorldChanged(IModelChangedEvent event) {
112: // Perform the refresh
113: refresh();
114: // Note: A deferred selection event is fired from radio buttons when
115: // their value is toggled, the user switches to another page, and the
116: // user switches back to the same page containing the radio buttons
117: // This appears to be a result of a SWT bug.
118: // If the radio button is the last widget to have focus when leaving
119: // the page, an event will be fired when entering the page again.
120: // An event is not fired if the radio button does not have focus.
121: // The solution is to redirect focus to a stable widget.
122: getPage().setLastFocusControl(fNameEntry.getText());
123: }
124:
125: private void createNameEntry(Composite client, FormToolkit toolkit,
126: IActionBars actionBars) {
127: fNameEntry = new FormEntry(client, toolkit,
128: PDEUIMessages.TargetDefinitionSection_name, null, false);
129: fNameEntry.setFormEntryListener(new FormEntryAdapter(this ,
130: actionBars) {
131: public void textValueChanged(FormEntry entry) {
132: getTarget().setName(entry.getValue());
133: }
134: });
135: GridData gd = (GridData) fNameEntry.getText().getLayoutData();
136: gd.horizontalSpan = 4;
137: fNameEntry.setEditable(isEditable());
138: }
139:
140: private void createLocation(Composite client, FormToolkit toolkit,
141: IActionBars actionBars) {
142: Label label = toolkit.createLabel(client,
143: PDEUIMessages.TargetDefinitionSection_targetLocation);
144: label.setForeground(toolkit.getColors().getColor(
145: IFormColors.TITLE));
146:
147: fUseDefault = toolkit.createButton(client,
148: PDEUIMessages.TargetDefinitionSection_sameAsHost,
149: SWT.RADIO);
150: GridData gd = new GridData();
151: gd.horizontalSpan = 5;
152: gd.horizontalIndent = 15;
153: fUseDefault.setLayoutData(gd);
154: fUseDefault.addSelectionListener(new SelectionAdapter() {
155: public void widgetSelected(SelectionEvent e) {
156: if (fUseDefault.getSelection()) {
157: fPath.getText().setEditable(false);
158: fPath.setValue("", true); //$NON-NLS-1$
159: fFileSystem.setEnabled(false);
160: fVariable.setEnabled(false);
161: getLocationInfo().setDefault(true);
162: }
163: }
164: });
165:
166: fCustomPath = toolkit.createButton(client,
167: PDEUIMessages.TargetDefinitionSection_location,
168: SWT.RADIO);
169: gd = new GridData();
170: gd.horizontalIndent = 15;
171: fCustomPath.setLayoutData(gd);
172: fCustomPath.addSelectionListener(new SelectionAdapter() {
173: public void widgetSelected(SelectionEvent e) {
174: if (fCustomPath.getSelection()) {
175: ILocationInfo info = getLocationInfo();
176: fPath.getText().setEditable(true);
177: fPath.setValue(info.getPath(), true);
178: fFileSystem.setEnabled(true);
179: fVariable.setEnabled(true);
180: info.setDefault(false);
181: }
182: }
183: });
184:
185: fPath = new FormEntry(client, toolkit, null, null, false);
186: fPath.getText().setLayoutData(
187: new GridData(GridData.FILL_HORIZONTAL));
188: fPath.setFormEntryListener(new FormEntryAdapter(this ,
189: actionBars) {
190: public void textValueChanged(FormEntry entry) {
191: getLocationInfo().setPath(fPath.getValue());
192: }
193: });
194:
195: fFileSystem = toolkit.createButton(client,
196: PDEUIMessages.TargetDefinitionSection_fileSystem,
197: SWT.PUSH);
198: fFileSystem.addSelectionListener(new SelectionAdapter() {
199: public void widgetSelected(SelectionEvent e) {
200: handleBrowseFileSystem();
201: }
202: });
203:
204: fVariable = toolkit.createButton(client,
205: PDEUIMessages.TargetDefinitionSection_variables,
206: SWT.PUSH);
207: fVariable.addSelectionListener(new SelectionAdapter() {
208: public void widgetSelected(SelectionEvent e) {
209: handleInsertVariable();
210: }
211: });
212: }
213:
214: /* (non-Javadoc)
215: * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
216: */
217: public void commit(boolean onSave) {
218: fNameEntry.commit();
219: fPath.commit();
220: super .commit(onSave);
221: }
222:
223: /* (non-Javadoc)
224: * @see org.eclipse.pde.internal.ui.editor.PDESection#cancelEdit()
225: */
226: public void cancelEdit() {
227: fNameEntry.cancelEdit();
228: fPath.cancelEdit();
229: super .cancelEdit();
230: }
231:
232: /* (non-Javadoc)
233: * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
234: */
235: public void refresh() {
236: ITarget target = getTarget();
237: fNameEntry.setValue(target.getName(), true);
238: ILocationInfo info = getLocationInfo();
239: fUseDefault.setSelection(info.useDefault());
240: fCustomPath.setSelection(!info.useDefault());
241: String path = (info.useDefault()) ? "" : info.getPath(); //$NON-NLS-1$
242: fPath.setValue(path, true);
243: fPath.getText().setEditable(!info.useDefault());
244: fFileSystem.setEnabled(!info.useDefault());
245: fVariable.setEnabled(!info.useDefault());
246: super .refresh();
247: }
248:
249: public boolean canPaste(Clipboard clipboard) {
250: Display d = getSection().getDisplay();
251: Control c = d.getFocusControl();
252: if (c instanceof Text)
253: return true;
254: return false;
255: }
256:
257: protected void handleBrowseFileSystem() {
258: DirectoryDialog dialog = new DirectoryDialog(getSection()
259: .getShell());
260: String text = fPath.getValue();
261: if (text.length() == 0)
262: text = TargetEditor.LAST_PATH;
263: dialog.setFilterPath(text);
264: dialog.setText(PDEUIMessages.BaseBlock_dirSelection);
265: dialog.setMessage(PDEUIMessages.BaseBlock_dirChoose);
266: String result = dialog.open();
267: if (result != null) {
268: fPath.setValue(result);
269: getLocationInfo().setPath(result);
270: TargetEditor.LAST_PATH = result;
271: }
272: }
273:
274: private void handleInsertVariable() {
275: StringVariableSelectionDialog dialog = new StringVariableSelectionDialog(
276: PDEPlugin.getActiveWorkbenchShell());
277: if (dialog.open() == Window.OK) {
278: fPath.getText().insert(dialog.getVariableExpression());
279: // have to setValue to make sure getValue reflects the actual text in the Text object.
280: fPath.setValue(fPath.getText().getText());
281: getLocationInfo().setPath(fPath.getText().getText());
282: }
283: }
284:
285: private ILocationInfo getLocationInfo() {
286: ILocationInfo info = getTarget().getLocationInfo();
287: if (info == null) {
288: info = getModel().getFactory().createLocation();
289: getTarget().setLocationInfo(info);
290: }
291: return info;
292: }
293:
294: private ITarget getTarget() {
295: return getModel().getTarget();
296: }
297:
298: private ITargetModel getModel() {
299: return (ITargetModel) getPage().getPDEEditor()
300: .getAggregateModel();
301: }
302: }
|