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