/*
* Copyright (C) 2012 Vinu K.N
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.domainmath.gui.debugger;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.nio.file.Path;
import javax.swing.*;
import org.domainmath.gui.MainFrame;
public class DbstackPanel extends JPanel {
JTable table;
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/domainmath/gui/debugger/resources/dbstack_en");
private final JPopupMenu popup = new JPopupMenu();
PopupActionListener popupActionListener;
TableMouseListener tableMouseListener;
private final JMenuItem openItem;
private final JMenuItem refreshItem;
private final DataFileTableModel model;
private final String directory;
private JToolBar dbstackToolBar;
private JButton openButton;
private JButton refreshButton;
private ActionListener toolBarActionListener;
private MainFrame frame;
public DbstackPanel(String directory,MainFrame frame) {
// super(new GridLayout(1, 0));
super(new BorderLayout());
this.frame= frame;
table = new JTable();
this.directory = directory;
popupActionListener = new PopupActionListener();
toolBarActionListener = new ToolBarActionListener();
tableMouseListener = new TableMouseListener();
model = new DataFileTableModel(directory);
model.init();
table.setModel(model);
// table.setAutoCreateColumnsFromModel(true);
// table.setAutoCreateRowSorter(true);
table.setShowGrid(false);
table.getTableHeader().setReorderingAllowed(false);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setFillsViewportHeight(true);
table.setRowHeight(20);
openItem = new JMenuItem(bundle.getString("openItem.name"));
refreshItem = new JMenuItem(bundle.getString("refreshItem.name"));
openItem.setToolTipText(bundle.getString("openItem.tooltip"));
refreshItem.setToolTipText(bundle.getString("refreshItem.tooltip"));
openItem.addActionListener(popupActionListener);
refreshItem.addActionListener(popupActionListener);
popup.add(openItem);
popup.addSeparator();
popup.add(refreshItem);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );
table.addMouseListener(tableMouseListener);
table.add(popup);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
setUpToolBar();
add(scrollPane,BorderLayout.CENTER);
}
private void setUpToolBar() {
dbstackToolBar = new JToolBar();
dbstackToolBar.setFloatable(false);
dbstackToolBar.setRollover(true);
openButton = new JButton();
refreshButton = new JButton();
openButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/domainmath/gui/icons/open_16.png")));
refreshButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/domainmath/gui/icons/view-refresh.png")));
openButton.setToolTipText(bundle.getString("openItem.tooltip"));
refreshButton.setToolTipText(bundle.getString("refreshItem.tooltip"));
openButton.addActionListener(toolBarActionListener);
refreshButton.addActionListener(toolBarActionListener);
dbstackToolBar.add(openButton);
dbstackToolBar.add(refreshButton);
add(dbstackToolBar,BorderLayout.PAGE_START);
}
public DataFileTableModel getModel() {
return model;
}
private void showPopup(MouseEvent e){
if(table.getSelectedRow() > -1 && e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
public void reload() {
new DataTask().execute();
}
private class PopupActionListener implements ActionListener {
private Path path;
@Override
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
if(source.equals(openItem)) {
if(table.getSelectedRow() >= 0) {
String dir =table.getValueAt(table.getSelectedRow(), 0).toString();
String dir2=dir.replaceAll("'", "");
File file = new File(dir2);
if(!org.domainmath.gui.MainFrame.fileNameList.contains(file.getAbsolutePath())) {
frame.open(file, org.domainmath.gui.MainFrame.FILE_TAB_INDEX);
frame.setCurrentDirFileTab(file.getParent());
int line = Integer.parseInt((table.getValueAt(table.getSelectedRow(), 2).toString()));
frame.gotoLine(file.getAbsolutePath(),line);
}else {
System.out.println(file.getAbsolutePath()+" already open!");
int line = Integer.parseInt((table.getValueAt(table.getSelectedRow(), 2).toString()));
frame.gotoLine(file.getAbsolutePath(),line);
}
}
}
else if(source.equals(refreshItem)) {
reload();
}
}
}
private class TableMouseListener implements MouseListener{
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2) {
if(table.getSelectedRow() >= 0) {
String dir =table.getValueAt(table.getSelectedRow(), 0).toString();
String dir2=dir.replaceAll("'", "");
File file = new File(dir2);
if(!org.domainmath.gui.MainFrame.fileNameList.contains(file.getAbsolutePath())) {
frame.open(file, org.domainmath.gui.MainFrame.FILE_TAB_INDEX);
frame.setCurrentDirFileTab(file.getParent());
int line = Integer.parseInt((table.getValueAt(table.getSelectedRow(), 2).toString()));
frame.gotoLine(file.getAbsolutePath(),line);
}else {
int line = Integer.parseInt((table.getValueAt(table.getSelectedRow(), 2).toString()));
frame.gotoLine(file.getAbsolutePath(),line);
System.out.println(file.getAbsolutePath()+" already open!");
}
}
}
}
@Override
public void mousePressed(MouseEvent e) {
showPopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
showPopup(e);
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
private class ToolBarActionListener implements ActionListener {
private Path path;
@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)(e.getSource());
if(openButton.equals(source) ) {
if(table.getSelectedRow() >= 0) {
String dir =table.getValueAt(table.getSelectedRow(), 0).toString();
String dir2=dir.replaceAll("'", "");
File file = new File(dir2);
if(!org.domainmath.gui.MainFrame.fileNameList.contains(file.getAbsolutePath())) {
frame.open(file, org.domainmath.gui.MainFrame.FILE_TAB_INDEX);
frame.setCurrentDirFileTab(file.getParent());
}else {
System.out.println(file.getAbsolutePath()+" already open!");
int line = Integer.parseInt((table.getValueAt(table.getSelectedRow(), 2).toString()));
frame.gotoLine(file.getAbsolutePath(),line);
}
}
}
else if(refreshButton.equals(source)) {
reload();
}
}
}
public void openFiles(){
for(int i=0; i<table.getRowCount(); i++){
String dir =table.getValueAt(i, 0).toString();
String dir2=dir.replaceAll("'", "");
File file = new File(dir2);
if(!org.domainmath.gui.MainFrame.fileNameList.contains(file.getAbsolutePath())) {
frame.open(file, org.domainmath.gui.MainFrame.FILE_TAB_INDEX);
frame.setCurrentDirFileTab(file.getParent());
int line = Integer.parseInt((table.getValueAt(i, 2).toString()));
frame.gotoLine(file.getAbsolutePath(),line);
frame.setContinueMark(file.getAbsolutePath(),line);
}else {
System.out.println(file.getAbsolutePath()+" already open!");
int line = Integer.parseInt((table.getValueAt(i, 2).toString()));
frame.gotoLine(file.getAbsolutePath(),line);
frame.setContinueMark(file.getAbsolutePath(),line);
}
}
}
private class DataTask extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() {
int delay = 2000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
model.refresh();
model.fireTableStructureChanged();
model.fireTableDataChanged();
table.repaint();
openFiles();
}
};
javax.swing.Timer t= new javax.swing.Timer(delay, taskPerformer);
t.setRepeats(false);
t.start();
return null;
}
@Override
protected void done() {
try {
int delay = 2000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
model.refresh();
model.fireTableStructureChanged();
model.fireTableDataChanged();
table.repaint();
openFiles();
}
};
javax.swing.Timer t= new javax.swing.Timer(delay, taskPerformer);
t.setRepeats(false);
t.start();
} catch (Exception ignore) {
}
}
}
}