View Javadoc

1   /***
2    * Copyright (c) 2003 held jointly by the individual authors.            
3    *                                                                          
4    * This library is free software; you can redistribute it and/or modify it    
5    * under the terms of the GNU Lesser General Public License as published      
6    * by the Free Software Foundation; either version 2.1 of the License, or 
7    * (at your option) any later version.                                            
8    *                                                                            
9    * This library is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; with out even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12   * GNU Lesser General Public License for more details.                                                  
13   *                                                                           
14   * You should have received a copy of the GNU Lesser General Public License   
15   * along with this library;  if not, write to the Free Software Foundation,   
16   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.              
17   *                                                                            
18   * > http://www.gnu.org/copyleft/lesser.html                                  
19   * > http://www.opensource.org/licenses/lgpl-license.php
20   */
21  package net.mlw.vlh.swing.support;
22  
23  import java.awt.GridBagConstraints;
24  import java.awt.GridBagLayout;
25  import java.awt.Insets;
26  import java.awt.event.ActionListener;
27  import java.text.MessageFormat;
28  import java.util.ResourceBundle;
29  
30  import javax.swing.JButton;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.SwingConstants;
34  
35  import net.mlw.vlh.PagingInfo;
36  import net.mlw.vlh.swing.PagingComponent;
37  import net.mlw.vlh.swing.ValueListHelper;
38  
39  /***
40   * @author Matthew L. Wilson, Andrej Zachar
41   * @version $Revision: 1.6 $ $Date: 2006/04/18 17:15:24 $
42   */
43  public class DefaultPagingPanel extends JPanel implements PagingComponent
44  {
45     private ActionListener actionListener;
46  
47     private PagingInfo pagingInfo = null;
48  
49     private JLabel description = new JLabel();
50  
51     private ResourceBundle resourceBundle;
52  
53     private JButton first;
54  
55     private JButton previous;
56  
57     private JButton next;
58  
59     private JButton last;
60  
61     private String pagingLabel = null;
62  
63     public DefaultPagingPanel()
64     {
65        first = new JButton(new ArrowIcon(SwingConstants.LEFT, 2));
66        first.setActionCommand(ValueListHelper.ACTION_COMMAND_FIRST);
67        first.setEnabled(false);
68  
69        previous = new JButton(new ArrowIcon(SwingConstants.LEFT));
70        previous.setActionCommand(ValueListHelper.ACTION_COMMAND_PREVIOUS);
71        previous.setEnabled(false);
72  
73        next = new JButton(new ArrowIcon(SwingConstants.RIGHT));
74        next.setActionCommand(ValueListHelper.ACTION_COMMAND_NEXT);
75        next.setEnabled(false);
76  
77        last = new JButton(new ArrowIcon(SwingConstants.RIGHT, 2));
78        last.setActionCommand(ValueListHelper.ACTION_COMMAND_LAST);
79        last.setEnabled(false);
80  
81        init();
82     }
83  
84     protected void init()
85     {
86        setLayout(new GridBagLayout());
87        GridBagConstraints gbc = new java.awt.GridBagConstraints();
88        gbc.insets = new Insets(1, 1, 1, 1);
89  
90        gbc.gridx = 0;
91        gbc.weightx = 0.2;
92        gbc.anchor = GridBagConstraints.WEST;
93        add(description, gbc);
94  
95        gbc.gridx = 1;
96        gbc.weightx = 0.1;
97        gbc.anchor = GridBagConstraints.CENTER;
98        add(first);
99  
100       gbc.gridx = 2;
101       add(previous);
102 
103       gbc.gridx = 3;
104       add(next);
105 
106       gbc.gridx = 4;
107       add(last);
108    }
109 
110    public void setPagingInfo(PagingInfo pagingInfo)
111    {
112       this.pagingInfo = pagingInfo;
113       int numberOfPages = pagingInfo.getTotalNumberOfEntries() / pagingInfo.getPagingNumberPer() + 1;
114 
115       Object[] messageParameters = new Object[]
116       { new Integer(pagingInfo.getPagingPage()), new Integer(pagingInfo.getTotalNumberOfPages()), new Integer(pagingInfo
117             .getTotalNumberOfEntries()) };
118 
119       if (pagingLabel == null)
120       {
121          description.setText(MessageFormat.format(resourceBundle.getString("paging.text.pageFromTotal"), messageParameters));
122       }
123       else
124       {
125          description.setText(MessageFormat.format(pagingLabel, messageParameters));
126       }
127 
128       first.setEnabled(pagingInfo.getPagingPage() > 1);
129       previous.setEnabled(pagingInfo.getPagingPage() > 1);
130       next.setEnabled(pagingInfo.getPagingPage() < pagingInfo.getTotalNumberOfPages());
131       last.setEnabled(pagingInfo.getPagingPage() < pagingInfo.getTotalNumberOfPages());
132    }
133 
134    /***
135     * @param actionListener
136     *            The actionListener to set.
137     */
138    public void addActionListener(ActionListener actionListener)
139    {
140       first.addActionListener(actionListener);
141       previous.addActionListener(actionListener);
142       next.addActionListener(actionListener);
143       last.addActionListener(actionListener);
144    }
145 
146    /***
147     * @return Returns the resourceBundle.
148     * If no resourceBundle is defined, it uses the classicLook.properties file. Thus it should be located in the classpath.
149     */
150    public ResourceBundle getResourceBundle()
151    {
152       if (resourceBundle == null)
153       {
154          resourceBundle = ResourceBundle.getBundle("swingLook");
155       }
156       return resourceBundle;
157    }
158 
159    /***
160     * @param resourceBundle
161     *            The resourceBundle to set.
162     */
163    public void setResourceBundle(ResourceBundle resourceBundle)
164    {
165       this.resourceBundle = resourceBundle;
166    }
167 
168    public void setPagingLabel(String pagingLabel)
169    {
170       this.pagingLabel = pagingLabel;
171    }
172 }