/* * StatusPane.java * * Created on March 20, 2001, 3:30 PM */ package com.c2kernel.gui.tabs; /** * @author abranson * @version */ import java.awt.GridBagConstraints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.Iterator; import javax.swing.Box; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import com.c2kernel.common.ObjectNotFoundException; import com.c2kernel.entity.C2KLocalObject; import com.c2kernel.entity.agent.Job; import com.c2kernel.entity.agent.JobList; import com.c2kernel.entity.proxy.EntityProxyObserver; import com.c2kernel.gui.MainFrame; import com.c2kernel.lifecycle.instance.stateMachine.Transitions; import com.c2kernel.persistency.ClusterStorage; import com.c2kernel.process.Gateway; import com.c2kernel.property.Property; import com.c2kernel.utils.Language; import com.c2kernel.utils.Logger; /** * Pane to display all work orders that this agent can execute, and activate * them on request from the user. Subscribes to NodeItem for WorkOrder objects. * @version $Revision: 1.4 $ $Date: 2004/10/21 08:02:21 $ * @author $Author: abranson $ */ public class JobListPane extends EntityTabPane implements ActionListener, EntityProxyObserver { JobList joblist; JoblistTableModel model; JTable eventTable; JButton startButton = new JButton("<<"); JButton prevButton = new JButton("<"); JButton nextButton = new JButton(">"); JButton endButton = new JButton(">>"); public static final int SIZE = 30; int currentSize = SIZE; public JobListPane() { super("Job List", "Agent Job List"); initPanel(); // add buttons Box navBox = Box.createHorizontalBox(); navBox.add(startButton); navBox.add(prevButton); navBox.add(nextButton); navBox.add(endButton); // setup buttons //startButton.setEnabled(false); nextButton.setEnabled(false); //prevButton.setEnabled(false); endButton.setEnabled(false); startButton.setActionCommand("start"); startButton.addActionListener(this); prevButton.setActionCommand("prev"); prevButton.addActionListener(this); nextButton.setActionCommand("next"); nextButton.addActionListener(this); endButton.setActionCommand("end"); endButton.addActionListener(this); getGridBagConstraints(); c.gridx = 0; c.gridy = 1; c.anchor = GridBagConstraints.NORTHWEST; c.fill=GridBagConstraints.NONE; c.weightx=0; c.weighty=0; gridbag.setConstraints(navBox, c); add(navBox); // Create table eventTable = new JTable(); JScrollPane eventScroll= new JScrollPane(eventTable); c.weightx = 1.0; c.weighty = 1.0; c.fill = GridBagConstraints.BOTH; c.gridy++; gridbag.setConstraints(eventScroll, c); add(eventScroll); // detect double clicked jobs eventTable.addMouseListener(new JobListMouseListener()); } public void reload() { joblist.clear(); jumpToEnd(); } public void run() { Thread.currentThread().setName("Joblist Pane Builder"); try { joblist = (JobList)sourceEntity.getEntity().getObject(ClusterStorage.JOB); sourceEntity.getEntity().subscribe(this, ClusterStorage.JOB, false); } catch (ObjectNotFoundException e) { Logger.error(e); } model = new JoblistTableModel(joblist); eventTable.setModel(model); jumpToEnd(); } public void jumpToEnd() { int lastEvent = joblist.getLastId(); int firstEvent = 0; currentSize = SIZE; if (lastEvent > currentSize) firstEvent = lastEvent - currentSize + 1; if (lastEvent < currentSize) currentSize = lastEvent + 1; Logger.msg(5, "JobListPane.run() - init table start "+firstEvent+" for "+currentSize); model.setView(firstEvent, currentSize); } public void add(C2KLocalObject contents) { reload(); } public void remove(String id) { reload(); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("end")) { jumpToEnd(); return; } int lastEvent = joblist.getLastId(); int startEvent = model.getStartId(); if (e.getActionCommand().equals("start")) { currentSize = SIZE; startEvent = 0; } else if (e.getActionCommand().equals("prev")) { currentSize = SIZE; startEvent-=currentSize; if (startEvent<0) startEvent = 0; } else if (e.getActionCommand().equals("next")) { currentSize = SIZE; startEvent+=currentSize; if (startEvent > lastEvent) startEvent = lastEvent - currentSize +1; } else { // unknown action return; } model.setView(startEvent, currentSize); } private class JoblistTableModel extends AbstractTableModel { Job[] job; Integer[] ids; String[] itemNames; int loaded = 0; int startId = 0; public JoblistTableModel(JobList joblist) { job = new Job[0]; ids = new Integer[0]; } public int getStartId() { return startId; } public void setView(int startId, int size) { job = new Job[size]; ids = new Integer[size]; itemNames = new String[size]; this.startId = startId; int count = 0; for (Iterator i = joblist.keySet().iterator(); i.hasNext();) { Integer thisJobId = new Integer((String)i.next()); if (count >= startId) { int idx = count-startId; ids[idx] = thisJobId; Job thisJob = joblist.getJob(thisJobId.intValue()); job[idx] = joblist.getJob(thisJobId.intValue()); itemNames[idx] = "Item Not Found"; try { itemNames[idx] = ((Property)Gateway.getStorage().get(job[count-startId].getItemSysKey(), ClusterStorage.PROPERTY+"/Name", null)).getValue(); } catch (Exception ex) { Logger.error(ex); } } count++; loaded = count-startId; if (count > (startId + size)) break; } fireTableStructureChanged(); } /** * @see javax.swing.table.TableModel#getColumnClass(int) */ public Class getColumnClass(int columnIndex) { switch(columnIndex) { case 0: return Integer.class; default: return String.class; } } /** * @see javax.swing.table.TableModel#getColumnCount() */ public int getColumnCount() { return 4; } /** * @see javax.swing.table.TableModel#getColumnName(int) */ public String getColumnName(int columnIndex) { switch(columnIndex) { case 0: return Language.translate("ID"); case 1: return Language.translate("Subject"); case 2: return Language.translate("Activity"); case 3: return Language.translate("Transition"); default: return ""; } } /** * @see javax.swing.table.TableModel#getRowCount() */ public int getRowCount() { return loaded; } /** * @see javax.swing.table.TableModel#getValueAt(int, int) */ public Object getValueAt(int rowIndex, int columnIndex) { if (job.length <= rowIndex || job[rowIndex] == null) return ""; try { switch (columnIndex) { case 0: return ids[rowIndex]; case 1: return itemNames[rowIndex]; case 2: return job[rowIndex].getStepName(); case 3: return Transitions.getTransitionName(job[rowIndex].getPossibleTransition()); default: return ""; } } catch (Exception e) { return null; } } /** * @see javax.swing.table.TableModel#isCellEditable(int, int) */ public boolean isCellEditable(int rowIndex, int columnIndex) { return false; } public Job getJobAtRow(int rowIndex) { return job[rowIndex]; } } private class JobListMouseListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { super.mouseClicked(e); if (e.getClickCount() == 2) { Job selectedJob = model.getJobAtRow(eventTable.getSelectedRow()); try { MainFrame.itemFinder.pushNewKey(selectedJob.getItemProxy().getName()); } catch (Exception ex) { Logger.error(ex); JOptionPane.showMessageDialog(null, "No Item Found", "Job references an unknown item", JOptionPane.ERROR_MESSAGE); } } } } }