KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > target > TargetDefinitionSection


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.target;
12
13 import org.eclipse.debug.ui.StringVariableSelectionDialog;
14 import org.eclipse.jface.window.Window;
15 import org.eclipse.pde.core.IModelChangedEvent;
16 import org.eclipse.pde.internal.core.itarget.ILocationInfo;
17 import org.eclipse.pde.internal.core.itarget.ITarget;
18 import org.eclipse.pde.internal.core.itarget.ITargetModel;
19 import org.eclipse.pde.internal.ui.PDEPlugin;
20 import org.eclipse.pde.internal.ui.PDEUIMessages;
21 import org.eclipse.pde.internal.ui.editor.FormEntryAdapter;
22 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory;
23 import org.eclipse.pde.internal.ui.editor.PDEFormPage;
24 import org.eclipse.pde.internal.ui.editor.PDESection;
25 import org.eclipse.pde.internal.ui.parts.FormEntry;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.dnd.Clipboard;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.widgets.Button;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.DirectoryDialog;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Text;
38 import org.eclipse.ui.IActionBars;
39 import org.eclipse.ui.forms.IFormColors;
40 import org.eclipse.ui.forms.widgets.ExpandableComposite;
41 import org.eclipse.ui.forms.widgets.FormToolkit;
42 import org.eclipse.ui.forms.widgets.Section;
43 import org.eclipse.ui.forms.widgets.TableWrapData;
44
45 public class TargetDefinitionSection extends PDESection {
46
47     private FormEntry fNameEntry;
48     private FormEntry fPath;
49     private Button fUseDefault;
50     private Button fCustomPath;
51     private Button fFileSystem;
52     private Button fVariable;
53     private static int NUM_COLUMNS = 5;
54     
55     public TargetDefinitionSection(PDEFormPage page, Composite parent) {
56         super(page, parent, ExpandableComposite.TITLE_BAR);
57         createClient(getSection(), page.getEditor().getToolkit());
58     }
59
60     /* (non-Javadoc)
61      * @see org.eclipse.pde.internal.ui.editor.PDESection#createClient(org.eclipse.ui.forms.widgets.Section, org.eclipse.ui.forms.widgets.FormToolkit)
62      */

63     protected void createClient(Section section, FormToolkit toolkit) {
64         section.setLayout(FormLayoutFactory.createClearTableWrapLayout(false, 1));
65         Composite client = toolkit.createComposite(section);
66         client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, NUM_COLUMNS));
67
68         IActionBars actionBars = getPage().getPDEEditor().getEditorSite().getActionBars();
69         
70         createNameEntry(client, toolkit, actionBars);
71         
72         createLocation(client, toolkit, actionBars);
73         
74         toolkit.paintBordersFor(client);
75         section.setClient(client);
76         section.setText(PDEUIMessages.TargetDefinitionSection_title);
77         TableWrapData data = new TableWrapData(TableWrapData.FILL_GRAB);
78         data.colspan = 2;
79         section.setLayoutData(data);
80         
81         // Register to be notified when the model changes
82
getModel().addModelChangedListener(this);
83     }
84     
85     /* (non-Javadoc)
86      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
87      */

88     public void dispose() {
89         ITargetModel model = getModel();
90         if (model != null) {
91             model.removeModelChangedListener(this);
92         }
93         super.dispose();
94     }
95     
96     /* (non-Javadoc)
97      * @see org.eclipse.pde.internal.ui.editor.PDESection#modelChanged(org.eclipse.pde.core.IModelChangedEvent)
98      */

99     public void modelChanged(IModelChangedEvent e) {
100         // No need to call super, handling world changed event here
101
if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) {
102             handleModelEventWorldChanged(e);
103         }
104     }
105     
106     /**
107      * @param event
108      */

109     private void handleModelEventWorldChanged(IModelChangedEvent event) {
110         // Perform the refresh
111
refresh();
112         // Note: A deferred selection event is fired from radio buttons when
113
// their value is toggled, the user switches to another page, and the
114
// user switches back to the same page containing the radio buttons
115
// This appears to be a result of a SWT bug.
116
// If the radio button is the last widget to have focus when leaving
117
// the page, an event will be fired when entering the page again.
118
// An event is not fired if the radio button does not have focus.
119
// The solution is to redirect focus to a stable widget.
120
getPage().setLastFocusControl(fNameEntry.getText());
121     }
122     
123     private void createNameEntry(Composite client, FormToolkit toolkit, IActionBars actionBars) {
124         fNameEntry = new FormEntry(client, toolkit, PDEUIMessages.TargetDefinitionSection_name, null, false);
125         fNameEntry.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
126             public void textValueChanged(FormEntry entry) {
127                 getTarget().setName(entry.getValue());
128             }
129         });
130         GridData gd = (GridData)fNameEntry.getText().getLayoutData();
131         gd.horizontalSpan = 4;
132         fNameEntry.setEditable(isEditable());
133     }
134
135     private void createLocation(Composite client, FormToolkit toolkit, IActionBars actionBars) {
136         Label label = toolkit.createLabel(client, PDEUIMessages.TargetDefinitionSection_targetLocation);
137         label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
138         
139         fUseDefault = toolkit.createButton(client, PDEUIMessages.TargetDefinitionSection_sameAsHost, SWT.RADIO);
140         GridData gd = new GridData();
141         gd.horizontalSpan = 5;
142         gd.horizontalIndent = 15;
143         fUseDefault.setLayoutData(gd);
144         fUseDefault.addSelectionListener(new SelectionAdapter() {
145             public void widgetSelected(SelectionEvent e) {
146                 if (fUseDefault.getSelection()) {
147                     fPath.getText().setEditable(false);
148                     fPath.setValue("", true); //$NON-NLS-1$
149
fFileSystem.setEnabled(false);
150                     fVariable.setEnabled(false);
151                     getLocationInfo().setDefault(true);
152                 }
153             }
154         });
155         
156         fCustomPath = toolkit.createButton(client, PDEUIMessages.TargetDefinitionSection_location, SWT.RADIO);
157         gd = new GridData();
158         gd.horizontalIndent = 15;
159         fCustomPath.setLayoutData(gd);
160         fCustomPath.addSelectionListener(new SelectionAdapter() {
161             public void widgetSelected(SelectionEvent e) {
162                 if (fCustomPath.getSelection()) {
163                     ILocationInfo info = getLocationInfo();
164                     fPath.getText().setEditable(true);
165                     fPath.setValue(info.getPath(), true);
166                     fFileSystem.setEnabled(true);
167                     fVariable.setEnabled(true);
168                     info.setDefault(false);
169                 }
170             }
171         });
172         
173         fPath = new FormEntry(client, toolkit, null, null, false);
174         fPath.getText().setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
175         fPath.setFormEntryListener(new FormEntryAdapter(this, actionBars) {
176             public void textValueChanged(FormEntry entry) {
177                 getLocationInfo().setPath(fPath.getValue());
178             }
179         });
180         
181         fFileSystem = toolkit.createButton(client, PDEUIMessages.TargetDefinitionSection_fileSystem, SWT.PUSH);
182         fFileSystem.addSelectionListener(new SelectionAdapter() {
183             public void widgetSelected(SelectionEvent e) {
184                 handleBrowseFileSystem();
185             }
186         });
187         
188         fVariable = toolkit.createButton(client, PDEUIMessages.TargetDefinitionSection_variables, SWT.PUSH);
189         fVariable.addSelectionListener(new SelectionAdapter() {
190             public void widgetSelected(SelectionEvent e) {
191                 handleInsertVariable();
192             }
193         });
194     }
195     
196     /* (non-Javadoc)
197      * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
198      */

199     public void commit(boolean onSave) {
200         fNameEntry.commit();
201         fPath.commit();
202         super.commit(onSave);
203     }
204     
205     /* (non-Javadoc)
206      * @see org.eclipse.pde.internal.ui.editor.PDESection#cancelEdit()
207      */

208     public void cancelEdit() {
209         fNameEntry.cancelEdit();
210         fPath.cancelEdit();
211         super.cancelEdit();
212     }
213     
214     /* (non-Javadoc)
215      * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
216      */

217     public void refresh() {
218         ITarget target = getTarget();
219         fNameEntry.setValue(target.getName(), true);
220         ILocationInfo info = getLocationInfo();
221         fUseDefault.setSelection(info.useDefault());
222         fCustomPath.setSelection(!info.useDefault());
223         String JavaDoc path = (info.useDefault()) ? "" : info.getPath(); //$NON-NLS-1$
224
fPath.setValue(path,true);
225         fPath.getText().setEditable(!info.useDefault());
226         fFileSystem.setEnabled(!info.useDefault());
227         fVariable.setEnabled(!info.useDefault());
228         super.refresh();
229     }
230     
231     public boolean canPaste(Clipboard clipboard) {
232         Display d = getSection().getDisplay();
233         Control c = d.getFocusControl();
234         if (c instanceof Text)
235             return true;
236         return false;
237     }
238     
239     protected void handleBrowseFileSystem() {
240         DirectoryDialog dialog = new DirectoryDialog(getSection().getShell());
241         String JavaDoc text = fPath.getValue();
242         if (text.length() == 0)
243             text = TargetEditor.LAST_PATH;
244         dialog.setFilterPath(text);
245         dialog.setText(PDEUIMessages.BaseBlock_dirSelection);
246         dialog.setMessage(PDEUIMessages.BaseBlock_dirChoose);
247         String JavaDoc result = dialog.open();
248         if (result != null) {
249             fPath.setValue(result);
250             getLocationInfo().setPath(result);
251             TargetEditor.LAST_PATH = result;
252         }
253     }
254     
255     private void handleInsertVariable() {
256         StringVariableSelectionDialog dialog =
257                     new StringVariableSelectionDialog(PDEPlugin.getActiveWorkbenchShell());
258         if (dialog.open() == Window.OK) {
259             fPath.getText().insert(dialog.getVariableExpression());
260             // have to setValue to make sure getValue reflects the actual text in the Text object.
261
fPath.setValue(fPath.getText().getText());
262             getLocationInfo().setPath(fPath.getText().getText());
263         }
264     }
265     
266     private ILocationInfo getLocationInfo() {
267         ILocationInfo info = getTarget().getLocationInfo();
268         if (info == null) {
269             info = getModel().getFactory().createLocation();
270             getTarget().setLocationInfo(info);
271         }
272         return info;
273     }
274     
275     private ITarget getTarget() {
276         return getModel().getTarget();
277     }
278     
279     private ITargetModel getModel() {
280         return (ITargetModel)getPage().getPDEEditor().getAggregateModel();
281     }
282 }
283
Popular Tags