KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException 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.IExportedPreferences;
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.swt.widgets.Event;
27 import org.eclipse.ui.internal.WorkbenchPlugin;
28 import org.eclipse.ui.internal.preferences.PreferenceTransferElement;
29
30 /**
31  * Page 1 of the base preference import Wizard
32  *
33  *
34  * @since 3.1
35  */

36 public class WizardPreferencesImportPage1 extends WizardPreferencesPage {
37
38     /**
39      * Create a new instance of the receiver with name pageName.
40      * @param pageName
41      */

42     protected WizardPreferencesImportPage1(String JavaDoc pageName) {
43         super(pageName);
44         setTitle(PreferencesMessages.WizardPreferencesImportPage1_importTitle);
45         setDescription(PreferencesMessages.WizardPreferencesImportPage1_importDescription);
46     }
47
48     /**
49      * Create an instance of this class
50      */

51     public WizardPreferencesImportPage1() {
52         this("preferencesImportPage1");//$NON-NLS-1$
53
}
54
55     /* (non-Javadoc)
56      * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getAllButtonText()
57      */

58     protected String JavaDoc getAllButtonText() {
59         return PreferencesMessages.WizardPreferencesImportPage1_all;
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getChooseButtonText()
64      */

65     protected String JavaDoc getChooseButtonText() {
66         return PreferencesMessages.WizardPreferencesImportPage1_choose;
67     }
68
69    
70     /* (non-Javadoc)
71      * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getTransfers()
72      */

73     protected PreferenceTransferElement[] getTransfers() {
74         if (validFromFile()) {
75             FileInputStream JavaDoc fis;
76
77             try {
78                 fis = new FileInputStream JavaDoc(getDestinationValue());
79             } catch (FileNotFoundException JavaDoc e) {
80                 WorkbenchPlugin.log(e.getMessage(), e);
81                 return new PreferenceTransferElement[0];
82             }
83             IPreferencesService service = Platform.getPreferencesService();
84             try {
85                 IExportedPreferences prefs;
86                 prefs = service.readPreferences(fis);
87                 PreferenceTransferElement[] transfers = super.getTransfers();
88                 IPreferenceFilter[] filters = new IPreferenceFilter[transfers.length];
89                 for (int i = 0; i < transfers.length; i++) {
90                     PreferenceTransferElement transfer = transfers[i];
91                     filters[i] = transfer.getFilter();
92                 }
93                 IPreferenceFilter[] matches = service.matches(prefs, filters);
94                 PreferenceTransferElement[] returnTransfers = new PreferenceTransferElement[matches.length];
95                 int index = 0;
96                 for (int i = 0; i < matches.length; i++) {
97                     IPreferenceFilter filter = matches[i];
98                     for (int j = 0; j < transfers.length; j++) {
99                         PreferenceTransferElement element = transfers[j];
100                         if (element.getFilter().equals(filter)) {
101                             returnTransfers[index++] = element;
102                         }
103                     }
104                 }
105
106                 PreferenceTransferElement[] destTransfers = new PreferenceTransferElement[index];
107                 System.arraycopy(returnTransfers, 0, destTransfers, 0, index);
108                 return destTransfers;
109             } catch (CoreException e) {
110                 //Do not log core exceptions, they indicate the chosen file is not valid
111
//WorkbenchPlugin.log(e.getMessage(), e);
112
} finally {
113                 try {
114                     fis.close();
115                 } catch (IOException JavaDoc e) {
116                     WorkbenchPlugin.log(e.getMessage(), e);
117                 }
118             }
119         }
120
121         return new PreferenceTransferElement[0];
122     }
123
124     /**
125      * Return whether or not the file is valid.
126      * @return <code>true</code> of the file is an existing
127      * file and not a directory
128      */

129     private boolean validFromFile() {
130         File JavaDoc fromFile = new File JavaDoc(getDestinationValue());
131         return fromFile.exists() && !fromFile.isDirectory();
132     }
133
134     protected void setPreferenceTransfers() {
135         super.setPreferenceTransfers();
136         
137         if(validFromFile() && (transfersTable.getItemCount() == 0)) {
138             text.setText(PreferencesMessages.WizardPreferences_noSpecificPreferenceDescription);
139         } else {
140             text.setText(""); //$NON-NLS-1$
141
}
142     }
143   
144     /* (non-Javadoc)
145      * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#createTransferArea(org.eclipse.swt.widgets.Composite)
146      */

147     protected void createTransferArea(Composite composite) {
148         createDestinationGroup(composite);
149         createTransfersList(composite);
150     }
151
152     /**
153      * Answer the string to display in self as the destination type
154      *
155      * @return java.lang.String
156      */

157     protected String JavaDoc getDestinationLabel() {
158         return PreferencesMessages.WizardPreferencesImportPage1_file;
159     }
160
161     /**
162      * @param filters
163      * @return <code>true</code> if the transfer was succesful, and
164      * <code>false</code> otherwise
165      */

166     protected boolean transfer(IPreferenceFilter[] filters) {
167         File JavaDoc importFile = new File JavaDoc(getDestinationValue());
168         FileInputStream JavaDoc fis = null;
169         try {
170             if (filters.length > 0) {
171                 try {
172                     fis = new FileInputStream JavaDoc(importFile);
173                 } catch (FileNotFoundException JavaDoc e) {
174                     WorkbenchPlugin.log(e.getMessage(), e);
175                     MessageDialog.openError(getControl().getShell(), new String JavaDoc(), e.getLocalizedMessage());
176                     return false;
177                 }
178                 IPreferencesService service = Platform.getPreferencesService();
179                 try {
180                     IExportedPreferences prefs = service.readPreferences(fis);
181                     
182                     service.applyPreferences(prefs, filters);
183                 } catch (CoreException e) {
184                     WorkbenchPlugin.log(e.getMessage(), e);
185                     MessageDialog.openError(getControl().getShell(), new String JavaDoc(), e.getLocalizedMessage());
186                     return false;
187                 }
188             }
189         } finally {
190             if (fis != null) {
191                 try {
192                     fis.close();
193                 } catch (IOException JavaDoc e) {
194                     WorkbenchPlugin.log(e.getMessage(), e);
195                     MessageDialog.openError(getControl().getShell(), new String JavaDoc(), e.getLocalizedMessage());
196                 }
197             }
198         }
199         return true;
200     }
201
202     /**
203      * Handle events and enablements for widgets in this page
204      *
205      * @param e
206      * Event
207      */

208     public void handleEvent(Event e) {
209         if (e.widget == destinationNameField) {
210             setPreferenceTransfers();
211         }
212
213         super.handleEvent(e);
214     }
215     
216     /* (non-Javadoc)
217      * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getFileDialogTitle()
218      */

219     protected String JavaDoc getFileDialogTitle(){
220         return PreferencesMessages.WizardPreferencesImportPage1_title;
221     }
222     
223     /* (non-Javadoc)
224      * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getFileDialogStyle()
225      */

226     protected int getFileDialogStyle() {
227         return SWT.OPEN;
228     }
229     
230     /* (non-Javadoc)
231      * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#validDestination()
232      */

233     protected boolean validDestination() {
234         return super.validDestination() && validFromFile();
235     }
236
237     /* (non-Javadoc)
238      * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getInvalidDestinationMessage()
239      */

240     protected String JavaDoc getInvalidDestinationMessage() {
241         return PreferencesMessages.WizardPreferencesImportPage1_invalidPrefFile;
242     }
243 }
244
Popular Tags