use of com.intellij.ide.DataManager in project intellij-community by JetBrains.
the class FocusTrackback method restoreFocus.
public void restoreFocus() {
if (isHeadlessOrWrongOS() || myConsumed || isScheduledForRestore())
return;
Project project = null;
DataManager dataManager = DataManager.getInstance();
if (dataManager != null) {
DataContext context = myParentWindow == null ? dataManager.getDataContext() : dataManager.getDataContext(myParentWindow);
if (context != null) {
project = CommonDataKeys.PROJECT.getData(context);
}
}
myScheduledForRestore = true;
final List<FocusTrackback> stack = getCleanStackForRoot();
final int index = stack.indexOf(this);
for (int i = index - 1; i >= 0; i--) {
if (stack.get(i).isScheduledForRestore()) {
dispose();
return;
}
}
if (project != null && !project.isDisposed()) {
final IdeFocusManager focusManager = IdeFocusManager.getInstance(project);
cleanParentWindow();
final Project finalProject = project;
focusManager.requestFocus(new MyFocusCommand(), myForcedRestore).doWhenProcessed(() -> dispose()).doWhenRejected(() -> focusManager.revalidateFocus(new ExpirableRunnable.ForProject(finalProject) {
@Override
public void run() {
if (UIUtil.isMeaninglessFocusOwner(focusManager.getFocusOwner())) {
focusManager.requestDefaultFocus(false);
}
}
}));
} else {
// no ide focus manager, so no way -- do just later
//noinspection SSBasedInspection
SwingUtilities.invokeLater(() -> {
_restoreFocus();
dispose();
});
}
}
use of com.intellij.ide.DataManager in project intellij-community by JetBrains.
the class SearchEverywhereAction method doNavigate.
private void doNavigate(final int index) {
final DataManager dataManager = DataManager.getInstance();
if (dataManager == null)
return;
final Project project = CommonDataKeys.PROJECT.getData(dataManager.getDataContext(getField().getTextEditor()));
assert project != null;
final SearchListModel model = getModel();
if (isMoreItem(index)) {
final String pattern = myPopupField.getText();
WidgetID wid = null;
if (index == model.moreIndex.classes)
wid = WidgetID.CLASSES;
else if (index == model.moreIndex.files)
wid = WidgetID.FILES;
else if (index == model.moreIndex.settings)
wid = WidgetID.SETTINGS;
else if (index == model.moreIndex.actions)
wid = WidgetID.ACTIONS;
else if (index == model.moreIndex.symbols)
wid = WidgetID.SYMBOLS;
else if (index == model.moreIndex.runConfigurations)
wid = WidgetID.RUN_CONFIGURATIONS;
if (wid != null) {
final WidgetID widgetID = wid;
myCurrentWorker.doWhenProcessed(() -> {
myCalcThread = new CalcThread(project, pattern, true);
myPopupActualWidth = 0;
myCurrentWorker = myCalcThread.insert(index, widgetID);
});
return;
}
}
final String pattern = getField().getText();
final Object value = myList.getSelectedValue();
saveHistory(project, pattern, value);
IdeFocusManager focusManager = IdeFocusManager.findInstanceByComponent(getField().getTextEditor());
if (myPopup != null && myPopup.isVisible()) {
myPopup.cancel();
}
if (value instanceof BooleanOptionDescription) {
final BooleanOptionDescription option = (BooleanOptionDescription) value;
option.setOptionState(!option.isOptionEnabled());
myList.revalidate();
myList.repaint();
getGlobalInstance().doWhenFocusSettlesDown(() -> {
getGlobalInstance().requestFocus(getField(), true);
});
return;
}
if (value instanceof OptionsTopHitProvider) {
//noinspection SSBasedInspection
SwingUtilities.invokeLater(() -> getField().setText("#" + ((OptionsTopHitProvider) value).getId() + " "));
return;
}
Runnable onDone = null;
AccessToken token = ApplicationManager.getApplication().acquireReadActionLock();
try {
if (value instanceof PsiElement) {
onDone = () -> NavigationUtil.activateFileWithPsiElement((PsiElement) value, true);
return;
} else if (isVirtualFile(value)) {
onDone = () -> OpenSourceUtil.navigate(true, new OpenFileDescriptor(project, (VirtualFile) value));
return;
} else if (isActionValue(value) || isSetting(value) || isRunConfiguration(value)) {
focusManager.requestDefaultFocus(true);
final Component comp = myContextComponent;
final AnActionEvent event = myActionEvent;
IdeFocusManager.getInstance(project).doWhenFocusSettlesDown(() -> {
Component c = comp;
if (c == null) {
c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
}
if (isRunConfiguration(value)) {
ChooseRunConfigurationPopup.ItemWrapper itemWrapper = (ChooseRunConfigurationPopup.ItemWrapper) value;
RunnerAndConfigurationSettings settings = ObjectUtils.tryCast(itemWrapper.getValue(), RunnerAndConfigurationSettings.class);
if (settings != null) {
Executor executor = findExecutor(settings);
if (executor != null) {
itemWrapper.perform(project, executor, dataManager.getDataContext(c));
}
}
} else {
GotoActionAction.openOptionOrPerformAction(value, pattern, project, c, event);
if (isToolWindowAction(value))
return;
}
});
return;
} else if (value instanceof Navigatable) {
onDone = () -> OpenSourceUtil.navigate(true, (Navigatable) value);
return;
}
} finally {
token.finish();
final ActionCallback callback = onFocusLost();
if (onDone != null) {
callback.doWhenDone(onDone);
}
}
focusManager.requestDefaultFocus(true);
}
use of com.intellij.ide.DataManager in project intellij-community by JetBrains.
the class GotoActionAction method performAction.
public static void performAction(Object element, @Nullable final Component component, @Nullable final AnActionEvent e, @Nullable final Runnable callback) {
// element could be AnAction (SearchEverywhere)
if (component == null)
return;
final AnAction action = element instanceof AnAction ? (AnAction) element : ((GotoActionModel.ActionWrapper) element).getAction();
TransactionGuard.getInstance().submitTransactionLater(ApplicationManager.getApplication(), () -> {
DataManager instance = DataManager.getInstance();
DataContext context = instance != null ? instance.getDataContext(component) : DataContext.EMPTY_CONTEXT;
InputEvent inputEvent = e == null ? null : e.getInputEvent();
AnActionEvent event = AnActionEvent.createFromAnAction(action, inputEvent, ActionPlaces.ACTION_SEARCH, context);
if (ActionUtil.lastUpdateAndCheckDumb(action, event, false)) {
if (action instanceof ActionGroup && ((ActionGroup) action).getChildren(event).length > 0) {
ListPopup popup = JBPopupFactory.getInstance().createActionGroupPopup(event.getPresentation().getText(), (ActionGroup) action, context, false, callback, -1);
Window window = SwingUtilities.getWindowAncestor(component);
if (window != null) {
popup.showInCenterOf(window);
} else {
popup.showInFocusCenter();
}
} else {
ActionUtil.performActionDumbAware(action, event);
if (callback != null)
callback.run();
}
}
});
}
use of com.intellij.ide.DataManager in project intellij-community by JetBrains.
the class FileInEditorProcessor method processCode.
public void processCode() {
if (myShouldOptimizeImports) {
myProcessor = new OptimizeImportsProcessor(myProject, myFile);
}
if (myProcessChangesTextOnly && !FormatChangedTextUtil.hasChanges(myFile)) {
myNoChangesDetected = true;
}
myProcessor = mixWithReformatProcessor(myProcessor);
if (myShouldRearrangeCode) {
myProcessor = mixWithRearrangeProcessor(myProcessor);
}
if (shouldNotify()) {
myProcessor.setCollectInfo(true);
myProcessor.setPostRunnable(() -> {
String message = prepareMessage();
if (!myEditor.isDisposed() && myEditor.getComponent().isShowing()) {
HyperlinkListener hyperlinkListener = new HyperlinkAdapter() {
@Override
protected void hyperlinkActivated(HyperlinkEvent e) {
AnAction action = ActionManager.getInstance().getAction("ShowReformatFileDialog");
DataManager manager = DataManager.getInstance();
if (manager != null) {
DataContext context = manager.getDataContext(myEditor.getContentComponent());
action.actionPerformed(AnActionEvent.createFromAnAction(action, null, "", context));
}
}
};
showHint(myEditor, message, hyperlinkListener);
}
});
}
myProcessor.run();
}
use of com.intellij.ide.DataManager in project intellij-community by JetBrains.
the class MoveClassToModuleFix method moveClass.
private void moveClass(Project project, Editor editor, PsiFile file, PsiClass aClass) {
RefactoringActionHandler moveHandler = RefactoringActionHandlerFactory.getInstance().createMoveHandler();
DataManager dataManager = DataManager.getInstance();
DataContext dataContext = dataManager.getDataContext();
final String fqName = aClass.getQualifiedName();
LOG.assertTrue(fqName != null);
PsiDirectory directory = PackageUtil.findOrCreateDirectoryForPackage(myCurrentModule, StringUtil.getPackageName(fqName), mySourceRoot, true);
DataContext context = SimpleDataContext.getSimpleContext(LangDataKeys.TARGET_PSI_ELEMENT.getName(), directory, dataContext);
moveHandler.invoke(project, new PsiElement[] { aClass }, context);
PsiReference reference = file.findReferenceAt(editor.getCaretModel().getOffset());
PsiClass newClass = JavaPsiFacade.getInstance(project).findClass(fqName, GlobalSearchScope.moduleScope(myCurrentModule));
if (reference != null && newClass != null) {
final QuestionAction action = new AddImportAction(project, reference, editor, newClass);
action.execute();
}
}
Aggregations