001 package net.sourceforge.mp3dings.utils;
002
003 /* Mp3dings - manage mp3 meta-information
004 * Copyright (C) 2003-2004 Moritz Ringler
005 * $Id: ChecksumFactoryCustomizer.java,v 1.5 2007/03/29 16:03:02 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.BorderLayout;
023 import java.awt.Component;
024 import java.awt.Container;
025 import java.awt.FlowLayout;
026 import java.awt.GridBagConstraints;
027 import java.awt.GridBagLayout;
028 import java.awt.Window;
029 import java.awt.event.ActionEvent;
030 import java.awt.event.ActionListener;
031 import java.awt.event.WindowEvent;
032 import java.io.File;
033 import java.io.IOException;
034 import java.util.prefs.Preferences;
035 import javax.swing.Box;
036 import javax.swing.JButton;
037 import javax.swing.JComponent;
038 import javax.swing.JFrame;
039 import javax.swing.JLabel;
040 import javax.swing.JOptionPane;
041 import javax.swing.JPanel;
042 import javax.swing.SwingUtilities;
043 import javax.swing.*;
044 import de.mospace.lang.BrowserLauncher;
045 import de.mospace.lang.ClassLoaderProvider;
046 import net.sourceforge.mp3dings.Globals;
047 import net.sourceforge.mp3dings.PreferencesDialogue;
048 import de.mospace.swing.ExtensionInstaller;
049 import de.mospace.swing.PathInput;
050
051 /**
052 * A Customizer for ChecksumFactory.
053 * @author Moritz Ringler
054 * @version $Revision: 1.5 $ ($Date: 2007/03/29 16:03:02 $)
055 * @see ChecksumFactory
056 */
057 public class ChecksumFactoryCustomizer extends AbstractBeanCustomizer{
058 JPanel uui = new JPanel(new BorderLayout());
059 private ClassLoaderProvider clp;
060
061 public ChecksumFactoryCustomizer(ChecksumFactory csf){
062 super(csf);
063 final String[] words = Globals.getStringArray("PREFS_NUMFILTER");
064 uui.add(rebuildUI(), BorderLayout.NORTH);
065 }
066
067 private JPanel rebuildUI(){
068 final JPanel ui = new JPanel();
069 ChecksumFactory csf = (ChecksumFactory) getModel();
070 if(csf.isJacksumAvailable()){
071 ui.setLayout(new GridBagLayout());
072 GridBagConstraints gbc = new GridBagConstraints();
073
074 int row = 0;
075
076 gbc.gridx = 0;
077 gbc.gridy = row++;
078 gbc.gridheight = 1;
079 gbc.weighty = 0.;
080 gbc.fill = GridBagConstraints.BOTH;
081 gbc.insets = Globals.INSETS_5;
082 gbc.gridwidth = 1;
083 gbc.weightx = 1.;
084
085 ui.add(new JLabel(Globals.getString("CHECKSUM_CONFIG_ALGORITHM")), gbc);
086
087 gbc.gridx = 1;
088
089 ui.add(getPropertyEditor("algorithm", csf.getAlgorithms()), gbc);
090
091 gbc.gridy = row++;
092 gbc.gridx = 0;
093
094 ui.add(new JLabel(Globals.getString("CHECKSUM_CONFIG_ENCODING")), gbc);
095
096 gbc.gridx = 1;
097
098 ui.add(getPropertyEditor("encoding", csf.getEncodings()), gbc);
099
100 gbc.gridy = row++;
101 gbc.gridx = 0;
102 gbc.gridwidth = 2;
103
104 ui.add(getPropertyEditor("ignoreTags",
105 new Object[]{
106 Globals.getString("CHECKSUM_CONFIG_IGNORE_TAGS")
107 }), gbc);
108
109 gbc.gridy = row++;
110 gbc.gridx = 0;
111
112 boolean isActive = (csf.getMaxBytesFromStart() > 0l || csf.getMaxBytesFromStart() > 0l);
113 final JCheckBox active = new JCheckBox("Read at most");
114 active.setSelected(isActive);
115
116 ui.add(active, gbc);
117 gbc.gridy = row++;
118
119 final JTextField fromStart = (JTextField) getPropertyEditor("maxBytesFromStart", null);
120 fromStart.setEnabled(isActive);
121
122 gbc.gridwidth = 1;
123 ui.add(fromStart, gbc);
124 gbc.gridx++;
125 ui.add(new JLabel("bytes from the start of the file, and at most"), gbc);
126
127 gbc.gridx = 0;
128 gbc.gridy++;
129
130 final JTextField fromEnd = (JTextField) getPropertyEditor("maxBytesFromEnd", null);
131 fromEnd.setEnabled(isActive);
132 ui.add(fromEnd, gbc);
133
134 gbc.gridx++;
135 ui.add(new JLabel("bytes from the end of the file."), gbc);
136
137 active.addActionListener(new ActionListener(){
138 public void actionPerformed(ActionEvent e){
139 ChecksumFactory cf = (ChecksumFactory) getModel();
140 if(active.isSelected()){
141 fromStart.setText("10000");
142 fromEnd.setText("0");
143 fromStart.setEnabled(true);
144 fromEnd.setEnabled(true);
145 } else {
146 fromStart.setText("");
147 fromStart.setEnabled(false);
148 fromEnd.setText("");
149 fromEnd.setEnabled(false);
150 }
151 }
152 });
153
154 } else {
155
156 ui.setLayout(new BorderLayout());
157 ui.add(new JLabel(
158 Globals.getString("JACKSUM_INSTALLER_PARAGRAPH_1")),
159 BorderLayout.NORTH);
160 JButton installJS = new JButton(Globals.getString("JACKSUM_INSTALLER"));
161 installJS.addActionListener(new ActionListener(){
162 public void actionPerformed(ActionEvent e){
163 Component trigger = (Component) e.getSource();
164 if(installJacksum(trigger, getClassLoaderProvider())){
165 trigger.setEnabled(false);
166 uui.remove(ui);
167 uui.add(rebuildUI(), BorderLayout.NORTH);
168 SwingUtilities.getWindowAncestor(uui).pack();
169 }
170 }
171 });
172 ui.add(installJS, BorderLayout.CENTER);
173 }
174 return ui;
175 }
176
177 public ChecksumFactoryCustomizer(){
178 this(new ChecksumFactory());
179 }
180
181 //public boolean installJacksum(Component trigger){
182
183 public boolean installJacksum(Component trigger, ClassLoaderProvider clp){
184 String jhjars = "jacksum.jar";
185 final Window parent = (trigger instanceof Window)? (Window) trigger : SwingUtilities.getWindowAncestor(trigger);
186 ExtensionInstaller extInst = new ExtensionInstaller(parent);
187 extInst.setTargetDirectory(new File(Globals.getDir("LIB")));
188 Box dialogPane = Box.createVerticalBox();
189 JLabel text = new JLabel(
190 Globals.getString("JACKSUM_INSTALLER_PARAGRAPH_1"));
191 dialogPane.add(text);//1
192 JButton button= new JButton(
193 Globals.getString("DOWNLOAD"));
194 button.addActionListener(new ActionListener(){
195 public void actionPerformed(ActionEvent e){
196 try{
197 BrowserLauncher.openURL(
198 "http://sourceforge.net/project/showfiles.php?group_id=74387");
199 } catch (IOException ex){
200 JOptionPane.showMessageDialog(parent,
201 Globals.getString("JACKSUM_INSTALLER_ERRORMSG_1"));
202 }
203 }
204 });
205 JPanel dl = new JPanel(new FlowLayout(FlowLayout.LEFT));
206 dl.add(new JLabel(Globals.getString("JACKSUM_INSTALLER_PARAGRAPH_2A")));
207 dl.add(button);
208 dl.add(new JLabel(Globals.getString("JACKSUM_INSTALLER_PARAGRAPH_2B")));
209 Component[] dialogc = dl.getComponents();
210 for(int i=0; i<dialogc.length; i++){
211 ((JComponent) dialogc[i]).setAlignmentX(0.0f);
212 }
213 dialogPane.add(Box.createVerticalStrut(10));
214 dialogPane.add(dl);//2
215 text = new JLabel(Globals.getString("JACKSUM_INSTALLER_PARAGRAPH_3"));
216 dialogPane.add(text);//3
217 PathInput pinz = new PathInput("",".zip");
218 dialogPane.add(pinz);//4
219 String pathsep = "\\Q"+File.pathSeparator+"\\E";
220 File libext = extInst.getTargetDirectory();
221 if(libext == null){
222 return false;
223 }
224 text = new JLabel(
225 Globals.getMessage("JACKSUM_INSTALLER_PARAGRAPH_4", libext.getPath()));
226 dialogPane.add(text);//5
227 dialogc = dialogPane.getComponents();
228 for(int i=0; i<dialogc.length; i++){
229 ((JComponent) dialogc[i]).setAlignmentX(0.0f);
230 }
231 int result = JOptionPane.showConfirmDialog(
232 parent,
233 dialogPane,
234 Globals.getString("JACKSUM_INSTALLER_TITLE"),
235 JOptionPane.OK_CANCEL_OPTION,
236 JOptionPane.INFORMATION_MESSAGE
237 );
238 if ((result == JOptionPane.OK_OPTION) &&
239 extInst.installExtension(new File(pinz.getPath()), jhjars, clp))
240 {
241 JOptionPane.showMessageDialog(parent,
242 Globals.getString("JACKSUM_INSTALLER_SUCCESS"));
243
244 return true;
245 } else {
246 JOptionPane.showMessageDialog(parent,
247 Globals.getMessage("JACKSUM_INSTALLER_ERRORMSG_2",
248 (new File(jhjars)).getName()));
249 return false;
250 }
251 }
252
253 private synchronized ClassLoaderProvider getClassLoaderProvider(){
254 if (clp == null){
255 PreferencesDialogue pd =
256 (PreferencesDialogue) SwingUtilities.
257 getAncestorOfClass(PreferencesDialogue.class, getUI());
258 if(pd != null){
259 clp = pd.getClassLoaderProvider();
260 }
261 }
262 return clp;
263 }
264
265 public static void main (String[] argv){
266 final Preferences pref = Preferences.userNodeForPackage(ChecksumFactoryCustomizer.class);
267 ChecksumFactory csf = (ChecksumFactory) ChecksumFactory.load(pref ,"test_csf");
268 final ChecksumFactory xcsf = (csf == null)
269 ? new ChecksumFactory()
270 : csf ;
271 JFrame frame = new JFrame(){
272 protected void processWindowEvent(WindowEvent e) {
273 xcsf.save(pref, "test_csf");
274 super.processWindowEvent(e);
275 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
276 Checksum cs = xcsf.getChecksum();
277 System.out.println(cs.readFile(
278 new File(Globals.getDir("LIB"),"jacksum.jar")));
279 System.out.println(cs.getValue());
280 System.exit(0);
281 }
282 }
283 };
284 frame.add((new ChecksumFactoryCustomizer(xcsf)).getUI());
285 frame.pack();
286 frame.setVisible(true);
287 }
288
289 public void setModel(Bean csf){
290 if(!(csf instanceof ChecksumFactory)){
291 throw new IllegalArgumentException("Model is not a ChecksumFactory.");
292 }
293 super.setModel(csf);
294 }
295
296 public Component getUI(){
297 return uui;
298 }
299
300 }