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