1 package net.sf.bibkeeper;
2
3 import java.awt.event;
4 import java.awt;
5 import javax.swing;
6 import net.sf.bibkeeper.undo.UndoablePreambleChange;
7
8 public class PreambleEditor extends JDialog {
9
10 // A reference to the entry this object works on.
11 BibtexDatabase base;
12 BibtexBaseFrame baseFrame;
13 BibkeeperPrefs prefs;
14
15 // Layout objects.
16 GridBagLayout gbl = new GridBagLayout();
17 GridBagConstraints con = new GridBagConstraints();
18 JLabel lab;
19 Container conPane = getContentPane();
20 //JToolBar tlb = new JToolBar();
21 JPanel pan = new JPanel();
22 FieldEditor ed;
23
24
25 public PreambleEditor(BibtexBaseFrame baseFrame, BibtexDatabase base, BibkeeperPrefs prefs) {
26 super(baseFrame);
27 this.baseFrame = baseFrame;
28 this.base = base;
29 this.prefs = prefs;
30
31 addWindowListener(new WindowAdapter() {
32 public void windowClosing(WindowEvent e) {
33 closeAction.actionPerformed(null);
34 }
35 public void windowOpened(WindowEvent e) {
36 ed.requestFocus();
37 }
38 });
39 setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
40 protected boolean accept(Component c) {
41 return (super.accept(c) && (c instanceof FieldEditor));
42 }
43 });
44
45 int prefHeight = (int)(GUIGlobals.PE_HEIGHT*GUIGlobals.FORM_HEIGHT[prefs.getInt("entryTypeFormHeightFactor")]);
46 setSize(GUIGlobals.FORM_WIDTH[prefs.getInt("entryTypeFormWidth")], prefHeight);
47
48 pan.setLayout(gbl);
49 con.fill = GridBagConstraints.BOTH;
50 con.weighty = 1;
51 con.insets = new Insets(10,5,10,5);
52
53 String content = base.getPreamble();
54
55 ed = new FieldTextArea("Preamble", ((content != null) ? content : ""));
56 setupJTextComponent((FieldTextArea)ed);
57 gbl.setConstraints(ed.getLabel(), con);
58 pan.add(ed.getLabel());
59
60 con.weightx = 1;
61
62 gbl.setConstraints(ed.getPane(), con);
63 pan.add(ed.getPane());
64
65 //tlb.add(closeAction);
66 //conPane.add(tlb, BorderLayout.NORTH);
67 conPane.add(pan, BorderLayout.CENTER);
68 setTitle("Edit preamble");
69 }
70
71 private void setupJTextComponent(javax.swing.text.JTextComponent ta) {
72 // Set up key bindings and focus listener for the FieldEditor.
73 ta.getInputMap().put(KeyStroke.getKeyStroke(GUIGlobals.closeKey), "close");
74 ta.getActionMap().put("close", closeAction);
75 ta.getInputMap().put(KeyStroke.getKeyStroke(GUIGlobals.storeFieldKey), "store");
76 ta.getActionMap().put("store", storeFieldAction);
77 ta.getInputMap().put(KeyStroke.getKeyStroke(GUIGlobals.copyKeyKey), "copyKey");
78 ta.getInputMap().put(GUIGlobals.exitDialog, "close");
79 ta.getActionMap().put("close", closeAction);
80
81 ta.getInputMap().put(GUIGlobals.undoStroke, "undo");
82 ta.getActionMap().put("undo", undoAction);
83 ta.getInputMap().put(GUIGlobals.redoStroke, "redo");
84 ta.getActionMap().put("redo", redoAction);
85
86
87 ta.addFocusListener(new FieldListener());
88 }
89
90 public void updatePreamble() {
91 ed.setText(base.getPreamble());
92 }
93
94 class FieldListener extends FocusAdapter {
95 /*
96 * Focus listener that fires the storeFieldAction when a FieldTextArea
97 * loses focus.
98 */
99 public void focusLost(FocusEvent e) {
100 if (!e.isTemporary())
101 storeFieldAction.actionPerformed(new ActionEvent(e.getSource(), 0, ""));
102 }
103
104 }
105
106 StoreFieldAction storeFieldAction = new StoreFieldAction();
107 class StoreFieldAction extends AbstractAction {
108 public StoreFieldAction() {
109 super("Store field value");
110 putValue(SHORT_DESCRIPTION, "Store field value");
111 }
112 public void actionPerformed(ActionEvent e) {
113 String toSet = null;
114 boolean set;
115 if (ed.getText().length() > 0)
116 toSet = ed.getText();
117 // We check if the field has changed, since we don't want to mark the
118 // base as changed unless we have a real change.
119 if (toSet == null) {
120 if (base.getPreamble() == null)
121 set = false;
122 else
123 set = true;
124 } else {
125 if ((base.getPreamble() != null)
126 && toSet.equals(base.getPreamble().toString()))
127 set = false;
128 else
129 set = true;
130 }
131
132 if (set) {
133 baseFrame.undoManager.addEdit(new UndoablePreambleChange
134 (base, baseFrame, base.getPreamble(), toSet));
135 base.setPreamble(toSet);
136 if ((toSet != null) && (toSet.length() > 0)) {
137 ed.setLabelColor(GUIGlobals.validFieldColor);
138 ed.setBackground(GUIGlobals.validFieldBackground);
139 } else {
140 ed.setLabelColor(GUIGlobals.nullFieldColor);
141 ed.setBackground(GUIGlobals.validFieldBackground);
142 }
143 baseFrame.markBaseChanged();
144 }
145
146 }
147 }
148
149 UndoAction undoAction = new UndoAction();
150 class UndoAction extends AbstractAction {
151 public UndoAction() {
152 super("Undo", new ImageIcon(GUIGlobals.undoIconFile));
153 putValue(SHORT_DESCRIPTION, "Undo");
154 }
155 public void actionPerformed(ActionEvent e) {
156 baseFrame.undoAction.actionPerformed(null);
157 }
158 }
159
160 RedoAction redoAction = new RedoAction();
161 class RedoAction extends AbstractAction {
162 public RedoAction() {
163 super("Undo", new ImageIcon(GUIGlobals.redoIconFile));
164 putValue(SHORT_DESCRIPTION, "Redo");
165 }
166 public void actionPerformed(ActionEvent e) {
167 baseFrame.redoAction.actionPerformed(null);
168 }
169 }
170
171 // The action concerned with closing the window.
172 CloseAction closeAction = new CloseAction();
173 class CloseAction extends AbstractAction {
174 public CloseAction() {
175 super("Close window",
176 new ImageIcon(GUIGlobals.closeIconFile));
177 putValue(SHORT_DESCRIPTION, "Close window (Ctrl-Q)");
178 }
179 public void actionPerformed(ActionEvent e) {
180 storeFieldAction.actionPerformed(null);
181 baseFrame.preambleEditorClosing();
182 dispose();
183 }
184 }
185
186 }