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.GridBagConstraints;
22  import java.awt.GridBagLayout;
23  
24  import javax.swing.ButtonGroup;
25  import javax.swing.JPanel;
26  import javax.swing.JRadioButton;
27  import javax.swing.JScrollPane;
28  import javax.swing.JTextArea;
29  
30  
31  /***
32   * @author christoph
33   *
34   */
35  public class JFilterEdit extends JPanel {
36  	private FilterMask filter;
37  	
38  	private JRadioButton indexFilter;
39  	private JRadioButton negativeIndexFilter;
40  	private JRadioButton regexFilter;
41  	
42  	private JIndexPanel indexPanel;
43  	private JIndexPanel negativeIndexPanel;
44  	private JTextArea regexText;
45  	private boolean disposeParent;
46  	
47  	private JFilterEdit(boolean disposeParent) {
48  		super(new GridBagLayout());
49  		
50  		this.disposeParent = disposeParent;
51  		
52  		//GridBagConstraints gbcl = new GridBagConstraints();
53  		
54  		GridBagConstraints gbcr = new GridBagConstraints();
55  		gbcr.gridwidth = GridBagConstraints.REMAINDER;
56  		gbcr.anchor = GridBagConstraints.WEST;
57  		
58  		ButtonGroup group = new ButtonGroup();
59  		
60  		this.indexFilter = new JRadioButton("Index");
61  		group.add(this.indexFilter);
62  		this.add(indexFilter, gbcr);
63  		
64  		indexPanel = new JIndexPanel();
65  		this.add(indexPanel, gbcr);
66  		
67  		this.negativeIndexFilter = new JRadioButton("Negative Index");
68  		group.add(this.negativeIndexFilter);
69  		this.add(negativeIndexFilter, gbcr);
70  		
71  		negativeIndexPanel = new JIndexPanel();
72  		this.add(negativeIndexPanel, gbcr);
73  		
74  		this.regexFilter = new JRadioButton("Regular Expression");
75  		group.add(this.regexFilter);
76  		this.add(regexFilter, gbcr);
77  		
78  		regexText = new JTextArea(4, 20);
79  		this.add(new JScrollPane(regexText), gbcr);
80  	}
81  	
82  	public void setFilterMask(FilterMask filter) {
83  		this.filter = filter;
84  		
85  		updateControls(this.filter);
86  	}
87  	
88  	public FilterMask getFilter() {
89  		if (indexFilter.isSelected()) {
90  			return new IndexFilterMask(indexPanel.getStartIndex(), indexPanel.getEndIndex());
91  		}
92  		else if (negativeIndexFilter.isSelected()) {
93  			return new IndexFilterMask(negativeIndexPanel.getStartIndex(), negativeIndexPanel.getEndIndex());
94  		}
95  		else {
96  			return new RegularExpressionFilter(regexText.getText());
97  		}
98  	}
99  	
100 	/***
101 	 * @param filter
102 	 */
103 	private void updateControls(FilterMask filter) {
104 		if (filter instanceof IndexFilterMask) {
105 			indexFilter.setSelected(true);
106 			
107 			IndexFilterMask indexFilter = (IndexFilterMask)filter;
108 			
109 			indexPanel.setStartIndex(indexFilter.getStartIndex());
110 			indexPanel.setEndIndex(indexFilter.getEndIndex());
111 		}
112 		else if (filter instanceof NegativeIndexFilterMask) {
113 			negativeIndexFilter.setSelected(true);
114 			
115 			NegativeIndexFilterMask negativeIndexFilter = (NegativeIndexFilterMask)filter;
116 			
117 			negativeIndexPanel.setStartIndex(negativeIndexFilter.getStartIndex());
118 			negativeIndexPanel.setEndIndex(negativeIndexFilter.getEndIndex());
119 		}
120 		else {
121 			regexFilter.setSelected(true);
122 			
123 			regexText.setText(((RegularExpressionFilter)filter).getRegex());
124 		}
125 	}
126 
127 	public static JFilterEdit getEditInstance(FilterMask filter, boolean disposeParent) {
128 		JFilterEdit edit = new JFilterEdit(disposeParent);
129 		edit.setFilterMask(filter);
130 		
131 		return edit;
132 	}
133 	
134 	public static JFilterEdit getAddInstance(boolean disposeParent) {
135 		JFilterEdit edit = new JFilterEdit(disposeParent);
136 		edit.setFilterMask(new IndexFilterMask(0,1));
137 		
138 		return edit;
139 	}
140 
141 	/***
142 	 * @return Returns the disposeParent.
143 	 */
144 	public boolean isDisposeParent() {
145 		return disposeParent;
146 	}
147 	/***
148 	 * @param disposeParent The disposeParent to set.
149 	 */
150 	public void setDisposeParent(boolean disposeParent) {
151 		this.disposeParent = disposeParent;
152 	}
153 }