e646eb43a74ba2b867f2b72fe718d961d1b58adb
[debian/jabref.git] / src / java / net / sf / jabref / PrefsDialog3.java
1 /*
2  Copyright (C) 2003 JabRef project
3
4  All programs in this directory and
5  subdirectories are published under the GNU General Public License as
6  described below.
7
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or (at
11  your option) any later version.
12
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  USA
22
23  Further information about the GNU GPL is available at:
24  http://www.gnu.org/copyleft/gpl.ja.html
25
26  */
27
28 package net.sf.jabref;
29
30 import java.awt.*;
31 import java.awt.event.*;
32 import java.io.*;
33 import java.util.*;
34
35 import javax.swing.*;
36 import javax.swing.event.*;
37
38 import net.sf.jabref.groups.GroupsPrefsTab;
39 import net.sf.jabref.gui.MainTable;
40
41 import com.jgoodies.forms.builder.ButtonBarBuilder;
42
43 /**
44  * Preferences dialog. Contains a TabbedPane, and tabs will be defined in
45  * separate classes. Tabs MUST implement the PrefsTab interface, since this
46  * dialog will call the storeSettings() method of all tabs when the user presses
47  * ok.
48  * 
49  * With this design, it should be very easy to add new tabs later.
50  * 
51  */
52 public class PrefsDialog3 extends JDialog {
53
54         JPanel main;
55
56         JabRefFrame frame;
57
58         public PrefsDialog3(JabRefFrame parent) {
59                 super(parent, Globals.lang("JabRef preferences"), false);
60                 final JabRefPreferences prefs = JabRefPreferences.getInstance();
61                 frame = parent;
62
63                 final JList chooser;
64
65                 JButton importPrefs = new JButton(Globals.lang("Import preferences"));
66                 JButton exportPrefs = new JButton(Globals.lang("Export preferences"));
67
68                 main = new JPanel();
69                 JPanel upper = new JPanel();
70                 JPanel lower = new JPanel();
71
72                 getContentPane().setLayout(new BorderLayout());
73                 getContentPane().add(upper, BorderLayout.CENTER);
74                 getContentPane().add(lower, BorderLayout.SOUTH);
75
76                 final CardLayout cardLayout = new CardLayout();
77                 main.setLayout(cardLayout);
78
79                 // ----------------------------------------------------------------
80                 // Add tabs to tabbed here. Remember, tabs must implement PrefsTab.
81                 // ----------------------------------------------------------------
82                 ArrayList tabs = new ArrayList();
83                 tabs.add(new GeneralTab(frame, prefs));
84         tabs.add(new EntryEditorPrefsTab(frame, prefs));
85         tabs.add(new GroupsPrefsTab(prefs));
86                 tabs.add(new AppearancePrefsTab(prefs));
87                 tabs.add(new ExternalTab(frame, this, prefs, parent.helpDiag));
88                 tabs.add(new TablePrefsTab(prefs, parent));
89                 tabs.add(new TableColumnsTab(prefs, parent));
90                 tabs.add(new TabLabelPattern(prefs, parent.helpDiag));
91                 tabs.add(new PreviewPrefsTab(prefs));
92                 tabs.add(new NameFormatterTab(parent.helpDiag));
93                 tabs.add(new XmpPrefsTab());
94         tabs.add(new AdvancedTab(prefs, parent.helpDiag));
95                 
96                 Iterator it = tabs.iterator();
97                 String[] names = new String[tabs.size()];
98                 int i = 0;
99                 while (it.hasNext()) {
100                         PrefsTab tab = (PrefsTab) it.next();
101                         names[i++] = tab.getTabName(); 
102                         main.add((Component) tab, tab.getTabName());
103                 }
104
105                 upper.setBorder(BorderFactory.createEtchedBorder());
106
107                 chooser = new JList(names);
108                 chooser.setBorder(BorderFactory.createEtchedBorder());
109                 // Set a prototype value to control the width of the list:
110                 chooser.setPrototypeCellValue("This should be wide enough");
111                 chooser.setSelectedIndex(0);
112                 chooser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
113
114                 // Add the selection listener that will show the correct panel when
115                 // selection changes:
116                 chooser.addListSelectionListener(new ListSelectionListener() {
117                         public void valueChanged(ListSelectionEvent e) {
118                                 if (e.getValueIsAdjusting())
119                                         return;
120                                 String o = (String) chooser.getSelectedValue();
121                                 cardLayout.show(main, o);
122                         }
123                 });
124
125                 JPanel one = new JPanel(), two = new JPanel();
126                 one.setLayout(new BorderLayout());
127                 two.setLayout(new BorderLayout());
128                 one.add(chooser, BorderLayout.CENTER);
129                 one.add(importPrefs, BorderLayout.SOUTH);
130                 two.add(one, BorderLayout.CENTER);
131                 two.add(exportPrefs, BorderLayout.SOUTH);
132                 upper.setLayout(new BorderLayout());
133                 upper.add(two, BorderLayout.WEST);
134                 upper.add(main, BorderLayout.CENTER);
135
136                 JButton ok = new JButton(Globals.lang("Ok")), cancel = new JButton(Globals.lang("Cancel"));
137                 ok.addActionListener(new OkAction());
138                 CancelAction cancelAction = new CancelAction();
139                 cancel.addActionListener(cancelAction);
140                 lower.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
141                 ButtonBarBuilder bb = new ButtonBarBuilder(lower);
142                 bb.addGlue();
143                 bb.addGridded(ok);
144                 bb.addGridded(cancel);
145                 bb.addGlue();
146                 // lower.add(ok);
147                 // lower.add(cancel);
148
149                 // Key bindings:
150                 ActionMap am = chooser.getActionMap();
151                 InputMap im = chooser.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
152                 im.put(frame.prefs().getKey("Close dialog"), "close");
153                 am.put("close", cancelAction);
154
155                 // Import and export actions:
156                 exportPrefs.setToolTipText(Globals.lang("Export preferences to file"));
157                 importPrefs.setToolTipText(Globals.lang("Import preferences from file"));
158                 exportPrefs.addActionListener(new ActionListener() {
159                         public void actionPerformed(ActionEvent e) {
160                                 String filename = Globals.getNewFile(frame, new File(System
161                                         .getProperty("user.home")), ".xml", JFileChooser.SAVE_DIALOG, false);
162                                 if (filename == null)
163                                         return;
164                                 File file = new File(filename);
165                                 if (!file.exists()
166                                         || (JOptionPane.showConfirmDialog(PrefsDialog3.this, "'" + file.getName()
167                                                 + "' " + Globals.lang("exists. Overwrite file?"), Globals
168                                                 .lang("Export preferences"), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)) {
169
170                                         try {
171                                                 prefs.exportPreferences(filename);
172                                         } catch (IOException ex) {
173                                                 JOptionPane.showMessageDialog(PrefsDialog3.this, Globals
174                                                         .lang("Could not export preferences")
175                                                         + ": " + ex.getMessage(), Globals.lang("Export preferences"),
176                                                         JOptionPane.ERROR_MESSAGE);
177                                                 // ex.printStackTrace();
178                                         }
179                                 }
180
181                         }
182                 });
183
184                 importPrefs.addActionListener(new ActionListener() {
185                         public void actionPerformed(ActionEvent e) {
186                                 String filename = Globals.getNewFile(frame, new File(System
187                                         .getProperty("user.home")), ".xml", JFileChooser.OPEN_DIALOG, false);
188                                 if (filename == null)
189                                         return;
190
191                                 try {
192                                         prefs.importPreferences(filename);
193                                         setValues();
194                                         BibtexEntryType.loadCustomEntryTypes(prefs);
195                                         frame.removeCachedEntryEditors();
196                     Globals.prefs.updateEntryEditorTabList();
197                 } catch (IOException ex) {
198                                         JOptionPane.showMessageDialog(PrefsDialog3.this, Globals
199                                                 .lang("Could not import preferences")
200                                                 + ": " + ex.getMessage(), Globals.lang("Import preferences"),
201                                                 JOptionPane.ERROR_MESSAGE);
202                                         // ex.printStackTrace();
203                                 }
204                         }
205
206                 });
207
208                 setValues();
209
210                 pack(); // setSize(440, 500);
211         }
212
213         class OkAction extends AbstractAction {
214                 public OkAction() {
215                         super("Ok");
216                 }
217
218                 public void actionPerformed(ActionEvent e) {
219
220                         AbstractWorker worker = new AbstractWorker() {
221                                 boolean ready = true;
222
223                                 public void run() {
224                                         // First check that all tabs are ready to close:
225                                         int count = main.getComponentCount();
226                                         Component[] comps = main.getComponents();
227                                         for (int i = 0; i < count; i++) {
228                                                 if (!((PrefsTab) comps[i]).readyToClose()) {
229                                                         ready = false;
230                                                         return; // If not, break off.
231                                                 }
232                                         }
233                                         // Then store settings and close:
234                                         for (int i = 0; i < count; i++) {
235                                                 ((PrefsTab) comps[i]).storeSettings();
236                                         }
237                                         Globals.prefs.flush();
238                                         // try { Thread.sleep(3000); } catch (InterruptedException
239                                         // ex) {}
240                                 }
241
242                                 public void update() {
243                                         if (!ready)
244                                                 return;
245                                         setVisible(false);
246                                         MainTable.updateRenderers();
247                                         frame.setupAllTables();
248                                         frame.groupSelector.revalidateGroups(); // icons may have
249                                         // changed
250                                         frame.output(Globals.lang("Preferences recorded."));
251                                 }
252                         };
253                         worker.getWorker().run();
254                         worker.getCallBack().update();
255
256                 }
257         }
258
259         public void setValues() {
260                 // Update all field values in the tabs:
261                 int count = main.getComponentCount();
262                 Component[] comps = main.getComponents();
263                 for (int i = 0; i < count; i++) {
264                         ((PrefsTab) comps[i]).setValues();
265                 }
266         }
267
268         class CancelAction extends AbstractAction {
269                 public CancelAction() {
270                         super("Cancel");
271                 }
272
273                 public void actionPerformed(ActionEvent e) {
274                         setVisible(false);
275                 }
276         }
277
278 }