Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011 JBoss Inc
   *
   * 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 org.drools.guvnor.client.explorer;
 
 import java.util.Map;
 import java.util.Set;
 
 
 public class MultiActivityManager implements
         PlaceChangeEvent.Handler,
         ClosePlaceEvent.Handler,
         CloseAllPlacesEvent.Handler {
 
     private final ActivityMapper activityMapper;
     private TabbedPanel tabbedPanel;
     private final EventBus eventBus;
     private final Map<PlacePairactiveActivities = new HashMap<PlacePair>();
 
     public MultiActivityManager(EventBus eventBusActivityMapper activityMapper) {
         this. = activityMapper;
         this. = eventBus;
 
         eventBus.addHandler(
                 .,
                 this);
         eventBus.addHandler(
                 .,
                 this);
         eventBus.addHandler(
                 .,
                 this);
     }
 
     public void setTabbedPanel(TabbedPanel tabbedPanel) {
         if (this. == null) {
             this. = tabbedPanel;
         } else {
             throw new IllegalStateException(TabbedPanel.class.getName() + " can only be set once.");
         }
     }
 
     public void onPlaceChange(PlaceChangeEvent event) {
 
         if ( == null) {
             throw new IllegalStateException(TabbedPanel.class.getName() + " is not set for " + MultiActivityManager.class.getName());
         } else {
             if (isActivityAlreadyActive(event.getNewPlace())) {
                 showExistingActivity(event.getNewPlace());
             } else if (ifPlaceExists(event)) {
                 startNewActivity(event.getNewPlace());
             }
         }
     }
 
     private void showExistingActivity(Place token) {
         .show(token);
     }
 
     private boolean isActivityAlreadyActive(Place token) {
         return .keySet().contains(token);
     }
 
     private void startNewActivity(final Place newPlace) {
         Activity activity = .getActivity(newPlace);
 
         final ResettableEventBus resettableEventBus = new ResettableEventBus();
 
         .put(newPlacenew Pair(activityresettableEventBus));
 
         activity.start(
                 new AcceptItem() {
                     public void add(String tabTitleIsWidget widget) {
                         .addTab(
                                 tabTitle,
                                widget,
                                newPlace);
                    }
                },
                resettableEventBus);
    }
    private boolean ifPlaceExists(PlaceChangeEvent event) {
        return !event.getNewPlace().equals(.);
    }
    public void onClosePlace(ClosePlaceEvent closePlaceEvent) {
        Pair pair = .get(closePlaceEvent.getPlace());
        if (pair != null && pair.getActivity().mayStop()) {
            pair.getActivity().onStop();
            pair.getResettableEventBus().removeHandlers();
            .remove(closePlaceEvent.getPlace());
            .close(closePlaceEvent.getPlace());
        }
    }
    public void onCloseAllPlaces(CloseAllPlacesEvent event) {
        Set<Placeplaces = new HashSet<Place>();
        places.addAll(.keySet());
        for (Place place : places) {
            .fireEvent(new ClosePlaceEvent(place));
        }
    }
    private class Pair {
        private Activity activity;
        private ResettableEventBus resettableEventBus;
        public Pair(Activity activityResettableEventBus resettableEventBus) {
            this. = activity;
            this. = resettableEventBus;
        }
        public Activity getActivity() {
            return ;
        }
        public ResettableEventBus getResettableEventBus() {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X