PageRenderTime 359ms CodeModel.GetById 151ms app.highlight 79ms RepoModel.GetById 122ms app.codeStats 0ms

/src/mpv5/ui/dialogs/Search2.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 295 lines | 226 code | 38 blank | 31 comment | 12 complexity | e192617d9acda924bf1624ef5a57d97b MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5
  6/*
  7 * SplashScreen.java
  8 *
  9 * Created on 30.03.2009, 21:55:52
 10 */
 11package mpv5.ui.dialogs;
 12
 13import java.awt.Component;
 14import javax.swing.DefaultComboBoxModel;
 15import javax.swing.JComponent;
 16import javax.swing.table.TableCellRenderer;
 17import mpv5.db.common.Context;
 18import mpv5.db.common.DatabaseObject;
 19import mpv5.db.common.DatabaseSearch;
 20import mpv5.db.common.NodataFoundException;
 21import mpv5.ui.frames.MPView;
 22import mpv5.utils.html.HtmlParser;
 23import mpv5.utils.models.MPTableModel;
 24import mpv5.utils.tables.TableFormat;
 25
 26/**
 27 *
 28 *  
 29 */
 30public class Search2 extends javax.swing.JDialog {
 31
 32    private static final long serialVersionUID = 1L;
 33    private String additionalSearchCondition = "";
 34    private static String oldSelection;
 35
 36    public static DatabaseObject showSearchFor(Context t) {
 37        Search2 s = new Search2(t);
 38        s.setVisible(true);
 39        s.requestFocus();
 40        return s.getSelectedObject();
 41    }
 42    private Context context;
 43    private DatabaseObject selection;
 44
 45    @Override
 46    public void dispose() {
 47        setVisible(false);
 48    }
 49
 50    /** Creates new form SplashScreen
 51     * @param addtabs Add the selected Object to the main tab pane
 52     */
 53    private Search2(Context t) {
 54        super(mpv5.YabsViewProxy.instance().getIdentifierFrame());
 55        initComponents();
 56        context = t;
 57        typelabel.setText(t.getDbIdentity().toUpperCase());
 58        setModalityType(ModalityType.APPLICATION_MODAL);
 59        setLocationRelativeTo(mpv5.YabsViewProxy.instance().getIdentifierFrame());
 60    }
 61
 62    /** This method is called from within the constructor to
 63     * initialize the form.
 64     * WARNING: Do NOT modify this code. The content of this method is
 65     * always regenerated by the Form Editor.
 66     */
 67    @SuppressWarnings("unchecked")
 68    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 69    private void initComponents() {
 70
 71        jPanel1 = new javax.swing.JPanel();
 72        jLabel1 = new javax.swing.JLabel();
 73        jLabel2 = new javax.swing.JLabel();
 74        key = new javax.swing.JComboBox();
 75        jCheckBox1 = new javax.swing.JCheckBox();
 76        jButton1 = new javax.swing.JButton();
 77        jButton2 = new javax.swing.JButton();
 78        jScrollPane1 = new javax.swing.JScrollPane();
 79        jTable1 = new  mpv5.ui.misc.MPTable(this) {
 80            public Component prepareRenderer(TableCellRenderer renderer,
 81                int rowIndex, int vColIndex) {
 82                Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
 83                if (c instanceof JComponent) {
 84                    JComponent jc = (JComponent)c;
 85                    jc.setToolTipText(String.valueOf(getValueAt(rowIndex, vColIndex)));
 86                }
 87                return c;
 88            }
 89        };
 90        typelabel = new javax.swing.JLabel();
 91
 92        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
 93        setName("Form"); // NOI18N
 94
 95       java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
 96        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("Search2.jPanel1.border.title"))); // NOI18N
 97        jPanel1.setName("jPanel1"); // NOI18N
 98
 99        jLabel1.setText(bundle.getString("Search2.jLabel1.text")); // NOI18N
100        jLabel1.setToolTipText(bundle.getString("Search2.jLabel1.toolTipText")); // NOI18N
101        jLabel1.setName("jLabel1"); // NOI18N
102
103        jLabel2.setText(bundle.getString("Search2.jLabel2.text")); // NOI18N
104        jLabel2.setToolTipText(bundle.getString("Search2.jLabel2.toolTipText")); // NOI18N
105        jLabel2.setName("jLabel2"); // NOI18N
106
107        key.setEditable(true);
108        key.setName("key"); // NOI18N
109        key.addActionListener(new java.awt.event.ActionListener() {
110            public void actionPerformed(java.awt.event.ActionEvent evt) {
111                keyActionPerformed(evt);
112            }
113        });
114
115        jCheckBox1.setText(bundle.getString("Search2.jCheckBox1.text")); // NOI18N
116        jCheckBox1.setToolTipText(bundle.getString("Search2.jCheckBox1.toolTipText")); // NOI18N
117        jCheckBox1.setName("jCheckBox1"); // NOI18N
118
119        jButton1.setText(bundle.getString("Search2.jButton1.text")); // NOI18N
120        jButton1.setToolTipText(bundle.getString("Search2.jButton1.toolTipText")); // NOI18N
121        jButton1.setName("jButton1"); // NOI18N
122        jButton1.addActionListener(new java.awt.event.ActionListener() {
123            public void actionPerformed(java.awt.event.ActionEvent evt) {
124                jButton1ActionPerformed(evt);
125            }
126        });
127
128        jButton2.setText(bundle.getString("Search2.jButton2.text")); // NOI18N
129        jButton2.setName("jButton2"); // NOI18N
130        jButton2.addActionListener(new java.awt.event.ActionListener() {
131            public void actionPerformed(java.awt.event.ActionEvent evt) {
132                jButton2ActionPerformed(evt);
133            }
134        });
135
136        jScrollPane1.setName("jScrollPane1"); // NOI18N
137
138        jTable1.setAutoCreateRowSorter(true);
139        jTable1.setModel(new javax.swing.table.DefaultTableModel(
140            new Object [][] {
141                {},
142                {},
143                {},
144                {}
145            },
146            new String [] {
147
148            }
149        ));
150        jTable1.setName("result"); // NOI18N
151        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
152            public void mouseClicked(java.awt.event.MouseEvent evt) {
153                jTable1MouseClicked(evt);
154            }
155        });
156        jScrollPane1.setViewportView(jTable1);
157
158        typelabel.setText(bundle.getString("Search2.typelabel.text")); // NOI18N
159        typelabel.setName("typelabel"); // NOI18N
160
161        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
162        jPanel1.setLayout(jPanel1Layout);
163        jPanel1Layout.setHorizontalGroup(
164            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
165            .addGroup(jPanel1Layout.createSequentialGroup()
166                .addContainerGap()
167                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
168                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
169                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
170                .addGap(65, 65, 65)
171                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
172                    .addComponent(typelabel, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
173                    .addGroup(jPanel1Layout.createSequentialGroup()
174                        .addComponent(jCheckBox1)
175                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
176                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
177                    .addComponent(key, 0, 215, Short.MAX_VALUE)))
178            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
179                .addContainerGap()
180                .addComponent(jButton2))
181            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 341, Short.MAX_VALUE)
182        );
183        jPanel1Layout.setVerticalGroup(
184            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
185            .addGroup(jPanel1Layout.createSequentialGroup()
186                .addContainerGap()
187                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
188                    .addComponent(typelabel, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
189                    .addComponent(jLabel1))
190                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
191                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
192                    .addComponent(jLabel2)
193                    .addComponent(key, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
194                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
195                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
196                    .addComponent(jCheckBox1)
197                    .addComponent(jButton1))
198                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
199                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
200                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
201                .addComponent(jButton2))
202        );
203
204        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
205
206        pack();
207    }// </editor-fold>//GEN-END:initComponents
208
209    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
210
211         if (context != null) {
212                try {
213                    selection = DatabaseObject.getObject(context, Integer.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString()));
214                } catch (Exception ex) {
215                    mpv5.logging.Log.Debug(this, ex.getMessage());
216                }
217                this.dispose();
218            }
219    }//GEN-LAST:event_jButton2ActionPerformed
220
221    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
222
223        if (evt.getClickCount() > 1) {
224            if (context != null) {
225                try {
226                    selection = DatabaseObject.getObject(context, Integer.valueOf(jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString()));
227                } catch (NodataFoundException ex) {
228                    mpv5.logging.Log.Debug(ex);
229                }
230                this.dispose();
231            }
232        }
233    }//GEN-LAST:event_jTable1MouseClicked
234
235    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
236        search();
237}//GEN-LAST:event_jButton1ActionPerformed
238
239    private void keyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keyActionPerformed
240        search();
241}//GEN-LAST:event_keyActionPerformed
242    // Variables declaration - do not modify//GEN-BEGIN:variables
243    private javax.swing.JButton jButton1;
244    private javax.swing.JButton jButton2;
245    private javax.swing.JCheckBox jCheckBox1;
246    private javax.swing.JLabel jLabel1;
247    private javax.swing.JLabel jLabel2;
248    private javax.swing.JPanel jPanel1;
249    private javax.swing.JScrollPane jScrollPane1;
250    private javax.swing.JTable jTable1;
251    private javax.swing.JComboBox key;
252    private javax.swing.JLabel typelabel;
253    // End of variables declaration//GEN-END:variables
254
255    private void search() {
256        String newSelection = (String) key.getSelectedItem();
257
258        if (newSelection != null && !newSelection.equals(oldSelection)) {
259            key.addItem(newSelection);
260            oldSelection = newSelection;
261        }
262
263        if (newSelection == null || newSelection.equals("null")) {
264            newSelection = "";
265        }
266
267        Object[][] data = null;
268
269        context.addReference(Context.getGroup());
270        DatabaseSearch s = new DatabaseSearch(context);
271        data = s.getValuesFor(context.getDbIdentity() + ".ids," + context.getDbIdentity() + ".cname," + "groups.cname," + context.getDbIdentity() + ".dateadded", new String[]{"cname"}, newSelection, !jCheckBox1.isSelected());
272        jTable1.setModel(new MPTableModel(data));
273        TableFormat.hideHeader(jTable1);
274        TableFormat.stripFirstColumn(jTable1);
275        //TableFormat.format(jTable1, 1, 100);
276    }
277
278    private DatabaseObject getSelectedObject() {
279        return selection;
280    }
281
282    /**
283     * @return the additionalSearchCondition
284     */
285    public String getAdditionalSearchCondition() {
286        return additionalSearchCondition;
287    }
288
289    /**
290     * @param additionalSearchCondition the additionalSearchCondition to set
291     */
292    public void setAdditionalSearchCondition(String additionalSearchCondition) {
293        this.additionalSearchCondition = additionalSearchCondition;
294    }
295}