PageRenderTime 73ms CodeModel.GetById 30ms app.highlight 16ms RepoModel.GetById 15ms app.codeStats 0ms

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

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 164 lines | 107 code | 20 blank | 37 comment | 8 complexity | b47ba69603e585a1aa05287dbbbd2a9c MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5
  6/*
  7 * TemplateFormatEditor.java
  8 *
  9 * Created on Oct 9, 2009, 10:08:54 AM
 10 */
 11package mpv5.ui.dialogs.subcomponents;
 12
 13import javax.swing.JTextField;
 14import javax.swing.table.TableColumnModel;
 15import javax.swing.table.TableModel;
 16import mpv5.logging.Log;
 17import mpv5.ui.dialogs.Popup;
 18
 19/**
 20 *
 21 * @author andreasw
 22 */
 23public class TemplateFormatEditor extends javax.swing.JPanel {
 24
 25    private static TemplateFormatEditor me;
 26    private final JTextField rec;
 27
 28    public static TemplateFormatEditor instanceOf(JTextField txt) {
 29        if (me == null) {
 30            me = new TemplateFormatEditor(txt);
 31        }
 32        try {
 33            String format = txt.getText();
 34            int[] f = new int[format.split(",").length];
 35            for (int i = 0; i < f.length; i++) {
 36                f[i] = Integer.valueOf(format.split(",")[i]);
 37            }
 38
 39            //Reset the table!
 40//            me.jTable1.setModel(originalmodel);
 41//            me.jTable1.setColumnModel(originalcolumns);
 42//            for (int ix = 0; ix < me.jTable1.getColumnCount(); ix++) {
 43//                me.jTable1.setValueAt(Boolean.FALSE, 0, ix);
 44//            }
 45
 46// 
 47            // set the format values
 48            for (int i = 0; i < f.length; i++) {
 49                int j = f[i];
 50                me.jTable1.setValueAt(Boolean.TRUE, 0, j - 1);
 51            }
 52
 53            //re-arrange!
 54//            for (int i = 0; i < f.length; i++) {
 55//                int j = f[i];
 56//                me.jTable1.getColumnModel().getColumn(j-1).setModelIndex(i);
 57//            }
 58
 59        } catch (Exception n) {
 60            Log.Debug(n);
 61            Popup.error(n);
 62        }
 63        return me;
 64    }
 65    private static TableModel originalmodel;
 66    private static TableColumnModel originalcolumns;
 67
 68    /** Creates new form TemplateFormatEditor
 69     * @param rec 
 70     */
 71    private TemplateFormatEditor(JTextField rec) {
 72        initComponents();
 73        originalmodel = jTable1.getModel();
 74        originalcolumns = jTable1.getColumnModel();
 75        this.rec = rec;
 76    }
 77
 78    /** This method is called from within the constructor to
 79     * initialize the form.
 80     * WARNING: Do NOT modify this code. The content of this method is
 81     * always regenerated by the Form Editor.
 82     */
 83    @SuppressWarnings("unchecked")
 84    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 85    private void initComponents() {
 86
 87        jScrollPane1 = new javax.swing.JScrollPane();
 88        jTable1 = new javax.swing.JTable();
 89        jButton1 = new javax.swing.JButton();
 90
 91        setName("Form"); // NOI18N
 92
 93        jScrollPane1.setName("jScrollPane1"); // NOI18N
 94
 95        jTable1.setAutoCreateRowSorter(true);
 96        jTable1.setModel(new javax.swing.table.DefaultTableModel(
 97            new Object [][] {
 98                {new Boolean(true), new Boolean(true), new Boolean(true), new Boolean(true), new Boolean(true), new Boolean(true), new Boolean(true), new Boolean(true), new Boolean(true), new Boolean(true), new Boolean(true)}
 99            },
100            new String [] {
101                "Count", "Quantity", "Measure", "Text", "Net", "Totalnet", "Taxrate", "Tax", "Total", "Link", "Optional"
102            }
103        ) {
104            Class[] types = new Class [] {
105                java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class
106            };
107
108            public Class getColumnClass(int columnIndex) {
109                return types [columnIndex];
110            }
111        });
112        jTable1.setName("jTable1"); // NOI18N
113        jScrollPane1.setViewportView(jTable1);
114
115        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
116        jButton1.setText(bundle.getString("TemplateFormatEditor.jButton1.text")); // NOI18N
117        jButton1.setName("jButton1"); // NOI18N
118        jButton1.addActionListener(new java.awt.event.ActionListener() {
119            public void actionPerformed(java.awt.event.ActionEvent evt) {
120                jButton1ActionPerformed(evt);
121            }
122        });
123
124        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
125        this.setLayout(layout);
126        layout.setHorizontalGroup(
127            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
128            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
129                .addContainerGap()
130                .addComponent(jButton1))
131            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 685, Short.MAX_VALUE)
132        );
133        layout.setVerticalGroup(
134            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
135            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
136                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
137                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
138                .addComponent(jButton1))
139        );
140    }// </editor-fold>//GEN-END:initComponents
141
142    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
143        setFormat();
144    }//GEN-LAST:event_jButton1ActionPerformed
145    // Variables declaration - do not modify//GEN-BEGIN:variables
146    private javax.swing.JButton jButton1;
147    private javax.swing.JScrollPane jScrollPane1;
148    private javax.swing.JTable jTable1;
149    // End of variables declaration//GEN-END:variables
150
151    private void setFormat() {
152        TableModel t = jTable1.getModel();
153        String format = "";
154        for (int i = 0; i < t.getColumnCount(); i++) {
155            int n = jTable1.getColumnModel().getColumn(i).getModelIndex() + 1;
156            if (((Boolean) jTable1.getValueAt(0, i))) {
157                format += n + ",";
158            }
159        }
160        if (format.length()>0) {
161            rec.setText(format.substring(0, format.length() - 1));
162        } else rec.setText("");
163    }
164}