Skip to content

Commit

Permalink
Fix displaying IDE (#6040)
Browse files Browse the repository at this point in the history
  • Loading branch information
azatsarynnyy committed Aug 18, 2017
1 parent b2d1dd7 commit 9d45a9a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 deletions.
Expand Up @@ -18,6 +18,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.gwt.user.client.Timer;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

Expand All @@ -32,12 +33,7 @@
import org.eclipse.che.ide.api.workspace.WorkspaceReadyEvent;
import org.eclipse.che.ide.util.loging.Log;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.toList;

/**
* Responsible for persisting and restoring IDE state across sessions.
Expand All @@ -55,10 +51,7 @@ public class AppStateManager {

private static final String WORKSPACE = "workspace";

/**
* Sorted by execution priority list of persistence state components.
*/
private final List<StateComponent> persistenceComponents;
private final Provider<StateComponentRegistry> stateComponentRegistry;

private final PreferencesManager preferencesManager;
private final JsonFactory jsonFactory;
Expand All @@ -67,15 +60,13 @@ public class AppStateManager {
private JsonObject allWsState;

@Inject
public AppStateManager(Set<StateComponent> persistenceComponents,
public AppStateManager(Provider<StateComponentRegistry> stateComponentRegistryProvider,
PreferencesManager preferencesManager,
JsonFactory jsonFactory,
PromiseProvider promises,
EventBus eventBus,
AppContext appContext) {
this.persistenceComponents = persistenceComponents.stream()
.sorted(comparingInt(StateComponent::getPriority).reversed())
.collect(toList());
this.stateComponentRegistry = stateComponentRegistryProvider;
this.preferencesManager = preferencesManager;
this.jsonFactory = jsonFactory;
this.promises = promises;
Expand Down Expand Up @@ -137,9 +128,7 @@ private void restoreState(JsonObject settings) {
JsonObject workspace = settings.getObject(WORKSPACE);
Promise<Void> sequentialRestore = promises.resolve(null);
for (String key : workspace.keys()) {
Optional<StateComponent> stateComponent = persistenceComponents.stream()
.filter(component -> component.getId().equals(key))
.findAny();
Optional<StateComponent> stateComponent = stateComponentRegistry.get().getComponentById(key);
if (stateComponent.isPresent()) {
StateComponent component = stateComponent.get();
Log.debug(getClass(), "Restore state for the component ID: " + component.getId());
Expand All @@ -158,7 +147,7 @@ public Promise<Void> persistWorkspaceState() {
JsonObject workspace = Json.createObject();
settings.put(WORKSPACE, workspace);

for (StateComponent entry : persistenceComponents) {
for (StateComponent entry : stateComponentRegistry.get().getComponents()) {
try {
Log.debug(getClass(), "Persist state for the component ID: " + entry.getId());
workspace.put(entry.getId(), entry.getState());
Expand Down
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2012-2017 Red Hat, Inc.
* 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:
* Red Hat, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.statepersistance;

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

import org.eclipse.che.ide.api.statepersistance.StateComponent;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.toList;

@Singleton
class StateComponentRegistry {

private final Set<StateComponent> persistenceComponents;

@Inject
StateComponentRegistry(Set<StateComponent> persistenceComponents) {
this.persistenceComponents = persistenceComponents;
}

/** Returns list of persistence state components sorted by execution priority. */
List<StateComponent> getComponents() {
return persistenceComponents.stream()
.sorted(comparingInt(StateComponent::getPriority).reversed())
.collect(toList());
}

Optional<StateComponent> getComponentById(String id) {
return persistenceComponents.stream()
.filter(component -> component.getId().equals(id))
.findAny();
}
}
Expand Up @@ -32,8 +32,9 @@
import org.mockito.Captor;
import org.mockito.Mock;

import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.any;
Expand All @@ -55,6 +56,10 @@ public class AppStateManagerTest {

private static final String WS_ID = "ws_id";

@Mock
private StateComponentRegistry stateComponentRegistry;
@Mock
private Provider<StateComponentRegistry> stateComponentRegistryProvider;
@Mock
private StateComponent component1;
@Mock
Expand All @@ -66,8 +71,6 @@ public class AppStateManagerTest {
@Mock
private Promise<Void> promise;
@Mock
private Promise<String> contentPromise;
@Mock
private PreferencesManager preferencesManager;
@Mock
private JsonFactory jsonFactory;
Expand Down Expand Up @@ -100,24 +103,29 @@ public void setUp() {
when(workspace.getId()).thenReturn(WS_ID);
when(appContext.getWorkspace()).thenReturn(workspace);

// Map<String, Provider<StateComponent>> components = new HashMap<>();
// components.put("component1", component1Provider);
// components.put("component2", component2Provider);
List<StateComponent> components = new ArrayList<>();
components.add(component1);
components.add(component2);

when(stateComponentRegistry.getComponents()).thenReturn(components);
when(stateComponentRegistry.getComponentById(anyString())).thenReturn(Optional.of(component1));
when(stateComponentRegistryProvider.get()).thenReturn(stateComponentRegistry);

when(component1Provider.get()).thenReturn(component1);
when(component2Provider.get()).thenReturn(component2);


Set<StateComponent> components = new HashSet<>();
components.add(component1);
components.add(component2);

when(component1.getId()).thenReturn("component1");
when(component2.getId()).thenReturn("component2");
when(preferencesManager.flushPreferences()).thenReturn(promise);
when(preferencesManager.getValue(AppStateManager.PREFERENCE_PROPERTY_NAME)).thenReturn("");
when(jsonFactory.parse(anyString())).thenReturn(pref = Json.createObject());
appStateManager = new AppStateManager(components, preferencesManager, jsonFactory, promiseProvider, eventBus, appContext);
appStateManager = new AppStateManager(stateComponentRegistryProvider,
preferencesManager,
jsonFactory,
promiseProvider,
eventBus,
appContext);
appStateManager.readStateFromPreferences();
}

Expand Down

0 comments on commit 9d45a9a

Please sign in to comment.