0a686c1fb5e0fb79c7f80d59e8507a6858251cf4
[debian/jabref.git] / src / java / net / sf / jabref / AdvancedTab.java
1 package net.sf.jabref;
2
3 import javax.swing.*;
4 import javax.swing.event.ChangeListener;
5 import javax.swing.event.ChangeEvent;
6 import java.awt.*;
7 import java.awt.event.*;
8 import com.jgoodies.forms.layout.*;
9 import com.jgoodies.forms.factories.*;
10 import com.jgoodies.forms.builder.*;
11 import net.sf.jabref.remote.RemoteListener;
12
13 public class AdvancedTab extends JPanel implements PrefsTab {
14
15     JabRefPreferences _prefs;
16     JabRefFrame _frame;
17     HelpDialog helpDiag;
18     HelpAction remoteHelp;
19     JPanel pan = new JPanel(),
20         lnf = new JPanel();
21     JLabel lab;
22     JCheckBox useDefault, useRemoteServer, useNativeFileDialogOnMac;
23     JTextField className, remoteServerPort;
24     JButton def1 = new JButton(Globals.lang("Default")),
25         def2 = new JButton(Globals.lang("Default"));
26     JPanel p1 = new JPanel(),
27         p2 = new JPanel();
28     String oldLnf = "";
29     boolean oldUseDef;
30     int oldPort = -1;
31
32     public AdvancedTab(JabRefPreferences prefs, HelpDialog diag) {
33         _prefs = prefs;
34
35
36     remoteHelp = new HelpAction(diag, GUIGlobals.remoteHelp, "Help",
37                 GUIGlobals.getIconUrl("helpSmall"));
38     useDefault = new JCheckBox(Globals.lang("Use other look and feel"));
39     useRemoteServer = new JCheckBox(Globals.lang("Listen for remote operation on port")+":");
40     useNativeFileDialogOnMac = new JCheckBox(Globals.lang("Use native file dialog"));
41     remoteServerPort = new JTextField();
42     className = new JTextField(50);
43     final JTextField clName = className;
44     useDefault.addChangeListener(new ChangeListener() {
45         public void stateChanged(ChangeEvent e) {
46             clName.setEnabled(((JCheckBox)e.getSource()).isSelected());
47         }
48         });
49
50     FormLayout layout = new FormLayout
51         ("1dlu, 8dlu, left:pref, 4dlu, fill:3dlu",//, 4dlu, fill:pref",// 4dlu, left:pref, 4dlu",
52          "");
53     DefaultFormBuilder builder = new DefaultFormBuilder(layout);
54     JPanel pan = new JPanel();
55
56     if (!Globals.ON_MAC) {
57         builder.appendSeparator(Globals.lang("Look and feel"));
58         JLabel lab = new JLabel(Globals.lang("Default look and feel")+": "
59                  +(Globals.ON_WIN ? GUIGlobals.windowsDefaultLookAndFeel :
60                    GUIGlobals.linuxDefaultLookAndFeel));
61         builder.nextLine();
62         builder.append(pan);
63         builder.append(lab);
64         builder.nextLine();
65         builder.append(pan);
66         builder.append(useDefault);
67         builder.nextLine();
68         builder.append(pan);
69         JPanel pan2 = new JPanel();
70         lab = new JLabel(Globals.lang("Class name")+":");
71         pan2.add(lab);
72         pan2.add(className);
73         builder.append(pan2);
74         builder.nextLine();
75         builder.append(pan);
76         lab = new JLabel(Globals.lang("Note that you must specify the fully qualified class name for the look and feel,"));
77         builder.append(lab);
78         builder.nextLine();
79         builder.append(pan);
80         lab = new JLabel(Globals.lang("and the class must be available in your classpath next time you start JabRef."));
81         builder.append(lab);
82         builder.nextLine();
83     }
84     builder.appendSeparator(Globals.lang("Remote operation"));
85     builder.nextLine();
86     builder.append(new JPanel());
87     JPanel p = new JPanel();
88     p.add(useRemoteServer);
89     p.add(remoteServerPort);
90     p.add(remoteHelp.getIconButton());
91     builder.append(p);
92
93     if (Globals.ON_MAC) {
94         builder.nextLine();
95         builder.appendSeparator(Globals.lang("Mac file dialog"));
96         builder.nextLine();
97         builder.append(new JPanel());
98         builder.append(useNativeFileDialogOnMac);
99     }
100
101     pan = builder.getPanel();
102     pan.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
103     setLayout(new BorderLayout());
104     add(pan, BorderLayout.CENTER);
105
106     }
107
108     public void setValues() {
109     oldUseDef = _prefs.getBoolean("useDefaultLookAndFeel");
110     oldLnf = _prefs.get("lookAndFeel");
111     useDefault.setSelected(!oldUseDef);
112     className.setText(oldLnf);
113     className.setEnabled(!oldUseDef);
114     useRemoteServer.setSelected(_prefs.getBoolean("useRemoteServer"));
115     oldPort = _prefs.getInt("remoteServerPort");
116     remoteServerPort.setText(String.valueOf(oldPort));
117         useNativeFileDialogOnMac.setSelected(Globals.prefs.getBoolean("useNativeFileDialogOnMac"));
118     }
119
120     public void storeSettings() {
121         _prefs.putBoolean("useDefaultLookAndFeel", !useDefault.isSelected());
122         _prefs.put("lookAndFeel", className.getText());
123         _prefs.putBoolean("useNativeFileDialogOnMac", useNativeFileDialogOnMac.isSelected());
124         try {
125             int port = Integer.parseInt(remoteServerPort.getText());
126             if (port != oldPort) {
127                 _prefs.putInt("remoteServerPort", port);
128                 /*JOptionPane.showMessageDialog(null, Globals.lang("You have changed the menu and label font size. "
129                         + "You must restart JabRef for this to come into effect."), Globals.lang("Changed font settings"),
130                         JOptionPane.WARNING_MESSAGE);*/
131             }
132
133         } catch (NumberFormatException ex) {
134             ex.printStackTrace();
135         }
136         _prefs.putBoolean("useRemoteServer", useRemoteServer.isSelected());
137         if (useRemoteServer.isSelected() && (JabRef.remoteListener == null)) {
138             // Start the listener now.
139
140             JabRef.remoteListener = RemoteListener.openRemoteListener(JabRef.ths);
141             if (JabRef.remoteListener != null) {
142                 JabRef.remoteListener.start();
143             }
144         } else if (!useRemoteServer.isSelected() && (JabRef.remoteListener != null)) {
145             JabRef.remoteListener.disable();
146             JabRef.remoteListener = null;
147         }
148
149         if ((useDefault.isSelected() == oldUseDef) ||
150             !oldLnf.equals(className.getText())) {
151             JOptionPane.showMessageDialog(null, Globals.lang("You have changed the look and feel setting. "
152                                                              +"You must restart JabRef for this to come into effect."), Globals.lang("Changed look and feel settings"),
153                                           JOptionPane.WARNING_MESSAGE);
154         }
155     }
156
157     public boolean readyToClose() {
158            
159         try {
160             int size = Integer.parseInt(remoteServerPort.getText());
161             return true; // Ok, the number was legal.
162         } catch (NumberFormatException ex) {
163             JOptionPane.showMessageDialog
164                     (null, Globals.lang("You must enter an integer value in the text field for") + " '" +
165                     Globals.lang("Remote server port") + "'", Globals.lang("Remote server port"),
166                             JOptionPane.ERROR_MESSAGE);
167             return false;
168         }
169
170     }
171
172 }