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