/**
* Copyright (c) 2014, the Temporal Random Indexing AUTHORS.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the University of Bari nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007
*
*/
package di.uniba.it.tri.shell.gui;
import di.uniba.it.tri.shell.gui.data.TimePeriod;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author pierpaolo
*/
public class TimeSetupDialog extends javax.swing.JDialog {
private Map<String, TimePeriod> periodMap;
/**
* Creates new form TimeSetupDialog
*
* @param parent
* @param modal
* @param periods
*/
public TimeSetupDialog(java.awt.Frame parent, boolean modal, DefaultComboBoxModel<String> periods) {
super(parent, modal);
initComponents();
this.comboBox.setModel(periods);
this.periodMap = new HashMap<>();
for (int i = 0; i < periods.getSize(); i++) {
periodMap.put(periods.getElementAt(i), new TimePeriod(periods.getElementAt(i)));
}
}
public Map<String, TimePeriod> getPeriodMap() {
return periodMap;
}
private void save(File file) throws Exception {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
for (Map.Entry<String, TimePeriod> e : periodMap.entrySet()) {
writer.append(e.getValue().printToFile());
writer.newLine();
}
writer.close();
}
private void load(File file) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader(file));
Map<String, TimePeriod> tmpPeriodMap = new HashMap<>();
while (reader.ready()) {
String[] split = reader.readLine().split("\t");
TimePeriod tp = new TimePeriod(split[0]);
tp.setStart(new Date(Long.parseLong(split[1])));
tp.setEnd(new Date(Long.parseLong(split[2])));
tmpPeriodMap.put(tp.getKey(), tp);
}
reader.close();
this.periodMap = tmpPeriodMap;
}
/**
* 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() {
java.awt.GridBagConstraints gridBagConstraints;
comboBox = new javax.swing.JComboBox<>();
daySpS = new javax.swing.JSpinner();
monthSpS = new javax.swing.JSpinner();
yearSpS = new javax.swing.JSpinner();
setp = new javax.swing.JButton();
daySpE = new javax.swing.JSpinner();
monthSpE = new javax.swing.JSpinner();
yearSpE = new javax.swing.JSpinner();
openb = new javax.swing.JButton();
saveb = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Time period setting");
setPreferredSize(new java.awt.Dimension(500, 180));
getContentPane().setLayout(new java.awt.GridBagLayout());
comboBox.setPreferredSize(new java.awt.Dimension(120, 29));
comboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
comboBoxActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
getContentPane().add(comboBox, gridBagConstraints);
daySpS.setModel(new javax.swing.SpinnerNumberModel(1, 1, 31, 1));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
getContentPane().add(daySpS, gridBagConstraints);
monthSpS.setModel(new javax.swing.SpinnerNumberModel(1, 1, 12, 1));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
getContentPane().add(monthSpS, gridBagConstraints);
yearSpS.setModel(new javax.swing.SpinnerNumberModel(2016, -2500, 2500, 1));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
getContentPane().add(yearSpS, gridBagConstraints);
setp.setText("Set");
setp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
setpActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 0;
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
getContentPane().add(setp, gridBagConstraints);
daySpE.setModel(new javax.swing.SpinnerNumberModel(1, 1, 31, 1));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
getContentPane().add(daySpE, gridBagConstraints);
monthSpE.setModel(new javax.swing.SpinnerNumberModel(1, 1, 12, 1));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
getContentPane().add(monthSpE, gridBagConstraints);
yearSpE.setModel(new javax.swing.SpinnerNumberModel(2016, -2500, 2500, 1));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
getContentPane().add(yearSpE, gridBagConstraints);
openb.setText("Open....");
openb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openbActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 12);
getContentPane().add(openb, gridBagConstraints);
saveb.setText("Save...");
saveb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
savebActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 4;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 12);
getContentPane().add(saveb, gridBagConstraints);
pack();
setLocationRelativeTo(null);
}// </editor-fold>//GEN-END:initComponents
private void comboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBoxActionPerformed
Object item = comboBox.getSelectedItem();
if (item != null) {
TimePeriod tp = periodMap.get(item.toString());
Calendar cs = Calendar.getInstance();
cs.setTime(tp.getStart());
daySpS.setValue(cs.get(Calendar.DAY_OF_MONTH));
monthSpS.setValue(cs.get(Calendar.MONTH) + 1);
yearSpS.setValue(cs.get(Calendar.YEAR));
Calendar ce = Calendar.getInstance();
ce.setTime(tp.getEnd());
daySpE.setValue(ce.get(Calendar.DAY_OF_MONTH));
monthSpE.setValue(ce.get(Calendar.MONTH) + 1);
yearSpE.setValue(ce.get(Calendar.YEAR));
}
}//GEN-LAST:event_comboBoxActionPerformed
private void setpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setpActionPerformed
Object item = comboBox.getSelectedItem();
if (item != null) {
TimePeriod tp = periodMap.get(item.toString());
Calendar cs = Calendar.getInstance();
cs.set(Calendar.DAY_OF_MONTH, ((Number) daySpS.getValue()).intValue());
cs.set(Calendar.MONTH, ((Number) monthSpS.getValue()).intValue() - 1);
cs.set(Calendar.YEAR, ((Number) yearSpS.getValue()).intValue());
tp.setStart(cs.getTime());
Calendar ce = Calendar.getInstance();
ce.set(Calendar.DAY_OF_MONTH, ((Number) daySpE.getValue()).intValue());
ce.set(Calendar.MONTH, ((Number) monthSpE.getValue()).intValue() - 1);
ce.set(Calendar.YEAR, ((Number) yearSpE.getValue()).intValue());
tp.setEnd(ce.getTime());
}
}//GEN-LAST:event_setpActionPerformed
private void openbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openbActionPerformed
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home"));
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
load(fileChooser.getSelectedFile());
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error to open time periods\n" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}//GEN-LAST:event_openbActionPerformed
private void savebActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_savebActionPerformed
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home"));
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
save(fileChooser.getSelectedFile());
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error to save time periods\n" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
}//GEN-LAST:event_savebActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JComboBox<String> comboBox;
private javax.swing.JSpinner daySpE;
private javax.swing.JSpinner daySpS;
private javax.swing.JSpinner monthSpE;
private javax.swing.JSpinner monthSpS;
private javax.swing.JButton openb;
private javax.swing.JButton saveb;
private javax.swing.JButton setp;
private javax.swing.JSpinner yearSpE;
private javax.swing.JSpinner yearSpS;
// End of variables declaration//GEN-END:variables
}