1 /*******************************************************************************
2 * Copyright (C) 2011, Jens Baumgart <jens.baumgart@sap.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org
.eclipse
.egit
.ui
.internal
.dialogs
;
11 import org
.eclipse
.egit
.ui
.Activator
;
12 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
13 import org
.eclipse
.jgit
.lib
.ObjectId
;
14 import org
.eclipse
.jgit
.lib
.Repository
;
17 * This class is used to load / save the state of a
18 * {@link CommitMessageComponentState} in the dialog settings.
21 public class CommitMessageComponentStateManager
{
23 private static final String COMMIT_MESSAGE_COMPONENT_SECTION
= "GitCommitMessageComponent"; //$NON-NLS-1$
25 private static final String EMPTY
= "empty"; //$NON-NLS-1$
27 private static final int MEMBER_COUNT
= 6; // number of members in
28 // CommitMessageComponentState
30 // number of members in CommitMessageComponentState, before caret
31 // positioning was introduced
32 private static final int MEMBER_COUNT_WITHOUT_CARET_POSITION
= 5;
38 public static void persistState(Repository repository
,
39 CommitMessageComponentState state
) {
40 IDialogSettings dialogSettings
= getDialogSettings();
41 String
[] values
= new String
[] { Boolean
.toString(state
.getAmend()),
42 state
.getAuthor(), state
.getCommitMessage(),
44 state
.getHeadCommit().getName().toString(),
45 String
.valueOf(state
.getCaretPosition()) };
46 dialogSettings
.put(repository
.getDirectory().getAbsolutePath(), values
);
53 public static CommitMessageComponentState
loadState(Repository repository
) {
54 IDialogSettings dialogSettings
= getDialogSettings();
55 String
[] values
= dialogSettings
.getArray(repository
.getDirectory()
58 || values
.length
< MEMBER_COUNT_WITHOUT_CARET_POSITION
) {
62 CommitMessageComponentState state
= new CommitMessageComponentState();
63 state
.setAmend(Boolean
.parseBoolean(values
[0]));
64 state
.setAuthor(values
[1]);
65 state
.setCommitMessage(values
[2]);
66 state
.setCommitter(values
[3]);
67 state
.setHeadCommit(ObjectId
.fromString(values
[4]));
68 if (values
.length
>= MEMBER_COUNT
) {
69 state
.setCaretPosition(Integer
.parseInt(values
[5]));
71 state
.setCaretPosition(
72 CommitMessageComponentState
.CARET_DEFAULT_POSITION
);
77 private static IDialogSettings
getDialogSettings() {
78 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
79 IDialogSettings section
= settings
80 .getSection(COMMIT_MESSAGE_COMPONENT_SECTION
);
82 section
= settings
.addNewSection(COMMIT_MESSAGE_COMPONENT_SECTION
);
89 public static void deleteState(Repository repository
) {
90 IDialogSettings dialogSettings
= getDialogSettings();
91 String key
= repository
.getDirectory().getAbsolutePath();
92 if (dialogSettings
!= null && dialogSettings
.getArray(key
) != null)
93 dialogSettings
.put(key
, new String
[] { EMPTY
});