PageRenderTime 246ms CodeModel.GetById 80ms app.highlight 101ms RepoModel.GetById 60ms app.codeStats 0ms

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

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 423 lines | 353 code | 55 blank | 15 comment | 19 complexity | c237be54e722fab3cd12b3c971e6e0ab MD5 | raw file
  1package mpv5.ui.dialogs.subcomponents;
  2
  3import java.awt.Component;
  4import java.awt.event.ActionEvent;
  5import java.awt.event.ActionListener;
  6import java.io.File;
  7import java.io.FileNotFoundException;
  8import java.util.ArrayList;
  9import java.util.List;
 10import java.util.logging.Level;
 11import java.util.logging.Logger;
 12import javax.swing.DefaultListModel;
 13import javax.swing.JMenuItem;
 14import javax.swing.JPopupMenu;
 15import javax.swing.SwingUtilities;
 16import mpv5.data.PropertyStore;
 17import mpv5.db.common.Context;
 18import mpv5.db.common.DatabaseObject;
 19import mpv5.db.common.NodataFoundException;
 20import mpv5.db.common.QueryHandler;
 21import mpv5.db.objects.User;
 22import mpv5.globals.Messages;
 23import mpv5.logging.Log;
 24import mpv5.pluginhandling.MP5Plugin;
 25import mpv5.pluginhandling.YabsPluginLoader;
 26import mpv5.pluginhandling.Plugin;
 27import mpv5.ui.dialogs.ControlApplet;
 28import mpv5.ui.dialogs.Popup;
 29import mpv5.ui.frames.MPView;
 30import mpv5.usermanagement.MPSecurityManager;
 31import mpv5.pluginhandling.UserPlugin;
 32import mpv5.pluginhandling.YabsPlugin;
 33
 34/**
 35 *
 36 * 
 37 */
 38public class ControlPanel_Plugins extends javax.swing.JPanel implements ControlApplet {
 39
 40    private static final long serialVersionUID = 1L;
 41    /**
 42     * This unique name identifies this control applet
 43     */
 44    public final String UNAME = "plugins";
 45    private PropertyStore oldvalues;
 46    private static ControlPanel_Plugins ident;
 47    private JPopupMenu popup;
 48
 49    public ControlPanel_Plugins() {
 50        initComponents();
 51        addPopupMenu();
 52        refresh();
 53        setVisible(true);
 54    }
 55
 56    /**
 57     * Import a plugin
 58     * @param file
 59     * @throws FileNotFoundException
 60     */
 61    public void importf(File file) throws FileNotFoundException {
 62        String s = Popup.Enter_Value(Messages.ENTER_A_DESCRIPTION);
 63        if (s == null) {
 64            s = file.getName();
 65        }
 66        YabsPluginLoader.importPlugin(s, file);
 67        refresh();
 68    }
 69
 70    private void addPopupMenu() {
 71        popup = new JPopupMenu();
 72        JMenuItem jmi1;
 73        popup.add(jmi1 = new JMenuItem(Messages.ACTION_DELETE.toString()));
 74        popup.add(new JPopupMenu.Separator());
 75        JMenuItem jmi2;
 76        popup.add(jmi2 = new JMenuItem(Messages.ACTION_OPEN.toString()));
 77        popup.add(new JPopupMenu.Separator());
 78
 79        jmi1.addActionListener(new ActionListener() {
 80
 81            public void actionPerformed(ActionEvent e) {
 82                if (MPSecurityManager.checkAdminAccess()) {
 83                    Plugin gin = (Plugin) list.getSelectedValue();
 84                    gin.delete();
 85                    refresh();
 86                }
 87            }
 88        });
 89
 90        jmi2.addActionListener(new ActionListener() {
 91
 92            public void actionPerformed(ActionEvent e) {
 93                Plugin gin = (Plugin) list.getSelectedValue();
 94                try {
 95                    mpv5.YabsViewProxy.instance().getPluginLoader().loadPlugin(gin);
 96                } catch (Exception ex) {
 97                    Popup.error(ex);
 98                } catch (Throwable ex) {
 99                    Popup.error(new Exception(ex));
100                }
101            }
102        });
103    }
104
105    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
106    private void initComponents() {
107
108        jPanel3 = new javax.swing.JPanel();
109        jLabel3 = new javax.swing.JLabel();
110        jLabel4 = new javax.swing.JLabel();
111        jScrollPane1 = new javax.swing.JScrollPane();
112        list = new javax.swing.JList();
113        jPanel5 = new javax.swing.JPanel();
114        jLabel8 = new javax.swing.JLabel();
115        jLabel9 = new javax.swing.JLabel();
116        labeledTextChooser2 = new mpv5.ui.beans.LabeledTextChooser();
117        jButton4 = new javax.swing.JButton();
118        jButton3 = new javax.swing.JButton();
119        jPanel2 = new javax.swing.JPanel();
120        jButton2 = new javax.swing.JButton();
121        jButton1 = new javax.swing.JButton();
122
123        setBackground(new java.awt.Color(255, 255, 255));
124        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
125        setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ControlPanel_Plugins.border.title"))); // NOI18N
126        setName("Form"); // NOI18N
127        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));
128
129        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
130        jPanel3.setName("jPanel3"); // NOI18N
131
132        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
133        jLabel3.setText(bundle.getString("ControlPanel_Plugins.jLabel3.text")); // NOI18N
134        jLabel3.setName("jLabel3"); // NOI18N
135
136        jLabel4.setFont(new java.awt.Font("Dialog", 0, 11));
137        jLabel4.setText(bundle.getString("ControlPanel_Plugins.jLabel4.text")); // NOI18N
138        jLabel4.setName("jLabel4"); // NOI18N
139
140        jScrollPane1.setName("jScrollPane1"); // NOI18N
141
142        list.setToolTipText(bundle.getString("ControlPanel_Plugins.list.toolTipText")); // NOI18N
143        list.setName("list"); // NOI18N
144        list.addMouseListener(new java.awt.event.MouseAdapter() {
145            public void mouseClicked(java.awt.event.MouseEvent evt) {
146                listMouseClicked(evt);
147            }
148        });
149        jScrollPane1.setViewportView(list);
150
151        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
152        jPanel3.setLayout(jPanel3Layout);
153        jPanel3Layout.setHorizontalGroup(
154            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
155            .addGroup(jPanel3Layout.createSequentialGroup()
156                .addContainerGap()
157                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
158                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
159                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
160                        .addComponent(jLabel3)
161                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
162                .addContainerGap())
163        );
164        jPanel3Layout.setVerticalGroup(
165            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
166            .addGroup(jPanel3Layout.createSequentialGroup()
167                .addContainerGap()
168                .addComponent(jLabel3)
169                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
170                .addComponent(jLabel4)
171                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
172                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
173                .addContainerGap())
174        );
175
176        add(jPanel3);
177
178        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
179        jPanel5.setMaximumSize(new java.awt.Dimension(32767, 150));
180        jPanel5.setName("jPanel5"); // NOI18N
181        jPanel5.setPreferredSize(new java.awt.Dimension(453, 100));
182
183        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12));
184        jLabel8.setText(bundle.getString("ControlPanel_Plugins.jLabel8.text")); // NOI18N
185        jLabel8.setName("jLabel8"); // NOI18N
186
187        jLabel9.setFont(new java.awt.Font("Dialog", 0, 11));
188        jLabel9.setText(bundle.getString("ControlPanel_Plugins.jLabel9.text")); // NOI18N
189        jLabel9.setName("jLabel9"); // NOI18N
190
191        labeledTextChooser2.set_Label(bundle.getString("ControlPanel_Plugins.labeledTextChooser2._Label")); // NOI18N
192        labeledTextChooser2.set_LabelFont(new java.awt.Font("Tahoma", 0, 11));
193        labeledTextChooser2.setName("labeledTextChooser2"); // NOI18N
194
195        jButton4.setText(bundle.getString("ControlPanel_Plugins.jButton4.text")); // NOI18N
196        jButton4.setName("jButton4"); // NOI18N
197        jButton4.addActionListener(new java.awt.event.ActionListener() {
198            public void actionPerformed(java.awt.event.ActionEvent evt) {
199                jButton4ActionPerformed(evt);
200            }
201        });
202
203        jButton3.setText(bundle.getString("ControlPanel_Plugins.jButton3.text")); // NOI18N
204        jButton3.setName("jButton3"); // NOI18N
205        jButton3.addActionListener(new java.awt.event.ActionListener() {
206            public void actionPerformed(java.awt.event.ActionEvent evt) {
207                jButton3ActionPerformed(evt);
208            }
209        });
210
211        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
212        jPanel5.setLayout(jPanel5Layout);
213        jPanel5Layout.setHorizontalGroup(
214            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
215            .addGroup(jPanel5Layout.createSequentialGroup()
216                .addContainerGap()
217                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
218                    .addComponent(jLabel8)
219                    .addGroup(jPanel5Layout.createSequentialGroup()
220                        .addComponent(labeledTextChooser2, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
221                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
222                        .addComponent(jButton4)
223                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
224                        .addComponent(jButton3))
225                    .addComponent(jLabel9))
226                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
227        );
228        jPanel5Layout.setVerticalGroup(
229            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
230            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
231                .addContainerGap()
232                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
233                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
234                        .addComponent(jButton4)
235                        .addComponent(jButton3))
236                    .addGroup(jPanel5Layout.createSequentialGroup()
237                        .addComponent(jLabel8)
238                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
239                        .addComponent(jLabel9)
240                        .addGap(9, 9, 9)
241                        .addComponent(labeledTextChooser2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
242                .addGap(276, 276, 276))
243        );
244
245        add(jPanel5);
246
247        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
248        jPanel2.setName("jPanel2"); // NOI18N
249        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
250
251        jButton2.setText(bundle.getString("ControlPanel_Plugins.jButton2.text")); // NOI18N
252        jButton2.setName("jButton2"); // NOI18N
253        jButton2.addActionListener(new java.awt.event.ActionListener() {
254            public void actionPerformed(java.awt.event.ActionEvent evt) {
255                jButton2ActionPerformed(evt);
256            }
257        });
258        jPanel2.add(jButton2);
259
260        jButton1.setText(bundle.getString("ControlPanel_Plugins.jButton1.text")); // NOI18N
261        jButton1.setName("jButton1"); // NOI18N
262        jButton1.addActionListener(new java.awt.event.ActionListener() {
263            public void actionPerformed(java.awt.event.ActionEvent evt) {
264                jButton1ActionPerformed(evt);
265            }
266        });
267        jPanel2.add(jButton1);
268
269        add(jPanel2);
270    }// </editor-fold>//GEN-END:initComponents
271
272    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
273
274        try {
275            List data = DatabaseObject.getReferencedObjects(mpv5.db.objects.User.getCurrentUser(), Context.getPluginsToUsers());
276            for (int i = 0; i < data.size(); i++) {
277                ((UserPlugin) data.get(i)).delete();
278            }
279        } catch (NodataFoundException ex) {
280            Log.Debug(ex);
281        }
282
283        Object[] plugs = list.getSelectedValues();
284
285        for (int i = 0; i < plugs.length; i++) {
286            Plugin gin = (Plugin) plugs[i];
287
288            UserPlugin up = new UserPlugin();
289            up.setUsersids(mpv5.db.objects.User.getCurrentUser().__getIDS());
290            up.setPluginsids(gin.__getIDS());
291            up.setCname(gin.__getCname());
292            up.save();
293        }
294}//GEN-LAST:event_jButton1ActionPerformed
295
296    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
297
298        Object[] plugs = list.getSelectedValues();
299        for (int i = 0; i < plugs.length; i++) {
300            Plugin gin = (Plugin) plugs[i];
301            try {
302                mpv5.YabsViewProxy.instance().getPluginLoader().loadPlugin(gin);
303            } catch (Exception ex) {
304                Popup.error(ex);
305            } catch (Throwable ex) {
306                Popup.error(new Exception(ex));
307            }
308        }
309    }//GEN-LAST:event_jButton2ActionPerformed
310
311    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
312        YabsPlugin p = null;
313        try {
314            if (YabsPluginLoader.checkPlugin(new File(labeledTextChooser2.get_Text(true))) != null) {
315                p = YabsPluginLoader.checkPlugin(new File(labeledTextChooser2.get_Text(true)));
316                try {
317                    YabsPluginLoader.loadPlugin(p);
318                } catch (Exception ex) {
319                    Popup.error(ex);
320                } catch (Throwable ex) {
321                    Popup.error(new Exception(ex));
322                }
323            } else {
324                Popup.notice(Messages.ERROR_OCCURED);
325            }
326        } catch (Exception e) {
327            Popup.notice(Messages.ERROR_OCCURED);
328        }
329}//GEN-LAST:event_jButton3ActionPerformed
330
331    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
332        if (labeledTextChooser2.hasText()) {
333            try {
334                importf(new File(labeledTextChooser2.get_Text(true)));
335            } catch (FileNotFoundException ex) {
336                Popup.error(ex);
337            }
338        }
339}//GEN-LAST:event_jButton4ActionPerformed
340
341    private void listMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listMouseClicked
342
343        if (SwingUtilities.isRightMouseButton(evt) && !list.isSelectionEmpty() && list.locationToIndex(evt.getPoint()) == list.getSelectedIndex()) {
344            popup.show(list, evt.getX(), evt.getY());
345        }
346}//GEN-LAST:event_listMouseClicked
347
348    public void setValues(PropertyStore values) {
349        oldvalues = values;
350    }
351
352    public String getUname() {
353        return UNAME;
354    }
355
356    public void reset() {
357        setValues(oldvalues);
358    }
359    // Variables declaration - do not modify//GEN-BEGIN:variables
360    private javax.swing.JButton jButton1;
361    private javax.swing.JButton jButton2;
362    private javax.swing.JButton jButton3;
363    private javax.swing.JButton jButton4;
364    private javax.swing.JLabel jLabel3;
365    private javax.swing.JLabel jLabel4;
366    private javax.swing.JLabel jLabel8;
367    private javax.swing.JLabel jLabel9;
368    private javax.swing.JPanel jPanel2;
369    private javax.swing.JPanel jPanel3;
370    private javax.swing.JPanel jPanel5;
371    private javax.swing.JScrollPane jScrollPane1;
372    private mpv5.ui.beans.LabeledTextChooser labeledTextChooser2;
373    private javax.swing.JList list;
374    // End of variables declaration//GEN-END:variables
375
376    private void refresh() {
377        DefaultListModel xl = new DefaultListModel();
378
379        ArrayList<Plugin> data = new ArrayList<Plugin>();
380        try {
381            data = DatabaseObject.getObjects(Context.getPlugins());
382        } catch (NodataFoundException ex) {
383            Log.Debug(this, ex.getMessage());
384        }
385        ArrayList<UserPlugin> udata = new ArrayList<UserPlugin>();
386        try {
387            udata = DatabaseObject.getObjects(Context.getPluginsToUsers());
388        } catch (NodataFoundException ex) {
389            Log.Debug(this, ex.getMessage());
390        }
391        ArrayList<Integer> selected = new ArrayList<Integer>();
392
393        Log.Debug(this, "Plugins found: " + data.size());
394
395        for (int i = 0; i < data.size(); i++) {
396            Plugin lugin = data.get(i);
397            xl.addElement(lugin);
398
399            for (int ij = 0; ij < udata.size(); ij++) {
400                UserPlugin userPlugin = udata.get(ij);
401                if (lugin.__getIDS().intValue() == userPlugin.__getPluginsids()) {
402                    selected.add(ij);
403                }
404            }
405        }
406
407        int[] sel = new int[selected.size()];
408
409        for (int i = 0; i < selected.size(); i++) {
410            Integer integer = selected.get(i);
411            sel[i] = integer.intValue();
412        }
413
414        list.setModel(xl);
415        list.setSelectedIndices(sel);
416    }
417
418    public Component getAndRemoveActionPanel() {
419        this.remove(jPanel2);
420        validate();
421        return jPanel2;
422    }
423}