All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.intellij.ui.RecentsManager Maven / Gradle / Ivy

Go to download

A packaging of the IntelliJ Community Edition platform-impl library. This is release number 1 of trunk branch 142.

The newest version!
/*
 * Copyright 2000-2012 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.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 org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * @author ven
 */
@State(
  name="RecentsManager",
  storages= {
    @Storage(
      file = StoragePathMacros.WORKSPACE_FILE
    )}
)
public class RecentsManager implements PersistentStateComponent {
  private final Map> myMap = new HashMap>();

  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) {
    return ServiceManager.getService(project, RecentsManager.class);
  }

  @Nullable
  public List getRecentEntries(String key) {
    return myMap.get(key);
  }

  public void registerRecentEntry(String key, String recentEntry) {
    LinkedList recents = myMap.get(key);
    if (recents == null) {
      recents = new LinkedList();
      myMap.put(key, recents);
    }

    add(recents, recentEntry);
  }

  private void add(final LinkedList recentEntrues, final String newEntry) {
    final int oldIndex = recentEntrues.indexOf(newEntry);
    if (oldIndex >= 0) {
      recentEntrues.remove(oldIndex);
    }
    else if (recentEntrues.size() == myRecentsNumberToKeep) {
      recentEntrues.removeLast();
    }

    recentEntrues.addFirst(newEntry);
  }

  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);
      LinkedList recents = new LinkedList();
      final List children = keyElement.getChildren(RECENT_ELEMENT_NAME);
      for (Iterator iterator1 = children.iterator(); iterator1.hasNext();) {
        recents.addLast(iterator1.next().getAttributeValue(NAME_ATTR));
      }

      myMap.put(key, recents);
    }
  }

  public Element getState() {
    Element element = new Element("state");
    final Set>> entries = myMap.entrySet();
    for (Map.Entry> entry : entries) {
      final Element keyElement = new Element(KEY_ELEMENT_NAME);
      keyElement.setAttribute(NAME_ATTR, entry.getKey());
      final LinkedList recents = entry.getValue();
      for (String recent : recents) {
        final Element recentElement = new Element(RECENT_ELEMENT_NAME);
        recentElement.setAttribute(NAME_ATTR, recent);
        keyElement.addContent(recentElement);
      }
      element.addContent(keyElement);
    }
    return element;
  }

  public void setRecentsNumberToKeep(final int recentsNumberToKeep) {
    myRecentsNumberToKeep = recentsNumberToKeep;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy