KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > imports > FeatureImportWizardFirstPage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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.wizards.imports;
12
13 import java.io.File JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.core.runtime.Preferences;
19 import org.eclipse.jface.dialogs.Dialog;
20 import org.eclipse.jface.dialogs.IDialogSettings;
21 import org.eclipse.jface.wizard.WizardPage;
22 import org.eclipse.pde.internal.core.ICoreConstants;
23 import org.eclipse.pde.internal.core.PDECore;
24 import org.eclipse.pde.internal.ui.IHelpContextIds;
25 import org.eclipse.pde.internal.ui.PDEUIMessages;
26 import org.eclipse.pde.internal.ui.util.SWTUtil;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.ModifyEvent;
29 import org.eclipse.swt.events.ModifyListener;
30 import org.eclipse.swt.events.SelectionAdapter;
31 import org.eclipse.swt.events.SelectionEvent;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Combo;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.DirectoryDialog;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.ui.PlatformUI;
41
42 public class FeatureImportWizardFirstPage extends WizardPage {
43
44     private static final String JavaDoc SETTINGS_DROPLOCATION = "droplocation"; //$NON-NLS-1$
45
private static final String JavaDoc SETTINGS_DOOTHER = "doother"; //$NON-NLS-1$
46
private static final String JavaDoc SETTINGS_NOT_BINARY = "notbinary"; //$NON-NLS-1$
47

48     private Label fOtherLocationLabel;
49     private Button fRuntimeLocationButton;
50     private Button fBrowseButton;
51     private Combo fDropLocation;
52     private Button fBinaryButton;
53
54     public FeatureImportWizardFirstPage() {
55         super("FeatureImportWizardPage"); //$NON-NLS-1$
56
setTitle(PDEUIMessages.FeatureImportWizard_FirstPage_title); //$NON-NLS-1$
57
setDescription(PDEUIMessages.FeatureImportWizard_FirstPage_desc); //$NON-NLS-1$
58
}
59
60     /*
61      * @see IDialogPage#createControl(Composite)
62      */

63     public void createControl(Composite parent) {
64         initializeDialogUnits(parent);
65
66         Composite composite = new Composite(parent, SWT.NONE);
67
68         GridLayout layout = new GridLayout();
69         layout.numColumns = 3;
70         composite.setLayout(layout);
71
72         fRuntimeLocationButton = new Button(composite, SWT.CHECK);
73         fillHorizontal(fRuntimeLocationButton, 3, false);
74         fRuntimeLocationButton.setText(
75             PDEUIMessages.FeatureImportWizard_FirstPage_runtimeLocation); //$NON-NLS-1$
76

77         fOtherLocationLabel = new Label(composite, SWT.NULL);
78         fOtherLocationLabel.setText(
79             PDEUIMessages.FeatureImportWizard_FirstPage_otherFolder); //$NON-NLS-1$
80

81         fDropLocation = new Combo(composite, SWT.DROP_DOWN);
82         fillHorizontal(fDropLocation, 1, true);
83
84         fBrowseButton = new Button(composite, SWT.PUSH);
85         fBrowseButton.setText(PDEUIMessages.FeatureImportWizard_FirstPage_browse); //$NON-NLS-1$
86
fBrowseButton.addSelectionListener(new SelectionAdapter() {
87             public void widgetSelected(SelectionEvent e) {
88                 IPath chosen = chooseDropLocation();
89                 if (chosen != null)
90                     fDropLocation.setText(chosen.toOSString());
91             }
92         });
93         fBrowseButton.setLayoutData(new GridData());
94         SWTUtil.setButtonDimensionHint(fBrowseButton);
95
96         fBinaryButton = new Button(composite, SWT.CHECK);
97         fillHorizontal(fBinaryButton, 3, false);
98         fBinaryButton.setText(
99             PDEUIMessages.FeatureImportWizard_FirstPage_binaryImport); //$NON-NLS-1$
100

101         initializeFields(getDialogSettings());
102         hookListeners();
103
104         setControl(composite);
105         Dialog.applyDialogFont(composite);
106         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.FEATURE_IMPORT_FIRST_PAGE);
107     }
108
109     private String JavaDoc getTargetHome() {
110         Preferences preferences = PDECore.getDefault().getPluginPreferences();
111         return preferences.getString(ICoreConstants.PLATFORM_PATH);
112     }
113
114     private void hookListeners() {
115         fRuntimeLocationButton.addSelectionListener(new SelectionAdapter() {
116             public void widgetSelected(SelectionEvent e) {
117                 setOtherEnabled(!fRuntimeLocationButton.getSelection());
118                 validateDropLocation();
119                 if (fRuntimeLocationButton.getSelection()) {
120                     fDropLocation.setText(getTargetHome());
121                 }
122             }
123         });
124
125         fDropLocation.addSelectionListener(new SelectionAdapter() {
126             public void widgetSelected(SelectionEvent e) {
127                 validateDropLocation();
128             }
129         });
130         fDropLocation.addModifyListener(new ModifyListener() {
131             public void modifyText(ModifyEvent e) {
132                 validateDropLocation();
133             }
134         });
135     }
136
137     private GridData fillHorizontal(Control control, int span, boolean grab) {
138         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
139         gd.horizontalSpan = span;
140         gd.grabExcessHorizontalSpace = grab;
141         control.setLayoutData(gd);
142         return gd;
143     }
144
145     private void initializeFields(IDialogSettings initialSettings) {
146         String JavaDoc[] dropItems = new String JavaDoc[0];
147         boolean doOther = false;
148         boolean binary = true;
149
150         if (initialSettings != null) {
151             doOther = initialSettings.getBoolean(SETTINGS_DOOTHER);
152             binary = !initialSettings.getBoolean(SETTINGS_NOT_BINARY);
153
154             ArrayList JavaDoc items = new ArrayList JavaDoc();
155             for (int i = 0; i < 6; i++) {
156                 String JavaDoc curr =
157                     initialSettings.get(
158                         SETTINGS_DROPLOCATION + String.valueOf(i));
159                 if (curr != null && !items.contains(curr)) {
160                     items.add(curr);
161                 }
162             }
163             dropItems = (String JavaDoc[]) items.toArray(new String JavaDoc[items.size()]);
164         }
165         fDropLocation.setItems(dropItems);
166         fRuntimeLocationButton.setSelection(!doOther);
167         setOtherEnabled(doOther);
168         if (doOther) {
169             if (dropItems.length > 0)
170                 fDropLocation.setText(dropItems[0]);
171         } else {
172             fDropLocation.setText(getTargetHome());
173         }
174         fBinaryButton.setSelection(binary);
175
176         validateDropLocation();
177     }
178
179     private void setOtherEnabled(boolean enabled) {
180         fOtherLocationLabel.setEnabled(enabled);
181         fDropLocation.setEnabled(enabled);
182         fBrowseButton.setEnabled(enabled);
183     }
184
185     public void storeSettings(boolean finishPressed) {
186         IDialogSettings settings = getDialogSettings();
187         boolean other = !fRuntimeLocationButton.getSelection();
188         boolean binary = fBinaryButton.getSelection();
189         if (finishPressed || fDropLocation.getText().length() > 0 && other) {
190             settings.put(
191                 SETTINGS_DROPLOCATION + String.valueOf(0),
192                 fDropLocation.getText());
193             String JavaDoc[] items = fDropLocation.getItems();
194             int nEntries = Math.min(items.length, 5);
195             for (int i = 0; i < nEntries; i++) {
196                 settings.put(
197                     SETTINGS_DROPLOCATION + String.valueOf(i + 1),
198                     items[i]);
199             }
200         }
201         if (finishPressed) {
202             settings.put(SETTINGS_DOOTHER, other);
203             settings.put(SETTINGS_NOT_BINARY, !binary);
204         }
205     }
206
207     /**
208      * Browses for a drop location.
209      */

210     private IPath chooseDropLocation() {
211         DirectoryDialog dialog = new DirectoryDialog(getShell());
212         dialog.setFilterPath(fDropLocation.getText());
213         dialog.setText(PDEUIMessages.FeatureImportWizard_messages_folder_title); //$NON-NLS-1$
214
dialog.setMessage(PDEUIMessages.FeatureImportWizard_messages_folder_message); //$NON-NLS-1$
215
String JavaDoc res = dialog.open();
216         if (res != null) {
217             return new Path(res);
218         }
219         return null;
220     }
221
222     private void validateDropLocation() {
223         String JavaDoc errorMessage = null;
224         if (isOtherLocation()) {
225             IPath curr = getDropLocation();
226             if (curr.segmentCount() == 0) {
227                 errorMessage =
228                     PDEUIMessages.FeatureImportWizard_errors_locationMissing; //$NON-NLS-1$
229
} else if (!Path.ROOT.isValidPath(fDropLocation.getText())) {
230                 errorMessage =
231                     PDEUIMessages.FeatureImportWizard_errors_buildFolderInvalid; //$NON-NLS-1$
232
} else {
233
234                 File JavaDoc file = curr.toFile();
235                 if (!file.exists() || !file.isDirectory()) {
236                     errorMessage =
237                         PDEUIMessages.FeatureImportWizard_errors_buildFolderMissing; //$NON-NLS-1$
238
}
239             }
240         }
241         setErrorMessage(errorMessage);
242         setPageComplete(errorMessage == null);
243     }
244     
245     public boolean isBinary(){
246         return fBinaryButton.getSelection();
247     }
248
249     /**
250      * Returns the drop location.
251      */

252     public IPath getDropLocation() {
253         return new Path(fDropLocation.getText());
254     }
255
256     public boolean isOtherLocation() {
257         return !fRuntimeLocationButton.getSelection();
258     }
259     
260     /* (non-Javadoc)
261      * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
262      */

263     public boolean isPageComplete() {
264         return getErrorMessage() == null;
265     }
266 }
267
Popular Tags