1
6 import java.awt.event.*;
7 import java.io.*;
8 import javax.swing.*;
9
10 public class LibraryAdder implements ActionListener {
11
12 JButton target;
13 LibraryUser user;
14 File file;
15
16 public LibraryAdder(JButton target,File file,LibraryUser user) {
17 this.file = file;
18 this.target = target;
19 this.user = user;
20 }
21
22 public void actionPerformed(ActionEvent e) {
23 Symbol sym = user.libSave();
24
25
26 JFileChooser choose = new JFileChooser(file);
27 if (choose.showSaveDialog(null)!=choose.APPROVE_OPTION) return;
28 File newfile = choose.getSelectedFile();
29 if (!newfile.getName().endsWith(".smb"))
30 newfile = new File(newfile.toString().concat(".smb"));
31
32 try {
33 FileOutputStream ostream = new FileOutputStream(newfile);
34 ObjectOutputStream out = new ObjectOutputStream(ostream);
35 out.writeObject(sym);
36 out.flush();
37 ostream.close();
38 } catch (IOException ex) {
39 System.out.println("I/O error writing "+sym+" to library "+newfile);
40 ErrorLog.exception(ex);
41 return;
42 }
43
44 LibraryItem item = LibraryItem.getItem(newfile,user);
45
46 if (item==null) return;
47
48 target.removeActionListener(this);
49 target.addActionListener(item);
50 target.setIcon(item);
51 }
52 }
53