001: package jimm.datavision.gui;
002:
003: import jimm.datavision.Report;
004: import jimm.datavision.ErrorHandler;
005: import jimm.datavision.gui.cmd.Command;
006: import jimm.util.I18N;
007: import java.awt.Dimension;
008: import java.awt.BorderLayout;
009: import java.awt.event.ActionListener;
010: import java.awt.event.ActionEvent;
011: import javax.swing.*;
012:
013: /**
014: * This is the abstract superclass of windows used for editing paragraphs of
015: * code such as formulas and where clauses. The text field accepts dragged
016: * report fields by using a {@link DropListenerTextArea}.
017: *
018: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
019: */
020: public abstract class CodeEditorWin extends JDialog implements
021: ActionListener {
022:
023: protected static final Dimension EDIT_SIZE = new Dimension(400, 225);
024:
025: protected Designer designer;
026: protected JTextArea codeField;
027: protected Command command;
028: protected String errorSuffix;
029: protected String errorTitle;
030:
031: /**
032: * Constructor.
033: *
034: * @param designer the design window to which this dialog belongs
035: * @param report the report
036: * @param initialText the initial text to edit
037: * @param title the window title
038: * @param errorSuffixKey I18N lookup key for error text suffix; may be
039: * <code>null</code>
040: * @param errorTitleKey I18N lookup key for error window title; may be
041: * <code>null</code>
042: */
043: public CodeEditorWin(Designer designer, Report report,
044: String initialText, String title, String errorSuffixKey,
045: String errorTitleKey) {
046: super (designer.getFrame(), title);
047: this .designer = designer;
048: errorSuffix = errorSuffixKey == null ? null : I18N
049: .get(errorSuffixKey);
050: errorTitle = errorTitleKey == null ? null : I18N
051: .get(errorTitleKey);
052: buildWindow(report, initialText);
053: pack();
054: setVisible(true);
055: }
056:
057: /**
058: * Builds the window contents.
059: *
060: * @param report the report
061: * @param initialText initial value of string
062: */
063: protected void buildWindow(Report report, String initialText) {
064: codeField = new DropListenerTextArea(report,
065: initialText == null ? "" : initialText);
066: JScrollPane scroller = new JScrollPane(codeField);
067: scroller.setPreferredSize(EDIT_SIZE); // Set preferred size for editor
068:
069: // Add edit panel and Ok/Cancel buttons to window
070: getContentPane().add(scroller, BorderLayout.CENTER);
071: getContentPane().add(buildButtonPanel(), BorderLayout.SOUTH);
072:
073: new FocusSetter(codeField);
074: }
075:
076: /**
077: * Builds and returns a panel containing the OK and Cancel
078: *
079: * @return a panel
080: */
081: protected JPanel buildButtonPanel() {
082: JPanel buttonPanel = new JPanel();
083: JButton button;
084:
085: buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
086: button.addActionListener(this );
087: button.setDefaultCapable(true);
088:
089: buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
090: button.addActionListener(this );
091:
092: return buttonPanel;
093: }
094:
095: /**
096: * Handles the OK and Cancel buttons. If performing the command throws
097: * an error, we catch it and display it here and do not close this window.
098: *
099: * @param e action event
100: */
101: public void actionPerformed(ActionEvent e) {
102: String cmd = e.getActionCommand();
103: try {
104: if (I18N.get("GUI.ok").equals(cmd)) {
105: save(codeField.getText());
106: if (command != null)
107: designer.performCommand(command);
108: dispose();
109: } else if (I18N.get("GUI.cancel").equals(cmd)) {
110: dispose();
111: }
112: } catch (Exception ex) {
113: String str = ex.toString();
114: if (errorSuffix != null)
115: str += "\n" + errorSuffix;
116: ErrorHandler.error(str, errorTitle);
117:
118: command = null;
119: }
120: }
121:
122: /**
123: * Implement this to do whatevery you gotta do with the text.
124: * You'll probably want to set <var>command</var> so it gets sent
125: * to the design window to be performed.
126: *
127: * @param text the text in the edit box
128: */
129: public abstract void save(String text);
130:
131: }
|