Java tutorial
/* * Copyright (C) 2015 Yann D'Isanto * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package org.netbeans.modules.mongodb.ui.wizards; import com.mongodb.client.MongoDatabase; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JComboBox; import javax.swing.JPanel; import javax.swing.event.ChangeListener; import org.bson.BsonDocument; import org.netbeans.modules.mongodb.api.FindCriteria; import org.netbeans.modules.mongodb.ui.components.FindCriteriaEditor; import org.openide.util.ChangeSupport; import org.openide.util.NbBundle.Messages; @Messages({ "ExportQueryStep=Query options" }) public final class ExportVisualPanel1 extends JPanel { private final ChangeSupport changeSupport = new ChangeSupport(this); private final FindCriteriaEditor criteriaEditor = new FindCriteriaEditor(); public ExportVisualPanel1(MongoDatabase db) { initComponents(); for (String collection : db.listCollectionNames()) { collectionComboBox.addItem(collection); } collectionComboBox.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { changeSupport.fireChange(); } }); } @Override public String getName() { return Bundle.ExportQueryStep(); } JComboBox<String> getCollectionComboBox() { return collectionComboBox; } FindCriteriaEditor getCriteriaEditor() { return criteriaEditor; } void updateQueryFieldsFromEditor() { FindCriteria findCriteria = criteriaEditor.getFindCriteria(); BsonDocument filter = findCriteria.getFilter(); BsonDocument projection = findCriteria.getProjection(); BsonDocument sort = findCriteria.getSort(); filterField.setText(filter != null ? filter.toJson() : ""); projectionField.setText(projection != null ? projection.toJson() : ""); sortField.setText(sort != null ? sort.toJson() : ""); } public final void addChangeListener(ChangeListener l) { changeSupport.addChangeListener(l); } public final void removeChangeListener(ChangeListener l) { changeSupport.removeChangeListener(l); } /** * This method 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. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { collectionLabel = new javax.swing.JLabel(); collectionComboBox = new javax.swing.JComboBox<>(); queryPanel = new javax.swing.JPanel(); editQueryButton = new javax.swing.JButton(); clearQueryButton = new javax.swing.JButton(); sortField = new javax.swing.JTextField(); sortLabel = new javax.swing.JLabel(); projectionLabel = new javax.swing.JLabel(); filterLabel = new javax.swing.JLabel(); filterField = new javax.swing.JTextField(); projectionField = new javax.swing.JTextField(); org.openide.awt.Mnemonics.setLocalizedText(collectionLabel, org.openide.util.NbBundle .getMessage(ExportVisualPanel1.class, "ExportVisualPanel1.collectionLabel.text")); // NOI18N queryPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle .getMessage(ExportVisualPanel1.class, "ExportVisualPanel1.queryPanel.border.title"))); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(editQueryButton, org.openide.util.NbBundle .getMessage(ExportVisualPanel1.class, "ExportVisualPanel1.editQueryButton.text")); // NOI18N editQueryButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { editQueryButtonActionPerformed(evt); } }); org.openide.awt.Mnemonics.setLocalizedText(clearQueryButton, org.openide.util.NbBundle .getMessage(ExportVisualPanel1.class, "ExportVisualPanel1.clearQueryButton.text")); // NOI18N clearQueryButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { clearQueryButtonActionPerformed(evt); } }); sortField.setEditable(false); org.openide.awt.Mnemonics.setLocalizedText(sortLabel, org.openide.util.NbBundle .getMessage(ExportVisualPanel1.class, "ExportVisualPanel1.sortLabel.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(projectionLabel, org.openide.util.NbBundle .getMessage(ExportVisualPanel1.class, "ExportVisualPanel1.projectionLabel.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(filterLabel, org.openide.util.NbBundle .getMessage(ExportVisualPanel1.class, "ExportVisualPanel1.filterLabel.text")); // NOI18N filterField.setEditable(false); projectionField.setEditable(false); javax.swing.GroupLayout queryPanelLayout = new javax.swing.GroupLayout(queryPanel); queryPanel.setLayout(queryPanelLayout); queryPanelLayout.setHorizontalGroup(queryPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(queryPanelLayout.createSequentialGroup().addContainerGap() .addGroup(queryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(queryPanelLayout.createSequentialGroup().addGap(6, 6, 6) .addComponent(editQueryButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(clearQueryButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 355, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(queryPanelLayout.createSequentialGroup().addComponent(filterLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(filterField)) .addGroup(queryPanelLayout.createSequentialGroup().addComponent(projectionLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(projectionField)) .addGroup(queryPanelLayout.createSequentialGroup().addComponent(sortLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(sortField))) .addContainerGap())); queryPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { filterLabel, projectionLabel, sortLabel }); queryPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { clearQueryButton, editQueryButton }); queryPanelLayout.setVerticalGroup(queryPanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(queryPanelLayout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(queryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(filterLabel).addComponent(filterField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(queryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(projectionLabel).addComponent(projectionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(queryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(sortLabel).addComponent(sortField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(queryPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(editQueryButton).addComponent(clearQueryButton)))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(collectionLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(collectionComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(queryPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(collectionLabel).addComponent(collectionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(queryPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); }// </editor-fold>//GEN-END:initComponents private void editQueryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editQueryButtonActionPerformed if (criteriaEditor.showDialog()) { updateQueryFieldsFromEditor(); } }//GEN-LAST:event_editQueryButtonActionPerformed private void clearQueryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearQueryButtonActionPerformed criteriaEditor.setFindCriteria(FindCriteria.EMPTY); updateQueryFieldsFromEditor(); }//GEN-LAST:event_clearQueryButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton clearQueryButton; private javax.swing.JComboBox<String> collectionComboBox; private javax.swing.JLabel collectionLabel; private javax.swing.JButton editQueryButton; private javax.swing.JTextField filterField; private javax.swing.JLabel filterLabel; private javax.swing.JTextField projectionField; private javax.swing.JLabel projectionLabel; private javax.swing.JPanel queryPanel; private javax.swing.JTextField sortField; private javax.swing.JLabel sortLabel; // End of variables declaration//GEN-END:variables }