/*
Copyright 2011 marcopar@gmail.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package eu.flatworld.cstrader;
import eu.flatworld.commons.log.LogX;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
public class ImportPricelistDialog extends javax.swing.JDialog {
String importedText;
eu.flatworld.cstrader.data.System system;
/** Creates new form UpdatePricelistDialog */
public ImportPricelistDialog(java.awt.Frame parent, eu.flatworld.cstrader.data.System system) {
super(parent, true);
this.system = system;
initComponents();
getRootPane().setDefaultButton(jbOK);
setTitle("Import pricelist for " + system.name());
}
public ImportPricelistDialog(java.awt.Frame parent) {
super(parent, true);
this.system = null;
initComponents();
getRootPane().setDefaultButton(jbOK);
setTitle("Import pricelists for all systems");
}
/** 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() {
jpmMenu = new javax.swing.JPopupMenu();
jmiPaste = new javax.swing.JMenuItem();
jmiClearAll = new javax.swing.JMenuItem();
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
jtaImportArea = new javax.swing.JTextArea();
jbOK = new javax.swing.JButton();
jbCancel = new javax.swing.JButton();
jbLoad = new javax.swing.JButton();
jmiPaste.setText("Paste");
jmiPaste.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jmiPasteActionPerformed(evt);
}
});
jpmMenu.add(jmiPaste);
jmiClearAll.setText("jmiClearAll");
jmiClearAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jmiClearAllActionPerformed(evt);
}
});
jpmMenu.add(jmiClearAll);
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Import pricelist");
jLabel1.setText("<html>Copy the pricelist(s) and then paste here, you can also paste the html source:");
jtaImportArea.setColumns(20);
jtaImportArea.setRows(5);
jtaImportArea.setComponentPopupMenu(jpmMenu);
jScrollPane1.setViewportView(jtaImportArea);
jbOK.setText("OK");
jbOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbOKActionPerformed(evt);
}
});
jbCancel.setText("Cancel");
jbCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbCancelActionPerformed(evt);
}
});
jbLoad.setText("Load...");
jbLoad.setToolTipText("Load a file containing the pricelist(s)");
jbLoad.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbLoadActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE).addGroup(layout.createSequentialGroup().addComponent(jbLoad).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 290, Short.MAX_VALUE).addComponent(jbCancel).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jbOK)).addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)).addContainerGap()));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jLabel1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jbOK).addComponent(jbCancel).addComponent(jbLoad)).addContainerGap()));
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width - 531) / 2, (screenSize.height - 536) / 2, 531, 536);
}// </editor-fold>//GEN-END:initComponents
private void jbOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbOKActionPerformed
importedText = jtaImportArea.getText();
if ((this.system != null) && (!importedText.toLowerCase().contains(system.name().toLowerCase()))) {
int resp = JOptionPane.showConfirmDialog(this, "It seems that the imported pricelist\nis not of the " + system.name() + " system.", "Pricelists warning", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if (resp == JOptionPane.CANCEL_OPTION) {
return;
}
}
dispose();
}//GEN-LAST:event_jbOKActionPerformed
private void jbCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelActionPerformed
importedText = null;
dispose();
}//GEN-LAST:event_jbCancelActionPerformed
private void jmiPasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiPasteActionPerformed
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
try {
Transferable content = c.getContents(this);
if (content != null) {
String dstData = (String) content.getTransferData(DataFlavor.stringFlavor);
jtaImportArea.setText(dstData);
}
} catch (Exception ex) {
LogX.log(Level.WARNING, "Error accessing clipboard", ex, true);
}
jbOK.grabFocus();
}//GEN-LAST:event_jmiPasteActionPerformed
private void jmiClearAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiClearAllActionPerformed
jtaImportArea.setText("");
}//GEN-LAST:event_jmiClearAllActionPerformed
private void jbLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLoadActionPerformed
JFileChooser jfc = new JFileChooser();
int rv = jfc.showOpenDialog(this);
if (rv == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
try {
BufferedReader irf = new BufferedReader(new FileReader(file));
StringBuilder intext = new StringBuilder();
String line = null;
while ((line = irf.readLine()) != null) {
intext.append(line);
intext.append(System.getProperty("line.separator"));
}
irf.close();
jtaImportArea.setText(intext.toString());
} catch (Exception ex) {
LogX.log(Level.SEVERE, "Error loading pricelists: " + file.getAbsolutePath(), ex, true);
}
} else {
LogX.log(Level.WARNING, "Open command cancelled by user.");
}
}//GEN-LAST:event_jbLoadActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton jbCancel;
private javax.swing.JButton jbLoad;
private javax.swing.JButton jbOK;
private javax.swing.JMenuItem jmiClearAll;
private javax.swing.JMenuItem jmiPaste;
private javax.swing.JPopupMenu jpmMenu;
private javax.swing.JTextArea jtaImportArea;
// End of variables declaration//GEN-END:variables
public String getImportedText() {
return importedText;
}
}