/*
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.Storage;
import eu.flatworld.cstrader.data.ItemLine;
import eu.flatworld.cstrader.data.Pricelist;
import eu.flatworld.commons.log.LogX;
import java.util.logging.Level;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.RowSorter;
import javax.swing.table.TableRowSorter;
public class GalaxyPricelistPanel extends javax.swing.JPanel {
public final static String P_PROPERTIESCHANGED = "galaxypanel.propertieschanged";
public final static String P_PRICELISTSUPDATED = "galaxypanel.pricelistsupdated";
Pricelist pricelists[];
Map<String, LocationProperty> locationsProperties;
Map<String, ItemProperty> itemsProperties;
Map<String, Long> weightCache;
/** Creates new form GalaxyPricelistPanel */
public GalaxyPricelistPanel() {
initComponents();
}
public void setup(Pricelist pricelists[]) {
this.pricelists = pricelists;
ArrayList<ItemLine> forSale = new ArrayList<ItemLine>();
ArrayList<ItemLine> wanted = new ArrayList<ItemLine>();
for (Pricelist pricelist : pricelists) {
forSale.addAll(pricelist.getForSale());
wanted.addAll(pricelist.getWanted());
}
locationsProperties = (Map<String, LocationProperty>) Storage.getStorage().get(Main.STORAGE_LOCATIONSPROPERTIES);
itemsProperties = (Map<String, ItemProperty>) Storage.getStorage().get(Main.STORAGE_ITEMPROPERTIES);
weightCache = (Map<String, Long>) Storage.getStorage().get(Main.STORAGE_WEIGHTCACHE);
List<? extends RowSorter.SortKey> sk = jtForSale.getRowSorter().getSortKeys();
jtForSale.setModel(new FullItemsTableModel(forSale, true));
//Tables.adjustColumnsWidth(jtForSale);
jtForSale.getRowSorter().setSortKeys(sk);
for (int i = 0; i < jtForSale.getModel().getColumnCount(); i++) {
jtForSale.setDefaultRenderer(jtForSale.getModel().getColumnClass(i), new ItemCellRenderer(locationsProperties, itemsProperties));
}
sk = jtWanted.getRowSorter().getSortKeys();
jtWanted.setModel(new FullItemsTableModel(wanted, false));
//Tables.adjustColumnsWidth(jtWanted);
jtWanted.getRowSorter().setSortKeys(sk);
for (int i = 0; i < jtWanted.getModel().getColumnCount(); i++) {
jtWanted.setDefaultRenderer(jtWanted.getModel().getColumnClass(i), new ItemCellRenderer(locationsProperties, itemsProperties));
}
//Tables.adjustColumnsWidth(jtWanted);
//Tables.adjustColumnsWidth(jtForSale);
}
public void setRowFilter(RowFilter forSaleFilter, RowFilter wantedFilter) {
if (forSaleFilter == null) {
((TableRowSorter) jtForSale.getRowSorter()).setRowFilter(null);
} else {
((TableRowSorter) jtForSale.getRowSorter()).setRowFilter(forSaleFilter);
}
if (wantedFilter == null) {
((TableRowSorter) jtWanted.getRowSorter()).setRowFilter(null);
} else {
((TableRowSorter) jtWanted.getRowSorter()).setRowFilter(wantedFilter);
}
}
public void forceTablesUpdate() {
((ItemsTableModel) jtForSale.getModel()).fireTableDataChanged();
((ItemsTableModel) jtWanted.getModel()).fireTableDataChanged();
}
/** 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() {
jpmWanted = new javax.swing.JPopupMenu();
jmiWantedEditPriceWatch = new javax.swing.JMenuItem();
jmicbWantedIgnore = new javax.swing.JCheckBoxMenuItem();
jpmForSale = new javax.swing.JPopupMenu();
jmiForSaleEditPriceWatch = new javax.swing.JMenuItem();
jmicbForSaleIgnore = new javax.swing.JCheckBoxMenuItem();
jPanel1 = new javax.swing.JPanel();
jbClearAll = new javax.swing.JButton();
jbExportAll = new javax.swing.JButton();
jbImportAll = new javax.swing.JButton();
jSplitPane1 = new javax.swing.JSplitPane();
jPanel2 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
jtWanted = new javax.swing.JTable();
jPanel3 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jtForSale = new javax.swing.JTable();
jpmWanted.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
jpmWantedPopupMenuWillBecomeVisible(evt);
}
});
jmiWantedEditPriceWatch.setText("Edit price watch");
jmiWantedEditPriceWatch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jmiWantedEditPriceWatchActionPerformed(evt);
}
});
jpmWanted.add(jmiWantedEditPriceWatch);
jmicbWantedIgnore.setText("Ignore location");
jmicbWantedIgnore.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jmicbWantedIgnoreActionPerformed(evt);
}
});
jpmWanted.add(jmicbWantedIgnore);
jpmForSale.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
}
public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
jpmForSalePopupMenuWillBecomeVisible(evt);
}
});
jmiForSaleEditPriceWatch.setText("Edit price watch");
jmiForSaleEditPriceWatch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jmiForSaleEditPriceWatchActionPerformed(evt);
}
});
jpmForSale.add(jmiForSaleEditPriceWatch);
jmicbForSaleIgnore.setText("Ignore location");
jmicbForSaleIgnore.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jmicbForSaleIgnoreActionPerformed(evt);
}
});
jpmForSale.add(jmicbForSaleIgnore);
jbClearAll.setText("Clear all pricelists");
jbClearAll.setMargin(new java.awt.Insets(0, 2, 0, 2));
jbClearAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbClearAllActionPerformed(evt);
}
});
jbExportAll.setText("Export all pricelists");
jbExportAll.setMargin(new java.awt.Insets(0, 2, 0, 2));
jbExportAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbExportAllActionPerformed(evt);
}
});
jbImportAll.setText("Update all pricelists");
jbImportAll.setMargin(new java.awt.Insets(0, 2, 0, 2));
jbImportAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jbImportAllActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addComponent(jbImportAll).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE).addComponent(jbExportAll).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jbClearAll)));
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jbClearAll).addComponent(jbExportAll).addComponent(jbImportAll)));
jSplitPane1.setDividerSize(5);
jSplitPane1.setResizeWeight(0.5);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Wanted"));
jtWanted.setAutoCreateRowSorter(true);
jtWanted.setModel(new javax.swing.table.DefaultTableModel(
new Object[][]{},
new String[]{}));
jtWanted.setComponentPopupMenu(jpmWanted);
jScrollPane2.setViewportView(jtWanted);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE));
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE));
jSplitPane1.setRightComponent(jPanel2);
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("For sale"));
jtForSale.setAutoCreateRowSorter(true);
jtForSale.setComponentPopupMenu(jpmForSale);
jScrollPane1.setViewportView(jtForSale);
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE));
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE));
jSplitPane1.setLeftComponent(jPanel3);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 431, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300, Short.MAX_VALUE).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(29, 29, 29).addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE).addGap(0, 0, 0))).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(279, Short.MAX_VALUE))));
}// </editor-fold>//GEN-END:initComponents
private void jmiWantedEditPriceWatchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiWantedEditPriceWatchActionPerformed
ItemsTableModel model = (ItemsTableModel) jtWanted.getModel();
int row = jtWanted.getSelectedRow();
ItemLine i = model.getItemAtRow(jtWanted.convertRowIndexToModel(row));
ItemProperty ip = itemsProperties.get(i.getItem().getName());
if (ip == null) {
ip = new ItemProperty();
ip.setName(i.getItem().getName());
}
PriceWatchDialog pwd = new PriceWatchDialog(JOptionPane.getFrameForComponent(this), ip);
pwd.setVisible(true);
if (pwd.getItemPorperties() != null) {
itemsProperties.put(ip.getName(), ip);
}
forceTablesUpdate();
}//GEN-LAST:event_jmiWantedEditPriceWatchActionPerformed
private void jmicbWantedIgnoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmicbWantedIgnoreActionPerformed
ItemsTableModel model = (ItemsTableModel) jtWanted.getModel();
int row = jtWanted.getSelectedRow();
ItemLine i = model.getItemAtRow(jtWanted.convertRowIndexToModel(row));
LocationProperty ln = locationsProperties.get(i.getLocation().getName());
if (ln == null) {
ln = new LocationProperty();
ln.setName(i.getLocation().getName());
ln.setIgnore(false);
locationsProperties.put(ln.getName(), ln);
}
ln.setIgnore(jmicbWantedIgnore.isSelected());
forceTablesUpdate();
firePropertyChange(P_PROPERTIESCHANGED, false, true);
}//GEN-LAST:event_jmicbWantedIgnoreActionPerformed
private void jpmWantedPopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jpmWantedPopupMenuWillBecomeVisible
ItemsTableModel model = (ItemsTableModel) jtWanted.getModel();
int row = jtWanted.getSelectedRow();
if (row != -1) {
jtWanted.setRowSelectionInterval(row, row);
ItemLine i = model.getItemAtRow(jtWanted.convertRowIndexToModel(row));
LocationProperty lp = locationsProperties.get(i.getLocation().getName());
if (lp == null || !lp.isIgnore()) {
jmicbWantedIgnore.setSelected(false);
} else {
jmicbWantedIgnore.setSelected(true);
}
jmiWantedEditPriceWatch.setEnabled(true);
jmicbWantedIgnore.setEnabled(true);
} else {
jmiWantedEditPriceWatch.setEnabled(false);
jmicbWantedIgnore.setEnabled(false);
}
}//GEN-LAST:event_jpmWantedPopupMenuWillBecomeVisible
private void jmiForSaleEditPriceWatchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmiForSaleEditPriceWatchActionPerformed
ItemsTableModel model = (ItemsTableModel) jtForSale.getModel();
int row = jtForSale.getSelectedRow();
ItemLine i = model.getItemAtRow(jtForSale.convertRowIndexToModel(row));
ItemProperty ip = itemsProperties.get(i.getItem().getName());
if (ip == null) {
ip = new ItemProperty();
ip.setName(i.getItem().getName());
}
PriceWatchDialog pwd = new PriceWatchDialog(JOptionPane.getFrameForComponent(this), ip);
pwd.setVisible(true);
if (pwd.getItemPorperties() != null) {
itemsProperties.put(ip.getName(), ip);
}
forceTablesUpdate();
}//GEN-LAST:event_jmiForSaleEditPriceWatchActionPerformed
private void jmicbForSaleIgnoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmicbForSaleIgnoreActionPerformed
ItemsTableModel model = (ItemsTableModel) jtForSale.getModel();
int row = jtForSale.getSelectedRow();
ItemLine i = model.getItemAtRow(jtForSale.convertRowIndexToModel(row));
LocationProperty ln = locationsProperties.get(i.getLocation().getName());
if (ln == null) {
ln = new LocationProperty();
ln.setName(i.getLocation().getName());
ln.setIgnore(false);
locationsProperties.put(ln.getName(), ln);
}
ln.setIgnore(jmicbForSaleIgnore.isSelected());
forceTablesUpdate();
firePropertyChange(P_PROPERTIESCHANGED, false, true);
}//GEN-LAST:event_jmicbForSaleIgnoreActionPerformed
private void jpmForSalePopupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jpmForSalePopupMenuWillBecomeVisible
ItemsTableModel model = (ItemsTableModel) jtForSale.getModel();
int row = jtForSale.getSelectedRow();
if (row != -1) {
jtForSale.setRowSelectionInterval(row, row);
ItemLine i = model.getItemAtRow(jtForSale.convertRowIndexToModel(row));
LocationProperty lp = locationsProperties.get(i.getLocation().getName());
if (lp == null || !lp.isIgnore()) {
jmicbForSaleIgnore.setSelected(false);
} else {
jmicbForSaleIgnore.setSelected(true);
}
jmiForSaleEditPriceWatch.setEnabled(true);
jmicbForSaleIgnore.setEnabled(true);
} else {
jmiForSaleEditPriceWatch.setEnabled(false);
jmicbForSaleIgnore.setEnabled(false);
}
}//GEN-LAST:event_jpmForSalePopupMenuWillBecomeVisible
private void jbClearAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbClearAllActionPerformed
int resp = JOptionPane.showConfirmDialog(this, "Do you want to clear ALL pricelist data?", "Exit", JOptionPane.YES_NO_OPTION);
if (resp != JOptionPane.YES_OPTION) {
return;
}
firePropertyChange(P_PRICELISTSUPDATED, null, null);
}//GEN-LAST:event_jbClearAllActionPerformed
private void jbExportAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbExportAllActionPerformed
ExportPricelistDialog ipd = new ExportPricelistDialog(JOptionPane.getFrameForComponent(this), pricelists);
ipd.setVisible(true);
}//GEN-LAST:event_jbExportAllActionPerformed
private void jbImportAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbImportAllActionPerformed
ImportPricelistDialog ipd = new ImportPricelistDialog(JOptionPane.getFrameForComponent(this));
ipd.setVisible(true);
String text = ipd.getImportedText();
if (text == null) {
return;
}
Pricelist old[] = pricelists;
Pricelist newpl[];
try {
if (text.toLowerCase().contains("<html>")) {
newpl = PricelistsTools.importAllPricelistFromSource(text);
} else {
newpl = PricelistsTools.importAllPricelist(text);
}
} catch (Exception ex) {
LogX.log(Level.SEVERE, "Error importing the pricelist.", ex, true);
LogX.log(Level.SEVERE, "[[[ " + text + " ]]]");
return;
}
if ((newpl == null) || (newpl[0] == null) || (newpl[1] == null)) {
LogX.log(Level.SEVERE, "newpl is either null or has null elements");
return;
}
firePropertyChange(P_PRICELISTSUPDATED, old, newpl);
}//GEN-LAST:event_jbImportAllActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JButton jbClearAll;
private javax.swing.JButton jbExportAll;
private javax.swing.JButton jbImportAll;
private javax.swing.JMenuItem jmiForSaleEditPriceWatch;
private javax.swing.JMenuItem jmiWantedEditPriceWatch;
private javax.swing.JCheckBoxMenuItem jmicbForSaleIgnore;
private javax.swing.JCheckBoxMenuItem jmicbWantedIgnore;
private javax.swing.JPopupMenu jpmForSale;
private javax.swing.JPopupMenu jpmWanted;
private javax.swing.JTable jtForSale;
private javax.swing.JTable jtWanted;
// End of variables declaration//GEN-END:variables
}