blob: 03803731d9995c2d9ba9bf801ad349dabb1476fc [file] [log] [blame]
package org.eclipse.wst.validation.ui.internal.preferences;
/*******************************************************************************
* Copyright (c) 2001, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.wst.validation.ValidationFramework;
import org.eclipse.wst.validation.Validator;
import org.eclipse.wst.validation.internal.ConfigurationManager;
import org.eclipse.wst.validation.internal.GlobalConfiguration;
import org.eclipse.wst.validation.internal.ValManager;
import org.eclipse.wst.validation.internal.ValPrefManagerGlobal;
import org.eclipse.wst.validation.internal.ValidatorMetaData;
import org.eclipse.wst.validation.internal.model.GlobalPreferences;
import org.eclipse.wst.validation.internal.ui.ContextIds;
import org.eclipse.wst.validation.internal.ui.DelegatingValidatorPreferencesDialog;
import org.eclipse.wst.validation.internal.ui.plugin.ValidationUIPlugin;
import org.eclipse.wst.validation.ui.internal.ImageNames;
import org.eclipse.wst.validation.ui.internal.ValUIMessages;
import org.eclipse.wst.validation.ui.internal.dialog.FilterDialog;
/**
* From this page the user can configure individual validators.
* <p>
* This class and its inner classes are not intended to be subclassed outside of
* the validation framework.
*/
public class ValidationPreferencePage extends PreferencePage implements
IWorkbenchPreferencePage, IPreferenceChangeListener {
private IValidationPage _pageImpl = null;
private Shell _shell;
/*
* Initially, this interface was created as an abstract class, and
* getControl() was implemented. (getProject() could also have been
* implemented in the abstract class.) However, at runtime, a
* NullPointerException was thrown; the inner class had lost its pointer to
* its enclosing class. After some experimentation, I discovered that if I
* changed the parent to an interface, the enclosing class could be found.
* (Merely moving the AValidationPage into its own file was insufficient.)
*/
public interface IValidationPage {
Composite createPage(Composite parent) throws InvocationTargetException;
boolean performOk() throws InvocationTargetException;
boolean performDefaults() throws InvocationTargetException;
Composite getControl();
void dispose();
void loseFocus();
void gainFocus();
}
public class InvalidPage implements IValidationPage {
private Composite page = null;
private Composite composite = null;
private GridLayout layout = null;
private Label messageLabel = null;
public InvalidPage(Composite parent) {
page = createPage(parent);
}
/**
* This page is added to the Properties guide if some internal problem
* occurred; for example, the highlighted item in the workbench is not
* an IProject (according to this page's plugin.xml, this page is only
* valid when an IProject is selected).
*/
public Composite createPage(Composite parent) {
// Don't create the default and apply buttons.
noDefaultAndApplyButton();
final ScrolledComposite sc1 = new ScrolledComposite(parent,
SWT.H_SCROLL | SWT.V_SCROLL);
sc1.setLayoutData(new GridData(GridData.FILL_BOTH));
composite = new Composite(sc1, SWT.NONE);
sc1.setContent(composite);
layout = new GridLayout();
composite.setLayout(layout);
PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, ContextIds.VALIDATION_PROPERTIES_PAGE);
messageLabel = new Label(composite, SWT.NONE);
messageLabel.setText(ValUIMessages.VBF_EXC_INVALID_REGISTER);
composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
return composite;
}
public boolean performDefaults() {
return true;
}
/**
* Since this page occurs under invalid circumstances, there is nothing
* to save.
*/
public boolean performOk() {
return true;
}
public Composite getControl() {
return page;
}
public void dispose() {
messageLabel.dispose();
// layout.dispose();
composite.dispose();
}
public void loseFocus() {
// This page does not depend on the contents of any other page in
// the wizard, so do nothing.
}
public void gainFocus() {
// This page does not depend on the contents of any other page in
// the wizard, so do nothing.
}
}
private class ValidatorListPage implements IValidationPage {
private Composite _page;
private Composite _composite;
private TableViewer _validatorList;
private Button _enableAllButton;
private Button _disableAllButton;
private Button _override;
private Button _suspend;
private Button _autoSave;
private Button _confirmButton;
private Label _listLabel;
private Table _validatorsTable;
private GlobalPreferences _globalPreferences = ValManager.getDefault().getGlobalPreferences();
/**
* This class is provided for the CheckboxTableViewer in the
* ValidationPropertiesPage$ValidatorListPage class.
*/
public class ValidationContentProvider implements IStructuredContentProvider {
public void dispose() {
}
public Object[] getElements(Object inputElement) {
if (inputElement instanceof ValidatorWrapper[]) {
return (ValidatorWrapper[]) inputElement;
}
return new Object[0];
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
/**
* This class is provided for
* ValidationPropertiesPage$ValidatorListPage's checkboxTableViewer
* element.
*/
public class ValidationLabelProvider extends LabelProvider implements ITableLabelProvider {
public String getText(Object element) {
if (element == null)return ""; //$NON-NLS-1$
else if (element instanceof ValidatorWrapper)
return ((ValidatorWrapper) element).getName();
else
return super.getText(element);
}
private Image getImage(String imageName) {
boolean isDisabled = !_validatorsTable.isEnabled();
if (isDisabled)
imageName = imageName + ImageNames.disabled;
return ValidationUIPlugin.getPlugin().getImage(imageName);
}
public Image getColumnImage(Object element, int columnIndex) {
ValidatorWrapper v = (ValidatorWrapper) element;
if (columnIndex == 1) {
return getImage(v.isManual() ? ImageNames.okTable
: ImageNames.failTable);
} else if (columnIndex == 2) {
return getImage(v.isBuild() ? ImageNames.okTable
: ImageNames.failTable);
} else if (columnIndex == 3) {
if (v.isV2())return getImage(ImageNames.settings);
if (v.getValidator().getDelegatingId() != null)return getImage(ImageNames.settings);
return null;
}
return null;
}
public String getColumnText(Object element, int columnIndex) {
if (columnIndex == 0)
return ((ValidatorWrapper) element).getName();
return null;
}
}
/**
* This class is used to sort the CheckboxTableViewer elements.
*/
public class ValidationViewerSorter extends ViewerSorter {
public int compare(Viewer viewer, Object e1, Object e2) {
// Can't instantiate ViewerSorter because it's abstract, so use this
// inner class to represent it.
return super.compare(viewer, e1, e2);
}
}
public ValidatorListPage(Composite parent) throws InvocationTargetException {
_page = createPage(parent);
}
private void setupTableColumns(Table table) {
TableColumn validatorColumn = new TableColumn(table, SWT.NONE);
validatorColumn.setText(ValUIMessages.VALIDATOR);
validatorColumn.setResizable(false);
validatorColumn.setWidth(320);
TableColumn manualColumn = new TableColumn(table, SWT.NONE);
manualColumn.setText(ValUIMessages.MANUAL);
manualColumn.setResizable(false);
manualColumn.setWidth(40);
TableColumn buildColumn = new TableColumn(table, SWT.NONE);
buildColumn.setText(ValUIMessages.BUILD);
buildColumn.setResizable(false);
buildColumn.setWidth(40);
TableColumn settingsColumn = new TableColumn(table, SWT.NONE);
settingsColumn.setText(ValUIMessages.SETTINGS);
settingsColumn.setResizable(false);
settingsColumn.setWidth(50);
}
public Composite createPage(Composite parent) throws InvocationTargetException {
final ScrolledComposite sc1 = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
sc1.setLayoutData(new GridData(GridData.FILL_BOTH));
_composite = new Composite(sc1, SWT.NONE);
sc1.setContent(_composite);
_composite.setLayout(new GridLayout());
PlatformUI.getWorkbench().getHelpSystem().setHelp(_composite, ContextIds.VALIDATION_PREFERENCE_PAGE);
Composite validatorGroup = new Composite(_composite, SWT.NONE);
GridLayout validatorGroupLayout = new GridLayout();
validatorGroupLayout.numColumns = 2;
validatorGroup.setLayout(validatorGroupLayout);
PlatformUI.getWorkbench().getHelpSystem().setHelp(validatorGroup, ContextIds.VALIDATION_PREFERENCE_PAGE);
new Label(validatorGroup, SWT.NONE).setLayoutData(new GridData());
addOverride(validatorGroup);
addSuspend(validatorGroup);
addAutoSave(validatorGroup);
addConfirm(validatorGroup);
_listLabel = new Label(validatorGroup, SWT.NONE);
GridData listLabelData = new GridData(GridData.FILL_HORIZONTAL);
listLabelData.horizontalSpan = 2;
_listLabel.setLayoutData(listLabelData);
_listLabel.setText(ValUIMessages.PREF_VALLIST_TITLE);
_validatorsTable = new Table(validatorGroup, SWT.BORDER | SWT.FULL_SELECTION);
TableLayout tableLayout = new TableLayout();
tableLayout.addColumnData(new ColumnWeightData(160, true));
tableLayout.addColumnData(new ColumnWeightData(40, true));
tableLayout.addColumnData(new ColumnWeightData(30, true));
tableLayout.addColumnData(new ColumnWeightData(40, true));
_validatorsTable.setHeaderVisible(true);
_validatorsTable.setLinesVisible(true);
_validatorsTable.setLayout(tableLayout);
_validatorList = new TableViewer(_validatorsTable);
GridData validatorListData = new GridData(GridData.FILL_HORIZONTAL);
validatorListData.horizontalSpan = 2;
_validatorsTable.setLayoutData(validatorListData);
_validatorList.getTable().setLayoutData(validatorListData);
_validatorList.setLabelProvider(new ValidationLabelProvider());
_validatorList.setContentProvider(new ValidationContentProvider());
_validatorList.setSorter(new ValidationViewerSorter());
setupTableColumns(_validatorsTable);
_validatorList.setInput(ValidatorWrapper.createWrappers());
_validatorsTable.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
if (e.button != 1)
return;
TableItem tableItem = _validatorsTable.getItem(new Point(
e.x, e.y));
if (tableItem == null || tableItem.isDisposed()) {
// item no longer exists
return;
}
int columnNumber;
int columnsCount = _validatorsTable.getColumnCount();
if (columnsCount == 0) {
// If no TableColumn, Table acts as if it has a single
// column
// which takes the whole width.
columnNumber = 0;
} else {
columnNumber = -1;
for (int i = 0; i < columnsCount; i++) {
Rectangle bounds = tableItem.getBounds(i);
if (bounds.contains(e.x, e.y)) {
columnNumber = i;
break;
}
}
if (columnNumber == -1) {
return;
}
}
columnClicked(columnNumber);
}
});
_validatorsTable.setMenu(createContextMenu());
_validatorsTable.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
super.focusGained(e);
if (_validatorsTable.getSelectionCount() == 0) {
_validatorsTable.select(0);
}
}
});
_enableAllButton = new Button(validatorGroup, SWT.PUSH);
_enableAllButton.setLayoutData(new GridData());
_enableAllButton.setText(ValUIMessages.PREF_BUTTON_ENABLEALL);
_enableAllButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
performEnableAll();
} catch (InvocationTargetException exc) {
displayAndLogError(
ValUIMessages.VBF_EXC_INTERNAL_TITLE,
ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
}
}
});
PlatformUI.getWorkbench().getHelpSystem().setHelp(_enableAllButton, ContextIds.VALIDATION_PREFERENCE_PAGE);
_disableAllButton = new Button(validatorGroup, SWT.PUSH);
_disableAllButton.setLayoutData(new GridData());
_disableAllButton.setText(ValUIMessages.PREF_BUTTON_DISABLEALL);
_disableAllButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
performDisableAll();
} catch (InvocationTargetException exc) {
displayAndLogError(
ValUIMessages.VBF_EXC_INTERNAL_TITLE,
ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
}
}
});
PlatformUI.getWorkbench().getHelpSystem().setHelp(_disableAllButton, ContextIds.VALIDATION_PREFERENCE_PAGE);
// Have to set the tab order or only the first checkbox in a
// Composite can
// be tab-ed to. (Seems to apply only to checkboxes. Have to use the
// arrow
// key to navigate the checkboxes.)
validatorGroup.setTabList(new Control[] { _suspend, _autoSave,
_validatorList.getTable(), _enableAllButton, _disableAllButton });
updateWidgets();
applyDialogFont(_composite);
_composite.setSize(_composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
return _composite;
}
private void addConfirm(Composite validatorGroup) {
GridData gd;
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
_confirmButton = new Button(validatorGroup, SWT.CHECK);
_confirmButton.setLayoutData(gd);
_confirmButton.setText(ValUIMessages.PrefPageConfirmDialog);
_confirmButton.setSelection(_globalPreferences.getConfirmDialog());
_confirmButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
_globalPreferences.setConfirmDialog(_confirmButton.getSelection());
_confirmButton.setFocus();
}
});
}
private void addAutoSave(Composite validatorGroup) {
GridData gd;
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
_autoSave = new Button(validatorGroup, SWT.CHECK);
_autoSave.setLayoutData(gd);
_autoSave.setText(ValUIMessages.PrefPage_always_save);
_autoSave.setSelection(_globalPreferences.getSaveAutomatically());
_autoSave.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
_globalPreferences.setSaveAutomatically(_autoSave.getSelection());
_autoSave.setFocus();
}
});
}
private void addSuspend(Composite validatorGroup) {
GridData gd;
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
_suspend = new Button(validatorGroup, SWT.CHECK);
_suspend.setLayoutData(gd);
_suspend.setText(ValUIMessages.DISABLE_VALIDATION);
_suspend.setSelection(_globalPreferences.getDisableAllValidation());
_suspend.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
_suspend.setFocus();
_validatorsTable.setEnabled(!_suspend.getSelection());
_enableAllButton.setEnabled(!_suspend.getSelection());
_disableAllButton.setEnabled(!_suspend.getSelection());
_validatorList.refresh();
}
});
}
private void addOverride(Composite validatorGroup) {
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
_override = new Button(validatorGroup, SWT.CHECK);
_override.setLayoutData(gd);
_override.setText(ValUIMessages.PREF_BUTTON_OVERRIDE);
_override.setEnabled(true);
_override.setSelection(_globalPreferences.getOverride());
_override.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
_globalPreferences.setOverride(_override.getSelection());
_override.setFocus();
}
});
}
protected Menu createContextMenu() {
final Menu menu = new Menu(_validatorsTable.getShell(), SWT.POP_UP);
final MenuItem manualItem = new MenuItem(menu, SWT.CHECK);
manualItem.setText(ValUIMessages.PREF_MNU_MANUAL);
final MenuItem buildItem = new MenuItem(menu, SWT.CHECK);
buildItem.setText(ValUIMessages.PREF_MNU_BUILD);
final MenuItem settingsItem = new MenuItem(menu, SWT.PUSH);
settingsItem.setText(ValUIMessages.PREF_MNU_SETTINGS);
class MenuItemListener extends SelectionAdapter {
public void widgetSelected(SelectionEvent e) {
MenuItem menuItem = (MenuItem) e.getSource();
int index = menu.indexOf(menuItem) + 1;
columnClicked(index);
}
}
MenuItemListener listener = new MenuItemListener();
manualItem.addSelectionListener(listener);
buildItem.addSelectionListener(listener);
settingsItem.addSelectionListener(listener);
menu.addMenuListener(new MenuAdapter() {
public void menuShown(MenuEvent e) {
IStructuredSelection selection = (IStructuredSelection) _validatorList
.getSelection();
ValidatorWrapper vw = (ValidatorWrapper) selection
.getFirstElement();
manualItem.setSelection(vw.isManual());
buildItem.setSelection(vw.isBuild());
// settingsItem.setEnabled(vmd.isDelegating());
}
});
return menu;
}
protected void columnClicked(int columnToEdit) {
IStructuredSelection selection = (IStructuredSelection) _validatorList.getSelection();
ValidatorWrapper val = (ValidatorWrapper) selection.getFirstElement();
switch (columnToEdit) {
case 1:
val.setManual(!val.isManual());
break;
case 2:
val.setBuild(!val.isBuild());
break;
case 3:
Validator.V2 v2 = val.getValidator().asV2Validator();
if (v2 != null){
FilterDialog fd = new FilterDialog(_shell, val.getValidator(), null);
fd.open();
}
else {
handleOldDelegate(val);
}
break;
default:
break;
}
_validatorList.refresh();
}
private void handleOldDelegate(ValidatorWrapper val) {
try {
Validator.V1 v1 = val.getValidator().asV1Validator();
if (v1 == null)return;
ValidatorMetaData vmd = v1.getVmd();
if (!vmd.isDelegating())return;
GlobalConfiguration gc = ConfigurationManager.getManager().getGlobalConfiguration();
String delegateID = gc.getDelegateUniqueName(vmd);
Shell shell = Display.getCurrent().getActiveShell();
DelegatingValidatorPreferencesDialog dialog =
new DelegatingValidatorPreferencesDialog(shell, vmd, delegateID);
dialog.setBlockOnOpen(true);
dialog.create();
int result = dialog.open();
if (result == Window.OK)gc.setDelegateUniqueName(vmd, dialog.getDelegateID());
}
catch (InvocationTargetException e){
}
}
protected void updateWidgets() throws InvocationTargetException {
// Need to update even the widgets that do not change based on another
// widgets because of performDefaults(). If performDefaults() is
// selected, then the pagePreferences values are reset, and these widgets
// might also need to be updated.
updateAllWidgets();
updateHelp();
}
protected void updateWidgetsForDefaults()
throws InvocationTargetException {
updateAllWidgets();
updateHelp();
}
private void updateAllWidgets() throws InvocationTargetException {
_suspend.setSelection(_globalPreferences.getDisableAllValidation());
_autoSave.setSelection(_globalPreferences.getSaveAutomatically());
_confirmButton.setSelection(_globalPreferences.getConfirmDialog());
_override.setSelection(_globalPreferences.getOverride());
_validatorsTable.setEnabled(!_suspend.getSelection());
_enableAllButton.setEnabled(!_suspend.getSelection());
_disableAllButton.setEnabled(!_suspend.getSelection());
_validatorList.setInput(ValidatorWrapper.createWrappers());
_validatorList.refresh();
}
public boolean performOk() throws InvocationTargetException {
_globalPreferences.setDisableAllValidation(_suspend.getSelection());
_globalPreferences.setSaveAutomatically(_autoSave.getSelection());
updateValidators();
ValPrefManagerGlobal vpm = new ValPrefManagerGlobal();
vpm.saveShallowPreferences(_globalPreferences, ValManager.getDefault().getValidators());
saveV1Preferences();
return true;
}
/**
* Save the V1 preferences, so that the old validators continue to work.
*/
private void saveV1Preferences(){
try {
GlobalConfiguration gc = ConfigurationManager.getManager().getGlobalConfiguration();
// gc.setCanProjectsOverride(overrideButton.getSelection());
if (_globalPreferences.getDisableAllValidation())gc.setDisableAllValidation(true);
//pagePreferences.setEnabledValidators(getEnabledValidators());
gc.setEnabledManualValidators(getEnabledManualValidators());
gc.setEnabledBuildValidators(getEnabledBuildValidators());
gc.passivate();
gc.store();
}
catch (InvocationTargetException e){
ValidationUIPlugin.getPlugin().handleException(e);
}
}
/**
* Answer all the V1 validators that are manually enabled.
* @return
*/
private ValidatorMetaData[] getEnabledManualValidators() {
List<ValidatorMetaData> list = new LinkedList<ValidatorMetaData>();
for (Validator v : ValManager.getDefault().getValidators()){
if (v.isManualValidation()){
Validator.V1 v1 = v.asV1Validator();
if (v1 != null){
list.add(v1.getVmd());
}
}
}
ValidatorMetaData[] result = new ValidatorMetaData[list.size()];
list.toArray(result);
return result;
}
/**
* Answer all the V1 validators that are enabled for build.
* @return
*/
private ValidatorMetaData[] getEnabledBuildValidators() {
List<ValidatorMetaData> list = new LinkedList<ValidatorMetaData>();
for (Validator v : ValManager.getDefault().getValidators()){
if (v.isBuildValidation()){
Validator.V1 v1 = v.asV1Validator();
if (v1 != null){
list.add(v1.getVmd());
}
}
}
ValidatorMetaData[] result = new ValidatorMetaData[list.size()];
list.toArray(result);
return result;
}
/**
* Update the validates to reflect the preference changes.
*/
private void updateValidators() {
for (TableItem ti : _validatorsTable.getItems()) {
ValidatorWrapper vw = (ValidatorWrapper) ti.getData();
vw.updateValidator();
}
}
public boolean performDefaults() throws InvocationTargetException {
ValManager.getDefault().restoreDefaults();
updateWidgetsForDefaults();
getDefaultsButton().setFocus();
return true;
}
public boolean performEnableAll() throws InvocationTargetException {
setAllValidators(true);
_enableAllButton.setFocus();
_validatorList.refresh();
return true;
}
private void setAllValidators(boolean bool) {
TableItem[] items = _validatorsTable.getItems();
for (int i = 0; i < items.length; i++) {
ValidatorWrapper vw = (ValidatorWrapper) items[i].getData();
vw.setManual(bool);
vw.setBuild(bool);
}
}
public boolean performDisableAll() throws InvocationTargetException {
setAllValidators(false);
_disableAllButton.setFocus();
_validatorList.refresh();
return true;
}
protected void updateHelp() {
PlatformUI.getWorkbench().getHelpSystem().setHelp(_suspend, ContextIds.VALIDATION_PREFERENCE_PAGE_DISABLE_ALL_ENABLED);
}
/*
* Store the current values of the controls into the preference store.
*/
public Composite getControl() {
return _page;
}
public void dispose() {
_autoSave.dispose();
_suspend.dispose();
_disableAllButton.dispose();
_enableAllButton.dispose();
_listLabel.dispose();
_suspend.dispose();
_validatorList.getTable().dispose();
_composite.dispose();
}
public void loseFocus() {
// This page does not need to cache anything before it loses focus.
}
public void gainFocus() {/*
* // This page depends on the Workbench
* Preference page, so update the value of
* the // isAutoBuild (in case the workbench
* page's value has changed), and then
* update // this page's widgets. try {
* //_isAutoBuildEnabled =
* ValidatorManager.getManager().isGlobalAutoBuildEnabled();
* updateWidgets(); } catch
* (InvocationTargetException exc) {
* displayAndLogError(ResourceHandler.getExternalizedMessage(ResourceConstants.VBF_EXC_INTERNAL_TITLE),
* ResourceHandler.getExternalizedMessage(ResourceConstants.VBF_EXC_INTERNAL_PAGE),
* exc); }
*/
}
}
/**
* Wrap a validator so that temporary changes can be made to it.
*
* @author karasiuk
*
*/
private static class ValidatorWrapper {
private boolean _manual;
private boolean _build;
private Validator _validator;
public ValidatorWrapper(Validator validator) {
_validator = validator;
_manual = validator.isManualValidation();
_build = validator.isBuildValidation();
}
/**
* Update the validator to reflect the value in the preference.
*/
public void updateValidator() {
_validator.setBuildValidation(_build);
_validator.setManualValidation(_manual);
}
public String getName() {
return _validator.getName();
}
/**
* Create wrappers for all the validators.
*/
public static ValidatorWrapper[] createWrappers() {
Validator[] validators = ValManager.getDefault().getValidators();
ValidatorWrapper[] wrappers = new ValidatorWrapper[validators.length];
for (int i = 0; i < validators.length; i++)
wrappers[i] = new ValidatorWrapper(validators[i]);
return wrappers;
}
public boolean isManual() {
return _manual;
}
/** Is this a version 2 validator? */
public boolean isV2() {
return _validator.asV2Validator() != null;
}
/**
* Refresh the wrapper settings from the under lying validator model.
*/
public void refreshWrapper() {
_build = _validator.isBuildValidation();
_manual = _validator.isManualValidation();
}
public void setManual(boolean manual) {
_manual = manual;
}
public boolean isBuild() {
return _build;
}
public void setBuild(boolean build) {
_build = build;
}
public Validator getValidator() {
return _validator;
}
}
/*
* @see PreferencePage#createContents(Composite)
*/
protected Control createContents(Composite parent) {
try {
_shell = parent.getShell();
ValidationFramework.getDefault().getPreferenceStore().addPreferenceChangeListener(this);
try {
_pageImpl = new ValidatorListPage(parent);
} catch (InvocationTargetException exc) {
_pageImpl = new InvalidPage(parent);
displayAndLogError(ValUIMessages.VBF_EXC_INTERNAL_TITLE, ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
} catch (Throwable exc) {
_pageImpl = new InvalidPage(parent);
displayAndLogError(ValUIMessages.VBF_EXC_INTERNAL_TITLE, ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
}
} catch (Throwable exc) {
_pageImpl = new InvalidPage(parent);
displayAndLogError(ValUIMessages.VBF_EXC_INTERNAL_TITLE, ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
}
return _pageImpl.getControl();
}
/*
* @see IWorkbenchPreferencePage#init(IWorkbench)
*/
public void init(IWorkbench workbench) {
// init
}
/**
* Performs special processing when this page's Defaults button has been
* pressed.
* <p>
* This is a framework hook method for subclasses to do special things when
* the Defaults button has been pressed. Subclasses may override, but should
* call <code>super.performDefaults</code>.
* </p>
*/
protected void performDefaults() {
super.performDefaults();
try {
_pageImpl.performDefaults();
} catch (InvocationTargetException exc) {
displayAndLogError(ValUIMessages.VBF_EXC_INTERNAL_TITLE,
ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
} catch (Throwable exc) {
displayAndLogError(ValUIMessages.VBF_EXC_INTERNAL_TITLE,
ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
}
}
/**
* When the user presses the "OK" or "Apply" button on the Properties
* Guide/Properties Page, respectively, some processing is performed by this
* PropertyPage. If the page is found, and completes successfully, true is
* returned. Otherwise, false is returned, and the guide doesn't finish.
*/
public boolean performOk() {
try {
return _pageImpl.performOk();
} catch (InvocationTargetException exc) {
displayAndLogError(ValUIMessages.VBF_EXC_INTERNAL_TITLE,
ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
return false;
} catch (Throwable exc) {
displayAndLogError(ValUIMessages.VBF_EXC_INTERNAL_TITLE,
ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
return false;
}
}
/**
* Since the pages are inner classes of a child PreferencePage, not a
* PreferencePage itself, DialogPage's automatic disposal of its children's
* widgets cannot be used. Instead, dispose of each inner class' widgets
* explicitly.
*/
public void dispose() {
super.dispose();
ValidationFramework.getDefault().getPreferenceStore()
.removePreferenceChangeListener(this);
try {
if (_pageImpl != null) {
_pageImpl.dispose();
_pageImpl = null;
}
// TODO figure out what this thing did
// ExtensionManger.instance().getDelegate().disposePreferencePage();
} catch (Throwable exc) {
displayAndLogError(ValUIMessages.VBF_EXC_INTERNAL_TITLE,
ValUIMessages.VBF_EXC_INTERNAL_PAGE, exc);
}
}
private void logError(Throwable exc) {
ValidationUIPlugin.getPlugin().handleException(exc);
}
/*
* package visibility because if this method is private, then the compiler
* needs to create a synthetic accessor method for the internal classes, and
* that can have performance implications.
*/
void displayAndLogError(String title, String message, Throwable exc) {
logError(exc);
displayMessage(title, message, org.eclipse.swt.SWT.ICON_ERROR);
}
private void displayMessage(String title, String message, int iIconType) {
MessageBox messageBox = new MessageBox(getShell(),
org.eclipse.swt.SWT.OK | iIconType
| org.eclipse.swt.SWT.APPLICATION_MODAL);
messageBox.setMessage(message);
messageBox.setText(title);
messageBox.open();
}
/**
* @see org.eclipse.jface.dialogs.IDialogPage#setVisible(boolean)
*/
public void setVisible(boolean visible) {
super.setVisible(visible);
if (_pageImpl == null)return;
if (visible)_pageImpl.gainFocus();
else _pageImpl.loseFocus();
}
/**
* @see org.eclipse.jface.preference.PreferencePage#getDefaultsButton()
*/
protected Button getDefaultsButton() {
return super.getDefaultsButton();
}
public void preferenceChange(PreferenceChangeEvent event) {
// String key = event.getKey();
}
}