Search in sources :

Example 16 with ListPopupImpl

use of com.intellij.ui.popup.list.ListPopupImpl in project intellij-community by JetBrains.

the class LoadContextAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Project project = getProject(e);
    assert project != null;
    DefaultActionGroup group = new DefaultActionGroup();
    final WorkingContextManager manager = WorkingContextManager.getInstance(project);
    List<ContextInfo> history = manager.getContextHistory();
    List<ContextHolder> infos = new ArrayList<>(ContainerUtil.map2List(history, (Function<ContextInfo, ContextHolder>) info -> new ContextHolder() {

        @Override
        void load(final boolean clear) {
            LoadContextUndoableAction undoableAction = LoadContextUndoableAction.createAction(manager, clear, info.name);
            UndoableCommand.execute(project, undoableAction, "Load context " + info.comment, "Context");
        }

        @Override
        void remove() {
            manager.removeContext(info.name);
        }

        @Override
        Date getDate() {
            return new Date(info.date);
        }

        @Override
        String getComment() {
            return info.comment;
        }

        @Override
        Icon getIcon() {
            return TasksIcons.SavedContext;
        }
    }));
    final TaskManager taskManager = TaskManager.getManager(project);
    List<LocalTask> tasks = taskManager.getLocalTasks();
    infos.addAll(ContainerUtil.mapNotNull(tasks, (NullableFunction<LocalTask, ContextHolder>) task -> {
        if (task.isActive()) {
            return null;
        }
        return new ContextHolder() {

            @Override
            void load(boolean clear) {
                LoadContextUndoableAction undoableAction = LoadContextUndoableAction.createAction(manager, clear, task);
                UndoableCommand.execute(project, undoableAction, "Load context " + TaskUtil.getTrimmedSummary(task), "Context");
            }

            @Override
            void remove() {
                SwitchTaskAction.removeTask(project, task, taskManager);
            }

            @Override
            Date getDate() {
                return task.getUpdated();
            }

            @Override
            String getComment() {
                return TaskUtil.getTrimmedSummary(task);
            }

            @Override
            Icon getIcon() {
                return task.getIcon();
            }
        };
    }));
    Collections.sort(infos, (o1, o2) -> o2.getDate().compareTo(o1.getDate()));
    final Ref<Boolean> shiftPressed = Ref.create(false);
    boolean today = true;
    Calendar now = Calendar.getInstance();
    for (int i = 0, historySize = Math.min(MAX_ROW_COUNT, infos.size()); i < historySize; i++) {
        final ContextHolder info = infos.get(i);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(info.getDate());
        if (today && (calendar.get(Calendar.YEAR) != now.get(Calendar.YEAR) || calendar.get(Calendar.DAY_OF_YEAR) != now.get(Calendar.DAY_OF_YEAR))) {
            group.addSeparator();
            today = false;
        }
        group.add(createItem(info, shiftPressed));
    }
    final ListPopupImpl popup = (ListPopupImpl) JBPopupFactory.getInstance().createActionGroupPopup("Load Context", group, e.getDataContext(), false, null, MAX_ROW_COUNT);
    popup.setAdText("Press SHIFT to merge with current context");
    popup.registerAction("shiftPressed", KeyStroke.getKeyStroke("shift pressed SHIFT"), new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            shiftPressed.set(true);
            popup.setCaption("Merge with Current Context");
        }
    });
    popup.registerAction("shiftReleased", KeyStroke.getKeyStroke("released SHIFT"), new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            shiftPressed.set(false);
            popup.setCaption("Load Context");
        }
    });
    popup.registerAction("invoke", KeyStroke.getKeyStroke("shift ENTER"), new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            popup.handleSelect(true);
        }
    });
    popup.addPopupListener(new JBPopupAdapter() {

        @Override
        public void onClosed(LightweightWindowEvent event) {
        }
    });
    popup.showCenteredInCurrentWindow(project);
}
Also used : ActionEvent(java.awt.event.ActionEvent) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) NullableFunction(com.intellij.util.NullableFunction) Function(com.intellij.util.Function) ContextInfo(com.intellij.tasks.context.ContextInfo) WorkingContextManager(com.intellij.tasks.context.WorkingContextManager) LocalTask(com.intellij.tasks.LocalTask) NullableFunction(com.intellij.util.NullableFunction) Project(com.intellij.openapi.project.Project) LoadContextUndoableAction(com.intellij.tasks.context.LoadContextUndoableAction) TaskManager(com.intellij.tasks.TaskManager) ListPopupImpl(com.intellij.ui.popup.list.ListPopupImpl) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter)

Aggregations

ListPopupImpl (com.intellij.ui.popup.list.ListPopupImpl)16 BaseListPopupStep (com.intellij.openapi.ui.popup.util.BaseListPopupStep)10 Project (com.intellij.openapi.project.Project)5 PopupStep (com.intellij.openapi.ui.popup.PopupStep)5 NotNull (org.jetbrains.annotations.NotNull)4 ColoredListCellRenderer (com.intellij.ui.ColoredListCellRenderer)3 RelativePoint (com.intellij.ui.awt.RelativePoint)3 PopupListElementRenderer (com.intellij.ui.popup.list.PopupListElementRenderer)3 ActionEvent (java.awt.event.ActionEvent)3 ListSelectionEvent (javax.swing.event.ListSelectionEvent)3 ListSelectionListener (javax.swing.event.ListSelectionListener)3 DefaultPsiElementCellRenderer (com.intellij.ide.util.DefaultPsiElementCellRenderer)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)2 XExpression (com.intellij.xdebugger.XExpression)2 PsiElementListCellRenderer (com.intellij.ide.util.PsiElementListCellRenderer)1 GotoRelatedItem (com.intellij.navigation.GotoRelatedItem)1 Disposable (com.intellij.openapi.Disposable)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 Result (com.intellij.openapi.application.Result)1 WriteAction (com.intellij.openapi.application.WriteAction)1