blob: f3ea63092379f37765e0540b4b97d96643c36c5b [file] [log] [blame]
/**
********************************************************************************
* Copyright (c) 2017 Robert Bosch GmbH and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
********************************************************************************
*/
package org.eclipse.app4mc.sca.ui.preferences;
import java.io.File;
import java.io.IOException;
import org.eclipse.app4mc.sca.logging.manager.Logmanager;
import org.eclipse.app4mc.sca.ui.Activator;
import org.eclipse.app4mc.sca.ui.util.SCAToolsUIUtil;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbenchPreferencePage;
/**
* Abstract preference page for the multicore preference page implementations.The page would contain the default
* implementation of all the common features agreed upon in the multicore architecture group meeting
*/
public abstract class AbstractSCAToolsPreferencePage extends FieldEditorPreferencePage
implements IWorkbenchPreferencePage {
/**
* Button to export the preferences to the options file
*/
protected Button exportPref;
/**
* Label for status image
*/
protected Label statusImage;
/**
* Label for status info
*/
protected Label infoLabel;
/**
* Default constructor to support preference pages implemented using SWT controls.
*/
public AbstractSCAToolsPreferencePage() {
}
/**
* Parameterized constructor to support preference pages implemented using field editors.
*
* @param style layouting style GRID or FLAT
*/
public AbstractSCAToolsPreferencePage(final int style) {
super(style);
}
@Override
protected void contributeButtons(final Composite parent) {
GridLayout layout = (GridLayout) parent.getLayout();
layout.numColumns = layout.numColumns + 3;
this.statusImage = new Label(parent, SWT.NONE);
this.statusImage.setImage(parent.getShell().getDisplay().getSystemImage(SWT.ICON_INFORMATION));
this.statusImage.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
this.statusImage.setVisible(false);
this.infoLabel = new Label(parent, SWT.None);
this.infoLabel.setText("Options file exported successfully!!!");
this.infoLabel.setVisible(false);
int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
this.exportPref = new Button(parent, SWT.PUSH);
this.exportPref.setText("Export Preferences");
Dialog.applyDialogFont(this.exportPref);
Point minButtonSize = this.exportPref.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
data.widthHint = Math.max(widthHint, minButtonSize.x);
this.exportPref.setLayoutData(data);
this.exportPref.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
savePreferences(parent);
}
});
super.contributeButtons(parent);
}
/**
* Exports all the preferences to options file
*/
private void savePreferences(final Composite parent) {
showStatus(false);
setErrorMessage(null);
FileDialog fileDialog = new FileDialog(parent.getShell(), SWT.SAVE | SWT.SHEET);
fileDialog.setText("Export");
fileDialog.setFilterPath(System.getProperty("user.home"));
fileDialog.setFileName("Preference.opt");
fileDialog.setFilterExtensions(new String[] { ".opt" });
String path = fileDialog.open();
if (path != null) {
File optionsFile = new File(path);
try {
boolean status = optionsFile.createNewFile();
if (!status) {
setErrorMessage("File with same name exists in the selected folder!");
}
else {
SCAToolsUIUtil.exportProperties(optionsFile, getPreferenceStore(), getToolId());
showStatus(true);
}
}
catch (IOException e) {
Logmanager.getInstance().logException(this.getClass(), e, Activator.PLUGIN_ID);
}
}
}
/**
* Enable/disable the status labels
*
* @param status {@link Boolean}
*/
private void showStatus(final boolean status) {
this.infoLabel.setVisible(status);
this.statusImage.setVisible(status);
}
/**
* Just returning
*/
@Override
public void createFieldEditors() {
return;
}
/**
* Returns the tool id for which the page has been contributed
*
* @return Sting
*/
public abstract String getToolId();
}