001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 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.wizards.exports;
011:
012: import java.io.File;
013:
014: import org.eclipse.core.runtime.Path;
015: import org.eclipse.jface.dialogs.IDialogSettings;
016: import org.eclipse.pde.internal.ui.PDEPlugin;
017: import org.eclipse.pde.internal.ui.PDEUIMessages;
018: import org.eclipse.pde.internal.ui.util.SWTUtil;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.events.ModifyEvent;
021: import org.eclipse.swt.events.ModifyListener;
022: import org.eclipse.swt.events.SelectionAdapter;
023: import org.eclipse.swt.events.SelectionEvent;
024: import org.eclipse.swt.layout.GridData;
025: import org.eclipse.swt.layout.GridLayout;
026: import org.eclipse.swt.widgets.Button;
027: import org.eclipse.swt.widgets.Combo;
028: import org.eclipse.swt.widgets.Composite;
029: import org.eclipse.swt.widgets.Control;
030: import org.eclipse.swt.widgets.DirectoryDialog;
031:
032: public class ExportDestinationTab extends AbstractExportTab {
033:
034: protected static final String S_EXPORT_DIRECTORY = "exportDirectory"; //$NON-NLS-1$
035: protected static final String S_DESTINATION = "destination"; //$NON-NLS-1$
036: protected static final String S_ZIP_FILENAME = "zipFileName"; //$NON-NLS-1$
037:
038: protected static String ZIP_EXTENSION = ".zip"; //$NON-NLS-1$
039:
040: protected Button fArchiveFileButton;
041: protected Combo fArchiveCombo;
042: protected Button fBrowseFile;
043: protected Button fDirectoryButton;
044: protected Combo fDirectoryCombo;
045: protected Button fBrowseDirectory;
046:
047: public ExportDestinationTab(AbstractExportWizardPage page) {
048: super (page);
049: }
050:
051: public Control createControl(Composite parent) {
052: Composite composite = new Composite(parent, SWT.NONE);
053: composite.setLayout(new GridLayout(2, false));
054: composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
055:
056: fDirectoryButton = new Button(composite, SWT.RADIO);
057: fDirectoryButton.setText(PDEUIMessages.ExportWizard_directory);
058: GridData gd = new GridData();
059: gd.horizontalSpan = 2;
060: fDirectoryButton.setLayoutData(gd);
061:
062: fDirectoryCombo = new Combo(composite, SWT.BORDER);
063: gd = new GridData(GridData.FILL_HORIZONTAL);
064: gd.horizontalIndent = 15;
065: fDirectoryCombo.setLayoutData(gd);
066:
067: fBrowseDirectory = new Button(composite, SWT.PUSH);
068: fBrowseDirectory.setText(PDEUIMessages.ExportWizard_browse);
069: fBrowseDirectory.setLayoutData(new GridData());
070: SWTUtil.setButtonDimensionHint(fBrowseDirectory);
071:
072: fArchiveFileButton = new Button(composite, SWT.RADIO);
073: fArchiveFileButton.setText(PDEUIMessages.ExportWizard_archive);
074: gd = new GridData();
075: gd.horizontalSpan = 2;
076: fArchiveFileButton.setLayoutData(gd);
077:
078: fArchiveCombo = new Combo(composite, SWT.BORDER);
079: gd = new GridData(GridData.FILL_HORIZONTAL);
080: gd.horizontalIndent = 15;
081: fArchiveCombo.setLayoutData(gd);
082:
083: fBrowseFile = new Button(composite, SWT.PUSH);
084: fBrowseFile.setText(PDEUIMessages.ExportWizard_browse);
085: fBrowseFile.setLayoutData(new GridData());
086: SWTUtil.setButtonDimensionHint(fBrowseFile);
087:
088: return composite;
089: }
090:
091: protected void initialize(IDialogSettings settings) {
092: String toDirectory = settings.get(S_EXPORT_DIRECTORY);
093: boolean useDirectory = toDirectory == null
094: || "true".equals(toDirectory); //$NON-NLS-1$
095: fDirectoryButton.setSelection(useDirectory);
096: fArchiveFileButton.setSelection(!useDirectory);
097: toggleDestinationGroup(useDirectory);
098: initializeCombo(settings, S_DESTINATION, fDirectoryCombo);
099: 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
114: .getSelection());
115: fPage.pageChanged();
116: }
117: });
118:
119: fBrowseFile.addSelectionListener(new SelectionAdapter() {
120: public void widgetSelected(SelectionEvent e) {
121: chooseFile(fArchiveCombo, "*" + ZIP_EXTENSION); //$NON-NLS-1$
122: }
123: });
124:
125: fArchiveCombo.addModifyListener(new ModifyListener() {
126: public void modifyText(ModifyEvent e) {
127: fPage.pageChanged();
128: }
129: });
130:
131: fDirectoryCombo.addModifyListener(new ModifyListener() {
132: public void modifyText(ModifyEvent e) {
133: fPage.pageChanged();
134: }
135: });
136:
137: fBrowseDirectory.addSelectionListener(new SelectionAdapter() {
138: public void widgetSelected(SelectionEvent e) {
139: chooseDestination();
140: }
141: });
142: }
143:
144: private void chooseDestination() {
145: DirectoryDialog dialog = new DirectoryDialog(fPage.getShell(),
146: SWT.SAVE);
147: String path = fDirectoryCombo.getText();
148: if (path.trim().length() == 0)
149: path = PDEPlugin.getWorkspace().getRoot().getLocation()
150: .toString();
151: dialog.setFilterPath(path);
152: dialog.setText(PDEUIMessages.ExportWizard_dialog_title);
153: dialog.setMessage(PDEUIMessages.ExportWizard_dialog_message);
154: String res = dialog.open();
155: if (res != null) {
156: if (fDirectoryCombo.indexOf(res) == -1)
157: fDirectoryCombo.add(res, 0);
158: fDirectoryCombo.setText(res);
159: }
160: }
161:
162: protected void saveSettings(IDialogSettings settings) {
163: settings.put(S_EXPORT_DIRECTORY, fDirectoryButton
164: .getSelection());
165: saveCombo(settings, S_DESTINATION, fDirectoryCombo);
166: saveCombo(settings, S_ZIP_FILENAME, fArchiveCombo);
167: }
168:
169: protected String validate() {
170: if (fArchiveFileButton.getSelection()
171: && fArchiveCombo.getText().trim().length() == 0)
172: return PDEUIMessages.ExportWizard_status_nofile;
173: if (fDirectoryButton.getSelection()
174: && fDirectoryCombo.getText().trim().length() == 0)
175: return PDEUIMessages.ExportWizard_status_nodirectory;
176: return null;
177: }
178:
179: protected String getFileName() {
180: if (fArchiveFileButton.getSelection()) {
181: String path = fArchiveCombo.getText();
182: if (path != null && path.length() > 0) {
183: String fileName = new Path(path).lastSegment();
184: if (!fileName.endsWith(ZIP_EXTENSION)) {
185: fileName += ZIP_EXTENSION;
186: }
187: return fileName;
188: }
189: }
190: return null;
191: }
192:
193: protected String getDestination() {
194: if (fArchiveFileButton.getSelection()) {
195: String path = fArchiveCombo.getText();
196: if (path.length() > 0) {
197: path = new Path(path).removeLastSegments(1)
198: .toOSString();
199: return new File(path).getAbsolutePath();
200: }
201: return ""; //$NON-NLS-1$
202: }
203:
204: File dir = new File(fDirectoryCombo.getText().trim());
205: return dir.getAbsolutePath();
206: }
207:
208: protected boolean doExportToDirectory() {
209: return fDirectoryButton.getSelection();
210: }
211:
212: }
|