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;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Dimension;
24  import java.awt.event.ActionEvent;
25  import java.awt.event.ActionListener;
26  import java.util.HashSet;
27  
28  import javax.swing.Action;
29  import javax.swing.BorderFactory;
30  import javax.swing.Box;
31  import javax.swing.BoxLayout;
32  import javax.swing.DefaultComboBoxModel;
33  import javax.swing.JComboBox;
34  import javax.swing.JLabel;
35  import javax.swing.JPanel;
36  import javax.swing.JScrollPane;
37  import javax.swing.JTable;
38  import javax.swing.ListSelectionModel;
39  import javax.swing.border.EmptyBorder;
40  
41  import org.xnap.XNap;
42  import org.xnap.gui.component.XNapMenuItem;
43  import org.xnap.gui.event.PopupListener;
44  import org.xnap.gui.event.SwingListListener;
45  import org.xnap.gui.menu.AbstractDynamicMenu;
46  import org.xnap.gui.table.HotlistTableModel;
47  import org.xnap.gui.util.*;
48  import org.xnap.gui.util.SwingSynchronizedCache;
49  import org.xnap.peer.HotlistItem;
50  import org.xnap.peer.HotlistManager;
51  import org.xnap.util.Preferences;
52  
53  
54  public class HotlistPanel extends JPanel 
55      implements ActionProvider, ActionListener, SwingListListener {
56  
57      // --- Data Field(s) ---
58  
59      private static Preferences prefs = Preferences.getInstance();
60  
61      private JTable jtaHotlist;
62      private HotlistTableModel tmHotlist;
63      private JComboBox jcbFilter;
64      private DefaultComboBoxModel dlmFilter;
65  	private HashSet categories = new HashSet();
66  
67      //--- Constructor(s) ---
68  	
69      public HotlistPanel()
70      {
71  		initialize();
72  
73  		setPreferredSize(new Dimension(300, 400));
74  
75  		SwingSynchronizedCache cache = new SwingSynchronizedCache(this);
76  		HotlistManager.getInstance().addListListener(cache);
77      }
78  
79      // --- Method(s) ---
80  
81      private void initialize()
82      {
83  		// hotlist table
84  		tmHotlist = new HotlistTableModel();
85  		jtaHotlist = tmHotlist.createTable(prefs, "hotlist");
86  		jtaHotlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
87  
88  		JScrollPane jsp = new JScrollPane(jtaHotlist);
89  		//jsp.setBorder(GUIHelper.createEmptyBorder());
90  
91  		// add existing items
92  		HotlistItem[] items = HotlistManager.getInstance().getItems();
93  		tmHotlist.itemsAdded(items);
94  
95  		// hotlist table popup
96  		HotlistItemMenu jm = new HotlistItemMenu();
97  
98  		jtaHotlist.addMouseListener(new PopupListener(jm));
99  
100 		// filter combo box
101 		JPanel jpFilter = new JPanel();
102 		jpFilter.setLayout(new BoxLayout(jpFilter, BoxLayout.X_AXIS));
103 		jpFilter.setBorder(new EmptyBorder(5, 5, 5, 5));
104 
105 		JLabel l = new JLabel(XNap.tr("Show"));
106 		jpFilter.add(l);
107 		jpFilter.add(Box.createHorizontalStrut(5));
108 
109         dlmFilter = new DefaultComboBoxModel();
110         dlmFilter.addElement(new HotlistTableModel.AllFilter());
111     	jcbFilter = new JComboBox(dlmFilter);
112 		l.setLabelFor(jcbFilter);
113 		jcbFilter.addActionListener(this);
114     	jpFilter.add(jcbFilter);
115 
116 		// title
117 		JLabel jlTitle = new JLabel(XNap.tr("Hotlist"));
118 		jlTitle.setIcon(IconHelper.getListIcon("kdmconfig.png"));
119 		jlTitle.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
120 
121 		JPanel jpNested = new JPanel(new BorderLayout());
122 		jpNested.add(jpFilter, BorderLayout.NORTH);
123 		jpNested.add(jsp, BorderLayout.CENTER);
124 
125 		setLayout(new BorderLayout());
126 		add(jlTitle, BorderLayout.NORTH);
127 		add(jpNested, BorderLayout.CENTER);
128 
129 		tmHotlist.setFilter
130 			((HotlistTableModel.UserFilter)jcbFilter.getSelectedItem());
131 		GUIHelper.setMnemonics(this);
132     }
133 
134     /***
135      * Called when a filter is selected.
136      */
137     public void actionPerformed(ActionEvent event) 
138     {
139 		tmHotlist.setFilter
140 			((HotlistTableModel.UserFilter)jcbFilter.getSelectedItem());
141     }
142 
143     public Action[] getActions()
144     {
145 		return null; //new AbstractAction[] {  };
146     }
147 
148     /***
149      * Invoked when items have been added.
150      */
151     public void itemsAdded(Object[] items)
152     {
153 		tmHotlist.itemsAdded(items);
154 		
155 		for (int i = 0; i < items.length; i++) {
156 			String category = ((HotlistItem)items[i]).getCategory();
157 			if (!categories.contains(category)) {
158 				categories.add(category);
159 				dlmFilter.addElement(new HotlistTableModel.CategoryFilter(category));
160 			}
161 		}
162     }
163 
164     /***
165      * Invoked when items have been removed.
166      */
167     public void itemsRemoved(Object[] items) 
168     {
169 		tmHotlist.itemsRemoved(items);
170     }
171 	
172     //--- Inner Class(es) ---
173 
174     public class HotlistItemMenu extends AbstractDynamicMenu {
175 
176 		public HotlistItemMenu()
177 		{
178 			super("");
179 		}
180 
181 		protected void willBecomeVisible()
182 		{
183 			removeAllTemporaries();
184 			//addActions(pp.getPeers());
185 			int row = jtaHotlist.getSelectedRow();
186 			Action[] actions = tmHotlist.get(row).getActions();
187 			if (actions != null) {
188 				for (int i = 0; i < actions.length; i++) {
189 					addTemporary(new XNapMenuItem(actions[i]));
190 				}
191 			}
192 		}
193 
194     }
195 
196 }
197