PageRenderTime 83ms CodeModel.GetById 20ms app.highlight 34ms RepoModel.GetById 17ms app.codeStats 0ms

/src/mpv5/ui/beans/PrinitingComboBox.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 244 lines | 185 code | 18 blank | 41 comment | 26 complexity | 21afc15199710bde924bdae232bba022 MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *      YaBS is free software: you can redistribute it and/or modify
  5 *      it under the terms of the GNU General Public License as published by
  6 *      the Free Software Foundation, either version 3 of the License, or
  7 *      (at your option) any later version.
  8 *
  9 *      YaBS is distributed in the hope that it will be useful,
 10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *      GNU General Public License for more details.
 13 *
 14 *      You should have received a copy of the GNU General Public License
 15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17/*
 18 * PrinitingComboBox.java
 19 *
 20 * Created on 17.02.2009, 09:53:14
 21 */
 22package mpv5.ui.beans;
 23
 24import java.awt.Component;
 25import java.awt.Graphics2D;
 26import java.awt.event.ItemEvent;
 27import java.awt.image.BufferedImage;
 28import java.awt.print.PrinterException;
 29import java.io.File;
 30import java.io.IOException;
 31import java.util.ResourceBundle;
 32import javax.imageio.ImageIO;
 33import javax.swing.DefaultComboBoxModel;
 34import javax.swing.JComboBox;
 35import javax.swing.JComponent;
 36import javax.swing.JTable;
 37import mpv5.db.common.Context;
 38import mpv5.db.common.DatabaseObject;
 39import mpv5.logging.Log;
 40import mpv5.ui.dialogs.DialogForFile;
 41import mpv5.utils.arrays.ArrayUtilities;
 42import mpv5.utils.date.DateConverter;
 43import mpv5.utils.files.FileActionHandler;
 44import mpv5.utils.files.FileReaderWriter;
 45import mpv5.utils.files.TableHtmlWriter;
 46import mpv5.utils.models.MPComboBoxModelItem;
 47import mpv5.utils.print.FilePrintJob;
 48import mpv5.utils.print.PrintJob;
 49import mpv5.utils.print.PrintJob2;
 50
 51/**
 52 *
 53 *  
 54 */
 55public class PrinitingComboBox extends javax.swing.JPanel {
 56
 57    private static final long serialVersionUID = 1L;
 58    public static final int MODE_DO = 0;
 59    public static final int MODE_TABLE = 1;
 60    private int mode = 0;
 61    private Object dataowner;
 62    public static final int MODE_COMPONENT = 2;
 63
 64    /** Creates new form PrinitingComboBox
 65     */
 66    public PrinitingComboBox() {
 67        initComponents();
 68    }
 69
 70    /**
 71     *
 72     * @param dataowner DatabaseObject or JTable
 73     */
 74    public void init(Object dataowner) {
 75        if (dataowner instanceof DatabaseObject
 76                && (((DatabaseObject) dataowner).getContext().equals(Context.getContact())
 77                || ((DatabaseObject) dataowner).getContext().equals(Context.getSupplier())
 78                || ((DatabaseObject) dataowner).getContext().equals(Context.getManufacturer())
 79                || ((DatabaseObject) dataowner).getContext().equals(Context.getCustomer())
 80                || ((DatabaseObject) dataowner).getContext().equals(Context.getUser())) ){
 81
 82            jComboBox1.setModel(new DefaultComboBoxModel(new Object[]{new MPComboBoxModelItem(-1, ""),
 83                        new MPComboBoxModelItem(0, "Printer"),
 84                        new MPComboBoxModelItem(1, "VCF Export"),
 85                        new MPComboBoxModelItem(2, "CSV Export"),
 86                        new MPComboBoxModelItem(3, "XML Export")}));
 87            mode = MODE_DO;
 88        } else if (dataowner instanceof DatabaseObject) {
 89
 90            jComboBox1.setModel(new DefaultComboBoxModel(new Object[]{new MPComboBoxModelItem(-1, ""),
 91                        new MPComboBoxModelItem(0, "Printer"),
 92                        new MPComboBoxModelItem(2, "CSV Export"),
 93                        new MPComboBoxModelItem(3, "XML Export")}));
 94            mode = MODE_DO;
 95        } else if (dataowner instanceof JTable) {
 96            jComboBox1.setModel(new DefaultComboBoxModel(new Object[]{new MPComboBoxModelItem(-1, ""),
 97                        new MPComboBoxModelItem(0, "Printer"),
 98                        new MPComboBoxModelItem(2, "CSV Export"),
 99                        new MPComboBoxModelItem(1, "HTML Export")}));
100            mode = MODE_TABLE;
101        } else if (dataowner instanceof JComponent) {
102            jComboBox1.setModel(new DefaultComboBoxModel(new Object[]{new MPComboBoxModelItem(-1, ""),
103                        new MPComboBoxModelItem(0, "Printer"),
104                        new MPComboBoxModelItem(1, "Export")
105                    }));
106            mode = MODE_COMPONENT;
107        }
108        this.dataowner = dataowner;
109
110
111    }
112
113    /** This method is called from within the constructor to
114     * initialize the form.
115     * WARNING: Do NOT modify this code. The content of this method is
116     * always regenerated by the Form Editor.
117     */
118    @SuppressWarnings("unchecked")
119    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
120    private void initComponents() {
121
122        jLabel1 = new javax.swing.JLabel();
123        jComboBox1 = new javax.swing.JComboBox();
124
125        setName("Form"); // NOI18N
126        setOpaque(false);
127        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));
128
129        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
130        jLabel1.setText(bundle.getString("PrinitingComboBox.jLabel1.text")); // NOI18N
131        jLabel1.setMaximumSize(new java.awt.Dimension(333, 333));
132        jLabel1.setName("jLabel1"); // NOI18N
133        jLabel1.setPreferredSize(new java.awt.Dimension(100, 18));
134        add(jLabel1);
135
136        jComboBox1.setFont(jComboBox1.getFont());
137        jComboBox1.setName("jComboBox1"); // NOI18N
138        jComboBox1.setPreferredSize(new java.awt.Dimension(100, 28));
139        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
140            public void itemStateChanged(java.awt.event.ItemEvent evt) {
141                jComboBox1ItemStateChanged(evt);
142            }
143        });
144        add(jComboBox1);
145    }// </editor-fold>//GEN-END:initComponents
146
147    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
148        JComboBox cb = (JComboBox) evt.getSource();
149        // Get the affected item
150        MPComboBoxModelItem item = (MPComboBoxModelItem) evt.getItem();
151
152        switch (mode) {
153            case MODE_DO:
154                if (evt.getStateChange() == ItemEvent.SELECTED) {
155                    switch (Integer.valueOf(item.getId().toString())) {
156                        case 0:
157                            new PrintJob().print((DatabaseObject) dataowner);
158                            break;
159                        case 1:
160                            new FilePrintJob((DatabaseObject) dataowner).toVCF();
161                            break;
162                        case 2:
163                            new FilePrintJob((DatabaseObject) dataowner).toCSV();
164                            break;
165                        case 3:
166                            new FilePrintJob((DatabaseObject) dataowner).toXML();
167//                            ((Contact) dataowner).toXML();
168                            break;
169                    }
170                }
171                break;
172
173            case MODE_TABLE:
174                if (evt.getStateChange() == ItemEvent.SELECTED) {
175                    switch (Integer.valueOf(item.getId().toString())) {
176                        case 0:
177                            try {
178                                ((JTable) dataowner).print();
179                            } catch (PrinterException ex) {
180                                Log.Debug(this, ex);
181                            }
182                            break;
183                        case 1:
184                            DialogForFile dialog = new DialogForFile(DialogForFile.FILES_ONLY, "export-" + DateConverter.getTodayDefDate() + ".html");
185                            if (dialog.saveFile()) {
186                                File f = new TableHtmlWriter((JTable) dataowner, dialog.getFile(), ((JTable) dataowner).getShowHorizontalLines(), ((JTable) dataowner).getShowVerticalLines()).createHtml();
187                                FileActionHandler.open(f);
188                            }
189                            break;
190                        case 2:
191                            DialogForFile dialog2 = new DialogForFile(DialogForFile.FILES_ONLY, "export-" + DateConverter.getTodayDefDate() + ".csv");
192                            if (dialog2.saveFile()) {
193                                FileReaderWriter r = new FileReaderWriter(dialog2.getFile());
194                                String title = "";
195                                for (int i = 0; i < ((JTable) dataowner).getColumnCount(); i++) {
196                                    title += ((JTable) dataowner).getColumnName(i) + ",";
197                                }
198                                title = title.substring(0, title.length() - 1);
199                                title += "\n";
200                                Object[][] ar = ArrayUtilities.tableModelToArray((JTable) dataowner);
201                                r.writeOnce(title);
202                                boolean write = r.write(ar, ",", true);
203                                FileActionHandler.open(dialog2.getSelectedFile());
204                            }
205                            break;
206                    }
207                    break;
208                }
209
210            case MODE_COMPONENT:
211                if (evt.getStateChange() == ItemEvent.SELECTED) {
212                    switch (Integer.valueOf(item.getId().toString())) {
213                        case 0:
214                            new PrintJob2((Component) dataowner);
215                            break;
216                        case 1:
217                            Component comp = (Component) dataowner;
218                            int w = comp.getWidth(),
219                             h = comp.getHeight();
220                            BufferedImage image = new BufferedImage(w, h,
221                                    BufferedImage.TYPE_INT_RGB);
222                            Graphics2D g2 = image.createGraphics();
223                            comp.paint(g2);
224                            g2.dispose();
225                            DialogForFile d = new DialogForFile(new File("export.jpg"));
226                            if (d.saveFile()) {
227                                try {
228                                    ImageIO.write(image, "jpeg", d.getFile());
229                                } catch (IOException ex) {
230                                    Log.Debug(ex);
231                                }
232                            }
233                            break;
234                    }
235                }
236                break;
237        }
238        cb.setSelectedIndex(-1);
239    }//GEN-LAST:event_jComboBox1ItemStateChanged
240    // Variables declaration - do not modify//GEN-BEGIN:variables
241    private javax.swing.JComboBox jComboBox1;
242    private javax.swing.JLabel jLabel1;
243    // End of variables declaration//GEN-END:variables
244}