View Javadoc

1   /*
2    * @(#) JMultiToolbarFrame.java 20-jul-2004
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.language.swing;
11  
12  import java.awt.BorderLayout;
13  import java.awt.Container;
14  import java.util.Vector;
15  
16  import javax.swing.JFrame;
17  import javax.swing.JPanel;
18  import javax.swing.JToolBar;
19  import javax.swing.SwingConstants;
20  
21  
22  /***
23   * JMultiToolbarFrame.java <br>
24   * (c) copyright 2003 Based on an example from
25   * http://cbl.fh-hagenberg.at/~aparamyt/ws_02/Assignments/Assignment_2/AdditionalInformation1.html
26   * <a href="http://www.simulation.tudelft.nl">Delft University of Technology
27   * </a>, the Netherlands. <br>
28   * See for project information <a
29   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
30   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
31   * License (GPL) </a>, no warranty <br>
32   * 
33   * @version 1.0 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/webstaf/royc/index.htm">Roy Chin
35   *         </a>
36   */
37  public class JMultiToolbarFrame extends JFrame implements SwingConstants
38  {
39  	/***
40  	 * The pane you can fiddle with -- to which content can be added, of which
41  	 * the layout can be changed, and so on. It's returned by <TT>
42  	 * getContentPane()</TT>.
43  	 */
44  	protected Container currentContentPane;
45  
46  	/***
47  	 * A stack of all the toolbar containers -- those "outside" of <TT>
48  	 * currentContentPane</TT>. The indices match those of <TT>toolbarStack
49  	 * </TT>.
50  	 */
51  	protected Vector containerStack;
52  
53  	/***
54  	 * A stack of all the toolbars. The indices match those of <TT>
55  	 * containerStack</TT>.
56  	 */
57  	protected Vector toolbarStack;
58  
59  	/***
60  	 * The container of <TT>currentContentPane</TT>.
61  	 */
62  	protected Container topContainer;
63  
64  	/***
65  	 * Whether the window is empty (i.e., has only an unedited, blank 'untitled'
66  	 * document in it).
67  	 */
68  	protected boolean isEmpty;
69  
70  	//------------------------------------------------------------
71  	// Constructors
72  	//------------------------------------------------------------
73  
74  	/***
75  	 * Returns a new <TT>JMultiToolbarFrame</TT>.
76  	 */
77  	public JMultiToolbarFrame()
78  	{
79  		this.isEmpty = true;
80  		this.containerStack = new Vector();
81  		this.toolbarStack = new Vector();
82  		this.currentContentPane = new JPanel(new BorderLayout());
83  		this.topContainer = super.getContentPane();
84  		this.topContainer.add(this.currentContentPane, BorderLayout.CENTER);
85  	}
86  
87  	//------------------------------------------------------------
88  	// Public instance methods
89  	//------------------------------------------------------------
90  
91  	/***
92  	 * Returns whether the window is empty.
93  	 * 
94  	 * @return isEmpty
95  	 */
96  	public boolean isEmpty()
97  	{
98  		return this.isEmpty;
99  	}
100 
101 	/***
102 	 * Sets whether the window is empty
103 	 * 
104 	 * @param empty Empty frame or not
105 	 */
106 	public void setEmpty(final boolean empty)
107 	{
108 		this.isEmpty = empty;
109 	}
110 
111 	/***
112 	 * Overrides <TT>JFrame.getContentPane()</TT>. The pane returned should
113 	 * always be "inside" the toolbars.
114 	 * 
115 	 * @return The content pane.
116 	 */
117 	public Container getContentPane()
118 	{
119 		return this.currentContentPane;
120 	}
121 
122 	/***
123 	 * Adds another toolbar to the specified side of the frame. Any attempt to
124 	 * add a toolbar that has already been added is ignored.
125 	 * 
126 	 * @param bar The toolbar to add.
127 	 * @param align One of <TT>SwingConstants.TOP</TT>,<TT>
128 	 *        SwingConstants.BOTTOM</TT>,<TT>SwingConstants.LEFT</TT>, and
129 	 *        <TT>SwingConstants.RIGHT</TT>.
130 	 */
131 	public void addJToolBar(final JToolBar bar, final int align)
132 	{
133 		if (!this.toolbarStack.contains(bar))
134 		{
135 			String border;
136 			int orientation;
137 			switch (align)
138 			{
139 				case TOP :
140 					border = BorderLayout.NORTH;
141 					orientation = HORIZONTAL;
142 					break;
143 				case BOTTOM :
144 					border = BorderLayout.SOUTH;
145 					orientation = HORIZONTAL;
146 					break;
147 				case LEFT :
148 					border = BorderLayout.WEST;
149 					orientation = VERTICAL;
150 					break;
151 				case RIGHT :
152 					border = BorderLayout.EAST;
153 					orientation = VERTICAL;
154 					break;
155 				default :
156 					throw new IllegalArgumentException(
157 							"Alignment argument passed to JMultiToolbarFrame.addJToolBar is not one of SwingConstants.TOP/BOTTOM/LEFT/RIGHT.");
158 			}
159 
160 			/*
161 			 * The current content pane is lifted from the top container and
162 			 * another container, containing the new toolbar, is inserted
163 			 * between them. The new container and new toolbar are added to the
164 			 * respective stacks, and the new container becomes the new top
165 			 * container. Note that the original content pane
166 			 * (super.getContentPane()) does act as a top container, but never
167 			 * contains a toolbar and is never added to the container stack. The
168 			 * current content pane always stays on top.
169 			 */
170 			this.topContainer.remove(this.currentContentPane);
171 			Container newContainer = new JPanel(new BorderLayout());
172 			newContainer.add(bar, border);
173 			newContainer.add(this.currentContentPane, BorderLayout.CENTER);
174 			this.topContainer.add(newContainer);
175 			this.topContainer = newContainer;
176 			this.containerStack.addElement(newContainer);
177 			bar.setOrientation(orientation);
178 			this.toolbarStack.addElement(bar);
179 		}
180 	}
181 
182 	/***
183 	 * Removes the specified toolbar. Any attempt to remove a toolbar that has
184 	 * never been added is ignored.
185 	 * 
186 	 * @param bar Toolbar to remove
187 	 */
188 	public void removeJToolBar(final JToolBar bar)
189 	{
190 		if (this.toolbarStack.contains(bar))
191 		{
192 			Container current;
193 			Container above;
194 			Container below;
195 
196 			int position = this.toolbarStack.indexOf(bar);
197 			if (position != 0)
198 			{
199 				below = (Container) this.containerStack.elementAt(position - 1);
200 			} else
201 			{
202 				below = super.getContentPane();
203 			}
204 
205 			if (position != (this.containerStack.size() - 1))
206 			{
207 				above = (Container) this.containerStack.elementAt(position + 1);
208 			} else
209 			{
210 				above = this.currentContentPane;
211 			}
212 
213 			current = (Container) this.containerStack.elementAt(position);
214 			below.remove(current);
215 			current.remove(above);
216 			below.add(above, BorderLayout.CENTER);
217 		}
218 	}
219 }