summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/gui/tabs/JobListPane.java
blob: 4893ae54e529ed4cd28ef1ba3715895cfeddb3ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * 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.agent.Job;
import com.c2kernel.entity.agent.JobList;
import com.c2kernel.entity.proxy.ProxyObserver;
import com.c2kernel.entity.proxy.MemberSubscription;
import com.c2kernel.gui.MainFrame;
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 ItemTabPane implements ActionListener, ProxyObserver<Job> {

    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());
    }

    @Override
	public void reload() {
        joblist.clear();
        jumpToEnd();
    }

    @Override
	public void run() {
        Thread.currentThread().setName("Joblist Pane Builder");
		try {
            joblist = (JobList)sourceItem.getItem().getObject(ClusterStorage.JOB);
            sourceItem.getItem().subscribe(new MemberSubscription<Job>(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);
    }

    @Override
	public void add(Job contents) {
        reload();
    }

    @Override
	public void remove(String id) {
        reload();
    }

	@Override
	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[idx] = joblist.getJob(thisJobId.intValue());
                    itemNames[idx] = "Item Not Found";
                    try {
                        itemNames[idx] = ((Property)Gateway.getStorage().get(job[count-startId].getItemPath(), 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)
		 */
		@Override
		public Class<?> getColumnClass(int columnIndex) {
			switch(columnIndex) {
				case 0:
					return Integer.class;
				default:
					return String.class;
			}
		}

		/**
		 * @see javax.swing.table.TableModel#getColumnCount()
		 */
		@Override
		public int getColumnCount() {
			return 4;
		}

		/**
		 * @see javax.swing.table.TableModel#getColumnName(int)
		 */
		@Override
		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()
		 */
		@Override
		public int getRowCount() {
			return loaded;
		}

		/**
		 * @see javax.swing.table.TableModel#getValueAt(int, int)
		 */
		@Override
		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 job[rowIndex].getTransition().getName();
					default: return "";
				}
			} catch (Exception e) {
				return null;
			}
		}

		/**
		 * @see javax.swing.table.TableModel#isCellEditable(int, int)
		 */
		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
		}

        public Job getJobAtRow(int rowIndex) {
            return job[rowIndex];
        }

	}

    private class JobListMouseListener extends MouseAdapter {

        @Override
		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);
                }
            }
        }
    }

	@Override
	public void control(String control, String msg) {
		// TODO Auto-generated method stub
		
	}
}