001 package net.sourceforge.mp3dings.preferences;
002
003 /* Mp3dings - manage mp3 meta-information
004 * Copyright (C) 2003-2006 Moritz Ringler
005 * $Id: SimpleFieldFilterDialog.java,v 1.3 2006/09/11 17:13:52 ringler Exp $
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020 */
021
022 import java.awt.Component;
023 import java.awt.Dimension;
024 import java.awt.GraphicsEnvironment;
025 import java.awt.GridBagConstraints;
026 import java.awt.GridBagLayout;
027 import java.awt.Insets;
028 import java.awt.Point;
029 import java.awt.Toolkit;
030 import java.awt.Window;
031 import java.awt.event.ActionEvent;
032 import java.awt.event.ActionListener;
033 import java.util.List;
034 import javax.swing.JButton;
035 import javax.swing.JDialog;
036 import javax.swing.JFrame;
037 import javax.swing.JPanel;
038 import javax.swing.SwingUtilities;
039 import javax.swing.UIManager;
040 import net.sourceforge.mp3dings.Globals;
041 import net.sourceforge.mp3dings.filter.FilterBean;
042 import net.sourceforge.mp3dings.utils.BeanList;
043 import net.sourceforge.mp3dings.utils.BeanSet;
044
045 /** The PrefPanel that configures which fields are available in
046 * which table.
047 * @author Moritz Ringler
048 * @version $Revision: 1.3 $ ($Date: 2006/09/11 17:13:52 $)
049 **/
050 public class SimpleFieldFilterDialog{
051 private BeanList filterList;
052 private JDialog myDialog;
053 private final BeanSet allFilters;
054 //private final JButton ok2 = new JButton();
055 private final JButton ok1 = new JButton();
056
057 private final static int OK1 = 0;
058 //private final static int OK2 = 1;
059 private final static int CANCEL = 2;
060
061 private final static String OK1_ADD_TEXT = Globals.getString("ADD");
062 //private final static String OK2_ADD_TEXT = "Add after existing filters";
063 private final static String OK1_REMOVE_TEXT = Globals.getString("REMOVE");
064
065 private final static String TITLE_ADD = Globals.getString("ADD_FILTERS");
066 private final static String TITLE_REMOVE = Globals.getString("REMOVE_FILTERS");
067 private final static Dimension MIN_SIZE = new Dimension(100,200);
068
069 private int button = CANCEL;
070 private Point screen;
071
072 public SimpleFieldFilterDialog(Component parent, BeanSet availableFilters){
073 Toolkit tk = Toolkit.getDefaultToolkit();
074 Dimension scr = tk.getScreenSize();
075 Insets ins = tk.getScreenInsets(
076 GraphicsEnvironment.getLocalGraphicsEnvironment()
077 .getDefaultScreenDevice().getDefaultConfiguration());
078 screen = new Point(scr.width-ins.right,scr.height-ins.bottom);
079
080 filterList = new BeanList();
081
082 Window w = (parent instanceof Window)? (Window) parent : SwingUtilities.getWindowAncestor(parent);
083 if(w instanceof JDialog){
084 myDialog = new JDialog((JDialog) w, true);
085 } else if(w instanceof JFrame){
086 myDialog = new JDialog((JFrame) w, true);
087 } else {
088 myDialog = new JDialog();
089 myDialog.setModal(true);
090 }
091 allFilters = availableFilters;
092 final JButton cancel = new JButton(Globals.CANCEL);
093 ActionListener buttonListener = new ActionListener(){
094 /**Handle button event*/
095 public void actionPerformed(ActionEvent e) {
096 if (e.getSource() == ok1) {
097 button = OK1;
098 //} else if (e.getSource() == ok2) {
099 // button = OK2;
100 } else if (e.getSource() == cancel) {
101 button = CANCEL;
102 }
103 myDialog.setVisible(false);
104 }
105 };
106 ok1.addActionListener(buttonListener);
107 //ok2.addActionListener(buttonListener);
108 cancel.addActionListener(buttonListener);
109
110 JPanel dialogContent = new JPanel(new GridBagLayout());
111 GridBagConstraints gbc = new GridBagConstraints();
112
113 gbc.gridx = 0;
114 gbc.gridy = 0;
115 gbc.gridheight = 1;
116 gbc.gridwidth = 2;
117 gbc.weightx = 1.;
118 gbc.weighty = 0.;
119 gbc.fill = GridBagConstraints.BOTH;
120 dialogContent.add(filterList.getTypeSelector(), gbc);
121
122 gbc.gridy = 1;
123 gbc.weighty = 1.;
124 dialogContent.add(filterList, gbc);
125
126 gbc.gridy = 2;
127 gbc.gridx = 0;
128 gbc.gridwidth = 1;
129 gbc.weighty = 0.;
130 gbc.weightx = 0.5;
131 dialogContent.add(ok1, gbc);
132 //dialogContent.add(ok2, gbc);
133
134 gbc.gridx = 1;
135 dialogContent.add(cancel, gbc);
136 myDialog.setContentPane(dialogContent);
137 myDialog.pack();
138
139
140 }
141
142 public Object[] showAddFilterDialog(FilteredField[] fields, Point origin){
143 BeanSet myFilters = allFilters;
144 List filters;
145
146 /* configure the filter list to show all filters that act on one or
147 * more of the filtered fields. */
148 filterList.clearRegisteredTypes();
149 filterList.registerType(FilterBean.class);
150 filterList.registerBeanTypes(myFilters);
151 filterList.setModel(allFilters.getNewListModel());
152 filterList.setVisibleBeanType(FilterBean.class);
153 filterList.getTypeSelector().setSelectedItem(FilterBean.class);
154
155 /* configure the dialog */
156 button = CANCEL;
157 ok1.setText(OK1_ADD_TEXT);
158 //ok2.setVisible(false);
159
160 /* show the dialog */
161 myDialog.setTitle(TITLE_ADD);
162 displayDialogAt(origin);
163
164 Object[] result;
165 if(button == OK1){
166 result = filterList.getSelectedValues();
167 } else {
168 result = new FilteredField[0];
169 }
170 return result;
171 }
172
173 private void displayDialogAt(Point origin){
174 Dimension p = filterList.getPreferredSize();
175 if(p.height < MIN_SIZE.height){
176 p.height = MIN_SIZE.height;
177 }
178 if(p.width < MIN_SIZE.width){
179 p.width = MIN_SIZE.width;
180 }
181 filterList.setPreferredSize(p);
182
183 myDialog.pack();
184 Dimension dialogSize = myDialog.getSize();
185 myDialog.setLocation(
186 (int) Math.min(origin.getX(),screen.getX()-dialogSize.width),
187 (int) Math.min(origin.getY(),screen.getY()-dialogSize.height));
188 myDialog.setVisible(true);
189 }
190
191 public Object[] showRemoveFilterDialog(FilteredField[] fields, Point origin){
192 BeanSet myFilters = new BeanSet();
193 List filters;
194
195 /* configure the filter list to show all filters that act on one or
196 * more of the filtered fields. */
197 filterList.clearRegisteredTypes();
198 filterList.registerType(FilterBean.class);
199 for(int i=0; i<fields.length; i++){
200 filters = fields[i].getFilters();
201 filterList.registerBeanTypes(filters);
202 myFilters.addAll(filters);
203 }
204 filterList.setModel(myFilters.getNewListModel());
205 filterList.setVisibleBeanType(FilterBean.class);
206 filterList.getTypeSelector().setSelectedItem(FilterBean.class);
207
208 /* configure the dialog */
209 button = CANCEL;
210 ok1.setText(OK1_REMOVE_TEXT);
211 //ok2.setVisible(false);
212
213 /* show the dialog */
214 myDialog.setTitle(TITLE_REMOVE);
215 displayDialogAt(origin);
216
217 Object[] result;
218 if(button == OK1){
219 result = filterList.getSelectedValues();
220 } else {
221 result = new FilteredField[0];
222 }
223 return result;
224 }
225
226 }