001 /* 002 * Copyright 2002-2006 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.samples.simple.ui; 017 018 import java.awt.BorderLayout; 019 import java.io.IOException; 020 021 import javax.swing.BorderFactory; 022 import javax.swing.JComponent; 023 import javax.swing.JLabel; 024 import javax.swing.JPanel; 025 import javax.swing.JScrollPane; 026 import javax.swing.JTextPane; 027 028 import org.springframework.core.io.Resource; 029 import org.springframework.richclient.application.support.AbstractView; 030 import org.springframework.richclient.application.support.DefaultViewDescriptor; 031 032 /** 033 * This class defines the initial view to be presented in the sample application. It is constructed automatically by the 034 * platform and configured according to the bean specification in the application context. Here's an example 035 * configuration: 036 * 037 * <pre> 038 * <bean id="initialView" 039 * class="org.springframework.richclient.application.support.DefaultViewDescriptor"> 040 * <property name="viewClass"> 041 * <value>org.springframework.richclient.samples.simple.ui.InitialView</value> 042 * </property> 043 * <property name="viewProperties"> 044 * <map> 045 * <entry key="firstMessage"> 046 * <value>This is the first message!</value> 047 * </entry> 048 * <entry key="descriptionTextPath"> 049 * <value>org/springframework/richclient/samples/simple/ui/initialViewText.html</value> 050 * </entry> 051 * </map> 052 * </property> 053 * </bean> 054 * </pre> 055 * 056 * Note that the configuration specifies the properties to be set on this class indirectly. The property set on the 057 * {@link DefaultViewDescriptor} is called <code>viewProperties</code> and it takes a map of key/value pairs. Each key 058 * is the name of a property to be set on the actual view class (this class) and the value is the value to set for that 059 * property. So, two properties have been configured, <code>firstMessage</code> and <code>descriptionTextPath</code>. 060 * The <code>firstMessage</code> value specifies the key of a message to be displayed and the 061 * <code>descriptionTextPath</code> specifies the path to a file containing the text to place in the HTML panel that 062 * makes up the main body of this view. 063 * @author Larry Streepy 064 */ 065 public class InitialView extends AbstractView { 066 067 private String firstMessage; 068 069 private Resource descriptionTextPath; 070 071 /** 072 * @return the firstMessage 073 */ 074 public String getFirstMessage() { 075 return firstMessage; 076 } 077 078 /** 079 * Set the key to the message to be displayed first in the view 080 * @param firstMessage the firstMessage to set 081 */ 082 public void setFirstMessage(String firstMessage) { 083 this.firstMessage = firstMessage; 084 } 085 086 /** 087 * @return the descriptionTextPath 088 */ 089 public Resource getDescriptionTextPath() { 090 return descriptionTextPath; 091 } 092 093 /** 094 * Set the resource that references the file containing the description text to place in the description areas of 095 * this view. Note that even though this property is of type Resource, the Spring platform will automatically 096 * convert a string path into a resource. 097 * @param descriptionTextPath the descriptionTextPath to set 098 */ 099 public void setDescriptionTextPath(Resource descriptionTextPath) { 100 this.descriptionTextPath = descriptionTextPath; 101 } 102 103 /** 104 * Create the actual UI control for this view. It will be placed into the window according to the layout of the page 105 * holding this view. 106 */ 107 protected JComponent createControl() { 108 // In this view, we're just going to use standard Swing to place a 109 // few controls. 110 111 // The location of the text to display has been set as a Resource in the 112 // property descriptionTextPath. So, use that resource to obtain a URL 113 // and set that as the page for the text pane. 114 115 JTextPane textPane = new JTextPane(); 116 JScrollPane spDescription = getComponentFactory().createScrollPane(textPane); 117 try { 118 textPane.setPage(getDescriptionTextPath().getURL()); 119 } 120 catch (IOException e) { 121 throw new RuntimeException("Unable to load description URL", e); 122 } 123 124 JLabel lblMessage = getComponentFactory().createLabel(getFirstMessage()); 125 lblMessage.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0)); 126 127 JPanel panel = getComponentFactory().createPanel(new BorderLayout()); 128 panel.add(spDescription); 129 panel.add(lblMessage, BorderLayout.SOUTH); 130 131 return panel; 132 } 133 }