| /** |
| * |
| * 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 java.util.Map; |
| |
| import org.eclipse.jface.dialogs.IDialogConstants; |
| import org.eclipse.jface.preference.FieldEditor; |
| import org.eclipse.osbp.preferences.DataSourceConfiguration; |
| import org.eclipse.osbp.preferences.EnumDatabaseVendor; |
| import org.eclipse.osbp.preferences.ItemDescription; |
| import org.eclipse.osbp.preferences.Preference; |
| import org.eclipse.osbp.preferences.ProductConfigurationPrefs.DDL_GENERATION; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.custom.TableTree; |
| import org.eclipse.swt.custom.TableTreeEditor; |
| import org.eclipse.swt.custom.TableTreeItem; |
| import org.eclipse.swt.events.ModifyEvent; |
| import org.eclipse.swt.events.ModifyListener; |
| import org.eclipse.swt.events.SelectionAdapter; |
| import org.eclipse.swt.events.SelectionEvent; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.widgets.Button; |
| import org.eclipse.swt.widgets.Combo; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.swt.widgets.Event; |
| import org.eclipse.swt.widgets.Label; |
| import org.eclipse.swt.widgets.Listener; |
| import org.eclipse.swt.widgets.Table; |
| import org.eclipse.swt.widgets.Text; |
| |
| public abstract class ATableTreeFieldEditor extends FieldEditor { |
| |
| private final static String DEFAULT_ADD_LABEL = "duplicate"; |
| private final static String DEFAULT_REMOVE_LABEL = "remove"; |
| |
| // The top-level control for the field editor. |
| private Composite top; |
| // The list of selected tags. |
| protected TableTree tableTree; |
| TableTreeEditor editor; |
| private Table innerTable; |
| // The button for adding the contents of |
| // the available list to the selected list. |
| private Button add; |
| // The button for removing the currently-selected list item. |
| private Button remove; |
| private final ItemDescription fItemDescription; |
| protected Label label; |
| |
| public ATableTreeFieldEditor( |
| String name, |
| String labelText, |
| Composite parent, |
| ItemDescription itemDescription) { |
| super(name, labelText, parent); |
| fItemDescription = itemDescription; |
| } |
| |
| protected ItemDescription getItemDescription() { |
| return fItemDescription; |
| } |
| |
| /** |
| * @see org.eclipse.jface.preference.FieldEditor#doFillIntoGrid |
| * (Composite, int) |
| */ |
| protected void doFillIntoGrid(Composite parent, int numColumns) { |
| top = parent; |
| |
| int buttonWidth = convertHorizontalDLUsToPixels(top, IDialogConstants.BUTTON_WIDTH); |
| int buttonHeight = convertVerticalDLUsToPixels(top, IDialogConstants.BUTTON_HEIGHT); |
| |
| GridData gd = new GridData(GridData.FILL_HORIZONTAL); |
| gd.horizontalSpan = numColumns; |
| top.setLayoutData(gd); |
| |
| label = getLabelControl(top); |
| GridData labelData = new GridData(); |
| labelData.horizontalSpan = numColumns; |
| label.setLayoutData(labelData); |
| |
| tableTree = new TableTree(top, SWT.BORDER | SWT.V_SCROLL | SWT.FILL); |
| GridData tableTreeData = new GridData(GridData.FILL_HORIZONTAL); |
| tableTreeData.horizontalSpan = numColumns; |
| tableTreeData.heightHint = buttonHeight * 15; |
| tableTree.setLayoutData(tableTreeData); |
| tableTree.addSelectionListener(new SelectionAdapter() { |
| public void widgetSelected(SelectionEvent e) { |
| selectionChanged(); |
| } |
| }); |
| |
| editor = new TableTreeEditor(tableTree); |
| //The editor must have the same size as the cell and must |
| //not be any smaller than 50 pixels. |
| editor.horizontalAlignment = SWT.LEFT; |
| editor.grabHorizontal = true; |
| editor.minimumWidth = 50; |
| // editing the second column |
| final int EDITABLECOLUMN = TableTreeDescribedItem.VALUE; |
| |
| tableTree.addSelectionListener(new SelectionAdapter() { |
| public void widgetSelected(SelectionEvent e) { |
| // Clean up any previous editor control |
| Control oldEditor = editor.getEditor(); |
| if (oldEditor != null) oldEditor.dispose(); |
| |
| // Identify the selected row |
| TableTreeItem item = (TableTreeItem)e.item; |
| if (item == null) return; |
| |
| // The control that will be the editor must be a child of the Table |
| Control newEditor; |
| if ((ATableTreeFieldEditor.this instanceof DataSourcesFieldEditor) && |
| (item.getText(0).toLowerCase().contains("type"))) { |
| final Combo comboEditor = new Combo(innerTable, SWT.DROP_DOWN | SWT.READ_ONLY); |
| newEditor = comboEditor; |
| String selection = item.getText(EDITABLECOLUMN); |
| int index = 0; |
| for (EnumDatabaseVendor vendor : EnumDatabaseVendor.values()) { |
| String text = vendor.toString(); |
| comboEditor.add(text); |
| if (text.equals(selection)) { |
| comboEditor.select(index); |
| } |
| index++; |
| } |
| comboEditor.addListener(SWT.Modify, new Listener() { |
| public void handleEvent(Event e) { |
| editor.getItem().setText(EDITABLECOLUMN, comboEditor.getText()); |
| } |
| }); |
| } |
| else if ((ATableTreeFieldEditor.this instanceof PersistenceUnitsFieldEditor) && |
| (item.getText(0).toLowerCase().contains("source"))) { |
| final Combo comboEditor = new Combo(innerTable, SWT.DROP_DOWN | SWT.READ_ONLY); |
| newEditor = comboEditor; |
| String selection = item.getText(EDITABLECOLUMN); |
| int index = 0; |
| String value = getPreferenceStore().getString(Preference.DATASOURCES.toString()); |
| Map<String, DataSourceConfiguration> dataItems = DataSourceConfiguration.deserialize(value); |
| for (DataSourceConfiguration dataItem : dataItems.values()) { |
| String text = dataItem.getName(); |
| comboEditor.add(text); |
| if (text.equals(selection)) { |
| comboEditor.select(index); |
| } |
| index++; |
| } |
| comboEditor.addListener(SWT.Modify, new Listener() { |
| public void handleEvent(Event e) { |
| editor.getItem().setText(EDITABLECOLUMN, comboEditor.getText()); |
| } |
| }); |
| } |
| else if ((ATableTreeFieldEditor.this instanceof PersistenceUnitsFieldEditor) && |
| (item.getText(0).toLowerCase().contains("ddl"))) { |
| final Combo comboEditor = new Combo(innerTable, SWT.DROP_DOWN | SWT.READ_ONLY); |
| newEditor = comboEditor; |
| String selection = item.getText(EDITABLECOLUMN); |
| int index = 0; |
| for (DDL_GENERATION dataItem : DDL_GENERATION.values()) { |
| String text = "EclipseLink: "+dataItem.eclipseLink()+" / Hibernate: "+dataItem.hibernate(); |
| comboEditor.add(text); |
| if (dataItem.eclipseLink().equals(selection)) { |
| comboEditor.select(index); |
| } |
| index++; |
| } |
| comboEditor.addListener(SWT.Modify, new Listener() { |
| public void handleEvent(Event e) { |
| editor.getItem().setText(EDITABLECOLUMN, comboEditor.getText().split(" ")[1]); |
| } |
| }); |
| } |
| else { |
| final Text textEditor = new Text(innerTable, SWT.NONE); |
| newEditor = textEditor; |
| textEditor.setText(item.getText(EDITABLECOLUMN)); |
| textEditor.addModifyListener(new ModifyListener() { |
| public void modifyText(ModifyEvent e) { |
| editor.getItem().setText(EDITABLECOLUMN, textEditor.getText()); |
| } |
| }); |
| textEditor.selectAll(); |
| } |
| newEditor.setFocus(); |
| editor.setEditor(newEditor, item, EDITABLECOLUMN); |
| } |
| }); |
| |
| // Create the TableTree and set some attributes on the underlying table |
| innerTable = tableTree.getTable(); |
| innerTable.setHeaderVisible(false); |
| innerTable.setLinesVisible(false); |
| GridData innerTableData = new GridData(GridData.FILL_BOTH); |
| innerTable.setLayoutData(innerTable); |
| |
| // Create the columns, passing the underlying table |
| (new org.eclipse.swt.widgets.TableColumn(innerTable, SWT.FILL)).setText("Attribute"); |
| (new org.eclipse.swt.widgets.TableColumn(innerTable, SWT.FILL)).setText("Value"); |
| (new org.eclipse.swt.widgets.TableColumn(innerTable, SWT.FILL)).setText(""); |
| |
| // Create the remove button. |
| if (hasRemoveButton()) { |
| remove = new Button(top, SWT.NONE); |
| remove.setEnabled(false); |
| remove.setText(DEFAULT_REMOVE_LABEL); |
| remove.addSelectionListener(new SelectionAdapter() { |
| public void widgetSelected(SelectionEvent e) { |
| remove(); |
| } |
| }); |
| GridData removeData = new GridData(); //GridData.FILL_HORIZONTAL); |
| removeData.heightHint = buttonHeight; |
| removeData.widthHint = buttonWidth; |
| remove.setLayoutData(removeData); |
| } |
| |
| // Create the add button. |
| if (hasAddButton()) { |
| add = new Button(top, SWT.NONE); |
| add.setEnabled(false); |
| add.setText(DEFAULT_ADD_LABEL); |
| add.addSelectionListener(new SelectionAdapter() { |
| public void widgetSelected(SelectionEvent e) { |
| add(); |
| } |
| }); |
| GridData addData = new GridData(); //GridData.FILL_HORIZONTAL); |
| addData.heightHint = buttonHeight; |
| addData.widthHint = buttonWidth; |
| add.setLayoutData(addData); |
| } |
| } |
| |
| abstract protected boolean hasRemoveButton(); |
| abstract protected boolean hasAddButton(); |
| |
| /** |
| * @see org.eclipse.jface.preference.FieldEditor#doLoad() |
| */ |
| protected void doLoad() { |
| doLoad(getPreferenceStore().getString(getPreferenceName())); |
| } |
| |
| /** |
| * @see org.eclipse.jface.preference.FieldEditor#doLoadDefault() |
| */ |
| protected void doLoadDefault() { |
| doLoad(getPreferenceStore().getDefaultString(getPreferenceName())); |
| } |
| |
| private void doLoad(String value) { |
| tableTree.removeAll(); |
| _doLoad(value, getPreferenceStore().getDefaultString(getPreferenceName())); |
| org.eclipse.swt.widgets.TableColumn[] columns = innerTable.getColumns(); |
| for (int i = 0, n = 3; i < n; i++) { |
| columns[i].pack(); |
| if (i == TableTreeDescribedItem.HINT) { |
| columns[i].setWidth(30); |
| } |
| else { |
| columns[i].setWidth(columns[i].getWidth()*2); |
| } |
| } |
| //columns[2].pack(); |
| //columns[2].setWidth(30); |
| Control oldEditor = editor.getEditor(); |
| if (oldEditor != null) { |
| oldEditor.dispose(); |
| } |
| // showDefaultHint(); |
| } |
| |
| abstract protected void _doLoad(String value, String defaults); |
| |
| /** |
| * @see org.eclipse.jface.preference.FieldEditor#doStore() |
| */ |
| protected void doStore() { |
| String s = _doStore(); |
| if (s != null) { |
| getPreferenceStore().setValue(getPreferenceName(), s); |
| doLoad(); |
| } |
| } |
| |
| abstract protected String _doStore(); |
| |
| /** |
| * @see org.eclipse.jface.preference.FieldEditor#getNumberOfControls() |
| */ |
| public int getNumberOfControls() { |
| // The button composite and the text field. |
| return 2; |
| } |
| |
| // Adds the string in the text field to the list. |
| private void add() { |
| if (tableTree.getSelectionCount() == 1) { |
| TableTreeItem source = tableTree.getSelection()[0]; |
| while ((source != null) && (source.getParentItem() != null)) { |
| source = source.getParentItem(); |
| } |
| if (source != null) { |
| TableTreeDescribedItem.clone(source, "Copy"); |
| Control oldEditor = editor.getEditor(); |
| if (oldEditor != null) oldEditor.dispose(); |
| } |
| // showDefaultHint(); |
| } |
| } |
| |
| private void remove() { |
| if (tableTree.getSelectionCount() == 1) { |
| TableTreeItem selected = tableTree.getSelection()[0]; |
| while ((selected != null) && (selected.getParentItem() != null)) { |
| selected = selected.getParentItem(); |
| } |
| if (selected != null) { |
| selected.dispose(); |
| Control oldEditor = editor.getEditor(); |
| if (oldEditor != null) oldEditor.dispose(); |
| } |
| // showDefaultHint(); |
| } |
| } |
| |
| /** |
| * Sets the label for the button that adds |
| * the contents of the text field to the list. |
| */ |
| public void setAddButtonText(String text) { |
| if (add != null) { |
| add.setText(text); |
| } |
| } |
| |
| /** |
| * Sets the label for the button that removes |
| * the selected item from the list. |
| */ |
| public void setRemoveButtonText(String text) { |
| if (remove != null) { |
| remove.setText(text); |
| } |
| } |
| |
| // Sets the enablement of the remove button depending |
| // on the selection in the list. |
| private void selectionChanged() { |
| int index = tableTree.getSelectionCount(); |
| if (add != null) { |
| add.setEnabled(index == 1); |
| } |
| if (remove != null) { |
| remove.setEnabled((index == 1) && (tableTree.getItemCount() > 1)); |
| } |
| } |
| |
| /** |
| * @see org.eclipse.jface.preference.FieldEditor#adjustForNumColumns(int) |
| */ |
| protected void adjustForNumColumns(int numColumns) { |
| ((GridData)top.getLayoutData()).horizontalSpan = numColumns; |
| } |
| |
| // protected void showDefaultHint() { |
| // TreeSet<String> active = new TreeSet<>(); |
| // for (String item : _doStore().split(";")) { |
| // active.add(item); |
| // } |
| // TreeSet<String> defaults = new TreeSet<>(); |
| // for (String item : getPreferenceStore().getDefaultString(getPreferenceName()).split(";")) { |
| // defaults.add(item); |
| // } |
| // APreferencePage.showLabelDefaultHint(label, active.equals(defaults)); |
| // } |
| |
| // @Override |
| // public Label getLabelControl(Composite parent) { |
| // if (label == null) { |
| // label = new CLabel(parent, SWT.LEFT | SWT.RIGHT_TO_LEFT); |
| // label.setFont(parent.getFont()); |
| // String text = getLabelText(); |
| // if (text != null) { |
| // label.setText(text); |
| // } |
| // label.addDisposeListener(new DisposeListener() { |
| // @Override |
| // public void widgetDisposed(DisposeEvent event) { |
| // label = null; |
| // } |
| // }); |
| // } else { |
| // checkParent(label, parent); |
| // } |
| // return null; |
| // } |
| |
| // @Override |
| // public void setEnabled(boolean enabled, Composite parent) { |
| // label.setEnabled(enabled); |
| // } |
| } |