View Javadoc

1   package com.explosion.expf.menusandtools.tool;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  import java.util.Vector;
23  
24  import javax.swing.JButton;
25  
26  /***
27   * Title:
28   * Description:
29   * @author Stephen Cowx
30   */
31  
32  public class ExpToolBarSegment
33  {
34    public static final int ALWAYS_FIRST_SEGMENT = 0;
35    public static final int ALWAYS_LAST_SEGMENT = 1;
36    public static final int ANY_POSITION = 2;
37  
38    private int startIndex = 0;
39    private int numberOfElements = 0;
40    private Vector toolBarElements;
41    private ExpToolBar toolBar;
42    private int relativePositionOfSegmentOnToolBar = 0;
43    private boolean hasSeparator = false;
44  
45    protected ExpToolBarSegment(int startIndex, ExpToolBar toolBar, Vector toolBarElements, int relativePositionOfSegmentOnToolBar)
46    {
47      this.startIndex = startIndex;
48      this.toolBar = toolBar;
49      this.toolBarElements = toolBarElements;
50      this.relativePositionOfSegmentOnToolBar = relativePositionOfSegmentOnToolBar;
51    }
52  
53    public int getStartIndex()
54    {
55      return startIndex;
56    }
57  
58    public int getRelativePositionOfSegmentOnMenu()
59    {
60      return relativePositionOfSegmentOnToolBar;
61    }
62  
63    /***
64     * This method adds an Item to a menu segment.  Using a null item will insert a separator
65     */
66    public void addElement(JButton item)
67    {
68      int numberAdded = 0;
69  
70      if (toolBarElements.size() == 0)
71      {
72        /* Just add the menu item */
73        if (item == null)
74          toolBarElements.addElement("Separator");
75        else
76          toolBarElements.addElement(item);
77  
78        numberOfElements++;
79        numberAdded++;
80      }
81      else
82      {
83        if (item != null)
84          toolBarElements.insertElementAt(item, startIndex + numberOfElements);
85        else
86          toolBarElements.insertElementAt("Separator", startIndex + numberOfElements);
87  
88        numberOfElements++;
89        numberAdded++;
90      }
91  
92      // notify the parent
93      ((ExpToolBar) toolBar).updateStartPositions(this, numberAdded);
94    }
95  
96    public void removeElement(JButton item) throws Exception
97    {
98      int numberRemoved = 0;
99  
100     if (numberOfElements != 0)
101     {
102       toolBarElements.remove(item);
103       numberOfElements--;
104       numberRemoved--;
105     }
106     else
107       throw new ToolBarException("Unable to remove element from ExpToolBarSegment because there are no elements in the segment.");
108 
109     // notify the parent
110     ((ExpToolBar) toolBar).updateStartPositions(this, numberRemoved);
111   }
112 
113   public int getSegmentSize()
114   {
115     return numberOfElements;
116   }
117 
118   public void modifyStartPos(int number)
119   {
120     this.startIndex += number;
121   }
122 
123 }