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.FileNotFoundException;
014: import java.io.FileOutputStream;
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.IEclipsePreferences;
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.ui.internal.WorkbenchPlugin;
026: import org.eclipse.ui.internal.preferences.PreferenceTransferElement;
027:
028: /**
029: * Page 1 of the base preference export Wizard
030: *
031: * @since 3.1
032: */
033: public class WizardPreferencesExportPage1 extends WizardPreferencesPage {
034:
035: // constants
036: private static final String PREFERENCESEXPORTPAGE1 = "preferencesExportPage1"; // //$NON-NLS-1$
037:
038: /**
039: * Create an instance of this class
040: */
041: protected WizardPreferencesExportPage1(String name) {
042: super (name);
043: setTitle(PreferencesMessages.WizardPreferencesExportPage1_exportTitle);
044: setDescription(PreferencesMessages.WizardPreferencesExportPage1_exportDescription);
045: }
046:
047: /**
048: * Create an instance of this class
049: */
050: public WizardPreferencesExportPage1() {
051: this (PREFERENCESEXPORTPAGE1);
052: }
053:
054: protected String getOutputSuffix() {
055: return ".epf"; //$NON-NLS-1$
056: }
057:
058: /**
059: * Answer the contents of self's destination specification widget
060: *
061: * @return java.lang.String
062: */
063: protected String getDestinationValue() {
064: String idealSuffix = getOutputSuffix();
065: String destinationText = super .getDestinationValue();
066:
067: // only append a suffix if the destination doesn't already have a . in
068: // its last path segment.
069: // Also prevent the user from selecting a directory. Allowing this will
070: // create a ".epf" file in the directory
071: if (destinationText.length() != 0
072: && !destinationText.endsWith(File.separator)) {
073: int dotIndex = destinationText.lastIndexOf('.');
074: if (dotIndex != -1) {
075: // the last path seperator index
076: int pathSepIndex = destinationText
077: .lastIndexOf(File.separator);
078: if (pathSepIndex != -1 && dotIndex < pathSepIndex) {
079: destinationText += idealSuffix;
080: }
081: } else {
082: destinationText += idealSuffix;
083: }
084: }
085:
086: return destinationText;
087: }
088:
089: protected String getAllButtonText() {
090: return PreferencesMessages.WizardPreferencesExportPage1_all;
091: }
092:
093: protected String getChooseButtonText() {
094: return PreferencesMessages.WizardPreferencesExportPage1_choose;
095: }
096:
097: /**
098: * @param composite
099: */
100: protected void createTransferArea(Composite composite) {
101: createTransfersList(composite);
102: createDestinationGroup(composite);
103: createOptionsGroup(composite);
104: }
105:
106: /**
107: * Answer the string to display in self as the destination type
108: *
109: * @return java.lang.String
110: */
111: protected String getDestinationLabel() {
112: return PreferencesMessages.WizardPreferencesExportPage1_file;
113: }
114:
115: /*
116: * return the PreferenceTransgerElements specified
117: */
118: protected PreferenceTransferElement[] getTransfers() {
119: PreferenceTransferElement[] elements = super .getTransfers();
120: PreferenceTransferElement[] returnElements = new PreferenceTransferElement[elements.length];
121: IPreferenceFilter[] filters = new IPreferenceFilter[1];
122: IPreferenceFilter[] matches;
123: IPreferencesService service = Platform.getPreferencesService();
124: int count = 0;
125: try {
126: for (int i = 0; i < elements.length; i++) {
127: PreferenceTransferElement element = elements[i];
128: filters[0] = element.getFilter();
129: matches = service.matches((IEclipsePreferences) service
130: .getRootNode().node("instance"), filters); //$NON-NLS-1$
131: if (matches.length > 0) {
132: returnElements[count++] = element;
133: }
134: }
135: elements = new PreferenceTransferElement[count];
136: System.arraycopy(returnElements, 0, elements, 0, count);
137: } catch (CoreException e) {
138: WorkbenchPlugin.log(e.getMessage(), e);
139: return new PreferenceTransferElement[0];
140: }
141: return elements;
142: }
143:
144: /**
145: * @param transfers
146: * @return <code>true</code> if the transfer was succesful, and
147: * <code>false</code> otherwise
148: */
149: protected boolean transfer(IPreferenceFilter[] transfers) {
150: File exportFile = new File(getDestinationValue());
151: if (!ensureTargetIsValid(exportFile)) {
152: return false;
153: }
154: FileOutputStream fos = null;
155: try {
156: if (transfers.length > 0) {
157: try {
158: fos = new FileOutputStream(exportFile);
159: } catch (FileNotFoundException e) {
160: WorkbenchPlugin.log(e.getMessage(), e);
161: MessageDialog.openError(getControl().getShell(),
162: new String(), e.getLocalizedMessage());
163: return false;
164: }
165: IPreferencesService service = Platform
166: .getPreferencesService();
167: try {
168: service.exportPreferences(service.getRootNode(),
169: transfers, fos);
170: } catch (CoreException e) {
171: WorkbenchPlugin.log(e.getMessage(), e);
172: MessageDialog.openError(getControl().getShell(),
173: new String(), e.getLocalizedMessage());
174: return false;
175: }
176: }
177: } finally {
178: if (fos != null) {
179: try {
180: fos.close();
181: } catch (IOException e) {
182: WorkbenchPlugin.log(e.getMessage(), e);
183: MessageDialog.openError(getControl().getShell(),
184: new String(), e.getLocalizedMessage());
185: return false;
186: }
187: }
188: }
189: return true;
190: }
191:
192: protected String getFileDialogTitle() {
193: return PreferencesMessages.WizardPreferencesExportPage1_title;
194: }
195:
196: protected int getFileDialogStyle() {
197: return SWT.SAVE;
198: }
199:
200: /* (non-Javadoc)
201: * @see org.eclipse.ui.internal.wizards.preferences.WizardPreferencesPage#getInvalidDestinationMessage()
202: */
203: protected String getInvalidDestinationMessage() {
204: return PreferencesMessages.WizardPreferencesExportPage1_noPrefFile;
205: }
206: }
|