-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[26148] add uri field editor composite, general os specific preference
- Loading branch information
Showing
9 changed files
with
180 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
....elexis.core.ui.e4/src/ch/elexis/core/ui/e4/jface/preference/URIFieldEditorComposite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package ch.elexis.core.ui.e4.jface.preference; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.eclipse.jface.preference.FieldEditor; | ||
import org.eclipse.jface.preference.FieldEditorPreferencePage; | ||
import org.eclipse.jface.preference.IPreferenceStore; | ||
import org.eclipse.jface.util.IPropertyChangeListener; | ||
import org.eclipse.jface.util.PropertyChangeEvent; | ||
import org.eclipse.jface.viewers.ArrayContentProvider; | ||
import org.eclipse.jface.viewers.ComboViewer; | ||
import org.eclipse.jface.viewers.LabelProvider; | ||
import org.eclipse.jface.viewers.StructuredSelection; | ||
import org.eclipse.swt.SWT; | ||
import org.eclipse.swt.layout.GridData; | ||
import org.eclipse.swt.widgets.Combo; | ||
import org.eclipse.swt.widgets.Composite; | ||
|
||
import ch.elexis.core.preferences.PreferencesUtil; | ||
import ch.elexis.core.utils.CoreUtil; | ||
import ch.elexis.core.utils.CoreUtil.OS; | ||
|
||
/** | ||
* A {@link Composite} containing an operating system selector and a | ||
* {@link URIFieldEditor} for file system URIs. The method | ||
* {@link URIFieldEditorComposite#getPreferenceName(OS)} should be overwritten | ||
* to select the correct preference. | ||
* | ||
*/ | ||
public class URIFieldEditorComposite extends Composite { | ||
|
||
private URIFieldEditor storePath; | ||
|
||
private String defaultPreference; | ||
|
||
private ComboViewer osCombo; | ||
|
||
public URIFieldEditorComposite(String defaultPreference, Composite parent, int style) { | ||
super(parent, style); | ||
setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1)); | ||
|
||
this.defaultPreference = defaultPreference; | ||
|
||
createContent(); | ||
} | ||
|
||
/** | ||
* Use this method to set the {@link IPreferenceStore} used by the | ||
* {@link URIFieldEditor}. If a {@link FieldEditorPreferencePage} is available | ||
* do not use this method, but set directly with | ||
* {@link URIFieldEditorComposite#getFieldEditor()}. <br /> | ||
* The field editor will store after each value change if this method is used. | ||
* | ||
* @param preferenceStore | ||
*/ | ||
public void setPreferenceStore(IPreferenceStore preferenceStore) { | ||
storePath.setPreferenceStore(preferenceStore); | ||
storePath.load(); | ||
|
||
// add | ||
storePath.setPropertyChangeListener(new IPropertyChangeListener() { | ||
|
||
@Override | ||
public void propertyChange(PropertyChangeEvent event) { | ||
storePath.store(); | ||
} | ||
}); | ||
} | ||
|
||
private void createContent() { | ||
Combo comboOs = new Combo(this, SWT.None); | ||
osCombo = new ComboViewer(comboOs); | ||
comboOs.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); | ||
osCombo.setContentProvider(ArrayContentProvider.getInstance()); | ||
osCombo.setLabelProvider(new LabelProvider() { | ||
@Override | ||
public String getText(Object element) { | ||
return ((CoreUtil.OS) element).name(); | ||
} | ||
}); | ||
osCombo.setInput(CoreUtil.OS.values()); | ||
|
||
storePath = new URIFieldEditor( | ||
PreferencesUtil.getOsSpecificPreferenceName(CoreUtil.getOperatingSystemType(), defaultPreference), | ||
StringUtils.EMPTY, this); | ||
storePath.setEmptyStringAllowed(true); | ||
|
||
osCombo.addSelectionChangedListener(event -> { | ||
CoreUtil.OS selection = (OS) event.getStructuredSelection().getFirstElement(); | ||
String preferenceName = PreferencesUtil.getOsSpecificPreferenceName(selection, defaultPreference); | ||
storePath.store(); | ||
storePath.setPreferenceName(preferenceName); | ||
storePath.load(); | ||
}); | ||
|
||
osCombo.setSelection(new StructuredSelection(CoreUtil.getOperatingSystemType())); | ||
} | ||
|
||
public FieldEditor getFieldEditor() { | ||
return storePath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
bundles/ch.elexis.core/src/ch/elexis/core/preferences/PreferencesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ch.elexis.core.preferences; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import ch.elexis.core.services.IConfigService; | ||
import ch.elexis.core.utils.CoreUtil; | ||
import ch.elexis.core.utils.CoreUtil.OS; | ||
|
||
public class PreferencesUtil { | ||
|
||
public static String getOsSpecificPreferenceName(CoreUtil.OS system, String defaultPreference) { | ||
switch (system) { | ||
case WINDOWS: | ||
return defaultPreference + "_WINDOWS"; | ||
case MAC: | ||
return defaultPreference + "_MAC"; | ||
case LINUX: | ||
return defaultPreference + "_LINUX"; | ||
default: | ||
return defaultPreference; | ||
} | ||
} | ||
|
||
public static String getOsSpecificPreference(String defaultPreference, | ||
IConfigService configService) { | ||
OS operatingSystem = CoreUtil.getOperatingSystemType(); | ||
String osSpecificPreference = getOsSpecificPreferenceName(operatingSystem, defaultPreference); | ||
String value = configService.get(osSpecificPreference, null); | ||
if (StringUtils.isBlank(value)) { | ||
LoggerFactory.getLogger(PreferencesUtil.class) | ||
.warn("No OS specific path set, reverting to generic setting"); | ||
value = configService.get(defaultPreference, null); | ||
} | ||
return value; | ||
} | ||
} |