1 package com.melloware.jukes.gui.view.component;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.MouseAdapter;
7 import java.awt.event.MouseEvent;
8 import java.io.File;
9 import java.io.FileWriter;
10 import java.io.IOException;
11 import java.util.Enumeration;
12
13 import javax.swing.AbstractAction;
14 import javax.swing.Icon;
15 import javax.swing.JFileChooser;
16 import javax.swing.JMenuItem;
17 import javax.swing.JPopupMenu;
18 import javax.swing.JTable;
19 import javax.swing.ListSelectionModel;
20 import javax.swing.table.TableCellRenderer;
21 import javax.swing.table.TableColumn;
22 import javax.swing.table.TableColumnModel;
23 import javax.swing.table.TableModel;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import com.jgoodies.uif.util.ResourceUtils;
29 import com.melloware.jukes.exception.InfrastructureException;
30 import com.melloware.jukes.file.filter.FilterFactory;
31 import com.melloware.jukes.gui.tool.Resources;
32 import com.melloware.jukes.util.MessageUtil;
33
34
35
36
37
38
39
40
41
42 public final class ExportableTable
43 extends JTable {
44
45 private static final Log LOG = LogFactory.getLog(ExportableTable.class);
46 private static Icon csvIcon = Resources.CSV_ICON;
47 private static String csvText = "Export To CSV";
48 private boolean popupEnabled = true;
49
50
51 private Color evenColor = Color.WHITE;
52 private Color oddColor = new Color(0xee, 0xee, 0xff);
53 private JPopupMenu popupMenu = null;
54
55 public ExportableTable() {
56 super();
57 constructor();
58 }
59
60 public ExportableTable(TableModel dm) {
61 super(dm);
62 constructor();
63 }
64
65 public ExportableTable(TableModel dm, TableColumnModel cm) {
66 super(dm, cm);
67 constructor();
68 }
69
70 public ExportableTable(int numRows, int numColumns) {
71 super(numRows, numColumns);
72 constructor();
73 }
74
75
76 public ExportableTable(Object[][] rowData, Object[] columnNames) {
77 super(rowData, columnNames);
78 constructor();
79 }
80
81 public ExportableTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
82 super(dm, cm, sm);
83 constructor();
84 }
85
86
87
88
89
90
91 public Color getEvenColor() {
92 return this.evenColor;
93 }
94
95
96
97
98
99
100 public Color getOddColor() {
101 return this.oddColor;
102 }
103
104
105
106
107
108
109 public void setEvenColor(Color aEvenColor) {
110 this.evenColor = aEvenColor;
111 }
112
113
114
115
116
117
118 public void setOddColor(Color aOddColor) {
119 this.oddColor = aOddColor;
120 }
121
122
123
124
125
126 public void addPopupMenu(JPopupMenu popup) {
127 this.popupMenu.addSeparator();
128 Component[] items = popup.getComponents();
129 for (int i = 0; i < items.length; i++) {
130 this.popupMenu.add(items[i]);
131 }
132 }
133
134 public void enablePopupMenu(boolean enabled) {
135 popupEnabled = enabled;
136 }
137
138
139
140
141 public Component prepareRenderer(TableCellRenderer aRenderer, int aRow, int aColumn) {
142 final Component component = super.prepareRenderer(aRenderer, aRow, aColumn);
143 if (!isRowSelected(aRow)) {
144 component.setBackground(((aRow % 2) == 0) ? getEvenColor() : getOddColor());
145 }
146 return component;
147 }
148
149 public void stopEditing() {
150 if (cellEditor != null) {
151 cellEditor.stopCellEditing();
152 }
153 }
154
155
156
157
158
159
160
161
162
163 protected void storeTableAsCSV(boolean isSelected) {
164 if (isSelected && (this.getSelectedRowCount() == 0)) {
165 return;
166 }
167
168 final StringBuffer sb = new StringBuffer("\"");
169 final TableColumnModel cm = this.getColumnModel();
170 final Enumeration enumeration = cm.getColumns();
171 while (enumeration.hasMoreElements()) {
172 final TableColumn tc = (TableColumn)enumeration.nextElement();
173 sb.append(tc.getHeaderValue().toString());
174 if (enumeration.hasMoreElements()) {
175 sb.append("\",\"");
176 } else {
177 sb.append("\"\n");
178 }
179 }
180
181 final int rowCount = this.getRowCount();
182 final int colCount = this.getColumnCount();
183 for (int i = 0; i < rowCount; i++) {
184 sb.append('"');
185 for (int j = 0; j < colCount; j++) {
186 if (!isSelected || this.isCellSelected(i, j)) {
187 if (this.getValueAt(i, j) == null) {
188 sb.append("");
189 } else {
190 String value = this.getValueAt(i, j).toString().replace(',', ' ');
191 sb.append(value);
192 }
193 if (j == (colCount - 1)) {
194 sb.append("\"\n");
195 } else {
196 sb.append("\",\"");
197 }
198 }
199 }
200 }
201
202 final JFileChooser chooser = new JFileChooser();
203 chooser.setDialogTitle("Export Results");
204 chooser.setFileFilter(FilterFactory.csvFileFilter());
205 chooser.setMultiSelectionEnabled(false);
206 chooser.setFileHidingEnabled(true);
207 final int returnVal = chooser.showSaveDialog(this);
208 if (returnVal != JFileChooser.APPROVE_OPTION) {
209 return;
210 }
211 File file = chooser.getSelectedFile();
212 if (LOG.isDebugEnabled()) {
213 LOG.debug("Absolute: " + file.getAbsolutePath());
214 }
215
216
217 file = FilterFactory.forceCsvExtension(file);
218
219 try {
220 final FileWriter writer = new FileWriter(file);
221 writer.write(sb.toString());
222 writer.close();
223 final String infoMessage = ResourceUtils.getString("messages.TableExportedSuccessfully");
224 MessageUtil.showInformation(this, infoMessage);
225 } catch (IOException ex) {
226 final String errorMessage = ResourceUtils.getString("label.Errorwritingfile");
227 MessageUtil.showError(this, errorMessage);
228 LOG.error(errorMessage + "\n\n" + ex.getMessage(), ex);
229
230 } catch (InfrastructureException ex) {
231 final String errorMessage = ResourceUtils.getString("label.Errorwritingfile");
232 MessageUtil.showError(this, errorMessage);
233 LOG.error(errorMessage + "\n\n" + ex.getMessage(), ex);
234 } catch (Exception ex) {
235 final String errorMessage = ResourceUtils.getString("label.Errorwritingfile");
236 MessageUtil.showError(this, errorMessage);
237 LOG.error(errorMessage, ex);
238 }
239 }
240
241 private JPopupMenu buildPopupMenu() {
242 final JPopupMenu pop = new JPopupMenu("Menu");
243 final AbstractAction storeAsCSVAction = new AbstractAction(csvText, csvIcon) {
244 public void actionPerformed(ActionEvent event) {
245 storeTableAsCSV(false);
246 }
247 };
248 final JMenuItem item = new JMenuItem(storeAsCSVAction);
249 pop.add(item);
250 pop.setLabel("");
251 this.addMouseListener(new MousePopupListener());
252 return pop;
253 }
254
255 private void constructor() {
256 popupMenu = buildPopupMenu();
257 }
258
259 private class MousePopupListener
260 extends MouseAdapter {
261
262 public void mouseClicked(MouseEvent e) {
263 checkPopup(e);
264 }
265
266 public void mousePressed(MouseEvent e) {
267 checkPopup(e);
268 }
269
270 public void mouseReleased(MouseEvent e) {
271 checkPopup(e);
272 }
273
274 private void checkPopup(MouseEvent e) {
275 if ((e.isPopupTrigger()) && (popupEnabled)) {
276 popupMenu.show(ExportableTable.this, e.getX(), e.getY());
277 }
278 }
279 }
280 }