001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.application.support;
017
018 import java.util.HashMap;
019 import java.util.Map;
020
021 import org.springframework.richclient.application.ApplicationPage;
022 import org.springframework.richclient.application.ApplicationWindow;
023 import org.springframework.richclient.application.PageComponentPane;
024 import org.springframework.richclient.application.ViewContext;
025 import org.springframework.richclient.command.ActionCommandExecutor;
026 import org.springframework.util.Assert;
027
028 /**
029 * Mediator between the application and the view. The application uses this
030 * class to get the view's local action handlers. The view uses this class to
031 * get information about how the view is displayed in the application (for
032 * example, on which window.)
033 *
034 * @author Keith Donald
035 */
036 public class DefaultViewContext implements ViewContext {
037
038 private PageComponentPane pane;
039
040 private ApplicationPage page;
041
042 private Map sharedCommandExecutors;
043
044 public DefaultViewContext(ApplicationPage page, PageComponentPane pane) {
045 Assert.notNull(page, "Views must be scoped relative to a page");
046 this.page = page;
047 this.pane = pane;
048 }
049
050 public ApplicationWindow getWindow() {
051 return page.getWindow();
052 }
053
054 public ApplicationPage getPage() {
055 return page;
056 }
057
058 public PageComponentPane getPane() {
059 return pane;
060 }
061
062 public ActionCommandExecutor getLocalCommandExecutor(String commandId) {
063 Assert.notNull(commandId, "The commandId is required");
064 if (this.sharedCommandExecutors == null) {
065 return null;
066 }
067 return (ActionCommandExecutor)this.sharedCommandExecutors.get(commandId);
068 }
069
070 public void register(String commandId, ActionCommandExecutor executor) {
071 Assert.notNull(commandId, "The command id is required");
072 if (this.sharedCommandExecutors == null) {
073 this.sharedCommandExecutors = new HashMap();
074 }
075 if (executor == null) {
076 this.sharedCommandExecutors.remove(commandId);
077 } else {
078 this.sharedCommandExecutors.put(commandId, executor);
079 }
080 }
081 }