View Javadoc

1   /*
2    * @(#) ShipmentContentPanel.java Oct 28, 2004
3    * 
4    * 
5    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
6    * Delft, the Netherlands. All rights reserved.
7    * 
8    * See for project information <a href="http://www.simulation.tudelft.nl/">
9    * www.simulation.tudelft.nl </a>.
10   * 
11   * The source code and binary code of this software is proprietary information
12   * of Delft University of Technology.
13   */
14  package org.gscg.gameleader.dialogs.content.components;
15  
16  import info.clearthought.layout.TableLayout;
17  import info.clearthought.layout.TableLayoutConstants;
18  
19  import java.awt.Color;
20  import java.awt.Component;
21  import java.io.IOException;
22  import java.net.URL;
23  
24  import javax.swing.JEditorPane;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.ScrollPaneConstants;
29  import javax.swing.SwingConstants;
30  
31  import nl.tudelft.simulation.language.io.URLResource;
32  import nl.tudelft.simulation.logger.Logger;
33  import nl.tudelft.simulation.supplychain.content.Shipment;
34  
35  import org.gscg.gameleader.dialogs.components.AbstractPanel;
36  
37  /***
38   * The ShipmentContentPanel is used to display the contents of a shipment.
39   * <p>
40   * 
41   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
42   * Delft, the Netherlands. All rights reserved.
43   * 
44   * See for project information <a href="http://www.simulation.tudelft.nl/">
45   * www.simulation.tudelft.nl </a>.
46   * 
47   * The source code and binary code of this software is proprietary information
48   * of Delft University of Technology.
49   * 
50   * @author <a
51   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
52   *         van Houten </a>
53   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:57 $
54   * @since 1.0.3
55   */
56  public class ShipmentContentPanel extends AbstractPanel
57  {
58  	/*** the serial version uid */
59  	private static final long serialVersionUID = 11L;
60  
61  	/*** the content */
62  	private Shipment content = null;
63  
64  	/***
65  	 * constructs a new ShipmentContentPanel
66  	 * 
67  	 * @param content the content
68  	 */
69  	public ShipmentContentPanel(final Object content)
70  	{
71  		super("shipment");
72  
73  		if (Shipment.class.isAssignableFrom(content.getClass()))
74  		{
75  			this.content = (Shipment) content;
76  		}
77  		if (this.content != null)
78  		{
79  			this.initialize();
80  		} else
81  		{
82  			Logger.severe(this, "<init>",
83  					"Could not extract content from received content of class: "
84  							+ content.getClass());
85  		}
86  	}
87  
88  	/***
89  	 * @see org.gscg.gameleader.dialogs.components.AbstractPanel#initialize()
90  	 */
91  	protected void initialize()
92  	{
93  		JPanel labelPanel = new JPanel();
94  		// col1, row1, col2, row2, hAlign, vAlign
95  		double[][] nameLabelPanelLayout = {
96  				{TableLayoutConstants.FILL, TableLayoutConstants.FILL},
97  				{20, 20, 20, 20, 20, 20}};
98  		labelPanel.setLayout(new TableLayout(nameLabelPanelLayout));
99  		labelPanel.setBackground(Color.WHITE);
100 
101 		JLabel senderLabel = super.getFormattedLabel("sender name");
102 		labelPanel.add(senderLabel, "0,0,L,C");
103 		JLabel senderNameLabel = super.getFormattedLabel(this.content
104 				.getSender().getName());
105 		senderNameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
106 		labelPanel.add(senderNameLabel, "1,0,R,C");
107 
108 		JLabel receiverLabel = super.getFormattedLabel("receiver name");
109 		labelPanel.add(receiverLabel, "0,1,L,C");
110 		JLabel receiverNameLabel = super.getFormattedLabel(this.content
111 				.getReceiver().getName());
112 		receiverNameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
113 		labelPanel.add(receiverNameLabel, "1,1,R,C");
114 
115 		JLabel productLabel = super.getFormattedLabel("product name");
116 		labelPanel.add(productLabel, "0,2,L,C");
117 		JLabel productNameLabel = super.getFormattedLabel(this.content
118 				.getProduct().getName());
119 		productNameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
120 		labelPanel.add(productNameLabel, "1,2,R,C");
121 
122 		JLabel statusLabel = super.getFormattedLabel("amount");
123 		labelPanel.add(statusLabel, "0,3,L,C");
124 		JLabel statusNameLabel = super.getFormattedLabel(this.content
125 				.getCargo().getAmount()
126 				+ "");
127 		statusNameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
128 		labelPanel.add(statusNameLabel, "1,3,R,C");
129 
130 		// the information about the content
131 		URL description = URLResource.getResource("/"
132 				+ this.content.getClass().getName().substring(
133 						this.content.getClass().getName().lastIndexOf(".") + 1)
134 				+ ".html");
135 		JEditorPane descriptionPane = null;
136 		try
137 		{
138 			descriptionPane = new JEditorPane(description);
139 		} catch (IOException exception)
140 		{
141 			description = URLResource.getResource("/general_info.html");
142 			try
143 			{
144 				descriptionPane = new JEditorPane(description);
145 			} catch (IOException exception2)
146 			{
147 				Logger.warning(this, "initialize", exception2);
148 				descriptionPane = new JEditorPane();
149 			}
150 		}
151 		descriptionPane.setAlignmentY(Component.CENTER_ALIGNMENT);
152 		descriptionPane.setAlignmentX(Component.TOP_ALIGNMENT);
153 		descriptionPane.setEditable(false);
154 		descriptionPane.setFocusable(false);
155 		descriptionPane.setBackground(Color.WHITE);
156 		descriptionPane.setVisible(true);
157 
158 		JScrollPane scrollPane = new JScrollPane(descriptionPane);
159 		scrollPane
160 				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
161 		scrollPane
162 				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
163 
164 		// setup the panel
165 		double[][] panelLayout = {{TableLayoutConstants.FILL},
166 				{80, TableLayoutConstants.FILL}};
167 		super.setLayout(new TableLayout(panelLayout));
168 
169 		// add the components
170 		super.add(labelPanel, "0,0,L,C");
171 		super.add(scrollPane, "0,1,L,T");
172 
173 		// we need this to make sure the description pane
174 		// is visible at the first time a dialog is opened
175 		super.validate();
176 		super.doLayout();
177 	}
178 }