PageRenderTime 332ms CodeModel.GetById 40ms app.highlight 245ms RepoModel.GetById 32ms app.codeStats 1ms

/src/mpv5/ui/panels/SearchPanel.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 293 lines | 220 code | 35 blank | 38 comment | 13 complexity | cf60065bd2571b16da74e67a7ae6698f MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5
  6/*
  7 * SearchPanel.java
  8 *
  9 * Created on Nov 30, 2008, 6:16:09 PM
 10 */
 11package mpv5.ui.panels;
 12
 13import java.awt.Component;
 14import java.awt.Cursor;
 15import javax.swing.JComponent;
 16import javax.swing.table.TableCellRenderer;
 17import mpv5.YabsViewProxy;
 18import mpv5.db.common.Context;
 19
 20import mpv5.db.common.DatabaseObject;
 21import mpv5.db.common.DatabaseSearch;
 22import mpv5.db.common.NodataFoundException;
 23import mpv5.db.objects.User;
 24import mpv5.db.objects.ValueProperty;
 25import mpv5.logging.Log;
 26import mpv5.ui.misc.MPTable;
 27import mpv5.utils.models.MPTableModel;
 28import mpv5.utils.tables.Selection;
 29import mpv5.utils.tables.TableFormat;
 30import mpv5.ui.misc.TableViewPersistenceHandler;
 31
 32/**
 33 *
 34 * 
 35 */
 36public class SearchPanel extends javax.swing.JPanel {
 37
 38    private static final long serialVersionUID = 1L;
 39    private Context context;
 40    private DataPanel panel;
 41    private java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle();
 42    private String sf;
 43    private DatabaseSearch d;
 44
 45    /** Creates new form SearchPanel */
 46    public SearchPanel() {
 47        initComponents();
 48        TableFormat.hideHeader(lookupResultTable);
 49        ((MPTable) lookupResultTable).setPersistanceHandler(new TableViewPersistenceHandler((MPTable) lookupResultTable, this));
 50    }
 51
 52    public SearchPanel(Context context, DataPanel panel) {
 53        this();
 54        setName("searchpanel");
 55        this.validate();
 56        this.context = context;
 57        this.panel = panel;
 58    }
 59
 60    /**
 61     * Reload the search result
 62     */
 63    public void refresh() {
 64//        search(lastneedle);
 65    }
 66//
 67//    public void setContextOwner(DatabaseObject object) {
 68//        context.setOwner(object);
 69//        refresh();
 70//    }
 71
 72    /** This me4thod is called from within the constructor to
 73     * initialize the form.
 74     * WARNING: Do NOT modify this code. The content of this method is
 75     * always regenerated by the Form Editor.
 76     */
 77    @SuppressWarnings("unchecked")
 78    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 79    private void initComponents() {
 80
 81        searchfields = new javax.swing.JPanel();
 82        textfield = new javax.swing.JTextField();
 83        results = new javax.swing.JPanel();
 84        resultsscrollpane = new javax.swing.JScrollPane();
 85        lookupResultTable = new  mpv5.ui.misc.MPTable(this) {
 86            public Component prepareRenderer(TableCellRenderer renderer,
 87                int rowIndex, int vColIndex) {
 88                Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
 89                if (c instanceof JComponent) {
 90                    JComponent jc = (JComponent)c;
 91                    jc.setToolTipText(String.valueOf(getValueAt(rowIndex, vColIndex)));
 92                }
 93                return c;
 94            }
 95        };
 96
 97        setName("Form"); // NOI18N
 98
 99        java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
100        searchfields.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("SearchPanel.searchfields.border.title"))); // NOI18N
101        searchfields.setName("searchfields"); // NOI18N
102        searchfields.setLayout(new javax.swing.BoxLayout(searchfields, javax.swing.BoxLayout.LINE_AXIS));
103
104        textfield.setText(bundle.getString("SearchPanel.textfield.text")); // NOI18N
105        textfield.setName("textfield"); // NOI18N
106        textfield.addActionListener(new java.awt.event.ActionListener() {
107            public void actionPerformed(java.awt.event.ActionEvent evt) {
108                textfieldActionPerformed(evt);
109            }
110        });
111        textfield.addKeyListener(new java.awt.event.KeyAdapter() {
112            public void keyTyped(java.awt.event.KeyEvent evt) {
113                textfieldKeyTyped(evt);
114            }
115        });
116        searchfields.add(textfield);
117
118        results.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("SearchPanel.results.border.title"))); // NOI18N
119        results.setName("results"); // NOI18N
120
121        resultsscrollpane.setName("resultsscrollpane"); // NOI18N
122
123        lookupResultTable.setAutoCreateRowSorter(true);
124        lookupResultTable.setModel(new javax.swing.table.DefaultTableModel(
125            new Object [][] {
126                {},
127                {},
128                {},
129                {}
130            },
131            new String [] {
132
133            }
134        ));
135        lookupResultTable.setName("lookupResultTable"); // NOI18N
136        lookupResultTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
137        lookupResultTable.setShowVerticalLines(false);
138        lookupResultTable.setSurrendersFocusOnKeystroke(true);
139        lookupResultTable.addMouseListener(new java.awt.event.MouseAdapter() {
140            public void mouseClicked(java.awt.event.MouseEvent evt) {
141                lookupResultTableMouseClicked(evt);
142            }
143        });
144        lookupResultTable.addKeyListener(new java.awt.event.KeyAdapter() {
145            public void keyTyped(java.awt.event.KeyEvent evt) {
146                lookupResultTableKeyTyped(evt);
147            }
148        });
149        resultsscrollpane.setViewportView(lookupResultTable);
150
151        javax.swing.GroupLayout resultsLayout = new javax.swing.GroupLayout(results);
152        results.setLayout(resultsLayout);
153        resultsLayout.setHorizontalGroup(
154            resultsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
155            .addComponent(resultsscrollpane, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
156        );
157        resultsLayout.setVerticalGroup(
158            resultsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
159            .addComponent(resultsscrollpane, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
160        );
161
162        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
163        this.setLayout(layout);
164        layout.setHorizontalGroup(
165            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
166            .addComponent(results, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
167            .addComponent(searchfields, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
168        );
169        layout.setVerticalGroup(
170            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
171            .addGroup(layout.createSequentialGroup()
172                .addComponent(searchfields, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
173                .addGap(0, 0, 0)
174                .addComponent(results, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
175        );
176    }// </editor-fold>//GEN-END:initComponents
177
178    private synchronized void search(final String value) {
179        if (sf == null) {
180            sf = "%%tablename%%.ids, %%tablename%%.cname";
181        }
182        if (d == null) {
183            d = new DatabaseSearch(context, 500);
184        }
185
186        if (this.isShowing()) {
187
188            Runnable runnable = new Runnable() {
189
190                @Override
191                public void run() {
192                    YabsViewProxy.instance().setProgressRunning(true);
193                    setCursor(new Cursor(Cursor.WAIT_CURSOR));
194                    try {
195                        DatabaseSearch s = new DatabaseSearch(context);
196                        Object[][] res = null;
197                        if (context.getDbIdentity().equals(Context.getItem().getDbIdentity())) {
198                            try {
199                                res = s.searchDataFor(sf, new Context[]{Context.getSubItem()}, new Context[]{Context.getCustomer()}, value);
200                            } catch (Exception ex) {
201                                Log.Debug(ex);
202                            }
203                        } else if (context.equals(Context.getProduct())) {
204                            try {
205                                res = s.searchDataFor(sf, new Context[]{Context.getSubItem()}, new Context[0], value);
206                            } catch (Exception ex) {
207                                Log.Debug(ex);
208                            }
209                        } else {
210                            try {
211                                res = s.searchDataFor(sf, new Context[0], new Context[0], value);
212                            } catch (Exception ex) {
213                                Log.Debug(ex);
214                            }
215                        }
216
217                        if (res != null) {
218                            lookupResultTable.setModel(new MPTableModel(res));
219                        }
220
221                        TableFormat.hideHeader(lookupResultTable);
222                        TableFormat.makeUneditable(lookupResultTable);
223                        TableFormat.stripColumns(lookupResultTable, new int[]{0, 4, 5, 6, 7, 8, 9});
224                    } catch (Exception e) {
225                        Log.Debug(e);
226                    } finally {
227                        YabsViewProxy.instance().setProgressRunning(false);
228                        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
229                    }
230                }
231            };
232            Log.Debug(this, "Starting search..");
233            new Thread(runnable).start();
234        }
235
236    }
237
238    private void lookupResultTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lookupResultTableMouseClicked
239        Selection sel = new Selection(lookupResultTable);
240        if (sel.checkID()) {
241            try {
242                panel.setDataOwner(DatabaseObject.getObject(context, sel.getId()), true);
243            } catch (NodataFoundException ex) {
244                Log.Debug(ex);
245                User.getCurrentUser().getLayoutProperties().clear();
246                try {
247                    ValueProperty.deleteProperty(User.getCurrentUser(), "layoutinfo");
248                } catch (Exception e) {
249                    Log.Debug(ex);
250                }
251
252//                mpv5.YabsViewProxy.instance().resetTables();
253            }
254        }
255    }//GEN-LAST:event_lookupResultTableMouseClicked
256
257    private void lookupResultTableKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lookupResultTableKeyTyped
258    }//GEN-LAST:event_lookupResultTableKeyTyped
259
260    private void textfieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textfieldActionPerformed
261        search(textfield.getText());
262    }//GEN-LAST:event_textfieldActionPerformed
263
264    private void textfieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textfieldKeyTyped
265
266    }//GEN-LAST:event_textfieldKeyTyped
267    // Variables declaration - do not modify//GEN-BEGIN:variables
268    private javax.swing.JTable lookupResultTable;
269    private javax.swing.JPanel results;
270    private javax.swing.JScrollPane resultsscrollpane;
271    private javax.swing.JPanel searchfields;
272    private javax.swing.JTextField textfield;
273    // End of variables declaration//GEN-END:variables
274
275    /**
276     * @return the context
277     */
278    public Context getContext() {
279        return context;
280    }
281
282    public void search() {
283    }
284
285    /**
286     * @param context the context to set
287     */
288    public void setContext(Context context) {
289        this.context = context;
290        this.d = null;
291        this.sf = null;
292    }
293}