View Javadoc

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