| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * 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: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.preferences.ui.component; |
| |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.jdt.core.IJavaProject; |
| import org.eclipse.jface.dialogs.Dialog; |
| import org.eclipse.jface.dialogs.IDialogConstants; |
| import org.eclipse.jface.preference.ComboFieldEditor; |
| import org.eclipse.jface.preference.FieldEditor; |
| import org.eclipse.jface.preference.FieldEditorPreferencePage; |
| import org.eclipse.jface.preference.IPreferenceStore; |
| import org.eclipse.jface.preference.StringFieldEditor; |
| import org.eclipse.jface.resource.ImageDescriptor; |
| import org.eclipse.jface.resource.JFaceResources; |
| import org.eclipse.osbp.preferences.Preference; |
| import org.eclipse.osbp.preferences.ProductConfiguration; |
| import org.eclipse.osbp.preferences.ui.data.ProductConfigurationStore; |
| import org.eclipse.osbp.preferences.ui.utils.ConfigurationFileVerifier.ConfigurationFileDescription; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.custom.CLabel; |
| 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.layout.GridData; |
| import org.eclipse.swt.layout.GridLayout; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.ui.IWorkbench; |
| import org.eclipse.ui.IWorkbenchPreferencePage; |
| import org.eclipse.ui.IWorkbenchPropertyPage; |
| import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; |
| import org.eclipse.xtext.ui.editor.preferences.fields.LabelFieldEditor; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| abstract public class APreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage, IWorkbenchPropertyPage, IPreferenceCheckStateProvider { |
| |
| protected enum PageType { |
| PREFENCE, |
| PROPERTY |
| } |
| |
| private static final Logger LOGGER = LoggerFactory.getLogger(ProductConfiguration.class); |
| |
| // Online REGEX Tester: http://java-regex-tester.appspot.com/ |
| public final static String REGEX_URL_XML_FILE = "(file|http|https)://(.)*\\.xml"; |
| |
| private final static String I18NPACKAGE = "preferences."; |
| |
| private final String fDescription; |
| private Item[] fItems; |
| private Button defaultsApplyButton; |
| protected boolean createDefaultAndApplyButton = true; |
| protected boolean defaultButtonAvailable = true; |
| |
| protected ProductConfigurationStore store; |
| |
| private PageType fPageType; |
| private IAdaptable fAdaptable; |
| |
| public final static String DEFAULT_VALUE_NO = "message_info.gif";//"defaultvalue-no.png"; |
| public final static String DEFAULT_VALUE_YES = "defaultvalue-yes.png"; |
| |
| public final static String STATUS_TODO = "open.gif"; |
| public final static String STATUS_OK = "ok.gif"; |
| public final static String STATUS_WARNING = "message_warning.gif"; |
| public final static String STATUS_INFO = "message_info.gif"; |
| public final static String STATUS_ERROR = "message_error.gif"; |
| |
| public final static String ITEM_TO_ADD = "add.gif"; |
| public final static String ITEM_TO_UPDATE = "update.gif"; |
| public final static String ITEM_TO_REMOVE = "delete.gif"; |
| |
| public static void showLabelDefaultHint(CLabel label, boolean isDefault) { |
| if (!isDefault) { |
| label.setImage(APreferencePage.getImage(DEFAULT_VALUE_NO)); |
| } |
| else { |
| label.setImage(APreferencePage.getImage(DEFAULT_VALUE_YES)); |
| } |
| } |
| |
| @Override |
| protected void noDefaultAndApplyButton() { |
| super.noDefaultAndApplyButton(); |
| createDefaultAndApplyButton = false; |
| } |
| |
| public static Image getImage(String imageName) { |
| try { |
| return getImageDesc(imageName).createImage(); |
| } |
| catch (Exception e) {} // NOP |
| return null; |
| } |
| |
| public static ImageDescriptor getImageDesc(String imageName) { |
| try { |
| return IDEWorkbenchPlugin.imageDescriptorFromPlugin( |
| "org.eclipse.osbp.preferences.ui.page", |
| "/icons/"+imageName);//$NON-NLS-1$ |
| } |
| catch (Exception e) {} // NOP |
| return null; |
| } |
| |
| public APreferencePage(String pageName, String description) { |
| super(I18NPACKAGE+pageName, GRID); |
| fPageType = PageType.PREFENCE; |
| setPreferenceStore(ProductConfigurationStore.instance()); |
| fDescription = description; |
| } |
| |
| @Override |
| public void setTitle(String title) { |
| super.setTitle(title); |
| // try { |
| // setImageDescriptor(getImageDesc("osbp-symbol-23x23.png")); |
| // } |
| // catch (Exception e) {} // NOP |
| } |
| |
| protected boolean isPageType(PageType pageType) { |
| return pageType == fPageType; |
| } |
| |
| @Override |
| public void dispose() { |
| store.removeProductConfigurationPrefsChangedListener(this); |
| super.dispose(); |
| } |
| |
| @Override |
| public void setPreferenceStore(IPreferenceStore store) { |
| super.setPreferenceStore(store); |
| this.store = (ProductConfigurationStore) store; |
| this.store.addProductConfigurationPrefsChangedListener(this); |
| } |
| |
| public void productConfigurationPrefsChanged() { |
| initialize(); |
| } |
| |
| protected abstract Item[] initializeItems(); |
| |
| protected Button getDefaultsApplyButton() { |
| return defaultsApplyButton; |
| } |
| |
| @Override |
| protected void contributeButtons(Composite buttonBar) { |
| if (createDefaultAndApplyButton) { |
| String[] labels = JFaceResources.getStrings(new String[] { |
| "defaults", "apply" }); //$NON-NLS-2$//$NON-NLS-1$ |
| int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); |
| defaultsApplyButton = new Button(buttonBar, SWT.PUSH); |
| defaultsApplyButton.setText((labels[0]+" + "+labels[1]).replace("&", "")); |
| Dialog.applyDialogFont(defaultsApplyButton); |
| GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
| Point minButtonSize = defaultsApplyButton.computeSize(SWT.DEFAULT, |
| SWT.DEFAULT, true); |
| data.widthHint = Math.max(widthHint, minButtonSize.x); |
| defaultsApplyButton.setLayoutData(data); |
| defaultsApplyButton.addSelectionListener(new SelectionAdapter() { |
| @Override |
| public void widgetSelected(SelectionEvent e) { |
| performDefaults(); |
| performOk(); |
| } |
| }); |
| GridLayout layout = (GridLayout) buttonBar.getLayout(); |
| layout.numColumns = layout.numColumns + 1; |
| } |
| } |
| |
| @Override |
| public void createControl(Composite parent) { |
| super.createControl(parent); |
| if (getDefaultsButton() != null) { |
| getDefaultsButton().setEnabled(defaultButtonAvailable); |
| } |
| } |
| |
| @Override |
| public final void createFieldEditors() { |
| fItems = initializeItems(); |
| Composite parent = getFieldEditorParent(); |
| for (Item item : fItems) { |
| boolean callEnable = true; |
| boolean enabled = item.fEnabled; |
| switch (item.fType) { |
| case BOOLEAN: |
| item.fEditor = new HookedBooleanFieldEditor(item.fPreference.toString(), item.fLabel, parent); |
| break; |
| case DIRECTORY: |
| item.fEditor = new HookedDirectoryFieldEditor(item.fPreference.toString(), item.fLabel, this, parent); |
| break; |
| case INTEGER: |
| item.fEditor = new HookedIntegerFieldEditor(item.fPreference.toString(), item.fLabel, this, parent); |
| break; |
| case LABEL: |
| item.fEditor = new LabelFieldEditor(item.fLabel, parent); |
| break; |
| case MULTILINE: |
| item.fEditor = new HookedMultilineStringFieldEditor(item.fPreference.toString(), item.fLabel, this, parent); |
| break; |
| case SELECT: |
| item.fEditor = new ComboFieldEditor(item.fPreference.toString(), item.fLabel, item.fNamesValues, parent); |
| break; |
| case TABLE: |
| // https://svn.apache.org/repos/asf/click/tags/clickide-1-6-0/net.sf.clickide/src/net/sf/clickide/preferences/ClickTemplatePreferencePage.java |
| break; |
| case TEXT: |
| case EMAIL: // @TODO: enable email validation |
| case PASSWORD: // @TODO: enable password "validation" |
| case SERVER: // @TODO: enable server validation |
| item.fEditor = new HookedStringFieldEditor(item.fPreference.toString(), item.fLabel, this, parent); |
| ((HookedStringFieldEditor)item.fEditor).getTextControl(parent).setEditable(enabled); |
| enabled = true; |
| break; |
| case LINK: |
| item.fEditor = new LinkFieldEditor(item.fPreference.toString(), item.fLabel, parent); |
| break; |
| case LIST_ADD_REMOVE: |
| item.fEditor = new AddRemoveListFieldEditor(item.fPreference.toString(), item.fLabel, item.fNamesValues, parent); |
| ((AddRemoveListFieldEditor) item.fEditor).setSeperator(item.fSeparator); |
| break; |
| case TREE_DATASOURCES: |
| item.fEditor = new DataSourcesFieldEditor(item.fPreference.toString(), item.fLabel, parent); |
| defaultButtonAvailable = false; |
| break; |
| case TREE_PERSISTENCEUNITS: |
| item.fEditor = new PersistenceUnitsFieldEditor(item.fPreference.toString(), item.fLabel, parent); |
| defaultButtonAvailable = false; |
| break; |
| case FILE_EDITOR: |
| item.fEditor = new FileOpenCallEditor(item.fPreference.toString(), item.fLabel, parent); |
| callEnable = false; |
| break; |
| case TREE_PRODUCTCONFIGURATION: |
| item.fEditor = new ProductConfigurationFieldEditor(item.fPreference.toString(), item.fLabel, parent); |
| defaultButtonAvailable = false; |
| break; |
| } |
| addField(item.fEditor); |
| if (callEnable) { |
| item.fEditor.setEnabled(enabled, parent); |
| } |
| } |
| } |
| |
| public FieldEditor getEditor(Preference preference) { |
| if (fItems != null) { |
| for (Item item : fItems) { |
| if (preference.equals(item.fPreference)) { |
| return item.fEditor; |
| } |
| } |
| } |
| return null; |
| } |
| |
| public Object getValue(Preference preference) { |
| FieldEditor editor = getEditor(preference); |
| Object value = null; |
| try { |
| if (editor != null) { |
| if (editor instanceof StringFieldEditor) { |
| value = ((StringFieldEditor)editor).getTextControl(getFieldEditorParent()).getText(); |
| } |
| } |
| } |
| catch (Exception e) { |
| e.printStackTrace(); |
| } |
| return value; |
| } |
| |
| @Override |
| protected void initialize() { |
| try { |
| super.initialize(); |
| } |
| catch (IllegalArgumentException iae) { |
| LOGGER.error("initialize()... ", iae); |
| } |
| } |
| |
| @Override |
| public boolean doCheckState(FieldEditor editor, String input) { |
| return true; |
| } |
| |
| @Override |
| public final void init(IWorkbench workbench) { |
| setDescription(fDescription); |
| } |
| |
| @Override |
| public IAdaptable getElement() { |
| return fAdaptable; |
| } |
| |
| @Override |
| public void setElement(IAdaptable element) { |
| fPageType = PageType.PROPERTY; |
| fAdaptable = element; |
| if (element instanceof IJavaProject) { |
| store.selectOsbpProductProject(((IJavaProject)element).getProject()); |
| } |
| } |
| } |