View Javadoc
1   // ////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
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  package net.sf.dexterim.ui;
20  
21  import java.awt.BorderLayout;
22  import java.awt.event.ActionEvent;
23  import java.util.Iterator;
24  
25  import javax.swing.AbstractAction;
26  import javax.swing.Action;
27  import javax.swing.DefaultListModel;
28  import javax.swing.JList;
29  import javax.swing.JPanel;
30  import javax.swing.JScrollPane;
31  import javax.swing.JSplitPane;
32  import javax.swing.JToolBar;
33  import javax.swing.event.ListSelectionEvent;
34  import javax.swing.event.ListSelectionListener;
35  
36  
37  /***
38   * @author christoph
39   *
40   */
41  public class JFilterManager extends JSplitPane {
42  	
43  	private JList list;
44  	private HexModel model;
45  	private JFilterEdit edit;
46  	
47  	public JFilterManager(int orientation) {
48  		super(orientation);
49  		
50  		JPanel listPanel = new JPanel(new BorderLayout());
51  		
52  		this.list = new JList();
53  
54  		
55  		listPanel.add(new JScrollPane(list), BorderLayout.CENTER);
56  		
57  		JToolBar controls = new JToolBar();
58  		controls.add(new AddFilterAction());
59  		controls.add(new RemoveFilterAction());
60  		listPanel.add(controls, BorderLayout.NORTH);
61  		
62  		add(listPanel, JSplitPane.TOP);
63  
64  		edit = JFilterEdit.getAddInstance(false);
65  		
66  		add(edit, JSplitPane.BOTTOM);
67  		
68  		this.list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
69  
70  			public void valueChanged(ListSelectionEvent e) {
71  				if (!e.getValueIsAdjusting()) {
72  					if (list.getSelectedValue() != null) {
73  						edit.setFilterMask((FilterMask)list.getSelectedValue());
74  					}
75  				}
76  				else {
77  					
78  				}
79  			}});
80  	}
81  	
82  	public void setModel(HexModel model) {
83  		this.model = model;
84  		
85  		updateListModel(model);
86  	}
87  
88  	/***
89  	 * @param model
90  	 */
91  	protected void updateListModel(HexModel model) {
92  		DefaultListModel listModel = new DefaultListModel();
93  		
94  		for (Iterator iter = model.getFilters().iterator(); iter.hasNext();) {
95  			FilterMask filter = (FilterMask) iter.next();
96  			
97  			listModel.addElement(filter);
98  		}
99  		
100 		list.setModel(listModel);
101 	}
102 	
103 	private void addFilter() {
104 		model.appendFilter(edit.getFilter());
105 		updateListModel(model);
106 	}
107 	
108 	private void removeFilter() {
109 		
110 		if (list.getSelectedValue() != null) {
111 			model.removeFilter((FilterMask)list.getSelectedValue());
112 			updateListModel(model);
113 		}
114 	}
115 	
116 	/***
117 	 * @author christoph
118 	 *
119 	 */
120 	private class AddFilterAction extends AbstractAction implements Action {
121 
122 		public AddFilterAction() {
123 			super("Add");
124 		}
125 		
126 		/*** Adds a new Filter to HexModel
127 		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
128 		 */
129 		public void actionPerformed(ActionEvent e) {
130 			addFilter();
131 		}
132 	}
133 	
134 	/***
135 	 * @author christoph
136 	 *
137 	 */
138 	private class RemoveFilterAction extends AbstractAction implements Action {
139 
140 		public RemoveFilterAction() {
141 			super("Remove");
142 		}
143 		
144 		/*** Adds a new Filter to HexModel
145 		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
146 		 */
147 		public void actionPerformed(ActionEvent e) {
148 			removeFilter();
149 		}
150 	}
151 }