Have icon for "reset" entry in reflog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / preferences / DoublePreferencesPreferencePage.java
blob57245c7605e788a1a848e7f50b2ba51d6f7f1efa
1 /*******************************************************************************
2 * Copyright (C) 2017 Thomas Wolf <thomas.wolf@paranor.ch>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.preferences;
13 import java.io.IOException;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.egit.ui.Activator;
18 import org.eclipse.jface.preference.FieldEditorPreferencePage;
19 import org.eclipse.jface.preference.IPersistentPreferenceStore;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.resource.JFaceResources;
23 import org.eclipse.jface.util.Policy;
25 /**
26 * A {@link FieldEditorPreferencePage} that provides access to a secondary
27 * {@link IPreferenceStore} and that takes care of storing that secondary store,
28 * if necessary.
30 public abstract class DoublePreferencesPreferencePage
31 extends FieldEditorPreferencePage {
33 private IPreferenceStore secondaryStore;
35 /**
36 * Creates a new {@link DoublePreferencesPreferencePage} with
37 * {@link FieldEditorPreferencePage#FLAT FLAT} style and neither title nor
38 * image.
40 public DoublePreferencesPreferencePage() {
41 super(FLAT);
44 /**
45 * Creates a new {@link DoublePreferencesPreferencePage} with the given
46 * style and neither title nor image.
48 * @param style
49 * to use
51 protected DoublePreferencesPreferencePage(int style) {
52 super(style);
55 /**
56 * Creates a new {@link DoublePreferencesPreferencePage} with the given
57 * style and title but without image.
59 * @param title
60 * for the page
61 * @param style
62 * to use
64 protected DoublePreferencesPreferencePage(String title, int style) {
65 super(title, style);
68 /**
69 * Creates a new {@link DoublePreferencesPreferencePage} with the given
70 * style, title, and image.
72 * @param title
73 * for the page
74 * @param imageDescriptor
75 * for the image for the page
76 * @param style
77 * to use
79 protected DoublePreferencesPreferencePage(String title,
80 ImageDescriptor imageDescriptor, int style) {
81 super(title, imageDescriptor, style);
84 /**
85 * Returns the secondary preference store of this preference page.
86 * <p>
87 * This is a framework hook method for subclasses to return a page-specific
88 * preference store. The default implementation returns {@code null}.
89 * </p>
91 * @return the preference store, or {@code null} if none
93 protected IPreferenceStore doGetSecondaryPreferenceStore() {
94 return null;
97 /**
98 * Returns the secondary preference store of this preference page.
100 * @return the preference store, or {@code null} if none
102 public IPreferenceStore getSecondaryPreferenceStore() {
103 if (secondaryStore == null) {
104 secondaryStore = doGetSecondaryPreferenceStore();
106 return secondaryStore;
109 @Override
110 public boolean performOk() {
111 boolean isOk = super.performOk();
112 if (isOk) {
113 saveSecondaryPreferenceStore();
115 return isOk;
118 @Override
119 public void dispose() {
120 super.dispose();
121 secondaryStore = null;
124 private void saveSecondaryPreferenceStore() {
125 IPreferenceStore store = getSecondaryPreferenceStore();
126 if (store != null && store.needsSaving()
127 && (store instanceof IPersistentPreferenceStore)) {
128 try {
129 ((IPersistentPreferenceStore) store).save();
130 } catch (IOException e) {
131 String message = JFaceResources.format(
132 "PreferenceDialog.saveErrorMessage", //$NON-NLS-1$
133 new Object[] { getTitle(), e.getMessage() });
134 Policy.getStatusHandler().show(
135 new Status(IStatus.ERROR, Activator.getPluginId(),
136 message, e),
137 JFaceResources
138 .getString("PreferenceDialog.saveErrorTitle")); //$NON-NLS-1$