/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * PUnitList.java * * Created on 8/10/2009, 2:40:30 AM */ package org.petah.spring.bai.gui; import org.petah.spring.bai.gui.model.UnitListModel; import java.awt.Font; import java.awt.event.MouseEvent; import java.lang.reflect.Field; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.SwingUtilities; import org.petah.common.option.Option; import org.petah.common.option.OptionsManager; import org.petah.spring.bai.ThreadManager; import org.petah.spring.bai.cache.CachedUnit; import org.petah.spring.bai.delegate.TeamDelegate; import org.petah.spring.bai.util.FormatUtil; /** * * @author Petah */ public class PUnitList extends javax.swing.JPanel { // Options private static Option<Font> font = OptionsManager.getOption( new Option<Font>("PUnitList.font", new Font("Courier New", Font.PLAIN, 10))); // Class properties private TeamDelegate teamDelegate; private String info; private CachedUnit unit; /** Creates new form PUnitList */ public PUnitList(TeamDelegate teamDelegate) { this.teamDelegate = teamDelegate; initComponents(); } private void refresh() { SwingUtilities.invokeLater(new Runnable() { public void run() { updateInfo(); int scrollBarPos = spInfo.getVerticalScrollBar().getValue(); tpInfo.setText(info); spInfo.getVerticalScrollBar().setValue(scrollBarPos); tpInfo.updateUI(); lFriendlyUnits.updateUI(); lEnemyUnits.updateUI(); } }); } private void startAutoUpdate() { ThreadManager.run("PUnitList.startAutoUpdate()", new Runnable() { public void run() { try { while (cbAutoRefresh.isSelected()) { refresh(); Thread.sleep(1000); } } catch (InterruptedException ex) { } } }); } private void listFields(Object o) { Class c = o.getClass(); Field fieldlist[] = c.getDeclaredFields(); for (int i = 0; i < fieldlist.length; i++) { try { // Get the reflected object Field field = fieldlist[i]; // Set accessible true field.setAccessible(true); info += FormatUtil.addWhiteSpace(field.getName(), 30) + " = " + FormatUtil.addWhiteSpace(field.get(o), 10) + " (" + field.getType().getSimpleName() + ")\n"; } catch (IllegalArgumentException ex) { Logger.getLogger(PUnitList.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(PUnitList.class.getName()).log(Level.SEVERE, null, ex); } } } private void updateInfo() { // System.err.println(unit); if (unit != null) { info = "Unit properties:\n"; listFields(unit); info += "\nUnitDef properties:\n"; listFields(unit.getDef()); } else { info = "No unit selected."; } } /** 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; toolBar = new javax.swing.JToolBar(); cbAutoRefresh = new javax.swing.JCheckBox(); bRefresh = new javax.swing.JButton(); spHorizontal = new javax.swing.JSplitPane(); spInfo = new javax.swing.JScrollPane(); tpInfo = new javax.swing.JTextPane(); spVerticle = new javax.swing.JSplitPane(); spFriendlyUnits = new javax.swing.JScrollPane(); lFriendlyUnits = new javax.swing.JList(); spEnemyUnits = new javax.swing.JScrollPane(); lEnemyUnits = new javax.swing.JList(); setLayout(new java.awt.GridBagLayout()); toolBar.setFloatable(false); toolBar.setRollover(true); cbAutoRefresh.setText("Auto Refresh"); cbAutoRefresh.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cbAutoRefreshActionPerformed(evt); } }); toolBar.add(cbAutoRefresh); bRefresh.setText("Refresh"); bRefresh.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bRefreshActionPerformed(evt); } }); toolBar.add(bRefresh); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; add(toolBar, gridBagConstraints); tpInfo.setEditable(false); tpInfo.setFont(font.getValue()); tpInfo.setText("No unit selected."); spInfo.setViewportView(tpInfo); spHorizontal.setRightComponent(spInfo); spVerticle.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT); lFriendlyUnits.setModel(new UnitListModel(teamDelegate.getFriendlyUnits())); lFriendlyUnits.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { lFriendlyUnitsMouseClicked(evt); } }); spFriendlyUnits.setViewportView(lFriendlyUnits); spVerticle.setTopComponent(spFriendlyUnits); lEnemyUnits.setModel(new UnitListModel(teamDelegate.getEnemyUnits())); lEnemyUnits.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { lEnemyUnitsMouseClicked(evt); } }); spEnemyUnits.setViewportView(lEnemyUnits); spVerticle.setRightComponent(spEnemyUnits); spHorizontal.setLeftComponent(spVerticle); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; add(spHorizontal, gridBagConstraints); }// </editor-fold>//GEN-END:initComponents private void bRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRefreshActionPerformed refresh(); }//GEN-LAST:event_bRefreshActionPerformed private void cbAutoRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbAutoRefreshActionPerformed if (cbAutoRefresh.isSelected()) { startAutoUpdate(); } }//GEN-LAST:event_cbAutoRefreshActionPerformed private void lFriendlyUnitsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lFriendlyUnitsMouseClicked if (evt.getButton() == MouseEvent.BUTTON1) { unit = ((UnitListModel) lFriendlyUnits.getModel()).getUnit(lFriendlyUnits.getSelectedIndex()); lEnemyUnits.clearSelection(); } }//GEN-LAST:event_lFriendlyUnitsMouseClicked private void lEnemyUnitsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lEnemyUnitsMouseClicked if (evt.getButton() == MouseEvent.BUTTON1) { unit = ((UnitListModel) lEnemyUnits.getModel()).getUnit(lEnemyUnits.getSelectedIndex()); lFriendlyUnits.clearSelection(); } }//GEN-LAST:event_lEnemyUnitsMouseClicked // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton bRefresh; private javax.swing.JCheckBox cbAutoRefresh; private javax.swing.JList lEnemyUnits; private javax.swing.JList lFriendlyUnits; private javax.swing.JScrollPane spEnemyUnits; private javax.swing.JScrollPane spFriendlyUnits; private javax.swing.JSplitPane spHorizontal; private javax.swing.JScrollPane spInfo; private javax.swing.JSplitPane spVerticle; private javax.swing.JToolBar toolBar; private javax.swing.JTextPane tpInfo; // End of variables declaration//GEN-END:variables }