View Javadoc

1   /*
2    * @(#) DSOLFrame.java Oct 13, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.dsol.gui.windows;
11  
12  import java.awt.BorderLayout;
13  import java.awt.Color;
14  import java.awt.Component;
15  import java.awt.Dimension;
16  import java.awt.Toolkit;
17  import java.io.IOException;
18  import java.net.URL;
19  
20  import javax.swing.BorderFactory;
21  import javax.swing.JEditorPane;
22  import javax.swing.JFrame;
23  import javax.swing.JMenuBar;
24  import javax.swing.JPanel;
25  import javax.swing.JScrollPane;
26  import javax.swing.JTree;
27  import javax.swing.SwingConstants;
28  import javax.swing.UIManager;
29  import javax.swing.WindowConstants;
30  
31  import nl.tudelft.simulation.dsol.experiment.Experiment;
32  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
33  import nl.tudelft.simulation.dsol.gui.UIInitializer;
34  import nl.tudelft.simulation.dsol.gui.menu.FileMenu;
35  import nl.tudelft.simulation.dsol.gui.menu.HelpMenu;
36  import nl.tudelft.simulation.dsol.gui.menu.ToolsMenu;
37  import nl.tudelft.simulation.dsol.gui.menu.WindowMenu;
38  import nl.tudelft.simulation.dsol.gui.panels.ControlPanel;
39  import nl.tudelft.simulation.dsol.gui.panels.ExperimentTree;
40  import nl.tudelft.simulation.dsol.gui.panels.Statusbar;
41  import nl.tudelft.simulation.dsol.gui.panels.TabbedPane;
42  import nl.tudelft.simulation.event.EventInterface;
43  import nl.tudelft.simulation.event.EventListenerInterface;
44  import nl.tudelft.simulation.language.io.URLResource;
45  import nl.tudelft.simulation.logger.Logger;
46  
47  /***
48   * DSOL Frame <br>
49   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
50   * University of Technology </a>, the Netherlands. <br>
51   * See for project information <a
52   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
53   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
54   * License (GPL) </a>, no warranty <br>
55   * 
56   * @version 1.0 18.10.2003 <br>
57   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
58   *         Jacobs </a>
59   */
60  public class DSOLFrame extends JFrame implements EventListenerInterface
61  {
62  	/*** the preferredSize is the preferredSize of the panel */
63  	public static final Dimension PREFERRED_SIZE = new Dimension(400, 375);
64  
65  	/*** the application */
66  	protected DSOLApplicationInterface application = null;
67  
68  	/*** tabbedPanel */
69  	protected TabbedPane tabbedPanel = null;
70  
71  	/*** the experimentTree */
72  	protected JTree experimentTree = null;
73  
74  	/*** the statusbar */
75  	protected Statusbar statusbar = null;
76  
77  	/*** the controlPanel */
78  	protected ControlPanel controlPanel = null;
79  
80  	/***
81  	 * constructs a new DSOLFrame
82  	 * 
83  	 * @param application the application
84  	 */
85  	public DSOLFrame(final DSOLApplicationInterface application)
86  	{
87  		super("DSOL main window");
88  
89  		try
90  		{
91  			UIManager
92  					.setLookAndFeel("com.jgoodies.plaf.plastic.Plastic3DLookAndFeel");
93  
94  			//The custom DSOL Color Look&Feel
95  			UIInitializer.initialize();
96  			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
97  			this.setLocation((int) Math.round(screenSize.width * 0.5 - 0.5
98  					* PREFERRED_SIZE.width), (int) Math.round(screenSize.height
99  					* 0.33 - 0.5 * PREFERRED_SIZE.height));
100 			this.application = application;
101 			this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
102 			this.application.addListener(this,
103 					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
104 		} catch (Exception exception)
105 		{
106 			Logger.warning(this, "DSOLFrame", exception);
107 		}
108 		this.initialize();
109 		this.pack();
110 		this.setVisible(true);
111 	}
112 
113 	/***
114 	 * initializes the DSOL Frame
115 	 */
116 	private void initialize()
117 	{
118 		JPanel contentPane = new JPanel();
119 		contentPane.setPreferredSize(PREFERRED_SIZE);
120 		contentPane.setLayout(new BorderLayout());
121 		this.setContentPane(contentPane);
122 
123 		JMenuBar menuBar = new JMenuBar();
124 		menuBar.setBorder(BorderFactory.createLineBorder(Color.GRAY));
125 
126 		menuBar.add(new FileMenu(this.application));
127 		menuBar.add(new ToolsMenu(this.application));
128 		menuBar.add(new WindowMenu(this.application));
129 		menuBar.add(new HelpMenu(this.application));
130 		this.setJMenuBar(menuBar);
131 
132 		this.controlPanel = new ControlPanel(this.application);
133 
134 		this.tabbedPanel = new TabbedPane(SwingConstants.TOP);
135 		this.tabbedPanel.setBorder(BorderFactory.createCompoundBorder(
136 				BorderFactory.createEtchedBorder(), BorderFactory
137 						.createEmptyBorder()));
138 		contentPane.add(this.tabbedPanel, BorderLayout.CENTER);
139 		this.statusbar = new Statusbar(this.application);
140 
141 		this.experimentTree = new JTree();
142 		this.experimentTree.setEditable(false);
143 		this.experimentTree.setFocusable(false);
144 
145 		URL about = URLResource.getResource("/html/about.html");
146 		JEditorPane aboutPane = null;
147 		try
148 		{
149 			aboutPane = new JEditorPane(about);
150 		} catch (IOException exception)
151 		{
152 			Logger.warning(this, "initialize", exception);
153 		}
154 		aboutPane.setAlignmentY(Component.BOTTOM_ALIGNMENT);
155 		aboutPane.setEditable(false);
156 		this.tabbedPanel.add("About", aboutPane);
157 	}
158 
159 	/***
160 	 * @see nl.tudelft.simulation.event.EventListenerInterface
161 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
162 	 */
163 	public void notify(final EventInterface event)
164 	{
165 		if (event.getSource().equals(this.application)
166 				&& event.getType().equals(
167 						DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT))
168 		{
169 			this.tabbedPanel.remove("Description");
170 			this.tabbedPanel.remove("Experiment");
171 			this.tabbedPanel.remove("Control");
172 			if (event.getContent() != null)
173 			{
174 				Experiment experiment = (Experiment) event.getContent();
175 				try
176 				{
177 					String descriptionName = "/";
178 					if (experiment.getModel().getClass().getPackage() != null)
179 					{
180 						descriptionName = descriptionName
181 								+ experiment.getModel().getClass().getPackage()
182 										.getName().replace('.', '/');
183 					}
184 					descriptionName = descriptionName + "/package.html";
185 					URL description = experiment.getModel().getClass()
186 							.getResource(descriptionName);
187 					JEditorPane descriptionPane = null;
188 					descriptionPane = new JEditorPane(description);
189 					descriptionPane.setAlignmentY(Component.BOTTOM_ALIGNMENT);
190 					descriptionPane.setEditable(false);
191 					this.tabbedPanel.add("Description", 0, new JScrollPane(
192 							descriptionPane));
193 				} catch (IOException exception)
194 				{
195 					Logger
196 							.fine(this, "initialize",
197 									"experiment has no package.html describing the experiment");
198 				}
199 				this.setTitle("DSOL main window ("
200 						+ experiment.getProperty(Experiment.EXPERIMENT_NAME)
201 						+ ")");
202 				this.experimentTree.setModel(new ExperimentTree(experiment));
203 				this.tabbedPanel.add("Experiment", 1, new JScrollPane(
204 						this.experimentTree));
205 				this.tabbedPanel.add("Control", 2, this.controlPanel);
206 				this.getContentPane().add(this.statusbar, BorderLayout.SOUTH);
207 			} else
208 			{
209 				this.setTitle("DSOL main window");
210 				this.getContentPane().remove(this.statusbar);
211 			}
212 			this.validate();
213 			this.repaint();
214 		}
215 	}
216 }