/platform/vcs-impl/src/com/intellij/openapi/vcs/ui/CommitMessage.java

https://bitbucket.org/nbargnesi/idea · Java · 179 lines · 137 code · 24 blank · 18 comment · 14 complexity · 54a39c2239b3cbbd39ab16d4bfa1968d MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.openapi.vcs.ui;
  17. import com.intellij.ide.DataManager;
  18. import com.intellij.openapi.Disposable;
  19. import com.intellij.openapi.actionSystem.*;
  20. import com.intellij.openapi.components.ServiceManager;
  21. import com.intellij.openapi.editor.Editor;
  22. import com.intellij.openapi.editor.ex.EditorEx;
  23. import com.intellij.openapi.extensions.Extensions;
  24. import com.intellij.openapi.fileTypes.FileTypes;
  25. import com.intellij.openapi.project.Project;
  26. import com.intellij.openapi.util.Key;
  27. import com.intellij.openapi.vcs.*;
  28. import com.intellij.ui.*;
  29. import com.intellij.util.Consumer;
  30. import org.jetbrains.annotations.Nullable;
  31. import javax.swing.*;
  32. import java.awt.*;
  33. import java.util.EnumSet;
  34. import java.util.Set;
  35. public class CommitMessage extends AbstractDataProviderPanel implements Disposable, CommitMessageI {
  36. public static final Key<DataContext> DATA_CONTEXT_KEY = Key.create("commit message data context");
  37. private final EditorTextField myEditorField;
  38. private final Project myProject;
  39. private Consumer<String> myMessageConsumer;
  40. private final TitledSeparator mySeparator;
  41. private boolean myCheckSpelling;
  42. public CommitMessage(Project project) {
  43. super(new BorderLayout());
  44. boolean checkSpelling = true;
  45. VcsConfiguration configuration = VcsConfiguration.getInstance(project);
  46. if (configuration != null) {
  47. checkSpelling = configuration.CHECK_COMMIT_MESSAGE_SPELLING;
  48. }
  49. myEditorField = createEditorField(project, checkSpelling);
  50. myProject = project;
  51. // Note that we assume here that editor used for commit message processing uses font family implied by LAF (in contrast,
  52. // IJ code editor uses monospaced font). Hence, we don't need any special actions here
  53. // (myEditorField.setFontInheritedFromLAF(true) should be used instead).
  54. add(myEditorField, BorderLayout.CENTER);
  55. JPanel labelPanel = new JPanel(new BorderLayout());
  56. labelPanel.setBorder(BorderFactory.createEmptyBorder());
  57. mySeparator = SeparatorFactory.createSeparator(VcsBundle.message("label.commit.comment"), myEditorField.getComponent(), true, true);
  58. JPanel separatorPanel = new JPanel(new BorderLayout());
  59. separatorPanel.add(mySeparator, BorderLayout.SOUTH);
  60. separatorPanel.add(Box.createVerticalGlue(), BorderLayout.NORTH);
  61. labelPanel.add(separatorPanel, BorderLayout.CENTER);
  62. ActionToolbar toolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, getToolbarActions(), true);
  63. toolbar.updateActionsImmediately();
  64. toolbar.setReservePlaceAutoPopupIcon(false);
  65. toolbar.getComponent().setBorder(BorderFactory.createEmptyBorder());
  66. labelPanel.add(toolbar.getComponent(), BorderLayout.EAST);
  67. add(labelPanel, BorderLayout.NORTH);
  68. setBorder(BorderFactory.createEmptyBorder());
  69. }
  70. @Override
  71. public void calcData(DataKey key, DataSink sink) {
  72. if (key.is(VcsDataKeys.COMMIT_MESSAGE_CONTROL.getName())) {
  73. sink.put(VcsDataKeys.COMMIT_MESSAGE_CONTROL, this);
  74. }
  75. }
  76. public void setSeparatorText(final String text) {
  77. mySeparator.setText(text);
  78. }
  79. @Override
  80. public void setCommitMessage(String currentDescription) {
  81. setText(currentDescription);
  82. }
  83. private static EditorTextField createEditorField(final Project project, final boolean checkSpelling) {
  84. EditorTextField editorField = createCommitTextEditor(project, checkSpelling);
  85. editorField.getDocument().putUserData(DATA_CONTEXT_KEY, DataManager.getInstance().getDataContext(editorField.getComponent()));
  86. return editorField;
  87. }
  88. public static EditorTextField createCommitTextEditor(Project project, boolean checkSpelling) {
  89. EditorTextFieldProvider service = ServiceManager.getService(project, EditorTextFieldProvider.class);
  90. Set<EditorCustomization.Feature> enabledFeatures = EnumSet.of(EditorCustomization.Feature.SOFT_WRAP);
  91. Set<EditorCustomization.Feature> disabledFeatures = EnumSet.of(EditorCustomization.Feature.ADDITIONAL_PAGE_AT_BOTTOM);
  92. if (checkSpelling) {
  93. enabledFeatures.add(EditorCustomization.Feature.SPELL_CHECK);
  94. }
  95. else {
  96. disabledFeatures.add(EditorCustomization.Feature.SPELL_CHECK);
  97. }
  98. return service.getEditorField(FileTypes.PLAIN_TEXT.getLanguage(), project, enabledFeatures, disabledFeatures);
  99. }
  100. @Nullable
  101. public static ActionGroup getToolbarActions() {
  102. return (ActionGroup)ActionManager.getInstance().getAction("Vcs.MessageActionGroup");
  103. }
  104. public EditorTextField getEditorField() {
  105. return myEditorField;
  106. }
  107. public void setText(final String initialMessage) {
  108. final String text = initialMessage == null ? "" : initialMessage;
  109. myEditorField.setText(text);
  110. if (myMessageConsumer != null) {
  111. myMessageConsumer.consume(text);
  112. }
  113. }
  114. public String getComment() {
  115. final String s = myEditorField.getDocument().getCharsSequence().toString();
  116. int end = s.length();
  117. while(end > 0 && Character.isSpaceChar(s.charAt(end-1))) {
  118. end--;
  119. }
  120. return s.substring(0, end);
  121. }
  122. public void requestFocusInMessage() {
  123. myEditorField.requestFocus();
  124. myEditorField.selectAll();
  125. }
  126. @Override
  127. public boolean isCheckSpelling() {
  128. return myCheckSpelling;
  129. }
  130. public void setCheckSpelling(boolean check) {
  131. myCheckSpelling = check;
  132. Editor editor = myEditorField.getEditor();
  133. if (!(editor instanceof EditorEx)) {
  134. return;
  135. }
  136. EditorEx editorEx = (EditorEx)editor;
  137. EditorCustomization[] customizations = Extensions.getExtensions(EditorCustomization.EP_NAME, myProject);
  138. EditorCustomization.Feature feature = EditorCustomization.Feature.SPELL_CHECK;
  139. for (EditorCustomization customization : customizations) {
  140. if (customization.getSupportedFeatures().contains(feature)) {
  141. if (check) {
  142. customization.addCustomization(editorEx, feature);
  143. }
  144. else {
  145. customization.removeCustomization(editorEx, feature);
  146. }
  147. }
  148. }
  149. }
  150. public void dispose() {
  151. }
  152. public void setMessageConsumer(Consumer<String> messageConsumer) {
  153. myMessageConsumer = messageConsumer;
  154. }
  155. }