Add ICommitMessageProvider2 for caret positioning in commit messages
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / dialogs / CommitMessageComponentStateManager.java
blob3c2b253c556c794c68b19ce3a7874a8639a44652
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;
16 /**
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;
34 /**
35 * @param repository
36 * @param state
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(),
43 state.getCommitter(),
44 state.getHeadCommit().getName().toString(),
45 String.valueOf(state.getCaretPosition()) };
46 dialogSettings.put(repository.getDirectory().getAbsolutePath(), values);
49 /**
50 * @param repository
51 * @return state
53 public static CommitMessageComponentState loadState(Repository repository) {
54 IDialogSettings dialogSettings = getDialogSettings();
55 String[] values = dialogSettings.getArray(repository.getDirectory()
56 .getAbsolutePath());
57 if (values == null
58 || values.length < MEMBER_COUNT_WITHOUT_CARET_POSITION) {
59 return null;
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]));
70 } else {
71 state.setCaretPosition(
72 CommitMessageComponentState.CARET_DEFAULT_POSITION);
74 return state;
77 private static IDialogSettings getDialogSettings() {
78 IDialogSettings settings = Activator.getDefault().getDialogSettings();
79 IDialogSettings section = settings
80 .getSection(COMMIT_MESSAGE_COMPONENT_SECTION);
81 if (section == null)
82 section = settings.addNewSection(COMMIT_MESSAGE_COMPONENT_SECTION);
83 return section;
86 /**
87 * @param repository
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 });