View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.menu;
21  
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.beans.PropertyChangeEvent;
25  import java.beans.PropertyChangeListener;
26  import java.util.Arrays;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.LinkedList;
30  import java.util.List;
31  
32  import javax.swing.JCheckBoxMenuItem;
33  import javax.swing.JMenu;
34  
35  import org.xnap.gui.action.MaintainSortOrderAction;
36  import org.xnap.gui.component.XNapCheckBoxMenuItem;
37  import org.xnap.gui.table.Column;
38  import org.xnap.gui.table.ColumnModel;
39  import org.xnap.util.TablePreferencesProvider;
40  
41  /***
42   * Used for menus that control table column visibility.
43   */
44  public class TableColumnsMenu extends JMenu implements PropertyChangeListener
45  {
46      
47      //--- Constant(s) ---
48      
49      //--- Data field(s) ---
50      
51      private TablePreferencesProvider tpp;
52      private String table;
53      private List buttons = new LinkedList();
54  
55      //--- Constructor(s) ---
56  	
57      public TableColumnsMenu(String name, ColumnModel cm, 
58  							TablePreferencesProvider tpp,
59  							String table, int skip)
60      {
61  		super(name);
62  
63  		this.tpp = tpp;
64  		this.table = table;
65  
66  		MaintainSortOrderAction acSortOrder
67  			= new MaintainSortOrderAction(tpp, table);
68  		JCheckBoxMenuItem jcbSortorder = new XNapCheckBoxMenuItem(acSortOrder);
69  		add(jcbSortorder);
70  		addSeparator();
71  
72  		ColumnListener listener = new ColumnListener();
73  		for (int i = 0; i < cm.getColumnCount(); i++) {
74  			JCheckBoxMenuItem jcb 
75  				= new JCheckBoxMenuItem(cm.getColumnAt(i).getName());
76  			jcb.setActionCommand(cm.getColumnAt(i).getKey());
77  			jcb.addActionListener(listener);
78  			buttons.add(jcb);
79  			if (i >= skip) {
80  				add(jcb);
81  			}
82  		}
83  
84  		updateVisibleColumns();
85  		tpp.addTableListener(table, this);
86      }
87  
88      public TableColumnsMenu(String name, ColumnModel cm, 
89  							TablePreferencesProvider tpp, String table)
90      {
91  		this(name, cm, tpp, table, 0);
92      }
93  
94      //--- Method(s) ---
95  
96      public static ColumnModel toModel(Column[] columns)
97      {
98  		return new ArrayColumnModel(columns);
99      }
100 
101     public void propertyChange(PropertyChangeEvent e)
102     {
103 		updateVisibleColumns();
104     }
105 
106     /***
107      * Updates buttons.
108      */
109     public void updateVisibleColumns()
110     {
111 		HashSet visiblesKeys 
112 			= new HashSet(Arrays.asList(tpp.getTableColumns(table)));
113 
114 		for (Iterator i = buttons.iterator(); i.hasNext();) {
115 			JCheckBoxMenuItem jcb = (JCheckBoxMenuItem)i.next();
116 			jcb.setSelected(visiblesKeys.contains(jcb.getActionCommand()));
117 		}
118     }
119 
120     private class ColumnListener implements ActionListener
121     {
122 	
123 		public void actionPerformed(ActionEvent event)
124 		{
125 			List visiblesKeys 
126 				= new LinkedList(Arrays.asList(tpp.getTableColumns(table)));
127 
128 			JCheckBoxMenuItem jcb = (JCheckBoxMenuItem)event.getSource();
129 			if (jcb.isSelected()) {
130 				visiblesKeys.add(jcb.getActionCommand());
131 			}
132 			else {
133 				visiblesKeys.remove(jcb.getActionCommand());
134 			}
135 
136 			String[] colKeys = (String[])visiblesKeys.toArray(new String[0]);
137 			tpp.setTableColumns(table, colKeys);
138 		}
139     }
140 
141     private static class ArrayColumnModel implements ColumnModel
142     {
143 	
144 		Column[] columns;
145 	
146 		public ArrayColumnModel(Column[] columns)
147 		{
148 			this.columns = columns;
149 		}
150 	
151 		public Column getColumnAt(int index)
152 		{
153 			return columns[index];
154 		}
155 	
156 		public int getColumnCount()
157 		{
158 			return columns.length;
159 		}
160     }
161 
162 }