part of EDU-257 updated all visible information about shortcuts but not immediately
authorliana.bakradze <liana.bakradze@jetbrains.com>
Tue, 28 Oct 2014 21:31:46 +0000 (00:31 +0300)
committerliana.bakradze <liana.bakradze@jetbrains.com>
Tue, 28 Oct 2014 21:31:46 +0000 (00:31 +0300)
python/edu/learn-python/src/com/jetbrains/python/edu/StudyInstructionPainter.java
python/edu/learn-python/src/com/jetbrains/python/edu/StudyTaskManager.java
python/edu/learn-python/src/com/jetbrains/python/edu/editor/StudyEditor.java

index 4fa323172bcccfa3efe71a95f6a9d8408df6d0d9..87f1f7a3545453313694b7f30b489aa6c715f1ab 100644 (file)
@@ -1,14 +1,20 @@
 package com.jetbrains.python.edu;
 
+import com.intellij.openapi.actionSystem.ActionManager;
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.actionSystem.KeyboardShortcut;
 import com.intellij.openapi.fileEditor.impl.EditorEmptyTextPainter;
 import com.intellij.openapi.fileEditor.impl.EditorsSplitters;
+import com.intellij.openapi.keymap.KeymapUtil;
 import com.intellij.openapi.util.Couple;
 import com.intellij.ui.Gray;
 import com.intellij.ui.JBColor;
 import com.intellij.util.PairFunction;
 import com.intellij.util.ui.GraphicsUtil;
 import com.intellij.util.ui.UIUtil;
+import com.jetbrains.python.edu.actions.*;
 
+import javax.swing.*;
 import java.awt.*;
 
 /**
@@ -27,12 +33,18 @@ public class StudyInstructionPainter extends EditorEmptyTextPainter {
     UIUtil.TextPainter painter = new UIUtil.TextPainter().withLineSpacing(1.5f);
 
     painter.appendLine("PyCharm Educational Edition").underlined(new JBColor(Gray._150, Gray._180));
-    painter.appendLine("Navigate to the next task window with Ctrl + Enter").smaller().withBullet();
-    painter.appendLine("Navigate between task windows with Ctrl + Shift + < and Ctrl + Shift + >").smaller().withBullet();
-    painter.appendLine("Navigate between tasks with Ctrl + < and Ctrl + >").smaller().withBullet();
-    painter.appendLine("Reset current task file with Ctrl + Shift + X").smaller().withBullet();
-    painter.appendLine("Check task with Alt + Ctrl + Enter").smaller().withBullet();
-    painter.appendLine("Get hint for the task window using Ctrl + 7").smaller().withBullet();
+    addAction(painter, "Navigate to the next task window", StudyNextWindowAction.ACTION_ID, StudyNextWindowAction.SHORTCUT2, true);
+    String shortcut1 = getShortcutText(StudyPrevWindowAction.ACTION_ID, StudyPrevWindowAction.SHORTCUT, false, false);
+    String shortcut2 = getShortcutText(StudyNextWindowAction.ACTION_ID, StudyNextWindowAction.SHORTCUT, false, false);
+    String text = "Navigate between task windows with " + shortcut1 +
+                  " and " + shortcut2;
+    painter.appendLine(text).smaller().withBullet();
+    shortcut1 = getShortcutText(StudyPreviousStudyTaskAction.ACTION_ID, StudyPreviousStudyTaskAction.SHORTCUT, false, false);
+    shortcut2 = getShortcutText(StudyNextStudyTaskAction.ACTION_ID, StudyNextStudyTaskAction.SHORTCUT, false, false);
+    painter.appendLine("Navigate between tasks with " + shortcut1 + " and " + shortcut2).smaller().withBullet();
+    addAction(painter, "Reset current task file", StudyRefreshTaskFileAction.ACTION_ID, StudyRefreshTaskFileAction.SHORTCUT, false);
+    addAction(painter, "Check task", StudyCheckAction.ACTION_ID, StudyCheckAction.SHORTCUT, false);
+    addAction(painter, "Get hint for the task window", StudyShowHintAction.ACTION_ID, StudyShowHintAction.SHORTCUT, false);
     painter.appendLine("To see your progress open the 'Course Description' panel").smaller().withBullet();
                        painter.draw(g, new PairFunction<Integer, Integer, Couple<Integer>>() {
                          @Override
@@ -42,4 +54,25 @@ public class StudyInstructionPainter extends EditorEmptyTextPainter {
                          }
                        });
   }
+  private static void addAction(UIUtil.TextPainter painter, String text, String actionId, String defaultShortcutString, boolean useDefault) {
+    String shortcut = getShortcutText(actionId, defaultShortcutString, useDefault, true);
+    String actionText = text + " with " + shortcut;
+    painter.appendLine(actionText).smaller().withBullet();
+  }
+
+  private static String getShortcutText(String actionId, String defaultShortcutString, boolean useDefault, boolean wrapTag) {
+    AnAction action = ActionManager.getInstance().getAction(actionId);
+    String shortcut = "";
+    if (!useDefault) {
+      shortcut = KeymapUtil.getFirstKeyboardShortcutText(action);
+    }
+    if (shortcut.isEmpty() && defaultShortcutString != null) {
+      KeyboardShortcut keyboardShortcut = new KeyboardShortcut(KeyStroke.getKeyStroke(defaultShortcutString), null);
+      shortcut = KeymapUtil.getShortcutText(keyboardShortcut);
+    }
+    if (!wrapTag) {
+      return shortcut;
+    }
+    return "<shortcut>" + shortcut + "</shortcut>";
+  }
 }
index ec7caf38eac4b75371bebe037bc7b7b49323694a..f37adf0b19f6b438e93bc354b9148871419454d0 100644 (file)
@@ -235,6 +235,10 @@ public class StudyTaskManager implements ProjectComponent, PersistentStateCompon
 
   private static void addShortcut(@NotNull final String shortcutString, @NotNull final String actionIdString) {
     Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
+    Shortcut[] shortcuts = keymap.getShortcuts(actionIdString);
+    if (shortcuts.length > 0) {
+      return;
+    }
     Shortcut studyActionShortcut = new KeyboardShortcut(KeyStroke.getKeyStroke(shortcutString), null);
     String[] actionsIds = keymap.getActionIds(studyActionShortcut);
     for (String actionId : actionsIds) {
index 1deeb4e0de3936522b48d40310c2e68c20e56cd1..b497b5216c2690ac3923c25995153313f42957d0 100644 (file)
@@ -4,6 +4,8 @@ import com.intellij.codeHighlighting.BackgroundEditorHighlighter;
 import com.intellij.icons.AllIcons;
 import com.intellij.ide.structureView.StructureViewBuilder;
 import com.intellij.openapi.actionSystem.ActionManager;
+import com.intellij.openapi.actionSystem.AnAction;
+import com.intellij.openapi.actionSystem.KeyboardShortcut;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.editor.Document;
 import com.intellij.openapi.editor.Editor;
@@ -16,6 +18,7 @@ import com.intellij.openapi.fileEditor.*;
 import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
 import com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl;
 import com.intellij.openapi.fileEditor.impl.text.TextEditorProvider;
+import com.intellij.openapi.keymap.KeymapUtil;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.Key;
@@ -48,8 +51,9 @@ import java.awt.*;
 import java.awt.datatransfer.StringSelection;
 import java.awt.event.*;
 import java.beans.PropertyChangeListener;
-import java.util.*;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * Implementation of StudyEditor which has panel with special buttons and task text
@@ -79,7 +83,14 @@ public class StudyEditor implements TextEditor {
     return myTaskFile;
   }
 
-  private static JButton addButton(@NotNull final JComponent parentComponent, String toolTipText, Icon icon) {
+  private static JButton addButton(@NotNull final JComponent parentComponent, String actionID, Icon icon,
+                                   @Nullable String defaultShortcutString) {
+    AnAction action = ActionManager.getInstance().getAction(actionID);
+    String toolTipText = KeymapUtil.createTooltipText(action.getTemplatePresentation().getText(), action);
+    if (!toolTipText.contains("(") && defaultShortcutString != null) {
+      KeyboardShortcut shortcut = new KeyboardShortcut(KeyStroke.getKeyStroke(defaultShortcutString), null);
+      toolTipText += " (" + KeymapUtil.getShortcutText(shortcut) + ")";
+    }
     JButton newButton = new JButton();
     newButton.setToolTipText(toolTipText);
     newButton.setIcon(icon);
@@ -192,13 +203,13 @@ public class StudyEditor implements TextEditor {
   }
 
   private void initializeButtons(@NotNull final JPanel taskActionsPanel, @NotNull final TaskFile taskFile) {
-    myCheckButton = addButton(taskActionsPanel, "Check Task (Ctrl + Alt + Enter)", StudyIcons.Resolve);
-    myPrevTaskButton = addButton(taskActionsPanel, "Previous Task (Ctrl + &lt;)", StudyIcons.Prev);
-    myNextTaskButton = addButton(taskActionsPanel, "Next Task (Ctrl + >)", AllIcons.Actions.Forward);
-    myRefreshButton = addButton(taskActionsPanel, "Reset Task File (Ctrl + Shift + X)", AllIcons.Actions.Refresh);
-    JButton myShowHintButton = addButton(taskActionsPanel, "Show Hint for Task Window (Ctrl + 7)", StudyIcons.ShowHint);
+    myCheckButton = addButton(taskActionsPanel, StudyCheckAction.ACTION_ID, StudyIcons.Resolve, StudyCheckAction.SHORTCUT);
+    myPrevTaskButton = addButton(taskActionsPanel, StudyPreviousStudyTaskAction.ACTION_ID, StudyIcons.Prev, StudyPreviousStudyTaskAction.SHORTCUT);
+    myNextTaskButton = addButton(taskActionsPanel, StudyNextStudyTaskAction.ACTION_ID, AllIcons.Actions.Forward, StudyNextStudyTaskAction.SHORTCUT);
+    myRefreshButton = addButton(taskActionsPanel, StudyRefreshTaskFileAction.ACTION_ID, AllIcons.Actions.Refresh, StudyRefreshTaskFileAction.SHORTCUT);
+    JButton myShowHintButton = addButton(taskActionsPanel, StudyShowHintAction.ACTION_ID, StudyIcons.ShowHint, StudyShowHintAction.SHORTCUT);
     if (!taskFile.getTask().getUserTests().isEmpty()) {
-      JButton runButton = addButton(taskActionsPanel, "Run", AllIcons.General.Run);
+      JButton runButton = addButton(taskActionsPanel, StudyRunAction.ACTION_ID, AllIcons.General.Run, null);
       runButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
@@ -206,7 +217,7 @@ public class StudyEditor implements TextEditor {
           studyRunAction.run(myProject);
         }
       });
-      JButton watchInputButton = addButton(taskActionsPanel, "Watch Test Input", StudyIcons.WatchInput);
+      JButton watchInputButton = addButton(taskActionsPanel, "WatchInputAction", StudyIcons.WatchInput, null);
       watchInputButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {