/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.cloud;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
/**
*
* @author Chen
*/
public class CloudObjectConsole extends javax.swing.JFrame {
private int page = 0;
private boolean isQuery = false;
private int [] scopeOptions = new int [] {
CloudObject.ACCESS_PRIVATE,
CloudObject.ACCESS_APPLICATION_READ_ONLY,
CloudObject.ACCESS_APPLICATION,
CloudObject.ACCESS_PUBLIC_READ_ONLY,
CloudObject.ACCESS_PUBLIC
};
/**
* Creates new form CloudObjectConsole
*/
public CloudObjectConsole() {
initComponents();
}
/**
* 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
dataTable = new javax.swing.JTable();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
pageNumber = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
scope = new javax.swing.JComboBox();
jLabel1 = new javax.swing.JLabel();
typeTxt = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
indexCombo = new javax.swing.JComboBox();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
queryCombo = new javax.swing.JComboBox();
queryTxt = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Cloud Storage");
jButton1.setText("Load");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
dataTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
}
));
jScrollPane1.setViewportView(dataTable);
jButton2.setText("Prev");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setText("Next");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
jLabel2.setText("Page");
pageNumber.setText("0");
jLabel3.setText("Scope");
scope.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PRIVATE", "APPLICATION_READ_ONLY", "APPLICATION", "PUBLIC_READ_ONLY", "PUBLIC" }));
jLabel1.setText("Type");
jLabel4.setText("Index");
indexCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Unsorted", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }));
buttonGroup1.add(jRadioButton1);
jRadioButton1.setSelected(true);
jRadioButton1.setText("Sort");
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
buttonGroup1.add(jRadioButton2);
jRadioButton2.setText("Query");
jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton2ActionPerformed(evt);
}
});
queryCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Equals", "Greater", "Lesser" }));
queryCombo.setEnabled(false);
queryTxt.setEnabled(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(typeTxt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(indexCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jRadioButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(queryCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(queryTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE))
.addComponent(scope, 0, 415, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 340, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(pageNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 556, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(scope, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(typeTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(indexCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(queryCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(queryTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(jLabel2)
.addComponent(pageNumber))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jButton3)
.addContainerGap())))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
loadData(null);
}//GEN-LAST:event_jButton1ActionPerformed
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
loadData(new Boolean(true));
}//GEN-LAST:event_jButton3ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
loadData(new Boolean(false));
}//GEN-LAST:event_jButton2ActionPerformed
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
isQuery = false;
queryCombo.setEnabled(false);
queryTxt.setEnabled(false);
}//GEN-LAST:event_jRadioButton1ActionPerformed
private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
isQuery = true;
queryCombo.setEnabled(true);
queryTxt.setEnabled(true);
}//GEN-LAST:event_jRadioButton2ActionPerformed
private void loadData(Boolean nextPage) {
final JDialog d = new JDialog(this);
d.setModal(false);
d.setLocationRelativeTo(this);
d.setAlwaysOnTop(true);
d.add(new JLabel("Loading..."));
d.pack();
d.setVisible(true);
final int [] requestPage = new int [1];
requestPage[0] = page;
if(nextPage != null){
if(nextPage.booleanValue()){
requestPage[0] += 30;
}else{
requestPage[0] -= 30;
if(requestPage[0] < 0){
requestPage[0] = 0;
}
}
}else{
page = 0;
requestPage[0] = 0;
}
new Thread() {
@Override
public void run() {
try {
String q = "*";
String queryType = typeTxt.getText();
if(queryType != null && queryType.length() > 0){
q = queryType;
}
int index = indexCombo.getSelectedIndex();
CloudObject[] objects = null;
if(isQuery){
int queryComboVal = queryCombo.getSelectedIndex();
String queryVal = queryTxt.getText();
switch(queryComboVal){
case 0:
objects = CloudStorage.getInstance().queryEquals(q, index, queryVal, requestPage[0], 30, scopeOptions[scope.getSelectedIndex()]);
break;
case 1:
objects = CloudStorage.getInstance().queryGreaterThan(q, index, queryVal, requestPage[0], 30, scopeOptions[scope.getSelectedIndex()]);
break;
case 2:
objects = CloudStorage.getInstance().queryLessThan(q, index, queryVal, requestPage[0], 30, scopeOptions[scope.getSelectedIndex()]);
break;
}
}else{
objects = CloudStorage.getInstance().querySorted(q, index, true, requestPage[0], 30, scopeOptions[scope.getSelectedIndex()]);
}
if (objects != null && objects.length > 0) {
Hashtable vals = objects[0].getValues();
final Vector cols = new Vector();
cols.add("CloudId");
cols.add("Type");
Enumeration keys = vals.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
if (key.equals("CN1Type")) {
continue;
}
if (key.startsWith("CN1")) {
cols.add(key.substring(3, key.length()));
}else{
cols.add(key);
}
}
final Vector data = new Vector();
for (int i = 0; i < objects.length; i++) {
Vector v = new Vector();
CloudObject cloudObject = objects[i];
v.add(cloudObject.getCloudId());
v.add(cloudObject.getType());
vals = cloudObject.getValues();
keys = vals.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
if (key.equals("CN1Type")) {
continue;
}
v.add(vals.get(key).toString());
}
data.add(v);
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
d.setVisible(false);
DefaultTableModel tableModel = new DefaultTableModel(data, cols) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
dataTable.setModel(tableModel);
page = requestPage[0];
pageNumber.setText("" + (page /30));
}
});
} else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
d.setVisible(false);
JOptionPane.showMessageDialog(CloudObjectConsole.this, "No Results Found");
}
});
}
} catch (CloudException ex) {
ex.printStackTrace();
}
}
}.start();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the
* default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CloudObjectConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CloudObjectConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CloudObjectConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CloudObjectConsole.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CloudObjectConsole().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JTable dataTable;
private javax.swing.JComboBox indexCombo;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel pageNumber;
private javax.swing.JComboBox queryCombo;
private javax.swing.JTextField queryTxt;
private javax.swing.JComboBox scope;
private javax.swing.JTextField typeTxt;
// End of variables declaration//GEN-END:variables
}