9dd1757670c6db1e0f39d3bb070e28273dd46d89
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / ui / CCCreateAnswerPlaceholderPanel.java
1 package com.jetbrains.edu.learning.ui;
2
3 import com.intellij.icons.AllIcons;
4 import com.intellij.openapi.actionSystem.ActionManager;
5 import com.intellij.openapi.actionSystem.AnAction;
6 import com.intellij.openapi.actionSystem.AnActionEvent;
7 import com.intellij.openapi.actionSystem.DefaultActionGroup;
8 import com.intellij.ui.ColorUtil;
9 import com.intellij.ui.JBColor;
10 import com.intellij.ui.components.JBLabel;
11 import com.intellij.uiDesigner.core.GridLayoutManager;
12 import com.intellij.util.ui.UIUtil;
13 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
14 import org.jetbrains.annotations.NotNull;
15
16 import javax.swing.*;
17 import java.awt.*;
18 import java.awt.event.FocusAdapter;
19 import java.awt.event.FocusEvent;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 public class CCCreateAnswerPlaceholderPanel {
24   private static String ourFirstHintText = "Type here to add hint";
25
26   private JPanel myPanel;
27   private JTextArea myHintTextArea;
28   private JPanel myHintsPanel;
29   private JBLabel myHintLabel;
30   private JPanel actionsPanel;
31   private JTextArea myPlaceholderTextArea;
32   private List<String> myHints = new ArrayList<String>() {
33   };
34   private int myShownHintNumber = 0;
35
36   public CCCreateAnswerPlaceholderPanel(@NotNull final AnswerPlaceholder answerPlaceholder) {
37     if (answerPlaceholder.getHints().isEmpty()) {
38       myHints.add(ourFirstHintText);
39     }
40     else {
41       myHints.addAll(answerPlaceholder.getHints());
42     }
43
44     myPlaceholderTextArea.setBorder(BorderFactory.createLineBorder(JBColor.border()));
45     myHintsPanel.setBorder(BorderFactory.createLineBorder(JBColor.border()));
46     ((GridLayoutManager)myHintsPanel.getLayout()).setHGap(1);
47
48     myHintTextArea.setFont(myPlaceholderTextArea.getFont());
49     myHintTextArea.addFocusListener(createFocusListenerToSetDefaultHintText());
50
51     actionsPanel.add(createHintToolbarComponent(), BorderLayout.WEST);
52     showHint(myHints.get(myShownHintNumber));
53   }
54
55   @NotNull
56   private FocusAdapter createFocusListenerToSetDefaultHintText() {
57     return new FocusAdapter() {
58       @Override
59       public void focusGained(FocusEvent e) {
60         if (myHintTextArea.getText().equals(ourFirstHintText)) {
61           myHintTextArea.setForeground(UIUtil.getActiveTextColor());
62           myHintTextArea.setText("");
63         }
64       }
65
66       @Override
67       public void focusLost(FocusEvent e) {
68         if (myShownHintNumber == 0 && myHintTextArea.getText().isEmpty()) {
69           myHintTextArea.setForeground(UIUtil.getInactiveTextColor());
70           myHintTextArea.setText(ourFirstHintText);
71         }
72       }
73     };
74   }
75
76   private JComponent createHintToolbarComponent() {
77     final DefaultActionGroup addRemoveGroup = new DefaultActionGroup();
78     addRemoveGroup.addAll(new AddHint(), new RemoveHint(), new ShowNext(), new ShowPrevious());
79     return ActionManager.getInstance().createActionToolbar("Hint", addRemoveGroup, false).getComponent();
80   }
81
82   private void updateHintNumberLabel() {
83     if (myHints.size() > 1) {
84       final String color = String.valueOf(ColorUtil.toHex(UIUtil.getHeaderInactiveColor()));
85       myHintLabel.setText(UIUtil.toHtml("Hint" + " <font color=\"" + color + "\">(" + (myShownHintNumber + 1) + "/" + myHints.size() + ")</font>:"));
86     }
87     else {
88       myHintLabel.setText("Hint: ");
89     }
90   }
91
92   public void showAnswerPlaceholderText(String answerPlaceholderText) {
93     myPlaceholderTextArea.setText(answerPlaceholderText);
94   }
95
96   public void showHint(String hintText) {
97     if (myHints.get(myShownHintNumber).equals(ourFirstHintText)) {
98       myHintTextArea.setForeground(UIUtil.getInactiveTextColor());
99     }
100     else {
101       myHintTextArea.setForeground(UIUtil.getActiveTextColor());
102     }
103
104     myHintTextArea.setText(hintText);
105     updateHintNumberLabel();
106   }
107
108   public String getAnswerPlaceholderText() {
109     return myPlaceholderTextArea.getText();
110   }
111
112   public List<String> getHints() {
113     final String hintText = myHintTextArea.getText();
114     if (myShownHintNumber == 0 && hintText.equals(ourFirstHintText)) {
115       myHints.set(myShownHintNumber, "");
116     }
117     else {
118       myHints.set(myShownHintNumber, hintText);
119     }
120
121     return myHints;
122   }
123
124   public JComponent getPreferredFocusedComponent() {
125     return myPlaceholderTextArea;
126   }
127
128   public JPanel getMailPanel() {
129     return myPanel;
130   }
131
132   private class ShowNext extends AnAction {
133
134     public ShowNext() {
135       super("Next Hint", "Next Hint", AllIcons.Actions.Forward);
136     }
137
138     @Override
139     public void actionPerformed(AnActionEvent e) {
140       myHints.set(myShownHintNumber, myHintTextArea.getText());
141       showHint(myHints.get(++myShownHintNumber));
142     }
143
144     @Override
145     public void update(AnActionEvent e) {
146       e.getPresentation().setEnabled(myShownHintNumber + 1 < myHints.size());
147     }
148   }
149
150   private class ShowPrevious extends AnAction {
151
152     public ShowPrevious() {
153       super("Previous Hint", "Previous Hint", AllIcons.Actions.Back);
154     }
155
156     @Override
157     public void actionPerformed(AnActionEvent e) {
158       myHints.set(myShownHintNumber, myHintTextArea.getText());
159       showHint(myHints.get(--myShownHintNumber));
160     }
161
162     @Override
163     public void update(AnActionEvent e) {
164       e.getPresentation().setEnabled(myShownHintNumber - 1 >= 0);
165     }
166   }
167
168   private class AddHint extends AnAction {
169
170     public AddHint() {
171       super("Add Hint", "Add Hint", AllIcons.General.Add);
172     }
173
174     @Override
175     public void actionPerformed(AnActionEvent e) {
176       myHints.add("");
177       myShownHintNumber++;
178       showHint("");
179     }
180   }
181
182   private class RemoveHint extends AnAction {
183
184     public RemoveHint() {
185       super("Remove Hint", "Remove Hint", AllIcons.General.Remove);
186     }
187
188     @Override
189     public void actionPerformed(AnActionEvent e) {
190       myHints.remove(myShownHintNumber);
191       myShownHintNumber += myShownHintNumber < myHints.size() ? 0 : -1;
192       showHint(myHints.get(myShownHintNumber));
193     }
194
195     @Override
196     public void update(AnActionEvent e) {
197       e.getPresentation().setEnabled(myHints.size() > 1);
198     }
199   }
200 }