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
;
26 * A {@link FieldEditorPreferencePage} that provides access to a secondary
27 * {@link IPreferenceStore} and that takes care of storing that secondary store,
30 public abstract class DoublePreferencesPreferencePage
31 extends FieldEditorPreferencePage
{
33 private IPreferenceStore secondaryStore
;
36 * Creates a new {@link DoublePreferencesPreferencePage} with
37 * {@link FieldEditorPreferencePage#FLAT FLAT} style and neither title nor
40 public DoublePreferencesPreferencePage() {
45 * Creates a new {@link DoublePreferencesPreferencePage} with the given
46 * style and neither title nor image.
51 protected DoublePreferencesPreferencePage(int style
) {
56 * Creates a new {@link DoublePreferencesPreferencePage} with the given
57 * style and title but without image.
64 protected DoublePreferencesPreferencePage(String title
, int style
) {
69 * Creates a new {@link DoublePreferencesPreferencePage} with the given
70 * style, title, and image.
74 * @param imageDescriptor
75 * for the image for the page
79 protected DoublePreferencesPreferencePage(String title
,
80 ImageDescriptor imageDescriptor
, int style
) {
81 super(title
, imageDescriptor
, style
);
85 * Returns the secondary preference store of this preference page.
87 * This is a framework hook method for subclasses to return a page-specific
88 * preference store. The default implementation returns {@code null}.
91 * @return the preference store, or {@code null} if none
93 protected IPreferenceStore
doGetSecondaryPreferenceStore() {
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
;
110 public boolean performOk() {
111 boolean isOk
= super.performOk();
113 saveSecondaryPreferenceStore();
119 public void dispose() {
121 secondaryStore
= null;
124 private void saveSecondaryPreferenceStore() {
125 IPreferenceStore store
= getSecondaryPreferenceStore();
126 if (store
!= null && store
.needsSaving()
127 && (store
instanceof IPersistentPreferenceStore
)) {
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(),
138 .getString("PreferenceDialog.saveErrorTitle")); //$NON-NLS-1$