|
| 1 | +package ch.elexis.core.ui.e4.jface.preference; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.apache.commons.lang3.StringUtils; |
| 7 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 8 | +import org.eclipse.jface.viewers.ArrayContentProvider; |
| 9 | +import org.eclipse.jface.viewers.ComboViewer; |
| 10 | +import org.eclipse.jface.viewers.LabelProvider; |
| 11 | +import org.eclipse.jface.viewers.StructuredSelection; |
| 12 | +import org.eclipse.swt.SWT; |
| 13 | +import org.eclipse.swt.layout.GridData; |
| 14 | +import org.eclipse.swt.layout.GridLayout; |
| 15 | +import org.eclipse.swt.widgets.Combo; |
| 16 | +import org.eclipse.swt.widgets.Composite; |
| 17 | +import org.eclipse.swt.widgets.Label; |
| 18 | + |
| 19 | +import ch.elexis.core.l10n.Messages; |
| 20 | +import ch.elexis.core.utils.CoreUtil; |
| 21 | +import ch.elexis.core.utils.CoreUtil.OS; |
| 22 | + |
| 23 | +/** |
| 24 | + * Several {@link URIFieldEditorComposite} sharing one operating system |
| 25 | + * selector. Use this instead of single {@link URIFieldEditorComposite} if a |
| 26 | + * preference page holds more than one path, so all paths of the page always |
| 27 | + * belong to the same operating system: |
| 28 | + * |
| 29 | + * <pre> |
| 30 | + * Betriebssystem [ WINDOWS v ] |
| 31 | + * Download [ ****************** ] [ Durchsuchen... ] |
| 32 | + * Upload [ ****************** ] [ Durchsuchen... ] |
| 33 | + * </pre> |
| 34 | + * |
| 35 | + * @since 3.14 |
| 36 | + */ |
| 37 | +public class OsPathEditorGroup extends Composite { |
| 38 | + |
| 39 | + private ComboViewer osCombo; |
| 40 | + |
| 41 | + private final List<URIFieldEditorComposite> editors = new ArrayList<>(); |
| 42 | + |
| 43 | + private IPreferenceStore preferenceStore; |
| 44 | + |
| 45 | + public OsPathEditorGroup(Composite parent, int style) { |
| 46 | + super(parent, style); |
| 47 | + setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1)); |
| 48 | + |
| 49 | + GridLayout layout = new GridLayout(2, false); |
| 50 | + layout.marginWidth = 0; |
| 51 | + layout.marginHeight = 0; |
| 52 | + layout.horizontalSpacing = 8; |
| 53 | + setLayout(layout); |
| 54 | + |
| 55 | + createOsSelector(); |
| 56 | + } |
| 57 | + |
| 58 | + private void createOsSelector() { |
| 59 | + Label label = new Label(this, SWT.NONE); |
| 60 | + label.setText(Messages.Core_OperatingSystem); |
| 61 | + label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); |
| 62 | + |
| 63 | + Combo combo = new Combo(this, SWT.DROP_DOWN | SWT.READ_ONLY); |
| 64 | + combo.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false)); |
| 65 | + osCombo = new ComboViewer(combo); |
| 66 | + osCombo.setContentProvider(ArrayContentProvider.getInstance()); |
| 67 | + osCombo.setLabelProvider(new LabelProvider() { |
| 68 | + @Override |
| 69 | + public String getText(Object element) { |
| 70 | + return ((CoreUtil.OS) element).name(); |
| 71 | + } |
| 72 | + }); |
| 73 | + osCombo.setInput(CoreUtil.OS.values()); |
| 74 | + |
| 75 | + osCombo.addSelectionChangedListener(event -> { |
| 76 | + OS selection = (OS) event.getStructuredSelection().getFirstElement(); |
| 77 | + for (URIFieldEditorComposite editor : getPathEditors()) { |
| 78 | + editor.setOperatingSystem(selection); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + osCombo.setSelection(new StructuredSelection(CoreUtil.getOperatingSystemType())); |
| 83 | + } |
| 84 | + |
| 85 | + public URIFieldEditorComposite addPathEditor(String preferenceName, String labelText) { |
| 86 | + URIFieldEditorComposite editor = new URIFieldEditorComposite(preferenceName, labelText, this, SWT.NONE); |
| 87 | + editor.setOsSelectorVisible(false); |
| 88 | + editor.setEmptyStringAllowed(true); |
| 89 | + editor.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); |
| 90 | + editor.setOperatingSystem(getOperatingSystem()); |
| 91 | + if (preferenceStore != null) { |
| 92 | + editor.setPreferenceStore(preferenceStore); |
| 93 | + } |
| 94 | + editors.add(editor); |
| 95 | + alignLabels(); |
| 96 | + return editor; |
| 97 | + } |
| 98 | + |
| 99 | + public void setPreferenceStore(IPreferenceStore preferenceStore) { |
| 100 | + this.preferenceStore = preferenceStore; |
| 101 | + for (URIFieldEditorComposite editor : getPathEditors()) { |
| 102 | + editor.setPreferenceStore(preferenceStore); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public OS getOperatingSystem() { |
| 107 | + return (OS) osCombo.getStructuredSelection().getFirstElement(); |
| 108 | + } |
| 109 | + |
| 110 | + public List<URIFieldEditorComposite> getPathEditors() { |
| 111 | + List<URIFieldEditorComposite> ret = new ArrayList<>(editors.size()); |
| 112 | + for (URIFieldEditorComposite editor : editors) { |
| 113 | + if (!editor.isDisposed()) { |
| 114 | + ret.add(editor); |
| 115 | + } |
| 116 | + } |
| 117 | + return ret; |
| 118 | + } |
| 119 | + |
| 120 | + public void adjustHorizontalSpan() { |
| 121 | + if (getParent().getLayout() instanceof GridLayout && getLayoutData() instanceof GridData) { |
| 122 | + ((GridData) getLayoutData()).horizontalSpan = ((GridLayout) getParent().getLayout()).numColumns; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private void alignLabels() { |
| 127 | + int width = 0; |
| 128 | + for (URIFieldEditorComposite editor : getPathEditors()) { |
| 129 | + Label label = editor.getLabelControl(); |
| 130 | + if (StringUtils.isNotBlank(label.getText())) { |
| 131 | + width = Math.max(width, label.computeSize(SWT.DEFAULT, SWT.DEFAULT).x); |
| 132 | + } |
| 133 | + } |
| 134 | + for (URIFieldEditorComposite editor : getPathEditors()) { |
| 135 | + Label label = editor.getLabelControl(); |
| 136 | + if (StringUtils.isNotBlank(label.getText())) { |
| 137 | + GridData labelData = new GridData(SWT.LEFT, SWT.CENTER, false, false); |
| 138 | + labelData.widthHint = width; |
| 139 | + label.setLayoutData(labelData); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments