PageRenderTime 91ms CodeModel.GetById 20ms app.highlight 32ms RepoModel.GetById 18ms app.codeStats 0ms

/src/mpv5/ui/dialogs/subcomponents/ControlPanel_Konsole.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 232 lines | 189 code | 33 blank | 10 comment | 13 complexity | 64c398faa5c020e2bfb6fee6e661b6eb MD5 | raw file
  1package mpv5.ui.dialogs.subcomponents;
  2
  3import groovy.lang.Binding;
  4import groovy.lang.GroovyShell;
  5import java.awt.Component;
  6import java.awt.event.KeyEvent;
  7import java.io.File;
  8import java.io.FileReader;
  9import java.util.logging.Level;
 10import java.util.logging.Logger;
 11import javax.swing.text.BadLocationException;
 12import mpv5.YabsViewProxy;
 13import mpv5.data.PropertyStore;
 14import mpv5.db.common.QueryHandler;
 15import mpv5.globals.Messages;
 16import mpv5.logging.Log;
 17import mpv5.ui.dialogs.ControlApplet;
 18import mpv5.ui.dialogs.Popup;
 19import mpv5.ui.frames.MPView;
 20import mpv5.ui.popups.CopyPasteMenu;
 21import mpv5.usermanagement.MPSecurityManager;
 22import mpv5.utils.files.FileDirectoryHandler;
 23import mpv5.utils.files.FileReaderWriter;
 24import org.codehaus.groovy.control.CompilationFailedException;
 25
 26/**
 27 *
 28 * 
 29 */
 30public class ControlPanel_Konsole extends javax.swing.JPanel implements ControlApplet {
 31
 32    private static final long serialVersionUID = 1L;
 33    /**
 34     * This unique name identifies this control applet
 35     */
 36    public final String UNAME = "console";
 37    private PropertyStore oldvalues;
 38    private static ControlPanel_Konsole ident;
 39    private GroovyShell groovyShell;
 40
 41    public ControlPanel_Konsole() {
 42        if (MPSecurityManager.checkAdminAccess()) {
 43            initComponents();
 44            new CopyPasteMenu(jTextArea1);
 45            setVisible(true);
 46        }
 47    }
 48
 49    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 50    private void initComponents() {
 51
 52        jScrollPane1 = new javax.swing.JScrollPane();
 53        jTextArea1 = new javax.swing.JTextArea();
 54        jScrollPane2 = new javax.swing.JScrollPane();
 55        jTextArea2 = new javax.swing.JTextArea();
 56        jPanel2 = new javax.swing.JPanel();
 57        jButton3 = new javax.swing.JButton();
 58        jButton2 = new javax.swing.JButton();
 59        jButton1 = new javax.swing.JButton();
 60
 61        setBackground(new java.awt.Color(255, 255, 255));
 62        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
 63        setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ControlPanel_Konsole.border.title"))); // NOI18N
 64        setName("Form"); // NOI18N
 65        setLayout(new java.awt.BorderLayout());
 66
 67        jScrollPane1.setName("jScrollPane1"); // NOI18N
 68
 69        jTextArea1.setColumns(20);
 70        jTextArea1.setRows(5);
 71        jTextArea1.setName("jTextArea1"); // NOI18N
 72        jTextArea1.addKeyListener(new java.awt.event.KeyAdapter() {
 73            public void keyPressed(java.awt.event.KeyEvent evt) {
 74                jTextArea1KeyPressed(evt);
 75            }
 76        });
 77        jScrollPane1.setViewportView(jTextArea1);
 78
 79        add(jScrollPane1, java.awt.BorderLayout.NORTH);
 80
 81        jScrollPane2.setName("jScrollPane2"); // NOI18N
 82
 83        jTextArea2.setBackground(new java.awt.Color(0, 24, 24));
 84        jTextArea2.setColumns(20);
 85        jTextArea2.setForeground(new java.awt.Color(255, 255, 204));
 86        jTextArea2.setRows(5);
 87        jTextArea2.setText(bundle.getString("ControlPanel_Konsole.jTextArea2.text")); // NOI18N
 88        jTextArea2.setName("jTextArea2"); // NOI18N
 89        jScrollPane2.setViewportView(jTextArea2);
 90
 91        add(jScrollPane2, java.awt.BorderLayout.CENTER);
 92
 93        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
 94        jPanel2.setName("jPanel2"); // NOI18N
 95        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
 96
 97        jButton3.setText(bundle.getString("ControlPanel_Konsole.jButton3.text")); // NOI18N
 98        jButton3.setName("jButton3"); // NOI18N
 99        jButton3.addActionListener(new java.awt.event.ActionListener() {
100            public void actionPerformed(java.awt.event.ActionEvent evt) {
101                jButton3ActionPerformed(evt);
102            }
103        });
104        jPanel2.add(jButton3);
105
106        jButton2.setText(bundle.getString("ControlPanel_Konsole.jButton2.text")); // NOI18N
107        jButton2.setName("jButton2"); // NOI18N
108        jButton2.addActionListener(new java.awt.event.ActionListener() {
109            public void actionPerformed(java.awt.event.ActionEvent evt) {
110                jButton2ActionPerformed(evt);
111            }
112        });
113        jPanel2.add(jButton2);
114
115        jButton1.setText(bundle.getString("ControlPanel_Konsole.jButton1.text")); // NOI18N
116        jButton1.setName("jButton1"); // NOI18N
117        jButton1.addActionListener(new java.awt.event.ActionListener() {
118            public void actionPerformed(java.awt.event.ActionEvent evt) {
119                jButton1ActionPerformed(evt);
120            }
121        });
122        jPanel2.add(jButton1);
123
124        add(jPanel2, java.awt.BorderLayout.PAGE_END);
125    }// </editor-fold>//GEN-END:initComponents
126
127    private void jTextArea1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea1KeyPressed
128        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
129            process();
130        }
131    }//GEN-LAST:event_jTextArea1KeyPressed
132
133    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
134        File tf = FileDirectoryHandler.getTempFile("mp_konsole_export", "txt");
135        FileReaderWriter f = new FileReaderWriter(tf);
136        f.write(jTextArea1.getText());
137        mpv5.YabsViewProxy.instance().showFilesaveDialogFor(tf);
138
139}//GEN-LAST:event_jButton3ActionPerformed
140
141    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
142        jTextArea1.setText("");
143        jTextArea2.setText("");
144}//GEN-LAST:event_jButton2ActionPerformed
145
146    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
147
148        process();
149}//GEN-LAST:event_jButton1ActionPerformed
150
151    public void setValues(PropertyStore values) {
152    }
153
154    public String getUname() {
155        return UNAME;
156    }
157
158    public void reset() {
159        setValues(oldvalues);
160    }
161    // Variables declaration - do not modify//GEN-BEGIN:variables
162    private javax.swing.JButton jButton1;
163    private javax.swing.JButton jButton2;
164    private javax.swing.JButton jButton3;
165    private javax.swing.JPanel jPanel2;
166    private javax.swing.JScrollPane jScrollPane1;
167    private javax.swing.JScrollPane jScrollPane2;
168    private javax.swing.JTextArea jTextArea1;
169    private javax.swing.JTextArea jTextArea2;
170    // End of variables declaration//GEN-END:variables
171
172    @Override
173    public Component getAndRemoveActionPanel() {
174        this.remove(jPanel2);
175        validate();
176        return jPanel2;
177    }
178
179    private void process() {
180        if (MPSecurityManager.checkAdminAccess()) {
181            if (jTextArea1.getText().startsWith("groovy")) {
182                runGroovy();
183            } else {
184                runSQL();
185            }
186            jTextArea1.setCaretPosition(0);
187        }}
188    
189
190    private void runSQL() {
191        for (int i = 0; i < jTextArea1.getLineCount(); i++) {
192            try {
193                String command = jTextArea1.getText().substring(jTextArea1.getLineStartOffset(i), jTextArea1.getLineEndOffset(i));
194                if (command.contains(";")) {
195                    command = command.substring(0, command.lastIndexOf(";"));
196                }
197                if (command.length() > 1) {
198                    if (command.toLowerCase().contains("update")) {
199                        QueryHandler.getConnection().freeUpdateQuery(command, jTextArea2, MPSecurityManager.CREATE_OR_DELETE, Messages.SEE_LOG.getValue());
200                    } else {
201                        QueryHandler.getConnection().freeQuery(command, jTextArea2, MPSecurityManager.CREATE_OR_DELETE, Messages.SEE_LOG.getValue());
202                    }
203                }
204                jTextArea1.replaceRange(command + ";--ok", jTextArea1.getLineStartOffset(i), jTextArea1.getLineEndOffset(i));
205            } catch (Exception ex) {
206                Log.Debug(this, ex.getMessage());
207            }
208        }
209    }
210
211    private void runGroovy() {
212        try {
213            jTextArea2.setText(String.valueOf(
214                    getGroovyShell().evaluate(jTextArea1.getText().replace("groovy", ""))));
215            jTextArea1.setText(null);
216        } catch (Exception exception) {
217            Log.Debug(exception);
218            jTextArea2.setText(exception.getMessage());
219        }
220    }
221
222    private GroovyShell getGroovyShell() {
223        synchronized (this) {
224            if (groovyShell == null) {
225                Binding binding = new Binding();
226                binding.setVariable("db", QueryHandler.instanceOf());
227                groovyShell = new GroovyShell(binding);
228            }
229            return groovyShell;
230        }
231    }
232}