PageRenderTime 80ms CodeModel.GetById 43ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/ui/dialogs/Search.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 325 lines | 253 code | 40 blank | 32 comment | 17 complexity | 3c810ba219a2b5c57fa7e3b710039e6f 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.ui.misc.Position;
 23import mpv5.utils.arrays.ArrayUtilities;
 24import mpv5.utils.html.HtmlParser;
 25import mpv5.utils.models.MPTableModel;
 26import mpv5.utils.tables.Selection;
 27import mpv5.utils.tables.TableFormat;
 28
 29/**
 30 *
 31 *  
 32 */
 33public class Search extends javax.swing.JDialog {
 34
 35    private static final long serialVersionUID = 1L;
 36    private static Search f;
 37    private String additionalSearchCondition = "";
 38
 39    public static Search instanceOf() {
 40        if (f == null) {
 41            f = new Search(true);
 42        }
 43        f.setVisible(true);
 44        f.requestFocus();
 45        return f;
 46    }
 47    private static String oldSelection;
 48    private DatabaseObject selection;
 49    private boolean addTabs = false;
 50
 51    @Override
 52    public void dispose() {
 53        setVisible(false);
 54    }
 55
 56    /** Creates new form SplashScreen
 57     * @param addtabs Add the selected Object to the main tab pane
 58     */
 59    public Search(boolean addtabs) {
 60        super(mpv5.YabsViewProxy.instance().getIdentifierFrame());
 61        initComponents();
 62        this.addTabs = addtabs;
 63        setModalityType(ModalityType.APPLICATION_MODAL);
 64        setLocationRelativeTo(mpv5.YabsViewProxy.instance().getIdentifierFrame());
 65    }
 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
 76        jPanel1 = new javax.swing.JPanel();
 77        jLabel2 = new javax.swing.JLabel();
 78        key = new javax.swing.JComboBox();
 79        jCheckBox1 = new javax.swing.JCheckBox();
 80        jButton1 = new javax.swing.JButton();
 81        jButton2 = new javax.swing.JButton();
 82        jScrollPane1 = new javax.swing.JScrollPane();
 83        jTable1 = new  mpv5.ui.misc.MPTable(this) {
 84            public Component prepareRenderer(TableCellRenderer renderer,
 85                int rowIndex, int vColIndex) {
 86                Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
 87                if (c instanceof JComponent) {
 88                    JComponent jc = (JComponent)c;
 89                    jc.setToolTipText(String.valueOf(getValueAt(rowIndex, vColIndex)));
 90                }
 91                return c;
 92            }
 93        };
 94        jCheckBox2 = new javax.swing.JCheckBox();
 95
 96        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
 97        setName("Form"); // NOI18N
 98
 99        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle();// NOI18N
100        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("Search.jPanel1.border.title"))); // NOI18N
101        jPanel1.setName("jPanel1"); // NOI18N
102
103        jLabel2.setText(bundle.getString("Search.jLabel2.text")); // NOI18N
104        jLabel2.setToolTipText(bundle.getString("Search.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("Search.jCheckBox1.text")); // NOI18N
116        jCheckBox1.setToolTipText(bundle.getString("Search.jCheckBox1.toolTipText")); // NOI18N
117        jCheckBox1.setFocusable(false);
118        jCheckBox1.setName("jCheckBox1"); // NOI18N
119
120        jButton1.setText(bundle.getString("Search.jButton1.text")); // NOI18N
121        jButton1.setToolTipText(bundle.getString("Search.jButton1.toolTipText")); // NOI18N
122        jButton1.setFocusPainted(false);
123        jButton1.setName("jButton1"); // NOI18N
124        jButton1.addActionListener(new java.awt.event.ActionListener() {
125            public void actionPerformed(java.awt.event.ActionEvent evt) {
126                jButton1ActionPerformed(evt);
127            }
128        });
129
130        jButton2.setText(bundle.getString("Search.jButton2.text")); // NOI18N
131        jButton2.setName("jButton2"); // NOI18N
132        jButton2.addActionListener(new java.awt.event.ActionListener() {
133            public void actionPerformed(java.awt.event.ActionEvent evt) {
134                jButton2ActionPerformed(evt);
135            }
136        });
137
138        jScrollPane1.setName("jScrollPane1"); // NOI18N
139
140        jTable1.setAutoCreateRowSorter(true);
141        jTable1.setModel(new javax.swing.table.DefaultTableModel(
142            new Object [][] {
143                {},
144                {},
145                {},
146                {}
147            },
148            new String [] {
149
150            }
151        ));
152        jTable1.setName("jTable1"); // NOI18N
153        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
154            public void mouseClicked(java.awt.event.MouseEvent evt) {
155                jTable1MouseClicked(evt);
156            }
157        });
158        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
159            public void keyTyped(java.awt.event.KeyEvent evt) {
160                jTable1KeyTyped(evt);
161            }
162        });
163        jScrollPane1.setViewportView(jTable1);
164
165        jCheckBox2.setText(bundle.getString("Search.jCheckBox2.text")); // NOI18N
166        jCheckBox2.setToolTipText(bundle.getString("Search.jCheckBox2.toolTipText")); // NOI18N
167        jCheckBox2.setFocusable(false);
168        jCheckBox2.setName("jCheckBox2"); // NOI18N
169        jCheckBox2.addItemListener(new java.awt.event.ItemListener() {
170            public void itemStateChanged(java.awt.event.ItemEvent evt) {
171                jCheckBox2ItemStateChanged(evt);
172            }
173        });
174
175        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
176        jPanel1.setLayout(jPanel1Layout);
177        jPanel1Layout.setHorizontalGroup(
178            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
179            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
180            .addGroup(jPanel1Layout.createSequentialGroup()
181                .addContainerGap()
182                .addComponent(jButton2))
183            .addGroup(jPanel1Layout.createSequentialGroup()
184                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
185                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
186                    .addComponent(jCheckBox2, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE))
187                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
188                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
189                    .addGroup(jPanel1Layout.createSequentialGroup()
190                        .addComponent(jCheckBox1)
191                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
192                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
193                    .addGroup(jPanel1Layout.createSequentialGroup()
194                        .addGap(27, 27, 27)
195                        .addComponent(key, 0, 234, Short.MAX_VALUE))))
196        );
197        jPanel1Layout.setVerticalGroup(
198            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
199            .addGroup(jPanel1Layout.createSequentialGroup()
200                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
201                    .addComponent(key, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
202                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
203                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
204                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
205                    .addComponent(jCheckBox1)
206                    .addComponent(jCheckBox2)
207                    .addComponent(jButton1))
208                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
209                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
210                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
211                .addComponent(jButton2))
212        );
213
214        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
215
216        pack();
217    }// </editor-fold>//GEN-END:initComponents
218
219    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
220        open();
221    }//GEN-LAST:event_jButton2ActionPerformed
222
223    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
224
225        if (evt.getClickCount() > 1) {
226            open();
227        }
228    }//GEN-LAST:event_jTable1MouseClicked
229
230    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
231        search();
232}//GEN-LAST:event_jButton1ActionPerformed
233
234    private void jCheckBox2ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBox2ItemStateChanged
235//        scope.setEnabled(!jCheckBox2.isSelected());
236}//GEN-LAST:event_jCheckBox2ItemStateChanged
237
238    private void keyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keyActionPerformed
239        search();
240}//GEN-LAST:event_keyActionPerformed
241
242    private void jTable1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyTyped
243    }//GEN-LAST:event_jTable1KeyTyped
244    // Variables declaration - do not modify//GEN-BEGIN:variables
245    private javax.swing.JButton jButton1;
246    private javax.swing.JButton jButton2;
247    private javax.swing.JCheckBox jCheckBox1;
248    private javax.swing.JCheckBox jCheckBox2;
249    private javax.swing.JLabel jLabel2;
250    private javax.swing.JPanel jPanel1;
251    private javax.swing.JScrollPane jScrollPane1;
252    private javax.swing.JTable jTable1;
253    private javax.swing.JComboBox key;
254    // End of variables declaration//GEN-END:variables
255
256    private void search() {
257        String newSelection = (String) key.getSelectedItem();
258
259        if (newSelection != null && !newSelection.equals(oldSelection)) {
260            key.addItem(newSelection);
261            oldSelection = newSelection;
262        }
263
264        if (newSelection == null || newSelection.equals("null")) {
265            newSelection = "";
266        }
267
268        Object[][] data = null;
269        if (!jCheckBox2.isSelected()) {
270            for (int i = 0; i < Context.getSearchableContexts().size(); i++) {
271                Context c = Context.getSearchableContexts().get(i);
272                c.addReference(Context.getGroup());
273                DatabaseSearch s = new DatabaseSearch(c);
274                data = ArrayUtilities.merge(data, s.getValuesFor(c.getDbIdentity() + ".ids, '" + c.getDbIdentity() + "', " + c.getDbIdentity() + ".cname," + "groups.cname," + c.getDbIdentity() + ".dateadded", DatabaseObject.getObject(c).getStringVars().toArray(new String[]{}), newSelection, !jCheckBox1.isSelected()));
275            }
276            jTable1.setModel(new MPTableModel(data));
277        } else {
278            DatabaseSearch s = new DatabaseSearch(Context.getSearchIndex());
279            data = s.getValuesFor("rowid, dbidentity, text", "text", newSelection, !jCheckBox1.isSelected());
280            jTable1.setModel(new MPTableModel(HtmlParser.getMarkedHtml(data, 2, newSelection)));
281        }
282
283        TableFormat.hideHeader(jTable1);
284        TableFormat.stripFirstColumn(jTable1);
285        //TableFormat.format(jTable1, 1, 100);
286    }
287
288    private DatabaseObject getSelectedObject() {
289        return selection;
290    }
291
292    /**
293     * @return the additionalSearchCondition
294     */
295    public String getAdditionalSearchCondition() {
296        return additionalSearchCondition;
297    }
298
299    /**
300     * @param additionalSearchCondition the additionalSearchCondition to set
301     */
302    public void setAdditionalSearchCondition(String additionalSearchCondition) {
303        this.additionalSearchCondition = additionalSearchCondition;
304    }
305
306    private void open() {
307        Context lastContext = null;
308        if (jTable1.getSelectedRow() >= 0) {
309            Selection s = new Selection(jTable1);
310            lastContext = Context.getMatchingContext(jTable1.getValueAt(jTable1.getSelectedRow(), jTable1.convertColumnIndexToView(1)).toString());
311
312            if (lastContext != null) {
313                try {
314                    selection = DatabaseObject.getObject(lastContext, Integer.valueOf(jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 0).toString()));
315                    if (addTabs) {
316                        mpv5.YabsViewProxy.instance().getIdentifierView().addTab(selection);
317                    }
318                } catch (NodataFoundException ex) {
319                    mpv5.logging.Log.Debug(ex);
320                }
321                this.dispose();
322            }
323        }
324    }
325}