KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > preferences > WizardPreferencesExportPage1


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.ui.internal.wizards.preferences;
12
13 import java.io.File JavaDoc;
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
21 import org.eclipse.core.runtime.preferences.IPreferenceFilter;
22 import org.eclipse.core.runtime.preferences.IPreferencesService;
23 import org.eclipse.jface.dialogs.MessageDialog;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.ui.internal.WorkbenchPlugin;
27 import org.eclipse.ui.internal.preferences.PreferenceTransferElement;
28
29 /**
30  * Page 1 of the base preference export Wizard
31  *
32  * @since 3.1
33  */

34 public class WizardPreferencesExportPage1 extends WizardPreferencesPage {
35
36     // constants
37
private static final String JavaDoc PREFERENCESEXPORTPAGE1 = "preferencesExportPage1"; // //$NON-NLS-1$
38

39     /**
40      * Create an instance of this class
41      */

42     protected WizardPreferencesExportPage1(String JavaDoc name) {
43         super(name);
44         setTitle(PreferencesMessages.WizardPreferencesExportPage1_exportTitle);
45         setDescription(PreferencesMessages.WizardPreferencesExportPage1_exportDescription);
46     }
47
48     /**
49      * Create an instance of this class
50      */

51     public WizardPreferencesExportPage1() {
52         this(PREFERENCESEXPORTPAGE1);
53     }
54
55     protected String JavaDoc getOutputSuffix() {
56         return ".epf"; //$NON-NLS-1$
57
}
58     
59     /**
60      * Answer the contents of self's destination specification widget
61      *
62      * @return java.lang.String
63      */

64     protected String JavaDoc getDestinationValue() {
65         String JavaDoc idealSuffix = getOutputSuffix();
66         String JavaDoc destinationText = super.getDestinationValue();
67
68         // only append a suffix if the destination doesn't already have a . in
69
// its last path segment.
70
// Also prevent the user from selecting a directory. Allowing this will
71
// create a ".epf" file in the directory
72
if (destinationText.length() != 0
73                 && !destinationText.endsWith(File.separator)) {
74             int dotIndex = destinationText.lastIndexOf('.');
75             if (dotIndex != -1) {
76                 // the last path seperator index
77
int pathSepIndex = destinationText.lastIndexOf(File.separator);
78                 if (pathSepIndex != -1 && dotIndex < pathSepIndex) {
79                     destinationText += idealSuffix;
80                 }
81             } else {
82                 destinationText += idealSuffix;
83             }
84         }
85
86         return destinationText;
87     }
88
89         
90     protected String JavaDoc getAllButtonText() {
91         return PreferencesMessages.WizardPreferencesExportPage1_all;
92     }
93
94     protected String JavaDoc getChooseButtonText() {
95         return PreferencesMessages.WizardPreferencesExportPage1_choose;
96     }
97
98     /**
99      * @param composite
100      */

101     protected void createTransferArea(Composite composite) {
102         createTransfersList(composite);
103         createDestinationGroup(composite);
104         createOptionsGroup(composite);
105     }
106
107     /**
108      * Answer the string to display in self as the destination type
109      *
110      * @return java.lang.String
111      */

112     protected String JavaDoc getDestinationLabel() {
113         return PreferencesMessages.WizardPreferencesExportPage1_file;
114     }
115
116     /*
117      * return the PreferenceTransgerElements specified
118      */

119     protected PreferenceTransferElement[] getTransfers() {
120         PreferenceTransferElement[] elements = super.getTransfers();
121         PreferenceTransferElement[] returnElements = new PreferenceTransferElement[elements.length];
122         IPreferenceFilter[] filters = new IPreferenceFilter[1];
123         IPreferenceFilter[] matches;
124         IPreferencesService service = Platform.getPreferencesService();
125         int count = 0;
126         try {
127             for (int i = 0; i < elements.length; i++) {
128                 PreferenceTransferElement element = elements[i];
129                 filters[0] = element.getFilter();
130                 matches = service.matches((IEclipsePreferences) service
131                         .getRootNode().node("instance"), filters); //$NON-NLS-1$
132
if (matches.length > 0) {
133                     returnElements[count++] = element;
134                 }
135             }
136             elements = new PreferenceTransferElement[count];
137             System.arraycopy(returnElements, 0, elements, 0, count);
138         } catch (CoreException e) {
139             WorkbenchPlugin.log(e.getMessage(), e);
140             return new PreferenceTransferElement[0];
141         }
142         return elements;
143     }
144
145     /**
146      * @param transfers
147      * @return <code>true</code> if the transfer was succesful, and
148      * <code>false</code> otherwise
149      */

150     protected boolean transfer(IPreferenceFilter[] transfers) {
151         File JavaDoc exportFile = new File JavaDoc(getDestinationValue());
152         if (!ensureTargetIsValid(exportFile)) {
153             return false;
154         }
155         FileOutputStream JavaDoc fos = null;
156         try {
157             if (transfers.length > 0) {
158                 try {
159                     fos = new FileOutputStream JavaDoc(exportFile);
160                 } catch (FileNotFoundException JavaDoc e) {
161                     WorkbenchPlugin.log(e.getMessage(), e);
162                     MessageDialog.openError(getControl().getShell(), new String JavaDoc(), e.getLocalizedMessage());
163                     return false;
164                 }
165                 IPreferencesService service = Platform.getPreferencesService();
166                 try {
167                     service.exportPreferences(service.getRootNode(), transfers,
168                             fos);
169                 } catch (CoreException e) {
170                     WorkbenchPlugin.log(e.getMessage(), e);
171                     MessageDialog.openError(getControl().getShell(), new String JavaDoc(), e.getLocalizedMessage());
172                     return false;
173                 }
174             }
175         } finally {
176             if (fos != null) {
177                 try {
178                     fos.close();
179                 } catch (IOException JavaDoc e) {
180                     WorkbenchPlugin.log(e.getMessage(), e);
181                     MessageDialog.openError(getControl().getShell(), new String JavaDoc(), e.getLocalizedMessage());
182                     return false;
183                 }
184             }
185         }
186         return true;
187     }
188
189     protected String JavaDoc getFileDialogTitle() {
190         return PreferencesMessages.WizardPreferencesExportPage1_title;
191     }
192
193     protected int getFileDialogStyle() {
194         return SWT.SAVE;
195     }
196     
197     /* (non-Javadoc)
198      * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getInvalidDestinationMessage()
199      */

200     protected String JavaDoc getInvalidDestinationMessage() {
201         return PreferencesMessages.WizardPreferencesExportPage1_noPrefFile;
202     }
203 }
204
Popular Tags