001: package jimm.datavision.gui;
002:
003: import jimm.datavision.Scripting;
004: import jimm.datavision.gui.cmd.ScriptingCommand;
005: import jimm.util.I18N;
006: import java.util.*;
007: import java.awt.BorderLayout;
008: import java.awt.event.*;
009: import javax.swing.*;
010: import javax.swing.event.DocumentListener;
011: import javax.swing.event.DocumentEvent;
012:
013: /**
014: * Editor dialog for a report's scripting language list and default language.
015: *
016: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
017: */
018: public class ScriptingWin extends EditWin implements ItemListener,
019: ActionListener, DocumentListener {
020:
021: protected static final int LANG_NAME_COLS = 16;
022: protected static final int CLASS_NAME_COLS = 40;
023:
024: protected Scripting scripting;
025: protected JComboBox defaultLangMenu;
026: protected JTextField langName;
027: protected JTextField langClass;
028: protected JButton addLangButton;
029: protected JButton testLangButton;
030: protected String defaultLang;
031: protected Map languages;
032:
033: /**
034: * Constructor.
035: *
036: * @param designer the window to which this dialog belongs
037: * @param scripting the scripting language information container
038: */
039: public ScriptingWin(Designer designer, Scripting scripting) {
040: super (designer, I18N.get("ScriptingWin.title"),
041: "ScriptingCommand.name");
042:
043: this .scripting = scripting;
044: defaultLang = scripting.getDefaultLanguage();
045: languages = new HashMap(scripting.getLanguages()); // Make a copy
046:
047: buildWindow();
048: pack();
049: setVisible(true);
050: }
051:
052: /**
053: * Builds the window contents.
054: */
055: protected void buildWindow() {
056: // All edit fields
057: JPanel editorPanel = buildEditor();
058:
059: // OK, Apply, Revert, and Cancel Buttons
060: JPanel buttonPanel = closeButtonPanel();
061:
062: // Add values and buttons to window
063: getContentPane().add(editorPanel, BorderLayout.CENTER);
064: getContentPane().add(buttonPanel, BorderLayout.SOUTH);
065:
066: fillEditFields();
067: }
068:
069: protected JPanel buildEditor() {
070: EditFieldLayout efl = new EditFieldLayout();
071:
072: defaultLangMenu = efl.addComboBox(I18N
073: .get("ScriptingWin.default_lang"), langChoices());
074: defaultLangMenu.addItemListener(this );
075:
076: efl.skipRow();
077: langName = efl.addTextField(I18N.get("ScriptingWin.add_name"),
078: "", LANG_NAME_COLS);
079: langClass = efl
080: .addTextField(I18N.get("ScriptingWin.add_class"), "",
081: CLASS_NAME_COLS);
082:
083: langName.getDocument().addDocumentListener(this );
084: langClass.getDocument().addDocumentListener(this );
085:
086: JPanel buttonPanel = new JPanel();
087: addLangButton = new JButton(I18N.get("ScriptingWin.add_button"));
088: testLangButton = new JButton(I18N
089: .get("ScriptingWin.test_button"));
090:
091: buttonPanel.add(addLangButton);
092: buttonPanel.add(testLangButton);
093: efl.add("", buttonPanel);
094:
095: addLangButton.addActionListener(this );
096: testLangButton.addActionListener(this );
097:
098: return efl.getPanel();
099: }
100:
101: protected Object[] langChoices() {
102: return languages.keySet().toArray();
103: }
104:
105: /** Fill with initial values. */
106: protected void fillEditFields() {
107: fillEditFields(defaultLang);
108: }
109:
110: /** Fill drop-down menu and text fields based on <var>lang</var>. */
111: protected void fillEditFields(String lang) {
112: defaultLangMenu.setSelectedItem(lang);
113: langName.setText(lang);
114: langClass.setText((String) languages.get(lang));
115: enableButtons();
116: }
117:
118: /** When language selected from popup, populate name and class fields. */
119: public void itemStateChanged(ItemEvent e) {
120: fillEditFields(e.getItem().toString());
121: }
122:
123: public void actionPerformed(ActionEvent e) {
124: String cmd = e.getActionCommand();
125: if (I18N.get("ScriptingWin.add_button").equals(cmd))
126: addLanguage();
127: else if (I18N.get("ScriptingWin.test_button").equals(cmd))
128: testLanguage();
129: else
130: super .actionPerformed(e);
131: }
132:
133: protected void addLanguage() {
134: String name = langName.getText().trim();
135: String klass = langClass.getText().trim();
136:
137: if (languages.get(name) == null) // Only add to menu if new
138: defaultLangMenu.addItem(name);
139: languages.put(name, klass); // Add or replace
140: fillEditFields();
141: }
142:
143: protected void testLanguage() {
144: String klass = langClass.getText().trim();
145:
146: String msg = null;
147: int type;
148: if (scripting.canFind(klass)) {
149: msg = I18N.get("ScriptingWin.lang_ok");
150: type = JOptionPane.INFORMATION_MESSAGE;
151: } else {
152: msg = I18N.get("ScriptingWin.lang_err");
153: type = JOptionPane.ERROR_MESSAGE;
154: }
155: JOptionPane.showMessageDialog(null, msg, I18N
156: .get("ScriptingWin.title"), type);
157: }
158:
159: public void changedUpdate(DocumentEvent e) {
160: enableButtons();
161: }
162:
163: public void insertUpdate(DocumentEvent e) {
164: enableButtons();
165: }
166:
167: public void removeUpdate(DocumentEvent e) {
168: enableButtons();
169: }
170:
171: protected void enableButtons() {
172: boolean enable = langName.getText().trim().length() > 0
173: && langClass.getText().trim().length() > 0;
174: addLangButton.setEnabled(enable);
175: testLangButton.setEnabled(enable);
176: }
177:
178: protected void doSave() {
179: defaultLang = (String) defaultLangMenu.getSelectedItem();
180: ScriptingCommand cmd = new ScriptingCommand(scripting,
181: defaultLang, languages);
182: cmd.perform();
183: commands.add(cmd);
184: }
185:
186: protected void doRevert() {
187: fillEditFields();
188: }
189:
190: }
|