Update org.apache.commons:commons-compress to 1.25.0
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / preferences / DoublePreferencesPreferencePage.java
blobfcdf310d9f19158a40f6be5edf249ced827bc557
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 void setValid(boolean b) {
111 super.setValid(b);
112 // Super class forgets to clear the error message.
113 if (b) {
114 setErrorMessage(null);
118 @Override
119 public boolean performOk() {
120 boolean isOk = super.performOk();
121 if (isOk) {
122 saveSecondaryPreferenceStore();
124 return isOk;
127 @Override
128 public void dispose() {
129 super.dispose();
130 secondaryStore = null;
133 private void saveSecondaryPreferenceStore() {
134 IPreferenceStore store = getSecondaryPreferenceStore();
135 if (store != null && store.needsSaving()
136 && (store instanceof IPersistentPreferenceStore)) {
137 try {
138 ((IPersistentPreferenceStore) store).save();
139 } catch (IOException e) {
140 String message = JFaceResources.format(
141 "PreferenceDialog.saveErrorMessage", //$NON-NLS-1$
142 new Object[] { getTitle(), e.getMessage() });
143 Policy.getStatusHandler().show(
144 new Status(IStatus.ERROR, Activator.PLUGIN_ID, message,
146 JFaceResources
147 .getString("PreferenceDialog.saveErrorTitle")); //$NON-NLS-1$