/src/mpv5/ui/panels/SearchPanel.java
Java | 293 lines | 220 code | 35 blank | 38 comment | 13 complexity | cf60065bd2571b16da74e67a7ae6698f MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * SearchPanel.java
- *
- * Created on Nov 30, 2008, 6:16:09 PM
- */
- package mpv5.ui.panels;
- import java.awt.Component;
- import java.awt.Cursor;
- import javax.swing.JComponent;
- import javax.swing.table.TableCellRenderer;
- import mpv5.YabsViewProxy;
- import mpv5.db.common.Context;
- import mpv5.db.common.DatabaseObject;
- import mpv5.db.common.DatabaseSearch;
- import mpv5.db.common.NodataFoundException;
- import mpv5.db.objects.User;
- import mpv5.db.objects.ValueProperty;
- import mpv5.logging.Log;
- import mpv5.ui.misc.MPTable;
- import mpv5.utils.models.MPTableModel;
- import mpv5.utils.tables.Selection;
- import mpv5.utils.tables.TableFormat;
- import mpv5.ui.misc.TableViewPersistenceHandler;
- /**
- *
- *
- */
- public class SearchPanel extends javax.swing.JPanel {
- private static final long serialVersionUID = 1L;
- private Context context;
- private DataPanel panel;
- private java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle();
- private String sf;
- private DatabaseSearch d;
- /** Creates new form SearchPanel */
- public SearchPanel() {
- initComponents();
- TableFormat.hideHeader(lookupResultTable);
- ((MPTable) lookupResultTable).setPersistanceHandler(new TableViewPersistenceHandler((MPTable) lookupResultTable, this));
- }
- public SearchPanel(Context context, DataPanel panel) {
- this();
- setName("searchpanel");
- this.validate();
- this.context = context;
- this.panel = panel;
- }
- /**
- * Reload the search result
- */
- public void refresh() {
- // search(lastneedle);
- }
- //
- // public void setContextOwner(DatabaseObject object) {
- // context.setOwner(object);
- // refresh();
- // }
- /** This me4thod is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- searchfields = new javax.swing.JPanel();
- textfield = new javax.swing.JTextField();
- results = new javax.swing.JPanel();
- resultsscrollpane = new javax.swing.JScrollPane();
- lookupResultTable = new mpv5.ui.misc.MPTable(this) {
- public Component prepareRenderer(TableCellRenderer renderer,
- int rowIndex, int vColIndex) {
- Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
- if (c instanceof JComponent) {
- JComponent jc = (JComponent)c;
- jc.setToolTipText(String.valueOf(getValueAt(rowIndex, vColIndex)));
- }
- return c;
- }
- };
- setName("Form"); // NOI18N
- java.util.ResourceBundle bundle = mpv5.i18n.LanguageManager.getBundle(); // NOI18N
- searchfields.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("SearchPanel.searchfields.border.title"))); // NOI18N
- searchfields.setName("searchfields"); // NOI18N
- searchfields.setLayout(new javax.swing.BoxLayout(searchfields, javax.swing.BoxLayout.LINE_AXIS));
- textfield.setText(bundle.getString("SearchPanel.textfield.text")); // NOI18N
- textfield.setName("textfield"); // NOI18N
- textfield.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- textfieldActionPerformed(evt);
- }
- });
- textfield.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyTyped(java.awt.event.KeyEvent evt) {
- textfieldKeyTyped(evt);
- }
- });
- searchfields.add(textfield);
- results.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("SearchPanel.results.border.title"))); // NOI18N
- results.setName("results"); // NOI18N
- resultsscrollpane.setName("resultsscrollpane"); // NOI18N
- lookupResultTable.setAutoCreateRowSorter(true);
- lookupResultTable.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- {},
- {},
- {},
- {}
- },
- new String [] {
- }
- ));
- lookupResultTable.setName("lookupResultTable"); // NOI18N
- lookupResultTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
- lookupResultTable.setShowVerticalLines(false);
- lookupResultTable.setSurrendersFocusOnKeystroke(true);
- lookupResultTable.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- lookupResultTableMouseClicked(evt);
- }
- });
- lookupResultTable.addKeyListener(new java.awt.event.KeyAdapter() {
- public void keyTyped(java.awt.event.KeyEvent evt) {
- lookupResultTableKeyTyped(evt);
- }
- });
- resultsscrollpane.setViewportView(lookupResultTable);
- javax.swing.GroupLayout resultsLayout = new javax.swing.GroupLayout(results);
- results.setLayout(resultsLayout);
- resultsLayout.setHorizontalGroup(
- resultsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(resultsscrollpane, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
- );
- resultsLayout.setVerticalGroup(
- resultsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(resultsscrollpane, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
- );
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
- this.setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(results, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(searchfields, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(searchfields, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 0, 0)
- .addComponent(results, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- }// </editor-fold>//GEN-END:initComponents
- private synchronized void search(final String value) {
- if (sf == null) {
- sf = "%%tablename%%.ids, %%tablename%%.cname";
- }
- if (d == null) {
- d = new DatabaseSearch(context, 500);
- }
- if (this.isShowing()) {
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- YabsViewProxy.instance().setProgressRunning(true);
- setCursor(new Cursor(Cursor.WAIT_CURSOR));
- try {
- DatabaseSearch s = new DatabaseSearch(context);
- Object[][] res = null;
- if (context.getDbIdentity().equals(Context.getItem().getDbIdentity())) {
- try {
- res = s.searchDataFor(sf, new Context[]{Context.getSubItem()}, new Context[]{Context.getCustomer()}, value);
- } catch (Exception ex) {
- Log.Debug(ex);
- }
- } else if (context.equals(Context.getProduct())) {
- try {
- res = s.searchDataFor(sf, new Context[]{Context.getSubItem()}, new Context[0], value);
- } catch (Exception ex) {
- Log.Debug(ex);
- }
- } else {
- try {
- res = s.searchDataFor(sf, new Context[0], new Context[0], value);
- } catch (Exception ex) {
- Log.Debug(ex);
- }
- }
- if (res != null) {
- lookupResultTable.setModel(new MPTableModel(res));
- }
- TableFormat.hideHeader(lookupResultTable);
- TableFormat.makeUneditable(lookupResultTable);
- TableFormat.stripColumns(lookupResultTable, new int[]{0, 4, 5, 6, 7, 8, 9});
- } catch (Exception e) {
- Log.Debug(e);
- } finally {
- YabsViewProxy.instance().setProgressRunning(false);
- setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
- }
- }
- };
- Log.Debug(this, "Starting search..");
- new Thread(runnable).start();
- }
- }
- private void lookupResultTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lookupResultTableMouseClicked
- Selection sel = new Selection(lookupResultTable);
- if (sel.checkID()) {
- try {
- panel.setDataOwner(DatabaseObject.getObject(context, sel.getId()), true);
- } catch (NodataFoundException ex) {
- Log.Debug(ex);
- User.getCurrentUser().getLayoutProperties().clear();
- try {
- ValueProperty.deleteProperty(User.getCurrentUser(), "layoutinfo");
- } catch (Exception e) {
- Log.Debug(ex);
- }
- // mpv5.YabsViewProxy.instance().resetTables();
- }
- }
- }//GEN-LAST:event_lookupResultTableMouseClicked
- private void lookupResultTableKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lookupResultTableKeyTyped
- }//GEN-LAST:event_lookupResultTableKeyTyped
- private void textfieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textfieldActionPerformed
- search(textfield.getText());
- }//GEN-LAST:event_textfieldActionPerformed
- private void textfieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textfieldKeyTyped
- }//GEN-LAST:event_textfieldKeyTyped
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private javax.swing.JTable lookupResultTable;
- private javax.swing.JPanel results;
- private javax.swing.JScrollPane resultsscrollpane;
- private javax.swing.JPanel searchfields;
- private javax.swing.JTextField textfield;
- // End of variables declaration//GEN-END:variables
- /**
- * @return the context
- */
- public Context getContext() {
- return context;
- }
- public void search() {
- }
- /**
- * @param context the context to set
- */
- public void setContext(Context context) {
- this.context = context;
- this.d = null;
- this.sf = null;
- }
- }