1   package net.sf.jhylafax;
2   
3   import static net.sf.jhylafax.JHylaFAX.i18n;
4   import java.util.ArrayList;
5   import java.util.Date;
6   import java.util.List;
7   import javax.swing.table.AbstractTableModel;
8   import javax.swing.table.TableModel;
9   import net.sf.jhylafax.JobHelper.FileStat;
10  import net.sf.jhylafax.fax.ReceivedFax;
11  import org.xnap.commons.gui.Builder;
12  import org.xnap.commons.gui.util.DoubleClickListener;
13  
14  public class ReceiveQueuePanel extends AbstractQueuePanel {
15  
16  	private ViewAction viewAction;
17  	private SaveAction saveAction;
18  	private DeleteAction deleteAction;
19  	private FaxTableModel faxTableModel;
20  
21  	public ReceiveQueuePanel(String queueName) {
22  		super(queueName);
23  		
24  		viewAction = new ViewAction();
25  		saveAction = new SaveAction();
26  		deleteAction = new DeleteAction();
27  
28  		getButtonPanel().add(Builder.createButton(viewAction));
29  		getButtonPanel().add(Builder.createButton(saveAction));
30  		getButtonPanel().add(Builder.createButton(deleteAction));
31  		
32  		getTablePopupMenu().add(Builder.createMenuItem(viewAction));
33  		getTablePopupMenu().add(Builder.createMenuItem(saveAction));
34  		getTablePopupMenu().add(Builder.createMenuItem(deleteAction));
35  		
36  		getTable().addMouseListener(new DoubleClickListener(viewAction));
37  		
38  		updateLabels();
39  		updateActions();
40  	}
41  	
42  	@Override
43  	public FileStat getSelectedFile()
44  	{
45  		int row = getSelectedRow();
46  		if (row == -1) {
47  			return null;
48  		}
49  		else {
50  			ReceivedFax doc = faxTableModel.getDocument(row);
51  			return new FileStat(doc.getFilename(), doc.getFilesize());
52  		}
53  	}
54  
55  	@Override
56  	protected TableModel getTableModel()
57  	{
58  		if (faxTableModel == null) {
59  			faxTableModel = new FaxTableModel();
60  		}
61  		return faxTableModel;
62  	}
63  	
64  	@Override
65  	protected void initializeTableLayout() {
66  		getTableLayout().setColumnProperties(0, "sender", 150);
67  		getTableLayout().setColumnProperties(1, "pages", 40);
68  		getTableLayout().setColumnProperties(2, "time", 40);
69  		getTableLayout().setColumnProperties(3, "filename", 100);
70  		getTableLayout().setColumnProperties(4, "filesize", 40);
71  		getTableLayout().setColumnProperties(5, "owner", 40);
72  		getTableLayout().setColumnProperties(6, "resolution", 40);
73  		getTableLayout().setColumnProperties(7, "singallingRate", 40);
74  		getTableLayout().setColumnProperties(8, "receiving", 40);
75  		getTableLayout().setColumnProperties(9, "error", 100);
76  	}
77  
78  	public void setData(List<ReceivedFax> data)
79  	{
80  		faxTableModel.setData(data);
81  	}
82  
83  	@Override
84  	public void updateActions() {
85  		FileStat file = getSelectedFile();
86  		viewAction.setEnabled(file != null);
87  		saveAction.setEnabled(file != null);
88  		deleteAction.setEnabled(file != null);
89  	}
90  
91  	@Override
92  	public void updateLabels() {
93  		super.updateLabels();
94  		
95  		viewAction.updateLabels();
96  		saveAction.updateLabels();
97  		deleteAction.updateLabels();
98  		
99  		getTableLayout().setColumnNames(new String[] {
100 				i18n.tr("Sender"),
101 				i18n.tr("Pages"), 
102 				i18n.tr("Time"),
103 				i18n.tr("Filename"),
104 				i18n.tr("Filesize"),
105 				i18n.tr("Owner"),
106 				i18n.tr("Resolution"),
107 				i18n.tr("Signalling Rate"),
108 				i18n.tr("Receiving"),
109 				i18n.tr("Error"),});
110 	}
111 
112 	private static class FaxTableModel extends AbstractTableModel {
113 	
114 		private static final Class[] columnClasses= {
115 			String.class, 
116 			Integer.class,
117 			Date.class,
118 			String.class,
119 			Long.class,
120 			String.class,
121 			Integer.class,
122 			Integer.class,
123 			Boolean.class,
124 			String.class,
125 		};
126 		
127 		private List<ReceivedFax> data = new ArrayList<ReceivedFax>(0);
128 		
129 		public FaxTableModel() {
130 		}
131 		
132 		public Class<?> getColumnClass(int column) {
133 	        return columnClasses[column];
134 	    }
135 	
136 		public int getColumnCount() {
137 			return columnClasses.length;
138 		}
139 	
140 		public ReceivedFax getDocument(int row) {
141 			return data.get(row);
142 		}
143 	
144 		public int getRowCount()
145 		{
146 			return data.size();
147 		}
148 		
149 	    public Object getValueAt(int row, int column)
150 		{
151 			ReceivedFax fax = data.get(row);
152 			switch (column) {
153 			case 0:
154 				return fax.getSender();
155 			case 1:
156 				return fax.getPageCount();
157 			case 2:
158 				return fax.getReceivedTime();
159 			case 3:
160 				return fax.getFilename();
161 			case 4:
162 				return fax.getFilesize();
163 			case 5:
164 				return fax.getOwner();
165 			case 6:
166 				return fax.getResolution();
167 			case 7:
168 				return fax.getSignallingRate();
169 			case 8:
170 				return fax.isReceiving();
171 			case 9:
172 				return fax.getLastError();
173 			default:
174 				return null;
175 			}
176 		}
177 		
178 		public void setData(List<ReceivedFax> data)
179 		{
180 			this.data = data;
181 			fireTableDataChanged();
182 		}
183 	
184 	}
185 
186 }