/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.simbuck.plugins.simbuckflash;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.*;
import javax.swing.border.Border;
import org.deben.Utils.FileManager;
import org.simbuck.GUI.SimbuckGUI;
import org.simbuck.application.core.SimbuckUSBComms;
import org.simbuck.application.core.SimbuckTableModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author RJONES65
*/
public class SimbuckFlashTab extends SimbuckGUI {
private Map<String, String> directoriesMap = new TreeMap<String, String>();
/**
*
*/
protected String directory;
/**
*
*/
protected File[] hexfiles;
String[] ext = {"hex", "xml", "xsl"};
String type = "hex";
JPanel filesPanel = new JPanel();
JComboBox selHEX = new JComboBox();
JPanel TestingPanel;
JPanel SimbuckDataPanel;
JScrollPane SimbuckDataPane;
/**
* The TextArea to present the file data
*
*/
protected JPanel fileDisplayPane;
/**
*
*/
protected JTextArea textArea;
private JPanel WidgetPanel;
private JPanel ButtonPanel;
private JPanel StatusPanel;
private JButton Boot;
private JButton Read;
private JButton Erase;
private JButton Write;
private JButton Verify;
private JButton Reset;
private JButton BootDone;
private JButton ReadDone;
private JButton EraseDone;
private JButton WriteDone;
private JButton VerifyDone;
private JButton ResetDone;
/**
*
*/
protected JScrollPane OutputPanel;
/**
*
*/
protected JTextArea simbuckconsole;
/**
*
*/
protected SimbuckUSBComms usbtask;
;
final int mainFrameWidth = 500;
final int mainFrameHeight = 100;
Border raisedbevel, loweredbevel, solidline, emptyborder;
/*
* local instance of error logger from slf4j library *
*/
private static final Logger _logger = LoggerFactory.getLogger(SimbuckFlashTab.class);
/**
*
*/
public SimbuckFlashTab() {
usbtask = new SimbuckUSBComms((short) 0x04D8, (short) 0x001F);
//usbtask = new SimbuckUSBComms((short)0x04D8, (short)0x003C);
}
/**
*
* @return the user tab as a JComponent
*/
public JComponent createSimbuckFlashTab() {
textArea = new JTextArea();
SimbuckDataPane = new JScrollPane(textArea);
simbuckconsole = new JTextArea();
OutputPanel = new JScrollPane(simbuckconsole);
OutputPanel.setMaximumSize(new Dimension(mainFrameWidth, mainFrameHeight));
OutputPanel.setMinimumSize(new Dimension(mainFrameWidth, mainFrameHeight));
OutputPanel.setPreferredSize(new Dimension(mainFrameWidth, mainFrameHeight));
raisedbevel = BorderFactory.createRaisedBevelBorder();
loweredbevel = BorderFactory.createLoweredBevelBorder();
solidline = BorderFactory.createLineBorder(Color.lightGray);
emptyborder = BorderFactory.createEmptyBorder(2, 2, 2, 2);
StringBuilder filename = new StringBuilder();
// Main Panel for user entry widgets
TestingPanel = new JPanel(new BorderLayout());
TestingPanel.setMaximumSize(new Dimension(mainFrameWidth, mainFrameHeight));
TestingPanel.setMinimumSize(new Dimension(mainFrameWidth, mainFrameHeight));
TestingPanel.setPreferredSize(new Dimension(mainFrameWidth, mainFrameHeight));
JPanel HexFilesPanel = new JPanel(new BorderLayout());
////JLabel hexlbl = new JLabel(".hex file");
////HexFilesPanel.add(hexlbl, BorderLayout.WEST);
selHEX = new JComboBox();
directory = setUserPath("hex");
// collate the relevant files
StringBuilder temp = new StringBuilder();
try {
File folder = new File(directory + System.getProperty("file.separator"));
hexfiles = folder.listFiles();
selHEX.addItem("select hex file");
for (int index = 0; index < hexfiles.length; index++) {
//Print out the name of files in the directory
temp.append(hexfiles[index]);
int idx = temp.lastIndexOf(System.getProperty("file.separator"));
temp.delete(0, idx + 1);
if (_logger.isInfoEnabled()) {
_logger.info("hex filename: " + temp.toString());
}
selHEX.addItem(temp.toString());
temp.delete(0, temp.length());
}
} catch (NullPointerException npe) {
if (_logger.isErrorEnabled()) {
_logger.error("Can't find files in " + directory);
}
}
selHEX.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
getDirectoriesMap().put("hex",
setUserPath("hex")
+ selHEX.getSelectedItem().toString());
statusBar.setText("File Loaded", 1);
//generateTableView(TestingPanel);
generateFileView(TestingPanel);
}
});
//filesPanel.add(selHEX, BorderLayout.NORTH);
// filesPanel.setBorder(loweredbevel);
WidgetPanel = new JPanel(new BorderLayout());
WidgetPanel.setMaximumSize(new Dimension(mainFrameWidth / 2, mainFrameHeight * 2));
WidgetPanel.setMinimumSize(new Dimension(mainFrameWidth / 2, mainFrameHeight * 2));
WidgetPanel.setPreferredSize(new Dimension(mainFrameWidth / 2, mainFrameHeight * 2));
ButtonPanel = new JPanel();
ButtonPanel.setLayout(new BoxLayout(ButtonPanel, BoxLayout.Y_AXIS));
//ButtonPanel.setBorder(loweredbevel);
ButtonPanel.setMaximumSize(new Dimension(mainFrameWidth / 3, mainFrameHeight * 2));
ButtonPanel.setMinimumSize(new Dimension(mainFrameWidth / 3, mainFrameHeight * 2));
ButtonPanel.setPreferredSize(new Dimension(mainFrameWidth / 3, mainFrameHeight * 2));
StatusPanel = new JPanel();
StatusPanel.setLayout(new BoxLayout(StatusPanel, BoxLayout.Y_AXIS));
StatusPanel.setBorder(loweredbevel);
StatusPanel.setMaximumSize(new Dimension(mainFrameWidth / 3, mainFrameHeight * 2));
StatusPanel.setMinimumSize(new Dimension(mainFrameWidth / 3, mainFrameHeight * 2));
StatusPanel.setPreferredSize(new Dimension(mainFrameWidth / 3, mainFrameHeight * 2));
Boot = new JButton("Boot");
Boot.setAction(new BootFlashUSBAction());
Read = new JButton("Read");
Erase = new JButton("Erase");
Write = new JButton("Write");
Verify = new JButton("Verify");
Reset = new JButton("Reset");
Reset.setAction(new ResetFlashUSBAction());
BootDone = new JButton("done");
ReadDone = new JButton("done");
EraseDone = new JButton("done");
WriteDone = new JButton("done");
VerifyDone = new JButton("done");
ResetDone = new JButton("done");
ButtonPanel.add(Boot);
ButtonPanel.add(Read);
ButtonPanel.add(Erase);
ButtonPanel.add(Write);
ButtonPanel.add(Verify);
ButtonPanel.add(Reset);
StatusPanel.add(BootDone);
StatusPanel.add(ReadDone);
StatusPanel.add(EraseDone);
StatusPanel.add(WriteDone);
StatusPanel.add(VerifyDone);
StatusPanel.add(ResetDone);
WidgetPanel.add(ButtonPanel, BorderLayout.NORTH);
selHEX.setBorder(emptyborder);
HexFilesPanel.setBorder(loweredbevel);
HexFilesPanel.add(selHEX, BorderLayout.CENTER);
TestingPanel.add(WidgetPanel, BorderLayout.WEST);
TestingPanel.add(SimbuckDataPane, BorderLayout.EAST);
TestingPanel.add(HexFilesPanel, BorderLayout.NORTH);
TestingPanel.add(OutputPanel, BorderLayout.CENTER);
return TestingPanel;
}
////////////////////////////////////////////////////////////////////////////////
/**
* set a user path to a filetype
*
* @param type the filetype
* @return the directory of the file type
*/
private String setUserPath(String type) {
// Don't use OS specific hard coded paths!!
StringBuilder path = new StringBuilder();
path.append(System.getProperty("user.dir"));
path.append(System.getProperty("file.separator"));
if (type.equals("hex")) /* ||
(type.equals("pdf")) ||
(type.equals("conf")) ||
(type.equals("DD")) ||
(type.equals("header")))*/ {
path.append("DeviceHex");
path.append(System.getProperty("file.separator"));
directoriesMap.put(type, path.toString());
}
if (_logger.isInfoEnabled()) {
_logger.info("User directory: " + System.getProperty("user.dir"));
}
return directoriesMap.get(type);
}
private void generateFileView(JPanel panel) {
SimbuckDataPanel = new JPanel(new BorderLayout());
fileDisplayPane = new JPanel(new BorderLayout());
try {
// clear the textArea
textArea.setText(null);
// load the new file
FileManager manager = new FileManager();
textArea.append(manager.openFile(directoriesMap.get("hex")));
if (_logger.isInfoEnabled()) {
_logger.info("Open: " + directoriesMap.get("hex"));
}
} catch (NullPointerException npe) {
if (_logger.isErrorEnabled()) {
_logger.error("Null pointer in createFileDisplayPane: " + npe);
}
System.out.println("Null pointer in createFileDisplayPane");
}
// Set the cursor to the top of the file
textArea.select(0, 0);
panel.validate();
}
/**
* set reference point for directory paths
*
* @param m
* @return size of directories Map
*/
public int setDirectoriesMap(Map<String, String> m) {
directoriesMap = new TreeMap<String, String>(m);
return directoriesMap.size();
}
/**
* get reference for directory paths
*
* @return directories map
*/
public Map<String, String> getDirectoriesMap() {
return directoriesMap;
}
}
/**
* A class to manage the type of files selected
*
* @author dzbm2p
*/
class FileListFilter implements FilenameFilter {
private String name;
private String extension;
/**
* set the file filter
*
* @param name filename
* @param extension file extension
*/
public FileListFilter(String name, String extension) {
this.name = name;
this.extension = extension;
}
/**
* apply the filter
*
* @param directory
* @param filename
* @return boolean flag ok = true not ok = false
*/
@Override
public boolean accept(File directory, String filename) {
boolean fileOK = true;
// if (name != null) {
// fileOK &= filename.contains(name);
//fileOK &= filename.startsWith(name);
// }
if (extension != null) {
fileOK &= filename.endsWith('.' + extension);
}
return fileOK;
}
}