001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 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.ui.internal.wizards.preferences;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.FileNotFoundException;
015: import java.io.IOException;
016:
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.core.runtime.preferences.IExportedPreferences;
020: import org.eclipse.core.runtime.preferences.IPreferenceFilter;
021: import org.eclipse.core.runtime.preferences.IPreferencesService;
022: import org.eclipse.jface.dialogs.MessageDialog;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Event;
026: import org.eclipse.ui.internal.WorkbenchPlugin;
027: import org.eclipse.ui.internal.preferences.PreferenceTransferElement;
028:
029: /**
030: * Page 1 of the base preference import Wizard
031: *
032: *
033: * @since 3.1
034: */
035: public class WizardPreferencesImportPage1 extends WizardPreferencesPage {
036:
037: /**
038: * Create a new instance of the receiver with name pageName.
039: * @param pageName
040: */
041: protected WizardPreferencesImportPage1(String pageName) {
042: super (pageName);
043: setTitle(PreferencesMessages.WizardPreferencesImportPage1_importTitle);
044: setDescription(PreferencesMessages.WizardPreferencesImportPage1_importDescription);
045: }
046:
047: /**
048: * Create an instance of this class
049: */
050: public WizardPreferencesImportPage1() {
051: this ("preferencesImportPage1");//$NON-NLS-1$
052: }
053:
054: /* (non-Javadoc)
055: * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getAllButtonText()
056: */
057: protected String getAllButtonText() {
058: return PreferencesMessages.WizardPreferencesImportPage1_all;
059: }
060:
061: /* (non-Javadoc)
062: * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getChooseButtonText()
063: */
064: protected String getChooseButtonText() {
065: return PreferencesMessages.WizardPreferencesImportPage1_choose;
066: }
067:
068: /* (non-Javadoc)
069: * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getTransfers()
070: */
071: protected PreferenceTransferElement[] getTransfers() {
072: if (validFromFile()) {
073: FileInputStream fis;
074:
075: try {
076: fis = new FileInputStream(getDestinationValue());
077: } catch (FileNotFoundException e) {
078: WorkbenchPlugin.log(e.getMessage(), e);
079: return new PreferenceTransferElement[0];
080: }
081: IPreferencesService service = Platform
082: .getPreferencesService();
083: try {
084: IExportedPreferences prefs;
085: prefs = service.readPreferences(fis);
086: PreferenceTransferElement[] transfers = super
087: .getTransfers();
088: IPreferenceFilter[] filters = new IPreferenceFilter[transfers.length];
089: for (int i = 0; i < transfers.length; i++) {
090: PreferenceTransferElement transfer = transfers[i];
091: filters[i] = transfer.getFilter();
092: }
093: IPreferenceFilter[] matches = service.matches(prefs,
094: filters);
095: PreferenceTransferElement[] returnTransfers = new PreferenceTransferElement[matches.length];
096: int index = 0;
097: for (int i = 0; i < matches.length; i++) {
098: IPreferenceFilter filter = matches[i];
099: for (int j = 0; j < transfers.length; j++) {
100: PreferenceTransferElement element = transfers[j];
101: if (element.getFilter().equals(filter)) {
102: returnTransfers[index++] = element;
103: }
104: }
105: }
106:
107: PreferenceTransferElement[] destTransfers = new PreferenceTransferElement[index];
108: System.arraycopy(returnTransfers, 0, destTransfers, 0,
109: index);
110: return destTransfers;
111: } catch (CoreException e) {
112: //Do not log core exceptions, they indicate the chosen file is not valid
113: //WorkbenchPlugin.log(e.getMessage(), e);
114: } finally {
115: try {
116: fis.close();
117: } catch (IOException e) {
118: WorkbenchPlugin.log(e.getMessage(), e);
119: }
120: }
121: }
122:
123: return new PreferenceTransferElement[0];
124: }
125:
126: /**
127: * Return whether or not the file is valid.
128: * @return <code>true</code> of the file is an existing
129: * file and not a directory
130: */
131: private boolean validFromFile() {
132: File fromFile = new File(getDestinationValue());
133: return fromFile.exists() && !fromFile.isDirectory();
134: }
135:
136: protected void setPreferenceTransfers() {
137: super .setPreferenceTransfers();
138:
139: if (validFromFile() && (transfersTable.getItemCount() == 0)) {
140: text
141: .setText(PreferencesMessages.WizardPreferences_noSpecificPreferenceDescription);
142: } else {
143: text.setText(""); //$NON-NLS-1$
144: }
145: }
146:
147: /* (non-Javadoc)
148: * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#createTransferArea(org.eclipse.swt.widgets.Composite)
149: */
150: protected void createTransferArea(Composite composite) {
151: createDestinationGroup(composite);
152: createTransfersList(composite);
153: }
154:
155: /**
156: * Answer the string to display in self as the destination type
157: *
158: * @return java.lang.String
159: */
160: protected String getDestinationLabel() {
161: return PreferencesMessages.WizardPreferencesImportPage1_file;
162: }
163:
164: /**
165: * @param filters
166: * @return <code>true</code> if the transfer was succesful, and
167: * <code>false</code> otherwise
168: */
169: protected boolean transfer(IPreferenceFilter[] filters) {
170: File importFile = new File(getDestinationValue());
171: FileInputStream fis = null;
172: try {
173: if (filters.length > 0) {
174: try {
175: fis = new FileInputStream(importFile);
176: } catch (FileNotFoundException e) {
177: WorkbenchPlugin.log(e.getMessage(), e);
178: MessageDialog.openError(getControl().getShell(),
179: new String(), e.getLocalizedMessage());
180: return false;
181: }
182: IPreferencesService service = Platform
183: .getPreferencesService();
184: try {
185: IExportedPreferences prefs = service
186: .readPreferences(fis);
187:
188: service.applyPreferences(prefs, filters);
189: } catch (CoreException e) {
190: WorkbenchPlugin.log(e.getMessage(), e);
191: MessageDialog.openError(getControl().getShell(),
192: new String(), e.getLocalizedMessage());
193: return false;
194: }
195: }
196: } finally {
197: if (fis != null) {
198: try {
199: fis.close();
200: } catch (IOException e) {
201: WorkbenchPlugin.log(e.getMessage(), e);
202: MessageDialog.openError(getControl().getShell(),
203: new String(), e.getLocalizedMessage());
204: }
205: }
206: }
207: return true;
208: }
209:
210: /**
211: * Handle events and enablements for widgets in this page
212: *
213: * @param e
214: * Event
215: */
216: public void handleEvent(Event e) {
217: if (e.widget == destinationNameField) {
218: setPreferenceTransfers();
219: }
220:
221: super .handleEvent(e);
222: }
223:
224: /* (non-Javadoc)
225: * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getFileDialogTitle()
226: */
227: protected String getFileDialogTitle() {
228: return PreferencesMessages.WizardPreferencesImportPage1_title;
229: }
230:
231: /* (non-Javadoc)
232: * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getFileDialogStyle()
233: */
234: protected int getFileDialogStyle() {
235: return SWT.OPEN;
236: }
237:
238: /* (non-Javadoc)
239: * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#validDestination()
240: */
241: protected boolean validDestination() {
242: return super .validDestination() && validFromFile();
243: }
244:
245: /* (non-Javadoc)
246: * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getInvalidDestinationMessage()
247: */
248: protected String getInvalidDestinationMessage() {
249: return PreferencesMessages.WizardPreferencesImportPage1_invalidPrefFile;
250: }
251: }
|