2 Copyright (C) 2003 JabRef project
4 All programs in this directory and
5 subdirectories are published under the GNU General Public License as
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.
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.
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
23 Further information about the GNU GPL is available at:
24 http://www.gnu.org/copyleft/gpl.ja.html
28 package net.sf.jabref;
31 import java.awt.event.*;
36 import javax.swing.event.*;
38 import net.sf.jabref.groups.GroupsPrefsTab;
39 import net.sf.jabref.gui.MainTable;
40 import com.jgoodies.forms.builder.ButtonBarBuilder;
43 * Preferences dialog. Contains a TabbedPane, and tabs will be defined
44 * in separate classes. Tabs MUST implement the PrefsTab interface,
45 * since this dialog will call the storeSettings() method of all tabs
46 * when the user presses ok.
48 * With this design, it should be very easy to add new tabs later.
51 public class PrefsDialog3 extends JDialog {
53 private JabRefPreferences _prefs;
54 JPanel upper = new JPanel(),
56 main = new JPanel();/* {
57 public void add(Component c, Object o) {
59 System.out.println(o+" "+c.getPreferredSize());
63 JButton importPrefs = new JButton(Globals.lang("Import preferences")),
64 exportPrefs = new JButton(Globals.lang("Export preferences"));
65 CardLayout cardLayout = new CardLayout();
66 HashMap panels = new HashMap();
68 PrefsDialog3 ths = this;
70 public PrefsDialog3(JabRefFrame parent, JabRefPreferences prefs) {
71 super(parent, Globals.lang("JabRef preferences"), false);
74 getContentPane().setLayout(new BorderLayout());
75 getContentPane().add(upper, BorderLayout.CENTER);
76 getContentPane().add(lower, BorderLayout.SOUTH);
78 // ----------------------------------------------------------------
79 // Add tabs to tabbed here. Remember, tabs must implement PrefsTab.
80 // ----------------------------------------------------------------
82 GEN = Globals.lang("General"),
83 APP = Globals.lang("Appearance"),
84 GRP = Globals.lang("Groups"), // JZTODO lyrics
85 EXT = Globals.lang("External programs"),
86 TAB = Globals.lang("Entry table"),
87 COL = Globals.lang("Entry table columns"),
88 KEY = Globals.lang("Key pattern"),
89 PRE = Globals.lang("Entry preview"),
90 //JOU = Globals.lang("Journal names"),
91 ADV = Globals.lang("Advanced"),
92 NAM = Globals.lang("Name formatter");
94 ArrayList al = new ArrayList();
107 main.setLayout(cardLayout);
109 main.add(new GeneralTab(frame, _prefs), GEN);
110 main.add(new AdvancedTab(_prefs, parent.helpDiag), ADV);
111 main.add(new GroupsPrefsTab(_prefs), GRP);
112 main.add(new AppearancePrefsTab(_prefs), APP);
113 main.add(new ExternalTab(frame, _prefs, parent.helpDiag), EXT);
114 main.add(new TablePrefsTab(_prefs, parent), TAB);
115 main.add(new TableColumnsTab(_prefs, parent), COL);
116 main.add(new TabLabelPattern(_prefs, parent.helpDiag), KEY);
117 main.add(new PreviewPrefsTab(_prefs), PRE);
118 main.add(new NameFormatterTab(parent.helpDiag), NAM);
120 //main.add(new ManageJournalsPanel(frame), JOU);
122 upper.setBorder(BorderFactory.createEtchedBorder());
124 chooser = new JList(al.toArray());
125 chooser.setBorder(BorderFactory.createEtchedBorder());
126 // Set a prototype value to control the width of the list:
127 chooser.setPrototypeCellValue("This should be wide enough");
128 chooser.setSelectedIndex(0);
129 chooser.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
131 // Add the selection listener that will show the correct panel when selection changes:
132 chooser.addListSelectionListener(new ListSelectionListener() {
133 public void valueChanged(ListSelectionEvent e) {
134 if (e.getValueIsAdjusting())
136 String o = (String)chooser.getSelectedValue();
137 cardLayout.show(main, o);
141 JPanel one = new JPanel(), two = new JPanel();
142 one.setLayout(new BorderLayout());
143 two.setLayout(new BorderLayout());
144 one.add(chooser, BorderLayout.CENTER);
145 one.add(importPrefs, BorderLayout.SOUTH);
146 two.add(one, BorderLayout.CENTER);
147 two.add(exportPrefs, BorderLayout.SOUTH);
148 upper.setLayout(new BorderLayout());
149 upper.add(two, BorderLayout.WEST);
150 upper.add(main, BorderLayout.CENTER);
154 ok = new JButton(Globals.lang("Ok")),
155 cancel = new JButton(Globals.lang("Cancel"));
156 ok.addActionListener(new OkAction());
157 CancelAction cancelAction = new CancelAction();
158 cancel.addActionListener(cancelAction);
159 lower.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
160 ButtonBarBuilder bb = new ButtonBarBuilder(lower);
163 bb.addGridded(cancel);
169 ActionMap am = chooser.getActionMap();
170 InputMap im = chooser.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
171 im.put(frame.prefs().getKey("Close dialog"), "close");
172 am.put("close", cancelAction);
174 // Import and export actions:
175 exportPrefs.setToolTipText(Globals.lang("Export preferences to file"));
176 importPrefs.setToolTipText(Globals.lang("Import preferences from file"));
177 exportPrefs.addActionListener(new ActionListener() {
178 public void actionPerformed(ActionEvent e) {
179 String filename = Globals.getNewFile
180 (frame, new File(System.getProperty("user.home")),
181 ".xml", JFileChooser.SAVE_DIALOG, false);
182 if (filename == null)
184 File file = new File(filename);
185 if (!file.exists() ||
186 (JOptionPane.showConfirmDialog
187 (ths, "'"+file.getName()+"' "+Globals.lang("exists. Overwrite file?"),
188 Globals.lang("Export preferences"), JOptionPane.OK_CANCEL_OPTION)
189 == JOptionPane.OK_OPTION)) {
192 _prefs.exportPreferences(filename);
193 } catch (IOException ex) {
194 JOptionPane.showMessageDialog
195 (ths, Globals.lang("Could not export preferences")+": "+ex.getMessage(), Globals.lang("Export preferences"), JOptionPane.ERROR_MESSAGE);
196 //ex.printStackTrace();
203 importPrefs.addActionListener(new ActionListener() {
204 public void actionPerformed(ActionEvent e) {
205 String filename = Globals.getNewFile
206 (frame, new File(System.getProperty("user.home")),
207 ".xml", JFileChooser.OPEN_DIALOG, false);
208 if (filename == null)
212 _prefs.importPreferences(filename);
214 BibtexEntryType.loadCustomEntryTypes(_prefs);
215 frame.removeCachedEntryEditors();
216 } catch (IOException ex) {
217 JOptionPane.showMessageDialog
218 (ths, Globals.lang("Could not import preferences")+": "+ex.getMessage(), Globals.lang("Import preferences"), JOptionPane.ERROR_MESSAGE);
219 //ex.printStackTrace();
228 pack(); //setSize(440, 500);
231 class OkAction extends AbstractAction {
235 public void actionPerformed(ActionEvent e) {
237 AbstractWorker worker = new AbstractWorker() {
238 boolean ready = true;
240 // First check that all tabs are ready to close:
241 int count = main.getComponentCount();
242 Component[] comps = main.getComponents();
243 for (int i = 0; i < count; i++) {
244 if (!((PrefsTab)comps[i]).readyToClose()) {
246 return; // If not, break off.
249 // Then store settings and close:
250 for (int i = 0; i < count; i++) {
251 ( (PrefsTab)comps[i]).storeSettings();
253 Globals.prefs.flush();
254 //try { Thread.sleep(3000); } catch (InterruptedException ex) {}
256 public void update() {
260 MainTable.updateRenderers();
261 frame.setupAllTables();
262 frame.groupSelector.revalidateGroups(); // icons may have changed
263 frame.output(Globals.lang("Preferences recorded."));
266 worker.getWorker().run();
267 worker.getCallBack().update();
272 public void setValues() {
273 // Update all field values in the tabs:
274 int count = main.getComponentCount();
275 Component[] comps = main.getComponents();
276 for (int i = 0; i < count; i++) {
277 ((PrefsTab)comps[i]).setValues();
281 class CancelAction extends AbstractAction {
282 public CancelAction() {
286 public void actionPerformed(ActionEvent e) {
288 // Just close dialog without recording changes.