forked from ISA-tools/ISAcreatorPlugins
/
DataEntrySheet.java
273 lines (210 loc) · 8.46 KB
/
DataEntrySheet.java
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
package org.isatools.plugins.metabolights.assignments.ui;
import org.apache.log4j.Logger;
import org.isatools.isacreator.common.UIHelper;
import org.isatools.isacreator.spreadsheet.Spreadsheet;
import org.isatools.isacreator.spreadsheet.TableReferenceObject;
import org.isatools.plugins.metabolights.assignments.IsaCreatorInfo;
import org.isatools.plugins.metabolights.assignments.io.FileLoader;
import org.isatools.plugins.metabolights.assignments.io.FileWriter;
import org.jdesktop.fuse.InjectedResource;
import org.jdesktop.fuse.ResourceInjector;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Created by the ISA team
*
* @author Eamonn Maguire (eamonnmag@gmail.com)
* <p/>
* Date: 03/10/2011
* Time: 16:24
*/
public class DataEntrySheet extends JPanel {
private static final long serialVersionUID = -7317091603657433515L;
private static Logger logger = Logger.getLogger(DataEntrySheet.class);
private Spreadsheet sheet;
private EditorUI parentFrame;
private TableReferenceObject tableReferenceObject;
private String fileName;
private IsaCreatorInfo isaCreatorInfo;
private IsaCreatorInfo getIsaCreatorInfo() {
if (isaCreatorInfo == null)
isaCreatorInfo = new IsaCreatorInfo();
return isaCreatorInfo;
}
@InjectedResource
private ImageIcon saveIcon, saveIconOver, loadIcon, loadIconOver, okIcon, okIconOver;
public DataEntrySheet(EditorUI parentFrame, TableReferenceObject tableReferenceObject) {
ResourceInjector.get("metabolights-fileeditor-package.style").inject(this);
this.parentFrame = parentFrame;
this.tableReferenceObject = tableReferenceObject;
setLayout(new BorderLayout());
setBackground(UIHelper.BG_COLOR);
}
public void createGUI() {
sheet = new Spreadsheet(parentFrame, getIsaCreatorInfo().addTableRefSampleColumns(tableReferenceObject), ""); // Add the sample columns to the definition
createTopPanel();
add(getIsaCreatorInfo().addSpreadsheetSampleColumns(sheet), BorderLayout.CENTER); // Add the sample columns to the spreadsheet
createBottomPanel();
}
public void createBottomPanel(){
JPanel bottomPannel = new JPanel(new BorderLayout());
bottomPannel.setBackground(UIHelper.BG_COLOR);
Box buttonContainer = Box.createHorizontalBox();
buttonContainer.setBackground(UIHelper.BG_COLOR);
JLabel file = new JLabel();
file.setBackground(UIHelper.BG_COLOR);
// Not available in the first load (without ISACreator)
if (!parentFrame.getAmIAlone()){
file.setText(getFileName());
}
bottomPannel.add(file);
final JLabel okButton = new JLabel(okIcon);
okButton.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
saveFile();
parentFrame.setCurrentCellValue(fileName);
parentFrame.confirm();
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
okButton.setIcon(okIcon);
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
okButton.setIcon(okIconOver);
}
});
buttonContainer.add(okButton);
bottomPannel.add(buttonContainer, BorderLayout.EAST);
add(bottomPannel, BorderLayout.SOUTH);
}
public void createTopPanel() {
JPanel topContainer = new JPanel(new BorderLayout());
topContainer.setBackground(UIHelper.BG_COLOR);
Box buttonContainer = Box.createHorizontalBox();
buttonContainer.setBackground(UIHelper.BG_COLOR);
final JLabel loadButton = new JLabel(loadIcon);
loadButton.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
loadButton.setIcon(loadIcon);
loadFile();
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
loadButton.setIcon(loadIcon);
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
loadButton.setIcon(loadIconOver);
}
});
final JLabel saveButton = new JLabel(saveIcon);
saveButton.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent mouseEvent) {
saveButton.setIcon(saveIcon);
saveFile();
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
saveButton.setIcon(saveIcon);
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
saveButton.setIcon(saveIconOver);
}
});
buttonContainer.add(saveButton);
buttonContainer.add(Box.createHorizontalStrut(5));
buttonContainer.add(loadButton);
topContainer.add(buttonContainer, BorderLayout.EAST);
add(topContainer, BorderLayout.NORTH);
}
private String getFileName(){
// if we do not have the property already set
if (fileName == null){
calculateFileName();
}
return fileName;
}
@SuppressWarnings("static-access")
private void calculateFileName(){
// Check if the current cell has any value
if (parentFrame.getCurrentCellValue() == null){
File file = new File(".");
// Get the assay name
String assayName = getIsaCreatorInfo().getCurrentAssay().getIdentifier();
// Remove the extension
assayName = assayName.substring(0, assayName.length()-4);
// Add a asigmentfile sufix
assayName = assayName + "_maf.csv";
// Get the path
String path ="";
try {
path = file.getCanonicalPath();
} catch (IOException e) {
// Do not use the canonical (this will have a /. at the end)
path = getIsaCreatorInfo().getFileLocation(); //file.getAbsolutePath();
}
// Compose the final file name
fileName = path + file.separator + assayName;
} else {
fileName = parentFrame.getCurrentCellValue();
}
}
private void saveFile(){
logger.info("Saving the file");
FileWriter fw = new FileWriter();
try {
fw.writeFile(getFileName(), sheet);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void loadFile(){
logger.info("Loading file");
loadFile(getIsaCreatorInfo().addTableRefSampleColumns(tableReferenceObject)); //Make sure all sample columns are present in the defintion
}
public void loadFile(TableReferenceObject tableReferenceObject){
String fn = getFileName();
File file = new File(fn);
// If the file exists...
if (file.exists()){
FileLoader fl = new FileLoader();
logger.info("Trying to load the metabolite assignment file: " + fn);
tableReferenceObject = fl.loadFile(getFileName(), tableReferenceObject);
Spreadsheet loadedSheet = new Spreadsheet(parentFrame,tableReferenceObject,""); //To map the columns that we load from the file
updateSpreadsheet(getIsaCreatorInfo().addSpreadsheetSampleColumns(loadedSheet)); // Load the existing spreadsheet and add any new sample columns
}
}
private void updateSpreadsheet(Spreadsheet newSpreadsheet){
logger.info("Removing existing spreadsheet");
// If we have a previous sheet
if (sheet != null){
remove(sheet);
}
logger.info("Adding the new sheet");
sheet = newSpreadsheet;
add(getIsaCreatorInfo().addSpreadsheetSampleColumns(sheet),BorderLayout.CENTER); //Add all missing sample columns to the spreadsheet
validate();
}
// http://www.javamex.com/tutorials/threads/invokelater.shtml
// http://java.sun.com/products/jfc/tsc/articles/painting/#smart
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// remove(sheet);
//
// add(newSpreadsheet, BorderLayout.SOUTH);
//
// repaint();
// }
// });
}