KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > CodeEditorWin


1 package jimm.datavision.gui;
2 import jimm.datavision.Report;
3 import jimm.datavision.ErrorHandler;
4 import jimm.datavision.gui.cmd.Command;
5 import jimm.util.I18N;
6 import java.awt.Dimension JavaDoc;
7 import java.awt.BorderLayout JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.awt.event.ActionEvent JavaDoc;
10 import javax.swing.*;
11
12 /**
13  * This is the abstract superclass of windows used for editing paragraphs of
14  * code such as formulas and where clauses. The text field accepts dragged
15  * report fields by using a {@link DropListenerTextArea}.
16  *
17  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
18  */

19 public abstract class CodeEditorWin extends JDialog implements ActionListener JavaDoc {
20
21 protected static final Dimension JavaDoc EDIT_SIZE = new Dimension JavaDoc(400, 225);
22
23 protected Designer designer;
24 protected JTextArea codeField;
25 protected Command command;
26 protected String JavaDoc errorSuffix;
27 protected String JavaDoc errorTitle;
28
29 /**
30  * Constructor.
31  *
32  * @param designer the design window to which this dialog belongs
33  * @param report the report
34  * @param initialText the initial text to edit
35  * @param title the window title
36  * @param errorSuffixKey I18N lookup key for error text suffix; may be
37  * <code>null</code>
38  * @param errorTitleKey I18N lookup key for error window title; may be
39  * <code>null</code>
40  */

41 public CodeEditorWin(Designer designer, Report report, String JavaDoc initialText,
42              String JavaDoc title, String JavaDoc errorSuffixKey, String JavaDoc errorTitleKey)
43 {
44     super(designer.getFrame(), title);
45     this.designer = designer;
46     errorSuffix = errorSuffixKey == null ? null : I18N.get(errorSuffixKey);
47     errorTitle = errorTitleKey == null ? null : I18N.get(errorTitleKey);
48     buildWindow(report, initialText);
49     pack();
50     setVisible(true);
51 }
52
53 /**
54  * Builds the window contents.
55  *
56  * @param report the report
57  * @param initialText initial value of string
58  */

59 protected void buildWindow(Report report, String JavaDoc initialText) {
60     codeField =
61     new DropListenerTextArea(report,
62                  initialText == null ? "" : initialText);
63     JScrollPane scroller = new JScrollPane(codeField);
64     scroller.setPreferredSize(EDIT_SIZE); // Set preferred size for editor
65

66     // Add edit panel and Ok/Cancel buttons to window
67
getContentPane().add(scroller, BorderLayout.CENTER);
68     getContentPane().add(buildButtonPanel(), BorderLayout.SOUTH);
69
70     new FocusSetter(codeField);
71 }
72
73 /**
74  * Builds and returns a panel containing the OK and Cancel
75  *
76  * @return a panel
77  */

78 protected JPanel buildButtonPanel() {
79     JPanel buttonPanel = new JPanel();
80     JButton button;
81
82     buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
83     button.addActionListener(this);
84     button.setDefaultCapable(true);
85
86     buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
87     button.addActionListener(this);
88
89     return buttonPanel;
90 }
91
92 /**
93  * Handles the OK and Cancel buttons. If performing the command throws
94  * an error, we catch it and display it here and do not close this window.
95  *
96  * @param e action event
97  */

98 public void actionPerformed(ActionEvent JavaDoc e) {
99     String JavaDoc cmd = e.getActionCommand();
100     try {
101     if (I18N.get("GUI.ok").equals(cmd)) {
102         save(codeField.getText());
103         if (command != null)
104         designer.performCommand(command);
105         dispose();
106     }
107     else if (I18N.get("GUI.cancel").equals(cmd)) {
108         dispose();
109     }
110     }
111     catch (Exception JavaDoc ex) {
112     String JavaDoc str = ex.toString();
113     if (errorSuffix != null) str += "\n" + errorSuffix;
114     ErrorHandler.error(str, errorTitle);
115
116     command = null;
117     }
118 }
119
120 /**
121  * Implement this to do whatevery you gotta do with the text.
122  * You'll probably want to set <var>command</var> so it gets sent
123  * to the design window to be performed.
124  *
125  * @param text the text in the edit box
126  */

127 public abstract void save(String JavaDoc text);
128
129 }
130
Popular Tags