/** | |
******************************************************************************** | |
* 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.sca2amalthea.ui.preferences; | |
import java.io.File; | |
import org.eclipse.app4mc.sca.ui.preferences.AbstractSCAToolsPreferencePage; | |
import org.eclipse.app4mc.sca.util.App4mcToolConstants; | |
import org.eclipse.app4mc.sca2amalthea.ui.util.SCAToAmaltheaPreferencePageConstants; | |
import org.eclipse.app4mc.sca2amalthea.utils.constants.SCA2AmaltheaConstants; | |
import org.eclipse.core.resources.IProject; | |
import org.eclipse.core.resources.ResourcesPlugin; | |
import org.eclipse.jface.dialogs.MessageDialog; | |
import org.eclipse.jface.layout.GridDataFactory; | |
import org.eclipse.jface.preference.BooleanFieldEditor; | |
import org.eclipse.jface.preference.DirectoryFieldEditor; | |
import org.eclipse.jface.preference.FieldEditor; | |
import org.eclipse.jface.preference.FileFieldEditor; | |
import org.eclipse.jface.preference.PathEditor; | |
import org.eclipse.jface.preference.PreferenceDialog; | |
import org.eclipse.jface.resource.JFaceResources; | |
import org.eclipse.jface.util.PropertyChangeEvent; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.events.FocusEvent; | |
import org.eclipse.swt.events.FocusListener; | |
import org.eclipse.swt.events.KeyEvent; | |
import org.eclipse.swt.events.KeyListener; | |
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.Control; | |
import org.eclipse.swt.widgets.Group; | |
import org.eclipse.swt.widgets.Text; | |
import org.eclipse.ui.IWorkbench; | |
/** | |
*/ | |
public class SCAToAmaltheaPreferencePage extends AbstractSCAToolsPreferencePage { | |
private DirectoryFieldEditor llvmExecutableEditor; | |
private DirectoryFieldEditor outputPathEditor; | |
private FileFieldEditor taskInfoEditor; | |
private PathEditor headerTextFileEditor; | |
private FileFieldEditor buildCmdLogFileEditor; | |
private BooleanFieldEditor enable; | |
private BooleanFieldEditor structMemberRemover; | |
private Group optional; | |
private Group configure; | |
private Group mandatory; | |
private FileFieldEditor lockFunctionsEditor; | |
/** | |
* Using GRID layout | |
*/ | |
public SCAToAmaltheaPreferencePage() { | |
super(1); | |
} | |
/** | |
* creating UI controls for sca2Amalthea preference page | |
*/ | |
@Override | |
public void createFieldEditors() { | |
this.mandatory=new Group(getFieldEditorParent(), SWT.SHADOW_OUT); | |
this.configure = new Group(getFieldEditorParent(), SWT.SHADOW_OUT); | |
this.optional = new Group(getFieldEditorParent(), SWT.SHADOW_OUT); | |
this.mandatory.setText("MANDATORY CONFIGURATION"); | |
this.optional.setText("OPTIONAL CONFIGURATION"); | |
this.configure.setText("EDIT OPTIONAL CONFIGURATION"); | |
GridLayout layout = new GridLayout(1, false); | |
this.mandatory.setLayout(layout); | |
this.configure.setLayout(layout); | |
this.optional.setLayout(layout); | |
GridDataFactory.defaultsFor(this.mandatory).grab(true, false).span(2, 1).applyTo(this.mandatory); | |
GridDataFactory.defaultsFor(this.configure).grab(true, false).span(2, 1).applyTo(this.configure); | |
GridDataFactory.defaultsFor(this.optional).grab(true, false).span(2, 1).applyTo(this.optional); | |
this.llvmExecutableEditor = | |
new DirectoryFieldEditor(SCA2AmaltheaConstants.AST_PATH, "&Path to the LLVM Executable", this.mandatory); | |
addField(this.llvmExecutableEditor); | |
this.llvmExecutableEditor.setFilterPath(new File(getProjectPath())); | |
this.outputPathEditor = | |
new DirectoryFieldEditor(SCA2AmaltheaConstants.OUTPUT_PATH, "&Output Directory Path:", this.optional); | |
this.outputPathEditor.getTextControl(this.optional) | |
.setToolTipText(SCAToAmaltheaPreferencePageConstants.OUTPUT_PATH_DEFAULT_MESSAGE); | |
addField(this.outputPathEditor); | |
this.outputPathEditor.setFilterPath(new File(getProjectPath())); | |
this.taskInfoEditor = new FileFieldEditor(SCA2AmaltheaConstants.TASK_INFO, "&Task Information file", this.optional); | |
this.taskInfoEditor.getTextControl(this.optional) | |
.setToolTipText(SCAToAmaltheaPreferencePageConstants.TASK_INFO_FILE_DEFAULT_MEAASGE); | |
addField(this.taskInfoEditor); | |
this.taskInfoEditor.setFilterPath(new File(getProjectPath())); | |
this.structMemberRemover = | |
new BooleanFieldEditor(SCA2AmaltheaConstants.ENABLE_STRUCT_MEMBER, "&Enable Structure member", this.configure); | |
addField(this.structMemberRemover); | |
this.enable = new BooleanFieldEditor(SCA2AmaltheaConstants.ENABLE_OPTIONAL_FIELDS, "&Configure optional fields", | |
this.configure); | |
addField(this.enable); | |
this.headerTextFileEditor = new PathEditor(SCA2AmaltheaConstants.HDIR_LIST, "&Header directories", | |
"Choose a header directory", this.optional); | |
this.headerTextFileEditor.getListControl(this.optional) | |
.setToolTipText(SCAToAmaltheaPreferencePageConstants.HEADER_TEXT_FILE_DEFAULT_MESSAGE); | |
addField(this.headerTextFileEditor); | |
this.buildCmdLogFileEditor = | |
new FileFieldEditor(SCA2AmaltheaConstants.BLOG, "&Build command log file", this.optional); | |
this.buildCmdLogFileEditor.getTextControl(this.optional) | |
.setToolTipText(SCAToAmaltheaPreferencePageConstants.BUILD_LOG_FILE_DEFAULT_MESSAGE); | |
addField(this.buildCmdLogFileEditor); | |
this.buildCmdLogFileEditor.setFilterPath(new File(getProjectPath())); | |
this.lockFunctionsEditor = | |
new FileFieldEditor(SCA2AmaltheaConstants.LOCK_INFO, "& Lock Function file", this.optional); | |
this.lockFunctionsEditor.getTextControl(this.optional) | |
.setToolTipText(SCAToAmaltheaPreferencePageConstants.LOCK_FUNCTIONS_DEFAULT_MESSAGE); | |
addField(this.lockFunctionsEditor); | |
addListeners(); | |
validate(); | |
} | |
/** | |
* Setting the preference store | |
* | |
* @param workbench the workBench | |
*/ | |
@Override | |
public void init(final IWorkbench workbench) { | |
setPreferenceStore(org.eclipse.app4mc.sca2amalthea.utils.Activator.getDefault().getPreferenceStore()); | |
setDescription("Source Code to Amalthea Preference Settings"); | |
} | |
/** | |
* Enable or disable the field editors based on "EDIT OPTIONAL CONFIGURATION" check box | |
*/ | |
@Override | |
protected void checkState() { | |
super.checkState(); | |
setValid(true); | |
if (this.enable.getBooleanValue()) { | |
this.outputPathEditor.setEnabled(true, this.optional); | |
this.outputPathEditor.getTextControl(this.optional).setEditable(false); | |
this.buildCmdLogFileEditor.setEnabled(true, this.optional); | |
this.buildCmdLogFileEditor.getTextControl(this.optional).setEditable(false); | |
this.headerTextFileEditor.setEnabled(true, this.optional); | |
this.taskInfoEditor.setEnabled(true, this.optional); | |
this.taskInfoEditor.getTextControl(this.optional).setEditable(false); | |
this.lockFunctionsEditor.setEnabled(true, this.optional); | |
this.lockFunctionsEditor.getTextControl(this.optional).setEditable(false); | |
clearOptionalFields(); | |
} | |
else { | |
this.outputPathEditor.setEnabled(false, this.optional); | |
this.outputPathEditor.setStringValue(getPreferenceStore().getDefaultString(SCA2AmaltheaConstants.OUTPUT_PATH)); | |
this.buildCmdLogFileEditor.setEnabled(false, this.optional); | |
this.buildCmdLogFileEditor.setStringValue(getPreferenceStore().getDefaultString(SCA2AmaltheaConstants.BLOG)); | |
this.headerTextFileEditor.setEnabled(false, this.optional); | |
this.headerTextFileEditor.getListControl(this.optional).removeAll(); | |
this.headerTextFileEditor.getListControl(this.optional) | |
.add(getPreferenceStore().getDefaultString(SCA2AmaltheaConstants.HDIR_LIST), 0); | |
this.headerTextFileEditor.getListControl(this.optional).deselect(0); | |
this.taskInfoEditor.setEnabled(false, this.optional); | |
this.taskInfoEditor.setStringValue(getPreferenceStore().getDefaultString(SCA2AmaltheaConstants.TASK_INFO)); | |
this.lockFunctionsEditor.setEnabled(false, this.optional); | |
this.lockFunctionsEditor.setStringValue(getPreferenceStore().getDefaultString(SCA2AmaltheaConstants.LOCK_INFO)); | |
setErrorMessage(null); | |
} | |
Control[] control = this.headerTextFileEditor.getButtonBoxControl(this.optional).getChildren(); | |
for (Control c : control) { | |
if (c instanceof Button) { | |
Button b = (Button) c; | |
if (b.getText().equals(JFaceResources.getString("ListEditor.up")) || | |
b.getText().equals(JFaceResources.getString("ListEditor.down"))) { | |
b.setVisible(false); | |
} | |
else { | |
b.setVisible(true); | |
} | |
} | |
} | |
GridData gridData = (GridData) this.headerTextFileEditor.getLabelControl(this.optional).getLayoutData(); | |
gridData.horizontalSpan = 1; | |
GridData gridDataText = (GridData) this.headerTextFileEditor.getListControl(this.optional).getLayoutData(); | |
gridDataText.horizontalSpan = 1; | |
GridData gridDataButton = (GridData) this.headerTextFileEditor.getButtonBoxControl(this.optional).getLayoutData(); | |
gridDataButton.horizontalSpan = 1; | |
validate(); | |
} | |
/** | |
* Called when the property of any of the field editors changes | |
*/ | |
@Override | |
public void propertyChange(final PropertyChangeEvent event) { | |
super.propertyChange(event); | |
if (event.getProperty().equals(FieldEditor.VALUE)) { | |
checkState(); | |
} | |
} | |
/** | |
* Just return true when user press OK | |
*/ | |
@Override | |
public boolean performOk() { | |
if(getErrorMessage()!=null){ | |
MessageDialog.openError(getShell(), "Error in Configuration", getErrorMessage()); | |
return false; | |
} | |
super.performOk(); | |
return true; | |
} | |
@Override | |
protected void performApply() { | |
performOk(); | |
} | |
/** | |
* clear the Optional fields | |
*/ | |
private void clearOptionalFields() { | |
if (this.outputPathEditor.getStringValue() | |
.equals(SCA2AmaltheaConstants.getPreferenceStore().getDefaultString(SCA2AmaltheaConstants.OUTPUT_PATH))) { | |
this.outputPathEditor.setStringValue(""); | |
} | |
} | |
private String getProjectPath() { | |
IProject[] project = ResourcesPlugin.getWorkspace().getRoot().getProjects(); | |
if ((project != null) && (project.length != 0)) { | |
return project[0].getLocation().toString(); | |
} | |
return ""; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public String getToolId() { | |
return App4mcToolConstants.SCA2AMALTHEA_TOOL_ID; | |
} | |
private void validate(){ | |
if(this.llvmExecutableEditor.getStringValue().isEmpty()){ | |
setErrorMessage("Path to the LLVM executable is mandatory and cannot be empty"); | |
} | |
else if(!new File(this.llvmExecutableEditor.getStringValue()).exists()){ | |
setErrorMessage("Path to the LLVM executable does not exist"); | |
} | |
else if (!this.taskInfoEditor.getStringValue().isEmpty() && !this.taskInfoEditor.getStringValue().endsWith(".csv")){ | |
setErrorMessage("Task Information file should have a .csv extension"); | |
} | |
else if(!this.taskInfoEditor.getStringValue().isEmpty() && !new File(this.taskInfoEditor.getStringValue()).exists()){ | |
setErrorMessage("Task Information file does not exist"); | |
} | |
else{ | |
setErrorMessage(null); | |
} | |
disableOrEnableOkAndApplyButton(); | |
} | |
private void addListeners() { | |
addFocusListenerToTextBox(this.outputPathEditor.getTextControl(this.optional)); | |
addKeyListenerToTextBox(this.outputPathEditor.getTextControl(this.optional)); | |
addFocusListenerToTextBox(this.llvmExecutableEditor.getTextControl(this.mandatory)); | |
addKeyListenerToTextBox(this.llvmExecutableEditor.getTextControl(this.mandatory)); | |
addFocusListenerToTextBox(this.taskInfoEditor.getTextControl(this.optional)); | |
addKeyListenerToTextBox(this.taskInfoEditor.getTextControl(this.optional)); | |
addFocusListenerToTextBox(this.buildCmdLogFileEditor.getTextControl(this.optional)); | |
addKeyListenerToTextBox(this.buildCmdLogFileEditor.getTextControl(this.optional)); | |
addFocusListenerToTextBox(this.lockFunctionsEditor.getTextControl(this.optional)); | |
addKeyListenerToTextBox(this.lockFunctionsEditor.getTextControl(this.optional)); | |
} | |
private void addFocusListenerToTextBox(final Text t) { | |
t.addFocusListener(new FocusListener() { | |
@Override | |
public void focusLost(final FocusEvent e) { | |
validate(); | |
} | |
@Override | |
public void focusGained(final FocusEvent e) { | |
validate(); | |
} | |
}); | |
} | |
private void addKeyListenerToTextBox(final Text t) { | |
t.addKeyListener(new KeyListener() { | |
@Override | |
public void keyReleased(final KeyEvent e) { | |
validate(); | |
} | |
@Override | |
public void keyPressed(final KeyEvent e) { | |
validate(); | |
} | |
}); | |
} | |
private void disableOrEnableOkAndApplyButton() { | |
Button okButton = null; | |
PreferenceDialog iPreferencePageContainer = (PreferenceDialog) getContainer(); | |
Composite buttons = (Composite) iPreferencePageContainer.buttonBar; | |
Control[] allbuttons = buttons.getChildren(); | |
Composite compositeForButtons = (Composite) allbuttons[1]; | |
for (Control b : compositeForButtons.getChildren()) { | |
if (b instanceof Button) { | |
Button presentButton = (Button) b; | |
if ("OK".equals(presentButton.getText())) { | |
okButton = presentButton; | |
} | |
} | |
} | |
Button applyButton = getApplyButton(); | |
if ((okButton != null) && (applyButton != null)) { | |
if (getErrorMessage() != null) { | |
setValid(false); | |
okButton.setEnabled(false); | |
applyButton.setEnabled(false); | |
} | |
else { | |
setValid(true); | |
okButton.setEnabled(true); | |
applyButton.setEnabled(true); | |
} | |
} | |
} | |
} |