001: package net.sourceforge.squirrel_sql.plugins.userscript.kernel;
002:
003: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
004: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
005: import net.sourceforge.squirrel_sql.fw.util.StringManager;
006: import net.sourceforge.squirrel_sql.plugins.userscript.UserScriptPlugin;
007:
008: import javax.swing.*;
009: import java.awt.*;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import java.net.URL;
013:
014: public class ScriptPropertiesController {
015:
016: private static final StringManager s_stringMgr = StringManagerFactory
017: .getStringManager(ScriptPropertiesController.class);
018:
019: private ScriptPropsListener m_scriptPropsListener;
020:
021: private Script m_script;
022: private boolean m_isNewScript;
023: private ScriptPropertiesDialog m_dlg;
024: private Frame m_owner;
025: private UserScriptPlugin m_plugin;
026:
027: public ScriptPropertiesController(Frame owner,
028: UserScriptPlugin plugin) {
029: this (owner, null, plugin);
030: }
031:
032: public ScriptPropertiesController(Frame owner, Script script,
033: UserScriptPlugin plugin) {
034: m_owner = owner;
035: m_plugin = plugin;
036: m_dlg = new ScriptPropertiesDialog(owner);
037: GUIUtils.centerWithinParent(m_dlg);
038: m_dlg.setVisible(true);
039:
040: if (null == script) {
041: m_script = new Script();
042: m_isNewScript = true;
043: } else {
044: m_script = script;
045: m_dlg.txtName.setText(m_script.getName());
046: m_dlg.txtScriptClass.setText(m_script.getScriptClass());
047: m_dlg.chkShowInStandard.setSelected(m_script
048: .isShowInStandard());
049: m_isNewScript = false;
050: }
051:
052: m_dlg.btnCheck.addActionListener(new ActionListener() {
053: public void actionPerformed(ActionEvent e) {
054: onCheck();
055: }
056: });
057:
058: m_dlg.btnOk.addActionListener(new ActionListener() {
059: public void actionPerformed(ActionEvent e) {
060: onOK();
061: }
062: });
063:
064: m_dlg.btnCancel.addActionListener(new ActionListener() {
065: public void actionPerformed(ActionEvent e) {
066: close();
067: }
068: });
069:
070: }
071:
072: private void onCheck() {
073: URL classUrl = findClass(m_dlg.txtScriptClass.getText());
074:
075: String msg;
076: if (null == classUrl) {
077: // i18n[userscript.classNotFound=Class {0} not found]
078: msg = s_stringMgr.getString("userscript.classNotFound",
079: m_dlg.txtScriptClass.getText());
080: } else {
081: String params[] = new String[] {
082: m_dlg.txtScriptClass.getText(), classUrl.getFile() };
083: // i18n[userscript.classNotFoundIn=Class {0} found in\n{1}]
084: msg = s_stringMgr.getString("userscript.classNotFoundIn",
085: params);
086: }
087:
088: JOptionPane.showMessageDialog(m_owner, msg);
089: }
090:
091: public URL findClass(final String className) {
092: return m_plugin.getUserScriptClassLoader().getResource(
093: asResourceName(className));
094: }
095:
096: protected static String asResourceName(String resource) {
097: resource = resource.replace('.', '/');
098: resource = resource + ".class";
099: return resource;
100: }
101:
102: private void onOK() {
103: if (null == m_dlg.txtName.getText()
104: || "".equals(m_dlg.txtName.getText().trim())) {
105: // i18n[userscript.enterScriptName=Please enter a script name]
106: JOptionPane.showMessageDialog(m_owner, s_stringMgr
107: .getString("userscript.enterScriptName"));
108: return;
109: }
110: if (null == m_dlg.txtScriptClass.getText()
111: || "".equals(m_dlg.txtScriptClass.getText().trim())) {
112: // i18n[userscript.enterScriptClass=Please enter a script class]
113: JOptionPane.showMessageDialog(m_owner, s_stringMgr
114: .getString("userscript.enterScriptClass"));
115: return;
116: }
117:
118: m_script.setName(m_dlg.txtName.getText());
119: m_script.setScriptClass(m_dlg.txtScriptClass.getText());
120: m_script
121: .setShowInStandard(m_dlg.chkShowInStandard.isSelected());
122:
123: close();
124:
125: if (m_isNewScript) {
126: m_scriptPropsListener.newScript(m_script);
127: } else {
128: m_scriptPropsListener.scriptEdited(m_script);
129: }
130: }
131:
132: private void close() {
133: m_dlg.setVisible(false);
134: m_dlg.dispose();
135: }
136:
137: public void setScriptPropsListener(
138: ScriptPropsListener scriptPropsListener) {
139: m_scriptPropsListener = scriptPropsListener;
140: }
141: }
|