KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > exports > ProductConfigurationSection


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.exports;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.window.Window;
22 import org.eclipse.pde.internal.ui.IPDEUIConstants;
23 import org.eclipse.pde.internal.ui.PDEPlugin;
24 import org.eclipse.pde.internal.ui.PDEUIMessages;
25 import org.eclipse.pde.internal.ui.util.FileExtensionFilter;
26 import org.eclipse.pde.internal.ui.util.FileValidator;
27 import org.eclipse.pde.internal.ui.util.SWTUtil;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Combo;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Group;
40 import org.eclipse.swt.widgets.Label;
41 import org.eclipse.swt.widgets.Text;
42 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
43 import org.eclipse.ui.model.WorkbenchContentProvider;
44 import org.eclipse.ui.model.WorkbenchLabelProvider;
45
46 public class ProductConfigurationSection {
47     
48     private static final String JavaDoc S_PRODUCT_CONFIG = "productConfig"; //$NON-NLS-1$
49
private static final String JavaDoc S_PRODUCT_ROOT = "productRoot"; //$NON-NLS-1$
50

51     private Text fProductRootText;
52     private Combo fProductCombo;
53
54     private ProductExportWizardPage fPage;
55
56     public ProductConfigurationSection(ProductExportWizardPage page) {
57         fPage = page;
58     }
59     
60     public Control createControl(Composite parent) {
61         Group group = new Group(parent, SWT.NONE);
62         group.setLayout(new GridLayout(3, false));
63         group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
64         group.setText(PDEUIMessages.ProductExportWizardPage_productGroup);
65         
66         Label label = new Label(group, SWT.NONE);
67         label.setText(PDEUIMessages.ProductExportWizardPage_config);
68         
69         fProductCombo = new Combo(group, SWT.BORDER);
70         fProductCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
71         
72         Button browse = new Button(group, SWT.PUSH);
73         browse.setText(PDEUIMessages.ProductExportWizardPage_browse);
74         browse.setLayoutData(new GridData());
75         browse.addSelectionListener(new SelectionAdapter() {
76             public void widgetSelected(SelectionEvent e) {
77                 handleBrowse();
78             }
79         });
80         SWTUtil.setButtonDimensionHint(browse);
81         
82         label = new Label(group, SWT.NONE);
83         label.setText(PDEUIMessages.ProductExportWizardPage_root);
84         
85         fProductRootText = new Text(group, SWT.SINGLE|SWT.BORDER);
86         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
87         gd.horizontalSpan = 2;
88         fProductRootText.setLayoutData(gd);
89         return group;
90     }
91     
92     private void handleBrowse() {
93         ElementTreeSelectionDialog dialog =
94             new ElementTreeSelectionDialog(
95                 fPage.getShell(),
96                 new WorkbenchLabelProvider(),
97                 new WorkbenchContentProvider());
98                 
99         dialog.setValidator(new FileValidator());
100         dialog.setAllowMultiple(false);
101         dialog.setTitle(PDEUIMessages.ProductExportWizardPage_fileSelection);
102         dialog.setMessage(PDEUIMessages.ProductExportWizardPage_productSelection);
103         dialog.addFilter(new FileExtensionFilter("product")); //$NON-NLS-1$
104
dialog.setInput(PDEPlugin.getWorkspace().getRoot());
105         IFile product = getProductFile();
106         if (product != null) dialog.setInitialSelection(product);
107
108         if (dialog.open() == Window.OK) {
109             IFile file = (IFile)dialog.getFirstResult();
110             String JavaDoc value = file.getFullPath().toString();
111             if (fProductCombo.indexOf(value) == -1)
112                 fProductCombo.add(value, 0);
113             fProductCombo.setText(value);
114         }
115     }
116     
117     protected void hookListeners() {
118         fProductCombo.addModifyListener(new ModifyListener() {
119             public void modifyText(ModifyEvent e) {
120                 updateProductFields();
121                 fPage.pageChanged();
122             }
123         });
124     }
125     
126     protected void initialize(IStructuredSelection selection, IDialogSettings settings) {
127         for (int i = 0; i < 6; i++) {
128             String JavaDoc curr = settings.get(S_PRODUCT_CONFIG + String.valueOf(i));
129             if (curr != null && fProductCombo.indexOf(curr) == -1) {
130                 IFile file = getProductFile(curr);
131                 if (file.exists())
132                     fProductCombo.add(curr);
133             }
134         }
135
136         if (selection.size() > 0) {
137             Object JavaDoc object = selection.getFirstElement();
138             if (object instanceof IFile) {
139                 IFile file = (IFile)object;
140                 if ("product".equals(file.getFileExtension())) { //$NON-NLS-1$
141
String JavaDoc entry = file.getFullPath().toString();
142                     if (fProductCombo.indexOf(entry) == -1)
143                         fProductCombo.add(entry, 0);
144                     fProductCombo.setText(entry);
145                 }
146             } else if (object instanceof IContainer) {
147                 IContainer container = (IContainer)object;
148                 try {
149                     if (container.isAccessible()) {
150                         IResource[] resources = container.members();
151                         for (int i = 0; i < resources.length; i++) {
152                             IResource resource = resources[i];
153                             if (resource instanceof IFile && resource.getName().endsWith(".product")) { //$NON-NLS-1$
154
String JavaDoc path = resource.getFullPath().toString();
155                                 if (fProductCombo.indexOf(path) == -1)
156                                     fProductCombo.add(path, 0);
157                             }
158                         }
159                     }
160                     if (fProductCombo.getItemCount() > 0)
161                         fProductCombo.setText(fProductCombo.getItem(0));
162                 } catch (CoreException e) {
163                 }
164             }
165         } else if (fProductCombo.getItemCount() > 0) {
166             fProductCombo.setText(fProductCombo.getItem(0));
167         }
168         
169         setRoot(settings);
170         hookListeners();
171     }
172     
173     protected IFile getProductFile() {
174         return getProductFile(fProductCombo.getText().trim());
175     }
176     
177     protected IFile getProductFile(String JavaDoc path) {
178         if (path == null || path.length() == 0)
179             return null;
180         
181         IPath thePath = new Path(path);
182         return thePath.segmentCount() < 2
183                     ? null
184                     : PDEPlugin.getWorkspace().getRoot().getFile(new Path(path));
185     }
186     
187     protected void updateProductFields() {
188         IFile file = getProductFile();
189         String JavaDoc root = null;
190         try {
191             if (file != null && file.exists())
192                 root = file.getPersistentProperty(IPDEUIConstants.DEFAULT_PRODUCT_EXPORT_ROOT);
193         } catch (CoreException e) {
194         }
195         if (root != null)
196             fProductRootText.setText(root);
197         
198         fPage.updateProductFields();
199     }
200
201     protected void setRoot(IDialogSettings settings) {
202         IFile file = getProductFile();
203         String JavaDoc root = null;
204         try {
205             if (file != null && file.exists())
206                 root = file.getPersistentProperty(IPDEUIConstants.DEFAULT_PRODUCT_EXPORT_ROOT);
207         } catch (CoreException e) {
208         }
209         if (root == null)
210             root = settings.get(S_PRODUCT_CONFIG);
211         if (root == null)
212             root = "eclipse"; //$NON-NLS-1$
213
fProductRootText.setText(root);
214     }
215     
216     protected String JavaDoc getRootDirectory() {
217         return fProductRootText.getText().trim();
218     }
219     
220     protected void saveSettings(IDialogSettings settings) {
221         saveCombo(settings);
222         settings.put(S_PRODUCT_ROOT, fProductRootText.getText().trim());
223         IFile file = getProductFile();
224         try {
225             if (file != null && file.exists()) {
226                 file.setPersistentProperty(IPDEUIConstants.DEFAULT_PRODUCT_EXPORT_ROOT, getRootDirectory());
227             }
228         } catch (CoreException e) {
229         }
230     }
231
232     protected void saveCombo(IDialogSettings settings) {
233         if (fProductCombo.getText().trim().length() > 0) {
234             settings.put(S_PRODUCT_CONFIG + String.valueOf(0), fProductCombo.getText().trim());
235             String JavaDoc[] items = fProductCombo.getItems();
236             int nEntries = Math.min(items.length, 5);
237             for (int i = 0; i < nEntries; i++) {
238                 settings.put(S_PRODUCT_CONFIG + String.valueOf(i + 1), items[i].trim());
239             }
240         }
241     }
242     
243     protected String JavaDoc validate() {
244         String JavaDoc configLocation = fProductCombo.getText().trim();
245         if (configLocation.length() == 0)
246             return PDEUIMessages.ProductExportWizardPage_noProduct;
247
248         IPath path = new Path(configLocation);
249         IResource resource = PDEPlugin.getWorkspace().getRoot().findMember(path);
250         if (resource == null || !(resource instanceof IFile))
251             return PDEUIMessages.ProductExportWizardPage_productNotExists;
252         
253         if (!resource.getName().endsWith(".product")) //$NON-NLS-1$
254
return PDEUIMessages.ProductExportWizardPage_wrongExtension;
255         
256         return null;
257     }
258     
259 }
260
Popular Tags