|
| 1 | +package de.vogella.preferences.test.ui; |
| 2 | + |
| 3 | +import org.eclipse.core.runtime.preferences.ConfigurationScope; |
| 4 | +import org.eclipse.swt.SWT; |
| 5 | +import org.eclipse.swt.events.SelectionAdapter; |
| 6 | +import org.eclipse.swt.events.SelectionEvent; |
| 7 | +import org.eclipse.swt.widgets.Button; |
| 8 | +import org.eclipse.swt.widgets.Composite; |
| 9 | +import org.osgi.service.prefs.BackingStoreException; |
| 10 | +import org.osgi.service.prefs.Preferences; |
| 11 | + |
| 12 | +public class ButtonComposite extends Composite { |
| 13 | + |
| 14 | + public ButtonComposite(Composite parent, int style) { |
| 15 | + super(parent, style); |
| 16 | + Button write = new Button(parent, SWT.PUSH); |
| 17 | + write.setText("Write"); |
| 18 | + write.addSelectionListener(new SelectionAdapter() { |
| 19 | + @Override |
| 20 | + public void widgetSelected(SelectionEvent e) { |
| 21 | + |
| 22 | + Preferences preferences = ConfigurationScope.INSTANCE |
| 23 | + .getNode("de.vogella.preferences.test"); |
| 24 | + Preferences sub1 = preferences.node("note1"); |
| 25 | + Preferences sub2 = preferences.node("node2"); |
| 26 | + sub1.put("h1", "Hello"); |
| 27 | + sub1.put("h2", "Hello again"); |
| 28 | + sub2.put("h1", "Moin"); |
| 29 | + |
| 30 | + try { |
| 31 | + // Forces the application to save the preferences |
| 32 | + preferences.flush(); |
| 33 | + } catch (BackingStoreException e2) { |
| 34 | + e2.printStackTrace(); |
| 35 | + } |
| 36 | + } |
| 37 | + }); |
| 38 | + Button read = new Button(parent, SWT.PUSH); |
| 39 | + read.setText("Read"); |
| 40 | + read.addSelectionListener(new SelectionAdapter() { |
| 41 | + @Override |
| 42 | + public void widgetSelected(SelectionEvent e) { |
| 43 | + Preferences preferences = ConfigurationScope.INSTANCE |
| 44 | + .getNode("de.vogella.preferences.test"); |
| 45 | + Preferences sub1 = preferences.node("note1"); |
| 46 | + Preferences sub2 = preferences.node("node2"); |
| 47 | + System.out.println(sub1.get("h1", "default")); |
| 48 | + System.out.println(sub1.get("h2", "default")); |
| 49 | + System.out.println(sub2.get("h1", "default")); |
| 50 | + |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + Button clear = new Button(parent, SWT.PUSH); |
| 55 | + clear.addSelectionListener(new SelectionAdapter() { |
| 56 | + @Override |
| 57 | + public void widgetSelected(SelectionEvent e) { |
| 58 | + Preferences preferences = ConfigurationScope.INSTANCE |
| 59 | + .getNode("de.vogella.preferences.test"); |
| 60 | + Preferences sub1 = preferences.node("note1"); |
| 61 | + Preferences sub2 = preferences.node("node2"); |
| 62 | + // Delete the existing settings |
| 63 | + try { |
| 64 | + sub1.clear(); |
| 65 | + sub2.clear(); |
| 66 | + preferences.flush(); |
| 67 | + } catch (BackingStoreException e1) { |
| 68 | + e1.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + }); |
| 72 | + clear.setText("clear"); |
| 73 | + } |
| 74 | +} |
0 commit comments