PageRenderTime 35ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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