propagate place to NewElement popup

This commit is contained in:
Gregory.Shrago
2018-04-06 21:09:43 +03:00
parent 4c97e3c035
commit 3922b23629
11 changed files with 111 additions and 220 deletions
@@ -1,50 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.facet.impl.ui.actions;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopupStep;
import javax.swing.*;
/**
* @author nik
*/
public class GroupToolbarAction extends AnAction {
private final ActionGroup myGroup;
private final JComponent myToolbarComponent;
public GroupToolbarAction(final ActionGroup group, JComponent toolbarComponent) {
super(group.getTemplatePresentation().getText(), group.getTemplatePresentation().getDescription(),
group.getTemplatePresentation().getIcon());
myGroup = group;
myToolbarComponent = toolbarComponent;
}
public void actionPerformed(AnActionEvent e) {
final JBPopupFactory popupFactory = JBPopupFactory.getInstance();
final ListPopupStep popupStep = popupFactory.createActionsStep(myGroup, e.getDataContext(), false, false,
myGroup.getTemplatePresentation().getText(), myToolbarComponent, false,
0, false);
popupFactory.createListPopup(popupStep).showUnderneathOf(myToolbarComponent);
}
public void update(AnActionEvent e) {
myGroup.update(e);
}
}
@@ -23,33 +23,40 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.util.Condition;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Konstantin Bulenkov
*/
@SuppressWarnings({"MethodMayBeStatic"})
public class NewElementAction extends AnAction implements DumbAware, PopupAction {
public class NewElementAction extends AnAction implements DumbAware, PopupAction {
@Override
public void actionPerformed(final AnActionEvent event) {
showPopup(event.getDataContext());
public void actionPerformed(AnActionEvent e) {
showPopup(e);
}
protected void showPopup(DataContext context) {
createPopup(context).showInBestPositionFor(context);
protected void showPopup(AnActionEvent e) {
createPopup(e).showInBestPositionFor(e.getDataContext());
}
protected ListPopup createPopup(DataContext dataContext) {
return JBPopupFactory.getInstance()
.createActionGroupPopup(getPopupTitle(),
getGroup(dataContext),
dataContext,
isShowNumbers(),
isShowDisabledActions(),
isHonorActionMnemonics(),
getDisposeCallback(),
getMaxRowCount(),
getPreselectActionCondition(dataContext));
@NotNull
protected ListPopup createPopup(@NotNull AnActionEvent e) {
return JBPopupFactory.getInstance().createActionGroupPopup(
getPopupTitle(),
getGroup(e.getDataContext()),
e.getDataContext(),
getActionSelectionAid(),
isShowDisabledActions(),
getDisposeCallback(),
getMaxRowCount(),
getPreselectActionCondition(e.getDataContext()),
e.getPlace());
}
@Nullable
protected JBPopupFactory.ActionSelectionAid getActionSelectionAid() {
return null;
}
protected int getMaxRowCount() {
@@ -66,24 +73,16 @@ public class NewElementAction extends AnAction implements DumbAware, PopupActio
return null;
}
protected boolean isHonorActionMnemonics() {
return false;
}
protected boolean isShowDisabledActions() {
return false;
}
protected boolean isShowNumbers() {
return false;
}
protected String getPopupTitle() {
return IdeBundle.message("title.popup.new.element");
}
@Override
public void update(AnActionEvent e){
public void update(AnActionEvent e) {
Presentation presentation = e.getPresentation();
Project project = e.getProject();
if (project == null) {
@@ -19,7 +19,6 @@ package com.intellij.ide.actions;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.ListPopup;
@@ -35,14 +34,14 @@ public class NewElementSamePlaceAction extends NewElementAction {
return LangDataKeys.IDE_VIEW.getData(e.getDataContext()) != null;
}
protected void showPopup(DataContext context) {
ListPopup popup = createPopup(context);
Project project = CommonDataKeys.PROJECT.getData(context);
protected void showPopup(AnActionEvent e) {
ListPopup popup = createPopup(e);
Project project = e.getData(CommonDataKeys.PROJECT);
if (project != null) {
popup.showCenteredInCurrentWindow(project);
}
else {
popup.showInBestPositionFor(context);
popup.showInBestPositionFor(e.getDataContext());
}
}
}
@@ -1,63 +0,0 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.ide.actions;
import com.intellij.ide.DataManager;
import com.intellij.ide.projectView.impl.ProjectViewImpl;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileSystemItem;
/**
* @author yole
*/
public class NewElementToolbarAction extends NewElementAction {
@Override
public void actionPerformed(AnActionEvent e) {
if (e.getData(LangDataKeys.IDE_VIEW) == null) {
final Project project = e.getData(CommonDataKeys.PROJECT);
final PsiFileSystemItem psiFile = e.getData(CommonDataKeys.PSI_FILE).getParent();
ProjectViewImpl.getInstance(project).selectCB(psiFile, psiFile.getVirtualFile(), true).doWhenDone(
() -> showPopup(DataManager.getInstance().getDataContext()));
}
else {
super.actionPerformed(e);
}
}
@Override
public void update(AnActionEvent event) {
super.update(event);
if (event.getData(LangDataKeys.IDE_VIEW) == null) {
Project project = event.getData(CommonDataKeys.PROJECT);
PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE);
if (project != null && psiFile != null) {
final ToolWindow projectViewWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.PROJECT_VIEW);
if (projectViewWindow.isVisible()) {
event.getPresentation().setEnabled(true);
}
}
}
}
}
@@ -926,11 +926,12 @@ public abstract class MasterDetailsComponent implements Configurable, DetailsCom
@Override
public void actionPerformed(AnActionEvent e) {
final JBPopupFactory popupFactory = JBPopupFactory.getInstance();
final DataContext dataContext = e.getDataContext();
final ListPopupStep step = popupFactory.createActionsStep(myActionGroup, dataContext, false, false,
myActionGroup.getTemplatePresentation().getText(), myTree, true,
myPreselection != null ? myPreselection.getDefaultIndex() : 0, true);
JBPopupFactory popupFactory = JBPopupFactory.getInstance();
DataContext dataContext = e.getDataContext();
ListPopupStep step = popupFactory.createActionsStep(
myActionGroup, dataContext, ActionPlaces.UNKNOWN, false,
false, myActionGroup.getTemplatePresentation().getText(), myTree,
true, myPreselection != null ? myPreselection.getDefaultIndex() : 0, true);
final ListPopup listPopup = popupFactory.createListPopup(step);
listPopup.setHandleAutoSelectionBeforeShow(true);
if (e instanceof AnActionButton.AnActionEventWrapper) {
@@ -95,24 +95,43 @@ public abstract class JBPopupFactory {
Runnable onNo,
int defaultOptionIndex);
@Deprecated
@NotNull
public abstract ListPopupStep createActionsStep(@NotNull ActionGroup actionGroup,
@NotNull DataContext dataContext,
boolean showNumbers,
boolean showDisabledActions,
String title,
Component component,
boolean honorActionMnemonics);
public ListPopupStep createActionsStep(@NotNull ActionGroup actionGroup,
@NotNull DataContext dataContext,
boolean showNumbers,
boolean showDisabledActions,
String title,
Component component,
boolean honorActionMnemonics) {
return createActionsStep(actionGroup, dataContext, null, showNumbers, showDisabledActions, title, component, honorActionMnemonics, 0, false);
}
@Deprecated
@NotNull
public ListPopupStep createActionsStep(@NotNull ActionGroup actionGroup,
@NotNull DataContext dataContext,
boolean showNumbers,
boolean showDisabledActions,
String title,
Component component,
boolean honorActionMnemonics,
int defaultOptionIndex,
boolean autoSelectionEnabled) {
return createActionsStep(actionGroup, dataContext, null, showNumbers, showDisabledActions, title, component, honorActionMnemonics, defaultOptionIndex, autoSelectionEnabled);
}
@NotNull
public abstract ListPopupStep createActionsStep(@NotNull ActionGroup actionGroup,
@NotNull DataContext dataContext,
@Nullable String actionPlace,
boolean showNumbers,
boolean showDisabledActions,
String title,
Component component,
boolean honorActionMnemonics,
int defaultOptionIndex, final boolean autoSelectionEnabled);
int defaultOptionIndex,
boolean autoSelectionEnabled);
@NotNull
public abstract RelativePoint guessBestPopupLocation(@NotNull JComponent component);
@@ -358,7 +358,8 @@ open class BasicOptionButtonUI : OptionButtonUI() {
open inner class OptionButtonPopupStep(actions: List<PopupFactoryImpl.ActionItem>, private val defaultSelection: Condition<AnAction>?)
: ActionPopupStep(actions, null,
Supplier<DataContext> { DataManager.getInstance().getDataContext(optionButton) }, true, defaultSelection, false, true) {
Supplier<DataContext> { DataManager.getInstance().getDataContext(optionButton) },
null, true, defaultSelection, false, true) {
// if there is no default selection condition - -1 should be returned, this way first enabled action should be selected by
// OptionButtonPopup.afterShow() (if corresponding ensureSelection parameter is true)
override fun getDefaultOptionIndex() = defaultSelection?.let { super.getDefaultOptionIndex() } ?: -1
@@ -5,6 +5,7 @@ import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ActionUtil;
import com.intellij.openapi.ui.popup.*;
import com.intellij.openapi.util.Condition;
import com.intellij.util.ObjectUtils;
import com.intellij.util.ui.StatusText;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
@@ -20,23 +21,26 @@ public class ActionPopupStep implements ListPopupStepEx<PopupFactoryImpl.ActionI
private final List<PopupFactoryImpl.ActionItem> myItems;
private final String myTitle;
private final Supplier<DataContext> myContext;
private final String myActionPlace;
private final boolean myEnableMnemonics;
private final int myDefaultOptionIndex;
private final boolean myAutoSelectionEnabled;
private final boolean myShowDisabledActions;
private Runnable myFinalRunnable;
@Nullable private final Condition<AnAction> myPreselectActionCondition;
private final Condition<AnAction> myPreselectActionCondition;
public ActionPopupStep(@NotNull final List<PopupFactoryImpl.ActionItem> items,
final String title,
public ActionPopupStep(@NotNull List<PopupFactoryImpl.ActionItem> items,
String title,
@NotNull Supplier<DataContext> context,
@Nullable String actionPlace,
boolean enableMnemonics,
@Nullable Condition<AnAction> preselectActionCondition,
final boolean autoSelection,
boolean autoSelection,
boolean showDisabledActions) {
myItems = items;
myTitle = title;
myContext = context;
myActionPlace = ObjectUtils.notNull(actionPlace, ActionPlaces.UNKNOWN);
myEnableMnemonics = enableMnemonics;
myDefaultOptionIndex = getDefaultOptionIndexFromSelectCondition(preselectActionCondition, items);
myPreselectActionCondition = preselectActionCondition;
@@ -65,8 +69,10 @@ public class ActionPopupStep implements ListPopupStepEx<PopupFactoryImpl.ActionI
boolean useAlphaAsNumbers,
boolean showDisabledActions,
String title,
boolean honorActionMnemonics, final boolean autoSelectionEnabled,
boolean honorActionMnemonics,
boolean autoSelectionEnabled,
Supplier<DataContext> contextSupplier,
@Nullable String actionPlace,
Condition<AnAction> preselectCondition,
int defaultOptionIndex) {
final ActionStepBuilder builder =
@@ -77,15 +83,13 @@ public class ActionPopupStep implements ListPopupStepEx<PopupFactoryImpl.ActionI
honorActionMnemonics &&
items.stream().anyMatch(actionItem -> actionItem.getAction().getTemplatePresentation().getMnemonic() != 0);
return new ActionPopupStep(items,
title,
contextSupplier,
enableMnemonics,
preselectCondition != null ? preselectCondition : action -> defaultOptionIndex >= 0 &&
defaultOptionIndex < items.size() &&
items.get(defaultOptionIndex).getAction().equals(action),
autoSelectionEnabled,
showDisabledActions);
return new ActionPopupStep(
items, title, contextSupplier, actionPlace, enableMnemonics,
preselectCondition != null ? preselectCondition :
action -> defaultOptionIndex >= 0 &&
defaultOptionIndex < items.size() && items.get(defaultOptionIndex).getAction().equals(action),
autoSelectionEnabled,
showDisabledActions);
}
@Override
@@ -164,16 +168,17 @@ public class ActionPopupStep implements ListPopupStepEx<PopupFactoryImpl.ActionI
final AnAction action = actionChoice.getAction();
final DataContext dataContext = myContext.get();
if (action instanceof ActionGroup && (!finalChoice || !((ActionGroup)action).canBePerformed(dataContext))) {
return
createActionsStep((ActionGroup)action,
dataContext,
myEnableMnemonics,
true,
myShowDisabledActions,
null,
false, false,
myContext,
myPreselectActionCondition, -1);
return createActionsStep(
(ActionGroup)action,
dataContext,
myEnableMnemonics,
true,
myShowDisabledActions,
null,
false, false,
myContext,
myActionPlace,
myPreselectActionCondition, -1);
}
else {
myFinalRunnable = () -> performAction(action, eventModifiers);
@@ -186,9 +191,10 @@ public class ActionPopupStep implements ListPopupStepEx<PopupFactoryImpl.ActionI
}
public void performAction(@NotNull AnAction action, int modifiers, InputEvent inputEvent) {
final DataContext dataContext = myContext.get();
final AnActionEvent event = new AnActionEvent(inputEvent, dataContext, ActionPlaces.UNKNOWN, action.getTemplatePresentation().clone(),
ActionManager.getInstance(), modifiers);
DataContext dataContext = myContext.get();
AnActionEvent event = new AnActionEvent(
inputEvent, dataContext, myActionPlace, action.getTemplatePresentation().clone(),
ActionManager.getInstance(), modifiers);
event.setInjectedContext(action.isInInjectedContext());
if (ActionUtil.lastUpdateAndCheckDumb(action, event, false)) {
ActionUtil.performActionDumbAware(action, event);
@@ -251,14 +251,15 @@ public class PopupFactoryImpl extends JBPopupFactory {
boolean showDisabledActions,
boolean honorActionMnemonics,
Condition<AnAction> preselectActionCondition,
@Nullable String actionPlace, boolean autoSelection) {
@Nullable String actionPlace,
boolean autoSelection) {
final Component component = PlatformDataKeys.CONTEXT_COMPONENT.getData(dataContext);
LOG.assertTrue(component != null, "dataContext has no component for new ListPopupStep");
List<ActionItem> items =
getActionItems(actionGroup, dataContext, showNumbers, useAlphaAsNumbers, showDisabledActions, honorActionMnemonics, actionPlace);
return new ActionPopupStep(items, title, getComponentContextSupplier(component), showNumbers || honorActionMnemonics && itemsHaveMnemonics(items),
return new ActionPopupStep(items, title, getComponentContextSupplier(component), actionPlace, showNumbers || honorActionMnemonics && itemsHaveMnemonics(items),
preselectActionCondition, autoSelection, showDisabledActions);
}
@@ -380,40 +381,23 @@ public class PopupFactoryImpl extends JBPopupFactory {
disposeCallback, maxRowCount, preselectActionCondition, null);
}
@NotNull
@Override
public ListPopupStep createActionsStep(@NotNull final ActionGroup actionGroup,
@NotNull DataContext dataContext,
final boolean showNumbers,
final boolean showDisabledActions,
final String title,
final Component component,
final boolean honorActionMnemonics) {
return createActionsStep(actionGroup, dataContext, showNumbers, showDisabledActions, title, component, honorActionMnemonics, 0, false);
}
@NotNull
@Override
public ListPopupStep createActionsStep(@NotNull ActionGroup actionGroup,
@NotNull DataContext dataContext,
@Nullable String actionPlace,
boolean showNumbers,
boolean showDisabledActions,
String title,
Component component,
boolean honorActionMnemonics,
int defaultOptionIndex,
final boolean autoSelectionEnabled) {
return ActionPopupStep.createActionsStep(actionGroup,
dataContext,
showNumbers,
true,
showDisabledActions,
title,
honorActionMnemonics,
autoSelectionEnabled,
getComponentContextSupplier(component),
null,
defaultOptionIndex);
boolean autoSelectionEnabled) {
return ActionPopupStep.createActionsStep(
actionGroup, dataContext, showNumbers, true, showDisabledActions,
title, honorActionMnemonics, autoSelectionEnabled,
getComponentContextSupplier(component),
actionPlace, null, defaultOptionIndex);
}
private static boolean itemsHaveMnemonics(final List<ActionItem> items) {
@@ -398,12 +398,6 @@
<group id="WeighingNewGroup" class="com.intellij.ide.actions.WeighingNewActionGroup"/>
<!-- Toolbar -->
<!--
<group id="ToolbarNewElement">
<action id="NewElementToolbarAction" class="com.intellij.ide.actions.NewElementToolbarAction" icon="/general/add.png"/>
<add-to-group group-id="MainToolBar" anchor="first"/>
</group>
-->
<action id="RevealIn" class="com.intellij.ide.actions.RevealFileAction">
<add-to-group group-id="RunContextPopupGroup" anchor="last"/>
</action>
@@ -121,8 +121,8 @@ public class SwitchTaskAction extends ComboBoxAction implements DumbAware {
popup.showCenteredInCurrentWindow(project);
}
private static ListPopupImpl createPopup(final DataContext dataContext,
@Nullable final Runnable onDispose,
private static ListPopupImpl createPopup(@NotNull DataContext dataContext,
@Nullable Runnable onDispose,
boolean withTitle) {
final Project project = CommonDataKeys.PROJECT.getData(dataContext);
final Ref<Boolean> shiftPressed = Ref.create(false);
@@ -138,8 +138,9 @@ public class SwitchTaskAction extends ComboBoxAction implements DumbAware {
return FINAL_CHOICE;
}
ActionGroup group = createActionsStep(selectedValues, project, shiftPressed);
return JBPopupFactory.getInstance()
.createActionsStep(group, DataManager.getInstance().getDataContext(componentRef.get()), false, false, null, null, true);
DataContext dataContext = DataManager.getInstance().getDataContext(componentRef.get());
return JBPopupFactory.getInstance().createActionsStep(
group, dataContext, null, false, false, null, null, true, 0, false);
}
@Override