This commit is contained in:
Vladimir Krivosheev
2015-10-02 21:18:53 +02:00
parent 6371a3cd9a
commit b6dcebb69c
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2015 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.
@@ -16,43 +16,37 @@
package com.intellij.ui;
import com.intellij.openapi.components.*;
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.openapi.project.Project;
import com.intellij.util.containers.HashMap;
import gnu.trove.THashMap;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author ven
*/
@State(
name="RecentsManager",
storages= {
@Storage(
file = StoragePathMacros.WORKSPACE_FILE
)}
)
@State(name = "RecentsManager", storages = @Storage(file = StoragePathMacros.WORKSPACE_FILE))
public class RecentsManager implements PersistentStateComponent<Element> {
private final Map<String, LinkedList<String>> myMap = new HashMap<String, LinkedList<String>>();
private int myRecentsNumberToKeep = 5;
@NonNls private static final String KEY_ELEMENT_NAME = "key";
@NonNls private static final String RECENT_ELEMENT_NAME = "recent";
@NonNls protected static final String NAME_ATTR = "name";
public static RecentsManager getInstance(Project project) {
private final Map<String, LinkedList<String>> myMap = new THashMap<String, LinkedList<String>>();
private int myRecentsNumberToKeep = 5;
@NotNull
public static RecentsManager getInstance(@NotNull Project project) {
return ServiceManager.getService(project, RecentsManager.class);
}
@Nullable
public List<String> getRecentEntries(String key) {
public List<String> getRecentEntries(@NotNull String key) {
return myMap.get(key);
}
public void registerRecentEntry(String key, String recentEntry) {
public void registerRecentEntry(@NotNull String key, String recentEntry) {
LinkedList<String> recents = myMap.get(key);
if (recents == null) {
recents = new LinkedList<String>();
@@ -62,45 +56,39 @@ public class RecentsManager implements PersistentStateComponent<Element> {
add(recents, recentEntry);
}
private void add(final LinkedList<String> recentEntrues, final String newEntry) {
final int oldIndex = recentEntrues.indexOf(newEntry);
private void add(final LinkedList<String> recentEntries, final String newEntry) {
final int oldIndex = recentEntries.indexOf(newEntry);
if (oldIndex >= 0) {
recentEntrues.remove(oldIndex);
recentEntries.remove(oldIndex);
}
else if (recentEntrues.size() == myRecentsNumberToKeep) {
recentEntrues.removeLast();
else if (recentEntries.size() == myRecentsNumberToKeep) {
recentEntries.removeLast();
}
recentEntrues.addFirst(newEntry);
recentEntries.addFirst(newEntry);
}
@Override
public void loadState(Element element) {
myMap.clear();
final List keyElements = element.getChildren(KEY_ELEMENT_NAME);
for (Iterator iterator = keyElements.iterator(); iterator.hasNext();) {
Element keyElement = (Element)iterator.next();
final String key = keyElement.getAttributeValue(NAME_ATTR);
for (Element keyElement : element.getChildren(KEY_ELEMENT_NAME)) {
LinkedList<String> recents = new LinkedList<String>();
final List children = keyElement.getChildren(RECENT_ELEMENT_NAME);
for (Iterator<Element> iterator1 = children.iterator(); iterator1.hasNext();) {
recents.addLast(iterator1.next().getAttributeValue(NAME_ATTR));
for (Element aChildren : keyElement.getChildren(RECENT_ELEMENT_NAME)) {
recents.addLast(aChildren.getAttributeValue(NAME_ATTR));
}
myMap.put(key, recents);
myMap.put(keyElement.getAttributeValue(NAME_ATTR), recents);
}
}
@Override
public Element getState() {
Element element = new Element("state");
final Set<Map.Entry<String, LinkedList<String>>> entries = myMap.entrySet();
for (Map.Entry<String, LinkedList<String>> entry : entries) {
final Element keyElement = new Element(KEY_ELEMENT_NAME);
for (Map.Entry<String, LinkedList<String>> entry : myMap.entrySet()) {
Element keyElement = new Element(KEY_ELEMENT_NAME);
keyElement.setAttribute(NAME_ATTR, entry.getKey());
final LinkedList<String> recents = entry.getValue();
for (String recent : recents) {
final Element recentElement = new Element(RECENT_ELEMENT_NAME);
recentElement.setAttribute(NAME_ATTR, recent);
keyElement.addContent(recentElement);
for (String recent : entry.getValue()) {
keyElement.addContent(new Element(RECENT_ELEMENT_NAME).setAttribute(NAME_ATTR, recent));
}
element.addContent(keyElement);
}