Skip to content

Commit

Permalink
CHE-2521: improve clear recent list action
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <oorel@codenvy.com>
  • Loading branch information
Oleksii Orel committed Sep 22, 2016
1 parent e96d45b commit 4255d96
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@
import org.eclipse.che.ide.api.keybinding.KeyBindingAgent;
import org.eclipse.che.ide.api.keybinding.KeyBuilder;
import org.eclipse.che.ide.connection.WsConnectionListener;
import org.eclipse.che.ide.imageviewer.ImageViewerProvider;
import org.eclipse.che.ide.machine.macro.ServerHostNameMacroProvider;
import org.eclipse.che.ide.machine.macro.ServerMacroProvider;
import org.eclipse.che.ide.machine.macro.ServerPortMacroProvider;
import org.eclipse.che.ide.machine.macro.ServerProtocolMacroProvider;
import org.eclipse.che.ide.part.editor.actions.SwitchNextEditorAction;
import org.eclipse.che.ide.part.editor.actions.SwitchPreviousEditorAction;
import org.eclipse.che.ide.imageviewer.ImageViewerProvider;
import org.eclipse.che.ide.newresource.NewFileAction;
import org.eclipse.che.ide.newresource.NewFolderAction;
import org.eclipse.che.ide.part.editor.actions.CloseAction;
Expand All @@ -79,6 +77,9 @@
import org.eclipse.che.ide.part.editor.actions.ReopenClosedFileAction;
import org.eclipse.che.ide.part.editor.actions.SplitHorizontallyAction;
import org.eclipse.che.ide.part.editor.actions.SplitVerticallyAction;
import org.eclipse.che.ide.part.editor.actions.SwitchNextEditorAction;
import org.eclipse.che.ide.part.editor.actions.SwitchPreviousEditorAction;
import org.eclipse.che.ide.part.editor.recent.ClearRecentListAction;
import org.eclipse.che.ide.part.editor.recent.OpenRecentFilesAction;
import org.eclipse.che.ide.part.explorer.project.TreeResourceRevealer;
import org.eclipse.che.ide.resources.action.CopyResourceAction;
Expand All @@ -96,6 +97,8 @@

import static org.eclipse.che.ide.api.action.IdeActions.GROUP_FILE_NEW;
import static org.eclipse.che.ide.api.constraints.Constraints.FIRST;
import static org.eclipse.che.ide.api.constraints.Constraints.LAST;
import static org.eclipse.che.ide.part.editor.recent.RecentFileStore.RECENT_GROUP_ID;
import static org.eclipse.che.ide.projecttype.BlankProjectWizardRegistrar.BLANK_CATEGORY;

/**
Expand Down Expand Up @@ -269,6 +272,9 @@ public interface ParserResource extends ClientBundle {
@Inject
private OpenRecentFilesAction openRecentFilesAction;

@Inject
private ClearRecentListAction clearRecentFilesAction;

@Inject
private CloseActiveEditorAction closeActiveEditorAction;

Expand Down Expand Up @@ -461,7 +467,12 @@ public void initialize() {

// Edit (New Menu)
DefaultActionGroup editGroup = (DefaultActionGroup)actionManager.getAction(IdeActions.GROUP_EDIT);

DefaultActionGroup recentGroup = new DefaultActionGroup(RECENT_GROUP_ID, true, actionManager);
actionManager.registerAction(IdeActions.GROUP_RECENT_FILES, recentGroup);
actionManager.registerAction("clearRecentList", clearRecentFilesAction);
recentGroup.addSeparator();
recentGroup.add(clearRecentFilesAction, LAST);
editGroup.add(recentGroup);
actionManager.registerAction("openRecentFiles", openRecentFilesAction);
editGroup.add(openRecentFilesAction);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.part.editor.recent;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.action.AbstractPerspectiveAction;
import org.eclipse.che.ide.api.action.ActionEvent;

import javax.validation.constraints.NotNull;

import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;

/**
* Action clears list of recently opened files.
*
* @author Oleksii Orel
*/
@Singleton
public class ClearRecentListAction extends AbstractPerspectiveAction {

private RecentFileList recentFileList;

@Inject
public ClearRecentListAction(RecentFileList recentFileList,
CoreLocalizationConstant locale) {
super(singletonList(PROJECT_PERSPECTIVE_ID), locale.openRecentFileClearTitle(), locale.openRecentFileClearDescription(), null,
null);

this.recentFileList = recentFileList;
}

/** {@inheritDoc} */
@Override
public void updateInPerspective(@NotNull ActionEvent event) {
event.getPresentation().setEnabledAndVisible(!recentFileList.isEmpty());
}

/** {@inheritDoc} */
@Override
public void actionPerformed(ActionEvent e) {
recentFileList.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,22 @@
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.action.AbstractPerspectiveAction;
import org.eclipse.che.ide.api.action.ActionEvent;
import org.eclipse.che.ide.api.action.ActionManager;
import org.eclipse.che.ide.api.action.DefaultActionGroup;
import org.eclipse.che.ide.api.action.IdeActions;
import org.eclipse.che.ide.api.constraints.Constraints;
import org.eclipse.che.ide.api.event.FileEvent;
import org.eclipse.che.ide.api.event.FileEvent.FileEventHandler;
import org.eclipse.che.ide.api.resources.File;
import org.eclipse.che.ide.api.resources.VirtualFile;
import org.eclipse.che.ide.util.Pair;

import javax.validation.constraints.NotNull;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import static com.google.common.collect.Lists.newLinkedList;
import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.api.constraints.Anchor.BEFORE;
import static org.eclipse.che.ide.api.constraints.Constraints.FIRST;
import static org.eclipse.che.ide.api.constraints.Constraints.LAST;
import static org.eclipse.che.ide.api.event.FileEvent.FileOperation.OPEN;
import static org.eclipse.che.ide.workspace.perspectives.project.ProjectPerspective.PROJECT_PERSPECTIVE_ID;

/**
* Default implementation of Recent File List.
Expand All @@ -55,38 +46,29 @@ public class RecentFileStore implements RecentFileList, FileEventHandler {
private final OpenRecentFilesPresenter openRecentFilesPresenter;
private final ActionManager actionManager;
private final RecentFileActionFactory recentFileActionFactory;
private final CoreLocalizationConstant locale;
private final DefaultActionGroup recentGroup;

private DefaultActionGroup recentGroup;
private LinkedList<File> recentStorage = newLinkedList();
private LinkedList<Pair<File, RecentFileAction>> fileToAction = newLinkedList();

@Inject
public RecentFileStore(EventBus eventBus,
OpenRecentFilesPresenter openRecentFilesPresenter,
ActionManager actionManager,
RecentFileActionFactory recentFileActionFactory,
CoreLocalizationConstant locale) {
RecentFileActionFactory recentFileActionFactory) {
this.openRecentFilesPresenter = openRecentFilesPresenter;
this.actionManager = actionManager;
this.recentFileActionFactory = recentFileActionFactory;
this.locale = locale;

ClearRecentListAction action = new ClearRecentListAction();

recentGroup = new DefaultActionGroup(RECENT_GROUP_ID, true, actionManager);
actionManager.registerAction(IdeActions.GROUP_RECENT_FILES, recentGroup);

actionManager.registerAction("clearRecentList", action);
recentGroup.addSeparator();
recentGroup.add(action, LAST);

DefaultActionGroup editGroup = (DefaultActionGroup)actionManager.getAction(IdeActions.GROUP_EDIT);
editGroup.add(recentGroup, new Constraints(BEFORE, "openRecentFiles"));

eventBus.addHandler(FileEvent.TYPE, this);
}

private void ensureGroupExist() {
if (recentGroup == null) {
recentGroup = (DefaultActionGroup)actionManager.getAction(IdeActions.GROUP_RECENT_FILES);
}
}

/** {@inheritDoc} */
@Override
public void onFileOperation(FileEvent event) {
Expand All @@ -107,7 +89,9 @@ public boolean isEmpty() {
/** {@inheritDoc} */
@Override
public boolean add(final File item) {
if (item == null) {
ensureGroupExist();

if (item == null || recentGroup == null) {
return false;
}

Expand All @@ -133,6 +117,10 @@ public boolean add(final File item) {
/** {@inheritDoc} */
@Override
public boolean remove(File item) {
if (recentGroup == null) {
return false;
}

recentStorage.remove(item);
openRecentFilesPresenter.setRecentFiles(getAll());

Expand Down Expand Up @@ -166,8 +154,11 @@ public List<File> getAll() {
/** {@inheritDoc} */
@Override
public void clear() {
openRecentFilesPresenter.clearRecentFiles();
if (recentGroup == null) {
return;
}

openRecentFilesPresenter.clearRecentFiles();
recentStorage.clear();

//de-register all previously registered actions
Expand Down Expand Up @@ -209,22 +200,4 @@ static String getShortPath(String path) {

return raw;
}

private class ClearRecentListAction extends AbstractPerspectiveAction {

public ClearRecentListAction() {
super(singletonList(PROJECT_PERSPECTIVE_ID), locale.openRecentFileClearTitle(), locale.openRecentFileClearDescription(), null,
null);
}

@Override
public void updateInPerspective(@NotNull ActionEvent event) {
event.getPresentation().setEnabledAndVisible(!isEmpty());
}

@Override
public void actionPerformed(ActionEvent e) {
clear();
}
}
}

0 comments on commit 4255d96

Please sign in to comment.