Search in sources :

Example 1 with IPreferenceFilter

use of org.eclipse.core.runtime.preferences.IPreferenceFilter in project knime-core by knime.

the class BatchExecutor method setPreferences.

/**
 * Sets the workspace preferences using the given file.
 *
 * @param preferenceFile the preferences file
 * @throws FileNotFoundException if the given file is not a file or does not exist
 * @throws CoreException if applying the preferences fails
 * @since 2.8
 */
public static void setPreferences(final File preferenceFile) throws FileNotFoundException, CoreException {
    if (!preferenceFile.isFile()) {
        throw new FileNotFoundException("Preference file '" + preferenceFile.getAbsolutePath() + "' does not exist");
    }
    InputStream in = new BufferedInputStream(new FileInputStream(preferenceFile));
    IPreferencesService prefService = Platform.getPreferencesService();
    IExportedPreferences prefs = prefService.readPreferences(in);
    IPreferenceFilter filter = new IPreferenceFilter() {

        @Override
        public String[] getScopes() {
            return new String[] { InstanceScope.SCOPE, ConfigurationScope.SCOPE, "profile" };
        }

        @Override
        @SuppressWarnings("rawtypes")
        public Map getMapping(final String scope) {
            // this filter is applicable for all nodes
            return null;
        }
    };
    /*
         * Calling this method with filters and not the applyPreferences without
         * filters is very important! The other method does not merge the
         * preferences but deletes all default values.
         */
    prefService.applyPreferences(prefs, new IPreferenceFilter[] { filter });
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IExportedPreferences(org.eclipse.core.runtime.preferences.IExportedPreferences) IPreferenceFilter(org.eclipse.core.runtime.preferences.IPreferenceFilter) FileInputStream(java.io.FileInputStream) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService)

Example 2 with IPreferenceFilter

use of org.eclipse.core.runtime.preferences.IPreferenceFilter in project mdw-designer by CenturyLinkCloud.

the class WorkspaceConfigurator method loadPreferences.

/**
 * Loads preferences from the workspace setup site.
 *
 * @return <code>true</code> if the load was successful
 */
protected boolean loadPreferences() {
    // import all
    IPreferenceFilter[] filters = new IPreferenceFilter[1];
    filters[0] = new IPreferenceFilter() {

        public String[] getScopes() {
            return new String[] { InstanceScope.SCOPE, ConfigurationScope.SCOPE };
        }

        public Map<String, PreferenceFilterEntry[]> getMapping(String scope) {
            return null;
        }
    };
    String baseUrl = getWorkspaceConfig().getMdwSettings().getWorkspaceSetupUrl();
    if (!baseUrl.endsWith("/"))
        baseUrl += "/";
    InputStream is = null;
    try {
        URL url = new URL(baseUrl + "MdwWorkspaceSetup.epf");
        URLConnection connection = url.openConnection();
        is = connection.getInputStream();
        IPreferencesService service = Platform.getPreferencesService();
        IExportedPreferences prefs = service.readPreferences(is);
        service.applyPreferences(prefs, filters);
    } catch (Exception ex) {
        PluginMessages.uiError(getShell(), ex, "Workspace Setup");
        return false;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            // do nothing
            }
        }
    }
    return true;
}
Also used : PreferenceFilterEntry(org.eclipse.core.runtime.preferences.PreferenceFilterEntry) InputStream(java.io.InputStream) IExportedPreferences(org.eclipse.core.runtime.preferences.IExportedPreferences) IPreferenceFilter(org.eclipse.core.runtime.preferences.IPreferenceFilter) IOException(java.io.IOException) Map(java.util.Map) URL(java.net.URL) URLConnection(java.net.URLConnection) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException)

Example 3 with IPreferenceFilter

use of org.eclipse.core.runtime.preferences.IPreferenceFilter in project knime-core by knime.

the class ImportPreferencesAction method run.

/**
 * Invoke the Import wizards selection Wizard.
 */
@Override
public void run() {
    IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (workbenchWindow == null) {
        // action has been disposed
        return;
    }
    FileDialog fileDialog = new FileDialog(workbenchWindow.getShell(), SWT.OPEN);
    fileDialog.setFilterExtensions(new String[] { "*.epf", "*.*" });
    fileDialog.setText("Specify the preferences file to import.");
    String filePath = fileDialog.open();
    if (filePath == null || filePath.trim().length() == 0) {
        return;
    }
    File inFile = new File(filePath);
    if (!inFile.isFile() || !inFile.canRead()) {
        MessageDialog.openError(workbenchWindow.getShell(), "File Selection Error", "Unable to read from specified location.");
        return;
    }
    InputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(inFile));
        IPreferencesService prefService = Platform.getPreferencesService();
        LOGGER.info("Importing preferences from file " + inFile.getAbsolutePath() + " now ...");
        IExportedPreferences prefs = prefService.readPreferences(in);
        IPreferenceFilter filter = new IPreferenceFilter() {

            @Override
            public String[] getScopes() {
                return new String[] { InstanceScope.SCOPE, ConfigurationScope.SCOPE, "profile" };
            }

            @Override
            @SuppressWarnings("rawtypes")
            public Map getMapping(final String scope) {
                // this filter is applicable for all nodes
                return null;
            }
        };
        /* Calling this method with filters and not the applyPreferences
             * without filters is very important! The other method does not
             * merge the preferences but deletes all default values. */
        prefService.applyPreferences(prefs, new IPreferenceFilter[] { filter });
        LOGGER.info("Import of preferences successfully finished.");
    } catch (Throwable t) {
        String msg = "Unable to read preferences from selected file";
        if (t.getMessage() != null && !t.getMessage().isEmpty()) {
            msg = t.getMessage();
        }
        MessageDialog.openError(workbenchWindow.getShell(), "Error Importing Preferences", msg);
        return;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IExportedPreferences(org.eclipse.core.runtime.preferences.IExportedPreferences) IPreferenceFilter(org.eclipse.core.runtime.preferences.IPreferenceFilter) IOException(java.io.IOException) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) FileInputStream(java.io.FileInputStream) IPreferencesService(org.eclipse.core.runtime.preferences.IPreferencesService)

Aggregations

InputStream (java.io.InputStream)3 IExportedPreferences (org.eclipse.core.runtime.preferences.IExportedPreferences)3 IPreferenceFilter (org.eclipse.core.runtime.preferences.IPreferenceFilter)3 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Map (java.util.Map)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 PreferenceFilterEntry (org.eclipse.core.runtime.preferences.PreferenceFilterEntry)1 FileDialog (org.eclipse.swt.widgets.FileDialog)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1