IDEA-108785 Allow applying the same context to many selected live templates

This commit is contained in:
peter
2013-09-24 21:35:40 +02:00
parent 247661b6bc
commit 0c0a6a67cc
2 changed files with 49 additions and 17 deletions
@@ -30,6 +30,7 @@ import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupAdapter;
import com.intellij.openapi.ui.popup.JBPopupFactory;
@@ -232,7 +233,7 @@ public class LiveTemplateSettingsEditor extends JPanel {
gbConstraints.gridx = 1;
gbConstraints.insets = new Insets(0, 4, 0, 0);
myExpandByCombo = new JComboBox(new Object[]{myDefaultShortcutItem, SPACE, TAB, ENTER});
myExpandByCombo = new ComboBox(new Object[]{myDefaultShortcutItem, SPACE, TAB, ENTER}, -1);
myExpandByCombo.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
@@ -309,6 +310,9 @@ public class LiveTemplateSettingsEditor extends JPanel {
final Runnable updateLabel = new Runnable() {
@Override
public void run() {
myExpandByCombo.setEnabled(isExpandableFromEditor());
updateHighlighter();
StringBuilder sb = new StringBuilder();
String oldPrefix = "";
for (TemplateContextType type : getApplicableContexts()) {
@@ -343,7 +347,7 @@ public class LiveTemplateSettingsEditor extends JPanel {
public boolean onClick(MouseEvent e, int clickCount) {
if (disposeContextPopup()) return false;
final JPanel content = createPopupContextPanel(updateLabel);
final JPanel content = createPopupContextPanel(updateLabel, myContext);
Dimension prefSize = content.getPreferredSize();
if (myLastSize != null && (myLastSize.width > prefSize.width || myLastSize.height > prefSize.height)) {
content.setPreferredSize(new Dimension(Math.max(prefSize.width, myLastSize.width), Math.max(prefSize.height, myLastSize.height)));
@@ -374,7 +378,7 @@ public class LiveTemplateSettingsEditor extends JPanel {
return false;
}
private JPanel createPopupContextPanel(final Runnable onChange) {
static JPanel createPopupContextPanel(final Runnable onChange, final Map<TemplateContextType, Boolean> context) {
JPanel panel = new JPanel(new BorderLayout());
MultiMap<TemplateContextType, TemplateContextType> hierarchy = new MultiMap<TemplateContextType, TemplateContextType>() {
@@ -383,7 +387,7 @@ public class LiveTemplateSettingsEditor extends JPanel {
return new LinkedHashMap<TemplateContextType, Collection<TemplateContextType>>();
}
};
for (TemplateContextType type : myContext.keySet()) {
for (TemplateContextType type : context.keySet()) {
hierarchy.putValue(type.getBaseContextType(), type);
}
@@ -401,17 +405,15 @@ public class LiveTemplateSettingsEditor extends JPanel {
protected void onNodeStateChanged(CheckedTreeNode node) {
final TemplateContextType type = (TemplateContextType)((Pair)node.getUserObject()).first;
if (type != null) {
myContext.put(type, node.isChecked());
context.put(type, node.isChecked());
}
myExpandByCombo.setEnabled(isExpandableFromEditor());
updateHighlighter();
onChange.run();
}
};
for (TemplateContextType type : hierarchy.get(null)) {
addContextNode(hierarchy, root, type);
addContextNode(hierarchy, root, type, context);
}
((DefaultTreeModel)checkboxTree.getModel()).nodeStructureChanged(root);
@@ -434,23 +436,23 @@ public class LiveTemplateSettingsEditor extends JPanel {
return panel;
}
private void addContextNode(MultiMap<TemplateContextType, TemplateContextType> hierarchy,
CheckedTreeNode parent,
TemplateContextType type) {
private static void addContextNode(MultiMap<TemplateContextType, TemplateContextType> hierarchy,
CheckedTreeNode parent,
TemplateContextType type, Map<TemplateContextType, Boolean> context) {
final Collection<TemplateContextType> children = hierarchy.get(type);
final String name = UIUtil.removeMnemonic(type.getPresentableName());
final CheckedTreeNode node = new CheckedTreeNode(Pair.create(children.isEmpty() ? type : null, name));
parent.add(node);
if (children.isEmpty()) {
node.setChecked(myContext.get(type));
node.setChecked(context.get(type));
}
else {
for (TemplateContextType child : children) {
addContextNode(hierarchy, node, child);
addContextNode(hierarchy, node, child, context);
}
final CheckedTreeNode other = new CheckedTreeNode(Pair.create(type, "Other"));
other.setChecked(myContext.get(type));
other.setChecked(context.get(type));
node.add(other);
}
}
@@ -30,12 +30,11 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SchemesManager;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.Splitter;
import com.intellij.openapi.ui.*;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.util.EmptyRunnable;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.ui.*;
import com.intellij.util.Alarm;
@@ -848,12 +847,43 @@ public class TemplateListPanel extends JPanel implements Disposable {
}
};
final DumbAwareAction changeContext = new DumbAwareAction("Change context...") {
@Override
public void update(AnActionEvent e) {
boolean enabled = !getSelectedTemplates().isEmpty();
e.getPresentation().setEnabled(enabled);
super.update(e);
}
@Override
public void actionPerformed(AnActionEvent e) {
Map<TemplateImpl, DefaultMutableTreeNode> templates = getSelectedTemplates();
Map<TemplateContextType, Boolean> context = ContainerUtil.newHashMap();
for (TemplateContextType type : TemplateManagerImpl.getAllContextTypes()) {
context.put(type, Boolean.FALSE);
}
JPanel contextPanel = LiveTemplateSettingsEditor.createPopupContextPanel(EmptyRunnable.INSTANCE, context);
DialogBuilder builder = new DialogBuilder(TemplateListPanel.this);
builder.setCenterPanel(contextPanel);
builder.setTitle("Change Context Type For Selected Templates");
int result = builder.show();
if (result == DialogWrapper.OK_EXIT_CODE) {
for (TemplateImpl template : templates.keySet()) {
getTemplateContext(template).putAll(context);
}
}
}
};
myTree.addMouseListener(new PopupHandler() {
@Override
public void invokePopup(Component comp, int x, int y) {
final DefaultActionGroup group = new DefaultActionGroup();
group.add(rename);
group.add(move);
group.add(changeContext);
ActionManager.getInstance().createActionPopupMenu(ActionPlaces.UNKNOWN, group).getComponent().show(comp, x, y);
}
});