Have icon for "reset" entry in reflog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / gerrit / GerritDialogSettings.java
blob9b3dc5c3a1d84b2a068c6bd1efbf1d1cd5c5fe2c
1 /*******************************************************************************
2 * Copyright (C) 2016, 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.gerrit;
13 import java.util.List;
15 import org.eclipse.egit.core.internal.gerrit.GerritUtil;
16 import org.eclipse.egit.ui.Activator;
17 import org.eclipse.egit.ui.internal.fetch.FetchGerritChangePage;
18 import org.eclipse.egit.ui.internal.push.PushToGerritPage;
19 import org.eclipse.jface.dialogs.IDialogSettings;
20 import org.eclipse.jgit.annotations.NonNull;
21 import org.eclipse.jgit.lib.Repository;
22 import org.eclipse.jgit.transport.RemoteConfig;
23 import org.eclipse.jgit.transport.URIish;
25 /**
26 * Helper class for managing the dialog setting of the
27 * {@link FetchGerritChangePage} and {@link PushToGerritPage}.
29 public final class GerritDialogSettings {
31 /**
32 * Name of the {@link IDialogSettings} section for the
33 * {@link FetchGerritChangePage}.
35 public static final String FETCH_FROM_GERRIT_SECTION = FetchGerritChangePage.class
36 .getSimpleName();
38 /**
39 * Name of the {@link IDialogSettings} section for the
40 * {@link PushToGerritPage}.
42 public static final String PUSH_TO_GERRIT_SECTION = PushToGerritPage.class
43 .getSimpleName();
45 /**
46 * Repository suffix for storing the last used URI in a section.
48 public static final String LAST_URI_SUFFIX = ".lastUri"; //$NON-NLS-1$
50 private GerritDialogSettings() {
51 // Utility class shall not be instantiated.
54 /**
55 * Updates dialog settings as appropriate. Called within the UI thread.
57 * @param repository
58 * the {@code config} belongs to
59 * @param config
60 * that was updated
62 public static void updateRemoteConfig(Repository repository,
63 RemoteConfig config) {
64 if (repository == null || config == null) {
65 return;
67 if (GerritUtil.isGerritFetch(config)) {
68 updateGerritFetch(repository, config);
70 if (GerritUtil.isGerritPush(config)) {
71 updateGerritPush(repository, config);
75 /**
76 * Gets the specified section from the activator's {@link IDialogSettings}.
77 * Creates the section if it doesn't exist.
79 * @param id
80 * of the section to get
81 * @return the section
83 public static @NonNull IDialogSettings getSection(String id) {
84 IDialogSettings settings = Activator.getDefault().getDialogSettings();
85 IDialogSettings section = settings.getSection(id);
86 if (section == null) {
87 section = settings.addNewSection(id);
88 if (section == null) {
89 throw new NullPointerException(
90 "IDialogSettings section could not be created"); //$NON-NLS-1$
93 return section;
96 private static void updateGerritFetch(@NonNull Repository repository,
97 @NonNull RemoteConfig config) {
98 IDialogSettings section = getSection(FETCH_FROM_GERRIT_SECTION);
99 String configured = section.get(repository + LAST_URI_SUFFIX);
100 if (configured == null || configured.isEmpty()) {
101 List<URIish> fetchUris = config.getURIs();
102 if (!fetchUris.isEmpty()) {
103 section.put(repository + LAST_URI_SUFFIX,
104 fetchUris.get(0).toPrivateString());
109 private static void updateGerritPush(@NonNull Repository repository,
110 @NonNull RemoteConfig config) {
111 IDialogSettings section = getSection(PUSH_TO_GERRIT_SECTION);
112 String configured = section.get(repository + LAST_URI_SUFFIX);
113 if (configured == null || configured.isEmpty()) {
114 List<URIish> pushUris = config.getPushURIs();
115 if (!pushUris.isEmpty()) {
116 section.put(repository + LAST_URI_SUFFIX,
117 pushUris.get(0).toPrivateString());