807f1018978fd2c5efceaad31bc9c114b466f31a
[debian/jabref.git] / src / java / net / sf / jabref / PreambleEditor.java
1 /*
2 Copyright (C) 2003 Nizar N. Batada, Morten O. Alver
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.event.*;
31 import java.awt.*;
32 import javax.swing.*;
33 import net.sf.jabref.undo.UndoablePreambleChange;
34
35 public class PreambleEditor extends JDialog {
36
37     // A reference to the entry this object works on.
38     BibtexDatabase base;
39     BasePanel panel;
40     JabRefFrame baseFrame;
41     JabRefPreferences prefs;
42
43     // Layout objects.
44     GridBagLayout gbl = new GridBagLayout();
45     GridBagConstraints con = new GridBagConstraints();
46     JLabel lab;
47     Container conPane = getContentPane();
48     //JToolBar tlb = new JToolBar();
49     JPanel pan = new JPanel();
50     FieldEditor ed;
51
52
53     public PreambleEditor(JabRefFrame baseFrame,
54                           BasePanel panel, BibtexDatabase base,
55                           JabRefPreferences prefs) {
56         super(baseFrame);
57         this.baseFrame = baseFrame;
58         this.panel = panel;
59         this.base = base;
60         this.prefs = prefs;
61
62         addWindowListener(new WindowAdapter() {
63                 public void windowClosing(WindowEvent e) {
64                     closeAction.actionPerformed(null);
65                 }
66                 public void windowOpened(WindowEvent e) {
67                     ed.requestFocus();
68                 }
69             });
70         setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
71                 protected boolean accept(Component c) {
72                     return (super.accept(c) && (c instanceof FieldEditor));
73                 }
74             });
75
76         int prefHeight = (int)(GUIGlobals.PE_HEIGHT*GUIGlobals.FORM_HEIGHT[prefs.getInt("entryTypeFormHeightFactor")]);
77         setSize(GUIGlobals.FORM_WIDTH[prefs.getInt("entryTypeFormWidth")], prefHeight);
78
79         pan.setLayout(gbl);
80         con.fill = GridBagConstraints.BOTH;
81         con.weighty = 1;
82         con.insets = new Insets(10,5,10,5);
83
84         String content = base.getPreamble();
85
86         ed = new FieldTextArea(Globals.lang("Preamble"), ((content != null) ? content : ""));
87         setupJTextComponent((FieldTextArea)ed);
88
89         gbl.setConstraints(ed.getLabel(), con);
90         pan.add(ed.getLabel());
91
92         con.weightx = 1;
93
94         gbl.setConstraints(ed.getPane(), con);
95         pan.add(ed.getPane());
96
97         //tlb.add(closeAction);
98         //conPane.add(tlb, BorderLayout.NORTH);
99         conPane.add(pan, BorderLayout.CENTER);
100         setTitle(Globals.lang("Edit preamble"));
101     }
102
103     private void setupJTextComponent(javax.swing.text.JTextComponent ta) {
104         // Set up key bindings and focus listener for the FieldEditor.
105         ta.getInputMap().put(prefs.getKey("Close preamble editor"), "close");
106         ta.getActionMap().put("close", closeAction);
107         ta.getInputMap().put(prefs.getKey("Preamble editor, store changes"), "store");
108         ta.getActionMap().put("store", storeFieldAction);
109         ta.getInputMap().put(prefs.getKey("Close preamble editor"), "close");
110         ta.getActionMap().put("close", closeAction);
111
112         ta.getInputMap().put(prefs.getKey("Undo"), "undo");
113         ta.getActionMap().put("undo", undoAction);
114         ta.getInputMap().put(prefs.getKey("Redo"), "redo");
115         ta.getActionMap().put("redo", redoAction);
116
117
118         ta.addFocusListener(new FieldListener());
119     }
120
121     public void updatePreamble() {
122         ed.setText(base.getPreamble());
123     }
124
125    class FieldListener extends FocusAdapter {
126         /*
127          * Focus listener that fires the storeFieldAction when a FieldTextArea
128          * loses focus.
129          */
130        public void focusLost(FocusEvent e) {
131            if (!e.isTemporary())
132                storeFieldAction.actionPerformed(new ActionEvent(e.getSource(), 0, ""));
133        }
134
135    }
136
137     StoreFieldAction storeFieldAction = new StoreFieldAction();
138     class StoreFieldAction extends AbstractAction {
139         public StoreFieldAction() {
140             super("Store field value");
141             putValue(SHORT_DESCRIPTION, "Store field value");
142         }
143         public void actionPerformed(ActionEvent e) {
144             String toSet = null;
145             boolean set;
146             if (ed.getText().length() > 0)
147                 toSet = ed.getText();
148             // We check if the field has changed, since we don't want to mark the
149             // base as changed unless we have a real change.
150             if (toSet == null) {
151                 if (base.getPreamble() == null)
152                     set = false;
153                 else
154                     set = true;
155             } else {
156                 if ((base.getPreamble() != null)
157                     && toSet.equals(base.getPreamble()))
158                     set = false;
159                     else
160                     set = true;
161             }
162
163             if (set) {
164                 panel.undoManager.addEdit(new UndoablePreambleChange
165                                   (base, panel, base.getPreamble(), toSet));
166                 base.setPreamble(toSet);
167                 if ((toSet != null) && (toSet.length() > 0)) {
168                     ed.setLabelColor(GUIGlobals.validFieldColor);
169                     ed.setBackground(GUIGlobals.validFieldBackground);
170                 } else {
171                     ed.setLabelColor(GUIGlobals.nullFieldColor);
172                     ed.setBackground(GUIGlobals.validFieldBackground);
173                 }
174                 panel.markBaseChanged();
175             }
176
177         }
178     }
179
180     UndoAction undoAction = new UndoAction();
181     class UndoAction extends AbstractAction {
182         public UndoAction() {
183             super("Undo", new ImageIcon(GUIGlobals.undoIconFile));
184             putValue(SHORT_DESCRIPTION, "Undo");
185         }
186         public void actionPerformed(ActionEvent e) {
187             try {
188                 panel.runCommand("undo");
189             } catch (Throwable ex) {}
190         }
191     }
192
193     RedoAction redoAction = new RedoAction();
194     class RedoAction extends AbstractAction {
195         public RedoAction() {
196             super("Undo", new ImageIcon(GUIGlobals.redoIconFile));
197             putValue(SHORT_DESCRIPTION, "Redo");
198         }
199         public void actionPerformed(ActionEvent e) {
200             try {
201                 panel.runCommand("redo");
202             } catch (Throwable ex) {}
203         }
204     }
205
206     // The action concerned with closing the window.
207     CloseAction closeAction = new CloseAction();
208     class CloseAction extends AbstractAction {
209         public CloseAction() {
210             super(Globals.lang("Close window"));
211             //, new ImageIcon(GUIGlobals.closeIconFile));
212             //putValue(SHORT_DESCRIPTION, "Close window (Ctrl-Q)");
213         }
214         public void actionPerformed(ActionEvent e) {
215             storeFieldAction.actionPerformed(null);
216             panel.preambleEditorClosing();
217             dispose();
218         }
219     }
220
221 }