KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
14
15 import org.eclipse.core.runtime.Path;
16 import org.eclipse.jface.dialogs.IDialogSettings;
17 import org.eclipse.pde.internal.ui.PDEPlugin;
18 import org.eclipse.pde.internal.ui.PDEUIMessages;
19 import org.eclipse.pde.internal.ui.util.SWTUtil;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.ModifyEvent;
22 import org.eclipse.swt.events.ModifyListener;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Combo;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.DirectoryDialog;
32
33 public class ExportDestinationTab extends AbstractExportTab {
34
35     protected static final String JavaDoc S_EXPORT_DIRECTORY = "exportDirectory"; //$NON-NLS-1$
36
protected static final String JavaDoc S_DESTINATION = "destination"; //$NON-NLS-1$
37
protected static final String JavaDoc S_ZIP_FILENAME = "zipFileName"; //$NON-NLS-1$
38

39     protected static String JavaDoc ZIP_EXTENSION = ".zip"; //$NON-NLS-1$
40

41     protected Button fArchiveFileButton;
42     protected Combo fArchiveCombo;
43     protected Button fBrowseFile;
44     protected Button fDirectoryButton;
45     protected Combo fDirectoryCombo;
46     protected Button fBrowseDirectory;
47
48     public ExportDestinationTab(AbstractExportWizardPage page) {
49         super(page);
50     }
51
52     public Control createControl(Composite parent) {
53         Composite composite = new Composite(parent, SWT.NONE);
54         composite.setLayout(new GridLayout(2, false));
55         composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
56
57         fDirectoryButton = new Button(composite, SWT.RADIO);
58         fDirectoryButton.setText(PDEUIMessages.ExportWizard_directory);
59         GridData gd = new GridData();
60         gd.horizontalSpan = 2;
61         fDirectoryButton.setLayoutData(gd);
62         
63         fDirectoryCombo = new Combo(composite, SWT.BORDER);
64         gd = new GridData(GridData.FILL_HORIZONTAL);
65         gd.horizontalIndent = 15;
66         fDirectoryCombo.setLayoutData(gd);
67
68         fBrowseDirectory = new Button(composite, SWT.PUSH);
69         fBrowseDirectory.setText(PDEUIMessages.ExportWizard_browse);
70         fBrowseDirectory.setLayoutData(new GridData());
71         SWTUtil.setButtonDimensionHint(fBrowseDirectory);
72
73         fArchiveFileButton = new Button(composite, SWT.RADIO);
74         fArchiveFileButton.setText(PDEUIMessages.ExportWizard_archive);
75         gd = new GridData();
76         gd.horizontalSpan = 2;
77         fArchiveFileButton.setLayoutData(gd);
78
79         fArchiveCombo = new Combo(composite, SWT.BORDER);
80         gd = new GridData(GridData.FILL_HORIZONTAL);
81         gd.horizontalIndent = 15;
82         fArchiveCombo.setLayoutData(gd);
83
84         fBrowseFile = new Button(composite, SWT.PUSH);
85         fBrowseFile.setText(PDEUIMessages.ExportWizard_browse);
86         fBrowseFile.setLayoutData(new GridData());
87         SWTUtil.setButtonDimensionHint(fBrowseFile);
88
89         return composite;
90     }
91
92     protected void initialize(IDialogSettings settings) {
93         String JavaDoc toDirectory = settings.get(S_EXPORT_DIRECTORY);
94         boolean useDirectory = toDirectory == null || "true".equals(toDirectory); //$NON-NLS-1$
95
fDirectoryButton.setSelection(useDirectory);
96         fArchiveFileButton.setSelection(!useDirectory);
97         toggleDestinationGroup(useDirectory);
98         initializeCombo(settings, S_DESTINATION, fDirectoryCombo);
99         initializeCombo(settings, S_ZIP_FILENAME, fArchiveCombo);
100         hookListeners();
101     }
102
103     protected void toggleDestinationGroup(boolean useDirectory) {
104         fArchiveCombo.setEnabled(!useDirectory);
105         fBrowseFile.setEnabled(!useDirectory);
106         fDirectoryCombo.setEnabled(useDirectory);
107         fBrowseDirectory.setEnabled(useDirectory);
108     }
109
110     protected void hookListeners() {
111         fArchiveFileButton.addSelectionListener(new SelectionAdapter() {
112             public void widgetSelected(SelectionEvent e) {
113                 toggleDestinationGroup(!fArchiveFileButton.getSelection());
114                 fPage.pageChanged();
115             }
116         });
117
118         fBrowseFile.addSelectionListener(new SelectionAdapter() {
119             public void widgetSelected(SelectionEvent e) {
120                 chooseFile(fArchiveCombo, "*" + ZIP_EXTENSION); //$NON-NLS-1$
121
}
122         });
123
124         fArchiveCombo.addModifyListener(new ModifyListener() {
125             public void modifyText(ModifyEvent e) {
126                 fPage.pageChanged();
127             }
128         });
129
130         fDirectoryCombo.addModifyListener(new ModifyListener() {
131             public void modifyText(ModifyEvent e) {
132                 fPage.pageChanged();
133             }
134         });
135
136         fBrowseDirectory.addSelectionListener(new SelectionAdapter() {
137             public void widgetSelected(SelectionEvent e) {
138                 chooseDestination();
139             }
140         });
141     }
142
143     private void chooseDestination() {
144         DirectoryDialog dialog = new DirectoryDialog(fPage.getShell(), SWT.SAVE);
145         String JavaDoc path = fDirectoryCombo.getText();
146         if (path.trim().length() == 0)
147             path = PDEPlugin.getWorkspace().getRoot().getLocation().toString();
148         dialog.setFilterPath(path);
149         dialog.setText(PDEUIMessages.ExportWizard_dialog_title);
150         dialog.setMessage(PDEUIMessages.ExportWizard_dialog_message);
151         String JavaDoc res = dialog.open();
152         if (res != null) {
153             if (fDirectoryCombo.indexOf(res) == -1)
154                 fDirectoryCombo.add(res, 0);
155             fDirectoryCombo.setText(res);
156         }
157     }
158     
159     protected void saveSettings(IDialogSettings settings) {
160         settings.put(S_EXPORT_DIRECTORY, fDirectoryButton.getSelection());
161         saveCombo(settings, S_DESTINATION, fDirectoryCombo);
162         saveCombo(settings, S_ZIP_FILENAME, fArchiveCombo);
163     }
164
165     protected String JavaDoc validate() {
166         if (fArchiveFileButton.getSelection()
167                 && fArchiveCombo.getText().trim().length() == 0)
168             return PDEUIMessages.ExportWizard_status_nofile;
169         if (fDirectoryButton.getSelection()
170                 && fDirectoryCombo.getText().trim().length() == 0)
171             return PDEUIMessages.ExportWizard_status_nodirectory;
172         return null;
173     }
174
175     protected String JavaDoc getFileName() {
176         if (fArchiveFileButton.getSelection()) {
177             String JavaDoc path = fArchiveCombo.getText();
178             if (path != null && path.length() > 0) {
179                 String JavaDoc fileName = new Path(path).lastSegment();
180                 if (!fileName.endsWith(ZIP_EXTENSION)) {
181                     fileName += ZIP_EXTENSION;
182                 }
183                 return fileName;
184             }
185         }
186         return null;
187     }
188
189     protected String JavaDoc getDestination() {
190         if (fArchiveFileButton.getSelection()) {
191             String JavaDoc path = fArchiveCombo.getText();
192             if (path.length() > 0) {
193                 path = new Path(path).removeLastSegments(1).toOSString();
194                 return new File JavaDoc(path).getAbsolutePath();
195             }
196             return ""; //$NON-NLS-1$
197
}
198
199         File JavaDoc dir = new File JavaDoc(fDirectoryCombo.getText().trim());
200         return dir.getAbsolutePath();
201     }
202
203     protected boolean doExportToDirectory() {
204         return fDirectoryButton.getSelection();
205     }
206
207 }
208
Popular Tags