PageRenderTime 323ms CodeModel.GetById 61ms app.highlight 211ms RepoModel.GetById 40ms app.codeStats 1ms

/src/mpv5/ui/panels/MassPrintPanel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 747 lines | 655 code | 66 blank | 26 comment | 58 complexity | 04af50f04f65f1c2114abbb29c5dd8f0 MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5
  6/*
  7 * MassPrintUI.java
  8 *
  9 * Created on 09.05.2011, 22:07:37
 10 */
 11package mpv5.ui.panels;
 12
 13import enoa.handler.TemplateHandler;
 14import java.awt.Font;
 15import java.awt.GraphicsEnvironment;
 16import java.awt.event.ActionEvent;
 17import java.awt.event.ActionListener;
 18import java.awt.event.MouseEvent;
 19import java.io.ByteArrayOutputStream;
 20import java.io.File;
 21import java.io.IOException;
 22import java.util.ArrayList;
 23import java.util.HashMap;
 24import java.util.List;
 25import javax.swing.DefaultComboBoxModel;
 26import javax.swing.DefaultListModel;
 27import javax.swing.JComboBox;
 28import javax.swing.JTextPane;
 29import javax.swing.JToggleButton;
 30import javax.swing.text.AttributeSet;
 31import javax.swing.text.BadLocationException;
 32import javax.swing.text.MutableAttributeSet;
 33import javax.swing.text.SimpleAttributeSet;
 34import javax.swing.text.StyleConstants;
 35import javax.swing.text.StyledDocument;
 36import javax.swing.text.StyledEditorKit;
 37import javax.swing.text.rtf.RTFEditorKit;
 38import mpv5.db.common.Context;
 39import mpv5.db.common.DatabaseObject;
 40import mpv5.db.common.DatabaseSearch;
 41import mpv5.db.common.NodataFoundException;
 42import mpv5.db.common.QueryHandler;
 43import mpv5.db.common.ReturnValue;
 44import mpv5.db.objects.Contact;
 45import mpv5.db.objects.MailMessage;
 46import mpv5.db.objects.MassprintRules;
 47import mpv5.db.objects.Template;
 48import mpv5.db.objects.User;
 49import mpv5.globals.Constants;
 50import mpv5.globals.LocalSettings;
 51import mpv5.globals.Messages;
 52import mpv5.logging.Log;
 53import mpv5.ui.dialogs.DialogForFile;
 54import mpv5.ui.dialogs.Popup;
 55import mpv5.ui.menus.ListMenuItem;
 56import mpv5.ui.popups.ListPopUp;
 57import mpv5.usermanagement.MPSecurityManager;
 58import mpv5.utils.export.Export;
 59import mpv5.utils.jobs.Job;
 60import mpv5.utils.jobs.Waitable;
 61
 62/**
 63 *
 64 * @author Samsung SA11
 65 */
 66public class MassPrintPanel
 67        extends javax.swing.JPanel
 68        implements ActionListener {
 69
 70    private static MassPrintPanel me;
 71    private final DefaultListModel model;
 72    private static final long serialVersionUID = -4425127949239926332L;
 73
 74    /**
 75     * Singleton
 76     * @return
 77     */
 78    public static MassPrintPanel instanceOf() {
 79        if (me == null) {
 80            me = new MassPrintPanel();
 81        }
 82        me.setContacts();
 83        me.setTemplateList();
 84        me.setMessageTemplateList();
 85        return me;
 86    }
 87    private int cursorPosition;
 88
 89    /** Creates new form MassPrintUI */
 90    public MassPrintPanel() {
 91        model = new DefaultListModel();
 92        initComponents();
 93        setName("MassPrintPanel");
 94    }
 95
 96    /** This method is called from within the constructor to
 97     * initialize the form.
 98     * WARNING: Do NOT modify this code. The content of this method is
 99     * always regenerated by the Form Editor.
100     */
101    @SuppressWarnings("unchecked")
102    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
103    private void initComponents() {
104
105        jPanel3 = new javax.swing.JPanel();
106        jComboBox2 = new javax.swing.JComboBox();
107        String fontSizes[] = {"8", "10", "11", "12", "14", "16", "18",
108            "20", "24", "30", "36", "40", "48", "60", "72"};
109        for (int index = 0; index < fontSizes.length; index++) {
110            jComboBox2.addItem(fontSizes[index]);
111        }
112        jComboBox1 = new javax.swing.JComboBox();
113        try {
114            String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
115            Log.Debug(this, "Ermittelte Fonts: " + fonts.length);
116
117            for (int index = 0; index < fonts.length; index++) {
118                jComboBox1.addItem(fonts[index]);
119            }
120        } catch (Exception e) {
121
122            Log.Debug(MassPrintPanel.class, e);
123        }
124        jToggleButton3 = new javax.swing.JToggleButton();
125        jToggleButton1 = new javax.swing.JToggleButton();
126        jToggleButton2 = new javax.swing.JToggleButton();
127        jLabel3 = new javax.swing.JLabel();
128        jScrollPane1 = new javax.swing.JScrollPane();
129        RTF_Text = new javax.swing.JTextPane();
130        dateTo = new mpv5.ui.beans.DateChooser();
131        templates = new javax.swing.JComboBox();
132        jButton1 = new javax.swing.JButton();
133        jLabel5 = new javax.swing.JLabel();
134        jButton2 = new javax.swing.JButton();
135        messageTpl = new javax.swing.JComboBox();
136        jLabel6 = new javax.swing.JLabel();
137        jLabel7 = new javax.swing.JLabel();
138        jPanel1 = new javax.swing.JPanel();
139        jScrollPane2 = new javax.swing.JScrollPane();
140        contactlist = new javax.swing.JList();
141
142        setName("Form"); // NOI18N
143        setPreferredSize(new java.awt.Dimension(1004, 333));
144        setLayout(new java.awt.BorderLayout());
145
146        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
147        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ConversationPanel.jPanel3.border.title"))); // NOI18N
148        jPanel3.setPreferredSize(new java.awt.Dimension(818, 333));
149
150        jComboBox2.setName("jComboBox2"); // NOI18N
151        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
152            public void actionPerformed(java.awt.event.ActionEvent evt) {
153                jComboBox2ActionPerformed(evt);
154            }
155        });
156
157        jComboBox1.setName("jComboBox1"); // NOI18N
158        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
159            public void actionPerformed(java.awt.event.ActionEvent evt) {
160                jComboBox1ActionPerformed(evt);
161            }
162        });
163
164        jToggleButton3.setFont(jToggleButton3.getFont());
165        jToggleButton3.setText(bundle.getString("ConversationPanel.jToggleButton3.text")); // NOI18N
166        jToggleButton3.setName("jToggleButton3"); // NOI18N
167        jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
168            public void actionPerformed(java.awt.event.ActionEvent evt) {
169                jToggleButton3ActionPerformed(evt);
170            }
171        });
172
173        jToggleButton1.setFont(new java.awt.Font("Tahoma", 1, 11));
174        jToggleButton1.setText(bundle.getString("ConversationPanel.jToggleButton1.text")); // NOI18N
175        jToggleButton1.setName("jToggleButton1"); // NOI18N
176        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
177            public void actionPerformed(java.awt.event.ActionEvent evt) {
178                jToggleButton1ActionPerformed(evt);
179            }
180        });
181
182        jToggleButton2.setFont(new java.awt.Font("Tahoma", 2, 11));
183        jToggleButton2.setText(bundle.getString("ConversationPanel.jToggleButton2.text")); // NOI18N
184        jToggleButton2.setName("jToggleButton2"); // NOI18N
185        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
186            public void actionPerformed(java.awt.event.ActionEvent evt) {
187                jToggleButton2ActionPerformed(evt);
188            }
189        });
190
191        jLabel3.setText(bundle.getString("ConversationPanel.jLabel3.text")); // NOI18N
192        jLabel3.setName("jLabel3"); // NOI18N
193
194        jScrollPane1.setName("jScrollPane1"); // NOI18N
195
196        RTF_Text.setDocument(new javax.swing.text.rtf.RTFEditorKit().createDefaultDocument());
197        RTF_Text.setEditorKit(new javax.swing.text.rtf.RTFEditorKit());
198        RTF_Text.setFont(Font.decode(LocalSettings.getProperty(LocalSettings.DEFAULT_FONT)).deriveFont(Font.PLAIN, 11));
199        RTF_Text.setName("RTF_Text"); // NOI18N
200        RTF_Text.addCaretListener(new javax.swing.event.CaretListener() {
201            public void caretUpdate(javax.swing.event.CaretEvent evt) {
202                RTF_TextCaretUpdate(evt);
203            }
204        });
205        jScrollPane1.setViewportView(RTF_Text);
206
207        dateTo.setMaximumSize(new java.awt.Dimension(32767, 20));
208        dateTo.setMinimumSize(new java.awt.Dimension(120, 20));
209        dateTo.setName("dateTo"); // NOI18N
210        dateTo.setPreferredSize(new java.awt.Dimension(150, 20));
211
212        templates.setModel(new DefaultComboBoxModel());
213        templates.setName("templates"); // NOI18N
214
215        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/32/bright_printer.png"))); // NOI18N
216        jButton1.setText(bundle.getString("MassPrintPanel.jButton1.text")); // NOI18N
217        jButton1.setName("jButton1"); // NOI18N
218        jButton1.addActionListener(new java.awt.event.ActionListener() {
219            public void actionPerformed(java.awt.event.ActionEvent evt) {
220                jButton1ActionPerformed(evt);
221            }
222        });
223
224        jLabel5.setText(bundle.getString("ConversationPanel.jLabel3.text")); // NOI18N
225        jLabel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());
226        jLabel5.setName("jLabel5"); // NOI18N
227
228        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mpv5/resources/images/32/acroread.png"))); // NOI18N
229        jButton2.setText(bundle.getString("MassPrintPanel.jButton2.text_1")); // NOI18N
230        jButton2.setName("jButton2"); // NOI18N
231        jButton2.addActionListener(new java.awt.event.ActionListener() {
232            public void actionPerformed(java.awt.event.ActionEvent evt) {
233                jButton2ActionPerformed(evt);
234            }
235        });
236
237        messageTpl.setModel(new DefaultComboBoxModel());
238        messageTpl.setName("messageTpl"); // NOI18N
239        messageTpl.addActionListener(new java.awt.event.ActionListener() {
240            public void actionPerformed(java.awt.event.ActionEvent evt) {
241                messageTplActionPerformed(evt);
242            }
243        });
244
245        jLabel6.setText(bundle.getString("MassPrintPanel.jLabel6.text")); // NOI18N
246        jLabel6.setName("jLabel6"); // NOI18N
247
248        jLabel7.setText(bundle.getString("MassPrintPanel.jLabel7.text")); // NOI18N
249        jLabel7.setName("jLabel7"); // NOI18N
250
251        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
252        jPanel3.setLayout(jPanel3Layout);
253        jPanel3Layout.setHorizontalGroup(
254            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
255            .addGroup(jPanel3Layout.createSequentialGroup()
256                .addContainerGap()
257                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
258                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 820, Short.MAX_VALUE)
259                    .addGroup(jPanel3Layout.createSequentialGroup()
260                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
261                            .addGroup(jPanel3Layout.createSequentialGroup()
262                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
263                                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
264                                    .addComponent(templates, javax.swing.GroupLayout.Alignment.LEADING, 0, 183, Short.MAX_VALUE))
265                                .addGap(18, 18, 18)
266                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
267                                    .addComponent(messageTpl, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
268                                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
269                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
270                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
271                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
272                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
273                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
274                            .addGroup(jPanel3Layout.createSequentialGroup()
275                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
276                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
277                                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
278                                .addGap(7, 7, 7)
279                                .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
280                                .addGap(2, 2, 2)
281                                .addComponent(jToggleButton2)
282                                .addGap(5, 5, 5)
283                                .addComponent(jToggleButton3)
284                                .addGap(22, 22, 22)
285                                .addComponent(jLabel3)
286                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
287                                .addComponent(dateTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
288                        .addGap(10, 10, 10))
289                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 820, Short.MAX_VALUE)))
290        );
291        jPanel3Layout.setVerticalGroup(
292            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
293            .addGroup(jPanel3Layout.createSequentialGroup()
294                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
295                    .addGroup(jPanel3Layout.createSequentialGroup()
296                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
297                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
298                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
299                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
300                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
301                            .addComponent(templates, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
302                            .addComponent(messageTpl, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
303                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
304                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
305                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
306                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
307                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
308                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
309                    .addComponent(jToggleButton1)
310                    .addComponent(jToggleButton2)
311                    .addComponent(jToggleButton3)
312                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
313                    .addComponent(dateTo, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
314                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
315                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
316                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
317                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
318        );
319
320        add(jPanel3, java.awt.BorderLayout.CENTER);
321
322        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("MassPrintPanel.jPanel1.border.title_1"))); // NOI18N
323        jPanel1.setPreferredSize(new java.awt.Dimension(170, 333));
324
325        jScrollPane2.setName("jScrollPane2"); // NOI18N
326
327        contactlist.setModel(model);
328        contactlist.setToolTipText(bundle.getString("MassPrintPanel.contactlist.toolTipText_1")); // NOI18N
329        contactlist.setName("contactlist"); // NOI18N
330        contactlist.setSelectionBackground(java.awt.Color.green);
331        contactlist.setSelectionForeground(java.awt.Color.black);
332        contactlist.addMouseListener(new java.awt.event.MouseAdapter() {
333            public void mouseReleased(java.awt.event.MouseEvent evt) {
334                contactlistMouseReleased(evt);
335            }
336        });
337        jScrollPane2.setViewportView(contactlist);
338
339        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
340        jPanel1.setLayout(jPanel1Layout);
341        jPanel1Layout.setHorizontalGroup(
342            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
343            .addGroup(jPanel1Layout.createSequentialGroup()
344                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
345                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
346        );
347        jPanel1Layout.setVerticalGroup(
348            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
349            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
350        );
351
352        add(jPanel1, java.awt.BorderLayout.WEST);
353    }// </editor-fold>//GEN-END:initComponents
354
355    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
356        JToggleButton btn = (JToggleButton) evt.getSource();
357        RTFEditorKit kit = (RTFEditorKit) RTF_Text.getEditorKit();
358        MutableAttributeSet attr = kit.getInputAttributes();
359        boolean bold = (StyleConstants.isBold(attr)) ? false : true;
360        btn.setSelected(bold);
361        SimpleAttributeSet sas = new SimpleAttributeSet();
362        StyleConstants.setBold(sas, bold);
363        RTF_Text.setCharacterAttributes(sas, false);
364    }//GEN-LAST:event_jToggleButton1ActionPerformed
365
366    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton2ActionPerformed
367        JToggleButton btn = (JToggleButton) evt.getSource();
368        RTFEditorKit kit = (RTFEditorKit) RTF_Text.getEditorKit();
369        MutableAttributeSet attr = kit.getInputAttributes();
370        boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
371        btn.setSelected(italic);
372        SimpleAttributeSet sas = new SimpleAttributeSet();
373        StyleConstants.setItalic(sas, italic);
374        RTF_Text.setCharacterAttributes(sas, false);
375    }//GEN-LAST:event_jToggleButton2ActionPerformed
376
377    private void jToggleButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton3ActionPerformed
378        JToggleButton btn = (JToggleButton) evt.getSource();
379        RTFEditorKit kit = (RTFEditorKit) RTF_Text.getEditorKit();
380        MutableAttributeSet attr = kit.getInputAttributes();
381        boolean under = (StyleConstants.isUnderline(attr)) ? false : true;
382        btn.setSelected(under);
383        SimpleAttributeSet sas = new SimpleAttributeSet();
384        StyleConstants.setUnderline(sas, under);
385        RTF_Text.setCharacterAttributes(sas, false);
386    }//GEN-LAST:event_jToggleButton3ActionPerformed
387
388    private void RTF_TextCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_RTF_TextCaretUpdate
389        JTextPane text = (JTextPane) evt.getSource();
390        StyledDocument doc = text.getStyledDocument();
391        if (doc.getLength() > 0) {
392            Boolean isBold = null;
393            Boolean isItalic = null;
394            Boolean isUnderline = null;
395            Boolean tmp = null;
396            String fontFamily = null;
397            int fontSize = 8;
398            cursorPosition = evt.getDot();
399            for (int i = evt.getMark(); i < cursorPosition; i++) {
400                AttributeSet atts = doc.getCharacterElement(i).getAttributes();
401                tmp = StyleConstants.isBold(atts);
402                if (isBold == null) {
403                    isBold = tmp;
404                } else if (tmp.equals(isBold) == false) {
405                    isBold = false;
406                }
407                tmp = StyleConstants.isItalic(atts);
408                if (isItalic == null) {
409                    isItalic = tmp;
410                } else if (tmp.equals(isItalic) == false) {
411                    isItalic = false;
412                }
413                tmp = StyleConstants.isUnderline(atts);
414                if (isUnderline == null) {
415                    isUnderline = tmp;
416                } else if (tmp.equals(isUnderline) == false) {
417                    isUnderline = false;
418                }
419                fontFamily = StyleConstants.getFontFamily(atts);
420                fontSize = StyleConstants.getFontSize(atts);
421            }
422            if (isBold == null && isItalic == null && isUnderline == null) {
423                AttributeSet atts = doc.getCharacterElement(evt.getDot() - 1).getAttributes();
424                isBold = StyleConstants.isBold(atts);
425                isItalic = StyleConstants.isItalic(atts);
426                isUnderline = StyleConstants.isUnderline(atts);
427            }
428            if (fontFamily == null || fontSize == 8) {
429                AttributeSet atts = doc.getCharacterElement(evt.getDot() - 1).getAttributes();
430                fontFamily = StyleConstants.getFontFamily(atts);
431                fontSize = StyleConstants.getFontSize(atts);
432            }
433            jToggleButton1.setSelected(isBold);
434            jToggleButton2.setSelected(isItalic);
435            jToggleButton3.setSelected(isUnderline);
436            jComboBox1.setSelectedItem(fontFamily);
437            jComboBox2.setSelectedItem(String.valueOf(fontSize));
438        }
439    }//GEN-LAST:event_RTF_TextCaretUpdate
440
441    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
442        JComboBox comboBox = (JComboBox) evt.getSource();
443        javax.swing.text.StyledEditorKit.FontFamilyAction action = new StyledEditorKit.FontFamilyAction("yyy", comboBox.getSelectedItem().toString());
444        action.actionPerformed(new ActionEvent(comboBox, ActionEvent.ACTION_PERFORMED, "yyy"));
445    }//GEN-LAST:event_jComboBox1ActionPerformed
446
447    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
448        JComboBox comboBox = (JComboBox) evt.getSource();
449        javax.swing.text.StyledEditorKit.FontSizeAction action = new StyledEditorKit.FontSizeAction("xxx", Integer.parseInt(comboBox.getSelectedItem().toString()));
450        action.actionPerformed(new ActionEvent(comboBox, ActionEvent.ACTION_PERFORMED, "xxx"));
451    }//GEN-LAST:event_jComboBox2ActionPerformed
452
453    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
454        Runnable runnable = new Runnable() {
455
456            @Override
457            public void run() {
458                Object[] sel = contactlist.getSelectedValues();
459                Template tpl = (Template) templates.getSelectedItem();
460                HashMap<String, Object> hm1;
461                List<File> files = new ArrayList<File>();
462                File f;
463                if (tpl != null) {
464                    for (int o = 0; o < sel.length; o++) {
465                        try {
466                            Contact cont = (Contact) (Contact.getObject(Context.getContact(), ((IdItem) sel[o]).getId()));
467                            hm1 = new HashMap<String, Object>();
468                            hm1 = me.ReplaceVariablesInContent(hm1, cont);
469                            f = Export.print2(TemplateHandler.loadTemplate(tpl),
470                                    cont, hm1);
471                            files.add(f);
472                            mpv5.YabsViewProxy.instance().getProgressbar().setValue((100 / sel.length) * (o + 1));
473                            mpv5.YabsViewProxy.instance().getProgressbar().setString(cont.__getCname());
474                            mpv5.YabsViewProxy.instance().getProgressbar().setStringPainted(true);
475                        } catch (NodataFoundException e) {
476                            Log.Debug(this, e);
477                        }
478                    }
479                } else {
480                    Popup.error(MassPrintPanel.instanceOf(), Messages.NO_TEMPLATE_DEFINDED.toString());
481                }
482
483
484                if (!files.isEmpty()) {
485                    try {
486                        Export.print3(files, tpl.__getPrinter());
487                    } catch (Exception ex1) {
488                        Log.Debug(ex1);
489                    }
490                }
491            }
492        };
493        new Thread(runnable).start();
494    }//GEN-LAST:event_jButton1ActionPerformed
495
496    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
497        Object[] sel = contactlist.getSelectedValues();
498        Template tpl = (Template) templates.getSelectedItem();
499        HashMap<String, Object> hm1;
500        List<Waitable> files = new ArrayList<Waitable>();
501        if (tpl != null) {
502            for (int o = 0; o < sel.length; o++) {
503                try {
504                    Contact cont = (Contact) (Contact.getObject(Context.getContact(), ((IdItem) sel[o]).getId()));
505                    hm1 = new HashMap<String, Object>();
506                    hm1 = this.ReplaceVariablesInContent(hm1, cont);
507                    files.add(Export.createFile(cont.__getCname(),
508                            TemplateHandler.loadTemplate(tpl),
509                            cont,
510                            hm1));
511                    mpv5.YabsViewProxy.instance().getProgressbar().setValue((100 / sel.length) * (o + 1));
512                    mpv5.YabsViewProxy.instance().getProgressbar().setString(cont.__getCname());
513                    mpv5.YabsViewProxy.instance().getProgressbar().setStringPainted(true);
514                } catch (NodataFoundException ex) {
515                    Log.Debug(this, ex);
516                }
517            }
518        } else {
519            Popup.error(this, Messages.NO_TEMPLATE_DEFINDED.toString());
520        }
521
522        if (!files.isEmpty()) {
523            DialogForFile.CURRENT_DIR = new File(LocalSettings.getProperty(LocalSettings.BASE_DIR) + File.separator + "test");
524            DialogForFile d = new DialogForFile();
525            Job job = new Job(files, d, files.size() + " PDF " + Messages.SAVED);
526            job.execute();
527            mpv5.YabsViewProxy.instance().getProgressbar().setValue(100);
528            mpv5.YabsViewProxy.instance().getProgressbar().setString(Messages.DONE.toString());
529        }
530    }//GEN-LAST:event_jButton2ActionPerformed
531
532    private void messageTplActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_messageTplActionPerformed
533        if (evt.getModifiers() == 16) {
534            try {
535                String cn = (String) messageTpl.getSelectedItem();
536                MailMessage msg = (MailMessage) (MailMessage.getObject(Context.getMessage(), cn));
537                StyledDocument doc = RTF_Text.getStyledDocument();
538                doc.insertString(cursorPosition, msg.__getDescription(), null);
539            } catch (BadLocationException ex) {
540                Log.Debug(this, ex);
541            } catch (NodataFoundException ex) {
542                Log.Debug(this, ex);
543            }
544        }
545    }//GEN-LAST:event_messageTplActionPerformed
546
547    private void contactlistMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_contactlistMouseReleased
548        if (evt.isPopupTrigger()) {
549            showMenu(evt);
550        }
551    }//GEN-LAST:event_contactlistMouseReleased
552    // Variables declaration - do not modify//GEN-BEGIN:variables
553    private javax.swing.JTextPane RTF_Text;
554    private javax.swing.JList contactlist;
555    private mpv5.ui.beans.DateChooser dateTo;
556    private javax.swing.JButton jButton1;
557    private javax.swing.JButton jButton2;
558    private javax.swing.JComboBox jComboBox1;
559    private javax.swing.JComboBox jComboBox2;
560    private javax.swing.JLabel jLabel3;
561    private javax.swing.JLabel jLabel5;
562    private javax.swing.JLabel jLabel6;
563    private javax.swing.JLabel jLabel7;
564    private javax.swing.JPanel jPanel1;
565    private javax.swing.JPanel jPanel3;
566    private javax.swing.JScrollPane jScrollPane1;
567    private javax.swing.JScrollPane jScrollPane2;
568    private javax.swing.JToggleButton jToggleButton1;
569    private javax.swing.JToggleButton jToggleButton2;
570    private javax.swing.JToggleButton jToggleButton3;
571    private javax.swing.JComboBox messageTpl;
572    private javax.swing.JComboBox templates;
573    // End of variables declaration//GEN-END:variables
574
575    private void setContacts() {
576        Runnable runnable = new Runnable() {
577
578            @Override
579            public void run() {
580                Object[][] data1 = null;
581                data1 = new DatabaseSearch(Context.getContact(), 200).getValuesFor("ids, cname", "cname", "", true);
582                model.clear();
583                for (int i = 0; i < data1.length; i++) {
584                    model.addElement(new IdItem(Integer.parseInt(data1[i][0].toString()), data1[i][1].toString()));
585                }
586            }
587        };
588        new Thread(runnable).start();
589    }
590
591    private void showMenu(MouseEvent evt) {
592        try {
593            Object[][] d = QueryHandler.instanceOf().clone(Context.getMassprint()).select(Context.DETAILS_MASSPRINT, false);
594            String[][] rules = new String[d.length + 1][2];
595            ActionListener[] act = new ActionListener[d.length + 1];
596            rules[0][0] = Messages.MASSPRINT_FILTER_ALL.toString();
597            rules[0][1] = "999999";
598            act[0] = this;
599
600            for (int i = 0; i < d.length; i++) {
601                rules[i + 1][0] = d[i][1].toString();
602                rules[i + 1][1] = d[i][0].toString();
603                act[i + 1] = this;
604            }
605            new ListPopUp(contactlist, rules, act);
606        } catch (NodataFoundException ex) {
607            Log.Debug(this, ex);
608        }
609    }
610
611    private void setTemplateList() {
612        Runnable runnable = new Runnable() {
613
614            @Override
615            public void run() {
616                DefaultComboBoxModel mdl = (DefaultComboBoxModel) templates.getModel();
617                Template[] tpl = TemplateHandler.getTemplatesForType(User.getCurrentUser().__getGroupsids(), Constants.TYPE_MASSPRINT);
618                mdl.removeAllElements();
619                for (int i = 0; i < tpl.length; i++) {
620                    mdl.addElement(tpl[i]);
621                    TemplateHandler.loadTemplate(tpl[i]);
622                }
623            }
624        };
625        new Thread(runnable).start();
626    }
627
628    private HashMap<String, Object> ReplaceVariablesInContent(HashMap<String, Object> hm1, Contact cont) {
629        String rtf = serialize();
630        hm1.put("massprint.cname", "");
631        hm1.put("massprint.addedby", User.getCurrentUser().getName());
632        hm1.put("massprint.dateadded", dateTo.getDate());
633        if (cont.__getisMale()) {
634            hm1.put("massprint.gender",
635                    Messages.CONTACT_TYPE_MALE.toString());
636            hm1.put("massprint.intro",
637                    Messages.CONTACT_INTRO_MALE.toString());
638        } else {
639            hm1.put("massprint.gender",
640                    Messages.CONTACT_TYPE_FEMALE.toString());
641            hm1.put("massprint.intro",
642                    Messages.CONTACT_INTRO_FEMALE.toString());
643        }
644        hm1.put("massprint.content", rtf);
645        return hm1;
646    }
647
648    private String serialize() {
649        ByteArrayOutputStream out;
650        RTFEditorKit kit = (RTFEditorKit) RTF_Text.getEditorKit();
651        StyledDocument doc = RTF_Text.getStyledDocument();
652        out = new ByteArrayOutputStream();
653        try {
654            kit.write(out,
655                    doc,
656                    0,
657                    doc.getLength());
658        } catch (BadLocationException ex) {
659            Log.Debug(this,
660                    ex.toString());
661        } catch (IOException ex) {
662            Log.Debug(this,
663                    ex.toString());
664        }
665        String str = null;
666
667        str = out.toString();
668        str = str.replaceAll("[\r\n]+",
669                " ");
670        str = str.replaceAll("  ",
671                " ");
672        return str;
673    }
674
675    private void setMessageTemplateList() {
676        Runnable runnable = new Runnable() {
677
678            @Override
679            public void run() {
680                DefaultComboBoxModel mdl = (DefaultComboBoxModel) messageTpl.getModel();
681                Object[][] data1 = null;
682                data1 = new DatabaseSearch(Context.getMessage(), 200).getValuesFor("cname", "cname", "", true);
683                mdl.removeAllElements();
684                for (int i = 0; i < data1.length; i++) {
685                    mdl.addElement((String) data1[i][0]);
686                }
687            }
688        };
689        new Thread(runnable).start();
690    }
691
692    public void actionPerformed(ActionEvent e) {
693        int cname = 0;
694        ListMenuItem mi = (ListMenuItem) e.getSource();
695        Log.Debug(this, " Executing Rule: " + mi.getID());
696        Object[][] data = null;
697        int ids = 0;
698        try {
699            if (mi.getID() == 999999) {
700                model.clear();
701                data = new DatabaseSearch(Context.getContact(), 200).getValuesFor("ids, cname", "cname", "", true);
702                for (int i = 0; i < data.length; i++) {
703                    model.addElement(new IdItem(Integer.parseInt(data[i][0].toString()), data[i][1].toString()));
704                }
705            } else {
706                MassprintRules mpr = (MassprintRules) (DatabaseObject.getObject(Context.getMassprint(), mi.getID()));
707                model.clear();
708                ReturnValue ret = QueryHandler.instanceOf().freeSelectQuery(mpr.__getContent(), MPSecurityManager.VIEW, null);
709                String[] cols = ret.getFullColumnNames();
710                for (int j = 0; j < cols.length; j++) {
711                    if (cols[j].equalsIgnoreCase("contacts.cname")) {
712                        cname = j;
713                    }
714                    if (cols[j].equalsIgnoreCase("contacts.ids")) {
715                        ids = j;
716                    }
717                }
718                data = ret.getData();
719                for (int i = 0; i < data.length; i++) {
720                    model.addElement(new IdItem(Integer.parseInt(data[i][ids].toString()), data[i][cname].toString()));
721                }
722            }
723        } catch (NodataFoundException ex) {
724            Log.Debug(this, ex);
725        }
726    }
727
728    class IdItem {
729
730        int id;
731        String description;
732
733        IdItem(int id, String description) {
734            this.id = id;
735            this.description = description;
736        }
737
738        public int getId() {
739            return id;
740        }
741
742        @Override
743        public String toString() {
744            return description;
745        }
746    }
747}