001    /*
002     * 
003     * $Revision: 1.4 $ $Date: 2009/03/04 11:49:12 $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.frontend.metsmods;
025    
026    import java.awt.Rectangle;
027    import java.util.ArrayList;
028    
029    import javax.swing.JPanel;
030    
031    
032    /**
033     * This class is part of the MetsMods module. It's a representation of a container for MCRMetsModsPictureItems.
034     * 
035     * @author Stefan Freitag (sasf)
036     * 
037     * @version $Revision: 1.4 $ $Date: 2009/03/04 11:49:12 $
038     */
039    
040    public class MCRMetsModsPictureContainer extends JPanel{
041    
042            private static final long serialVersionUID = 3355852105017958072L;
043    
044            ArrayList<MCRMetsModsPictureItem> itemlist = new ArrayList<MCRMetsModsPictureItem>();
045                    
046            int itemheight = 20;
047            int itemspace = 5;
048            
049            JPanel header = new JPanel();
050            
051            public MCRMetsModsPictureContainer()
052            {
053                    init();
054            }
055            
056            private void init()
057            {
058                    this.setLayout(null);
059            }
060            
061            public void addItem(MCRMetsModsPictureItem mcrmmpi)
062            {
063                    itemlist.add(mcrmmpi);
064                    refresh(null);
065            }
066    
067            public MCRMetsModsPictureItem searchItemByOrder(int order)
068            {
069                    for(int i=0;i<itemlist.size();i++)
070                    {
071                            MCRMetsModsPictureItem item = itemlist.get(i);
072                            if(item.getOrderValue()==order) return item;
073                    }
074                    
075                    return null;
076            }
077            
078            public void changeItem(MCRMetsModsPictureItem mcrmmpi, boolean direction)
079            {
080                    if(direction)   //means once up (+1)
081                    {
082                            int index = mcrmmpi.getOrderValue();
083                            
084                            if(index==itemlist.size()) return;
085                            
086                            MCRMetsModsPictureItem old_item = searchItemByOrder(index);
087                            MCRMetsModsPictureItem new_item = searchItemByOrder(index+1);
088                            old_item.getMetsModsPicture().setOrder(index+1);
089                            new_item.getMetsModsPicture().setOrder(index);
090                                                                            
091                            old_item.renew();
092                            new_item.renew();
093                            
094                            int lindex1 = itemlist.indexOf(old_item);
095                            int lindex2 = itemlist.indexOf(new_item);
096                            
097                            itemlist.set(lindex1, new_item);
098                            itemlist.set(lindex2, old_item);
099                            
100                    }
101                    else                    //means once down (-1)
102                    {
103                            int index = mcrmmpi.getOrderValue();
104                            
105                            if(index==1) return;
106                            
107                            MCRMetsModsPictureItem old_item = searchItemByOrder(index);
108                            MCRMetsModsPictureItem new_item = searchItemByOrder(index-1);
109                            old_item.getMetsModsPicture().setOrder(index-1);
110                            new_item.getMetsModsPicture().setOrder(index);
111                                                                            
112                            old_item.renew();
113                            new_item.renew();
114                            
115                            int lindex1 = itemlist.indexOf(old_item);
116                            int lindex2 = itemlist.indexOf(new_item);
117                            
118                            itemlist.set(lindex1, new_item);
119                            itemlist.set(lindex2, old_item);
120                    }
121                    Rectangle rect = this.getVisibleRect();
122                    refresh(rect);
123    
124            }
125            
126            public void changeOrderLabel(MCRMetsModsPictureItem mcrmmpi, String newlabel)
127            {
128                    int index = mcrmmpi.getOrderValue();
129                    
130                    MCRMetsModsPictureItem item = searchItemByOrder(index);
131                    item.getMetsModsPicture().setOrderlabel(newlabel);
132                    
133            }
134            
135            public void refresh(Rectangle pos)
136            {                       
137                    int y = 30;
138                    int x = 10;
139                    
140                    int w = 0;
141                    for(int i=0;i<itemlist.size();i++)
142                    {
143                            MCRMetsModsPictureItem item = itemlist.get(i);
144                            item.setContainer(this);
145                            
146                            if(item.getWidth()>w) w=item.getWidth();
147                            
148                            item.setLocation(x,y);
149                            y += (itemheight+itemspace);
150                            
151                            this.add(item);
152                    }
153                    
154                    this.setBounds(0,0,w,y);
155                    
156                    if(pos!=null)
157                    this.scrollRectToVisible(pos);
158            }
159    
160    }