/*
* Copyright (C) 2013 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 3 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.tools.files;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;
import org.domainmath.gui.MainFrame;
/**
*
* @author Vinu K.N
*/
public class FilesDialog extends javax.swing.JDialog{
private final DefaultListModel model;
private final DefaultComboBoxModel model2;
private final MainFrame frame;
/**
* Creates new form HistoryDialog
*/
public FilesDialog(MainFrame frame,boolean modal) {
super(frame, modal);
this.frame = frame;
model = new DefaultListModel();
model2=new DefaultComboBoxModel();
initComponents();
list.setModel(model);
this.jComboBox1.setModel(model2);
File f = new File(MainFrame.dirComboBox.getSelectedItem().toString());
if(f.exists()) {
jComboBox1.addItem(MainFrame.dirComboBox.getSelectedItem().toString());
File _l[]=f.listFiles();
for(int i=0; i<_l.length; i++) {
model.addElement(_l[i].getAbsolutePath());
}
}
list.setSelectedIndex(0);
list.setCellRenderer(new MyCellRenderer());
list.requestFocusInWindow();
list.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2) {
File fs=new File(list.getSelectedValue().toString());
if(fs.isDirectory()){
try{
File f = new File(jComboBox1.getSelectedItem().toString()+File.separator+fs.getName());
String dir=f.getAbsolutePath();
addFolder(dir);
MainFrame.dirComboBox.setSelectedItem(dir);
MainFrame.octavePanel.commandArea.append(fs.getName()+File.separator);
model.removeAllElements();
list.repaint();
if(f.exists()) {
File _l[]=f.listFiles();
for(int i=0; i<_l.length; i++) {
model.addElement(_l[i].getAbsolutePath());
}
list.setSelectedIndex(0);
}
}catch(Exception ex) {
System.out.println("No Parent");
}
}else{
MainFrame.octavePanel.commandArea.append(fs.getName());
dispose();
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
list.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()==KeyEvent.VK_ENTER) {
File f=new File(list.getSelectedValue().toString());
if(f.isDirectory()) {
MainFrame.octavePanel.commandArea.append(f.getName()+File.separator);
}else{
MainFrame.octavePanel.commandArea.append(f.getName());
}
dispose();
}else if(e.getKeyChar()==KeyEvent.VK_UP) {
int n=list.getSelectedIndex();
int t=model.getSize();
if(n<t) {
list.setSelectedIndex(n++);
}else{
list.setSelectedIndex(0);
}
}else if(e.getKeyChar()==KeyEvent.VK_DOWN) {
int n=list.getSelectedIndex();
int t=model.getSize();
if(n>=t) {
list.setSelectedIndex(n--);
}else{
list.setSelectedIndex(t);
}
}else if(e.getKeyChar()==KeyEvent.VK_HOME) {
list.setSelectedIndex(0);
}else if(e.getKeyChar()==KeyEvent.VK_END) {
list.setSelectedIndex(model.getSize());
}else if(e.getKeyChar()==KeyEvent.VK_SPACE) {
MainFrame.octavePanel.commandArea.append(list.getSelectedValue().toString());
dispose();
}else if(e.getKeyChar()==KeyEvent.VK_ESCAPE) {
dispose();
}else{
MainFrame.octavePanel.commandArea.append(String.valueOf(e.getKeyChar()));
dispose();
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
list = new javax.swing.JList();
jToolBar1 = new javax.swing.JToolBar();
jLabel1 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
upButton = new javax.swing.JButton();
addPathButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/domainmath/gui/tools/files/resources/files_en"); // NOI18N
setTitle(bundle.getString("FilesDialog.title")); // NOI18N
list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
jScrollPane1.setViewportView(list);
jToolBar1.setFloatable(false);
jToolBar1.setRollover(true);
jLabel1.setText("Current Directory:");
jToolBar1.add(jLabel1);
jComboBox1.setEditable(true);
jComboBox1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox1ItemStateChanged(evt);
}
});
jToolBar1.add(jComboBox1);
upButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/domainmath/gui/tools/files/resources/go-up.png"))); // NOI18N
upButton.setFocusable(false);
upButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
upButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
upButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
upButtonActionPerformed(evt);
}
});
jToolBar1.add(upButton);
addPathButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/domainmath/gui/tools/files/resources/add.png"))); // NOI18N
addPathButton.setFocusable(false);
addPathButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
addPathButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
addPathButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addPathButtonActionPerformed(evt);
}
});
jToolBar1.add(addPathButton);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, 0)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
if(evt.getStateChange()== ItemEvent.SELECTED){
Object s =this.jComboBox1.getSelectedItem();
int n= jComboBox1.getItemCount();
if(jComboBox1.getSelectedIndex()<=-1) { // added exterally.
if(n==0){
jComboBox1.addItem(s);
cd(s.toString());
}else{
for(int i=0; i<jComboBox1.getItemCount();i++) {
if(s==jComboBox1.getItemAt(i)){
System.out.println("Repeated");
}else{
jComboBox1.removeItem(s);
jComboBox1.insertItemAt(s, 0);
jComboBox1.setSelectedIndex(0);
cd(s.toString());
break;
}
}
}
}else{
if (jComboBox1.getSelectedIndex()>0) {
Object item = jComboBox1.getSelectedItem();
jComboBox1.removeItem(item);
jComboBox1.insertItemAt(item, 0);
jComboBox1.setSelectedIndex(0);
cd(item.toString());
}else{
}
}
}
}//GEN-LAST:event_jComboBox1ItemStateChanged
public void addFolder(String s) {
for(int i=0; i<jComboBox1.getItemCount();i++) {
if(s==jComboBox1.getItemAt(i)){
System.out.println("Repeated");
}else{
jComboBox1.removeItem(s);
jComboBox1.insertItemAt(s, 0);
jComboBox1.setSelectedIndex(0);
break;
}
}
}
private void upButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upButtonActionPerformed
try{
File f = new File(jComboBox1.getSelectedItem().toString());
String dir=f.getParent();
if(dir !=null) {
addFolder(dir);
MainFrame.dirComboBox.setSelectedItem(dir);
frame.fileTreePanel.updateFileTree(f.getParentFile());
model.removeAllElements();
list.repaint();
File _l[]=new File(f.getParent()).listFiles();
for(int i=0; i<_l.length; i++) {
model.addElement(_l[i].getAbsolutePath());
}
list.setSelectedIndex(0);
}else{
System.out.println("Error");
dispose();
}
}catch(Exception e) {
System.out.println("No Parent");
dispose();
}
}//GEN-LAST:event_upButtonActionPerformed
private void addPathButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPathButtonActionPerformed
String dir=this.jComboBox1.getSelectedItem().toString();
int i =
JOptionPane.showConfirmDialog(this,
"Add this folder to Path List?-\n"+dir, "DomainMath IDE", JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(i == JOptionPane.YES_OPTION) {
MainFrame.octavePanel.evaluate("addpath(genpath("+"'"+dir+"'));");
MainFrame.octavePanel.evaluate("savepath();");
}
}//GEN-LAST:event_addPathButtonActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(FilesDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(FilesDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(FilesDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(FilesDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
FilesDialog dialog = new FilesDialog(null, true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addPathButton;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JList list;
private javax.swing.JButton upButton;
// End of variables declaration//GEN-END:variables
private void cd(String s) {
System.out.println("In"+s);
MainFrame.octavePanel.evaluate("chdir "+"'"+s+"'");
MainFrame.addFolder(s);
frame.fileTreePanel.updateFileTree(new File(s));
model.removeAllElements();
list.repaint();
File f = new File(s);
if(f.exists()) {
File _l[]=f.listFiles();
for(int i=0; i<_l.length; i++) {
model.addElement(_l[i].getAbsolutePath());
}
list.setSelectedIndex(0);
}
}
public class MyCellRenderer extends JLabel implements ListCellRenderer
{
java.net.URL imgURL = getClass().getResource("file.png");
java.net.URL imgURL2 = getClass().getResource("folder.png");
ImageIcon icon = new ImageIcon(imgURL);
ImageIcon icon2 = new ImageIcon(imgURL2);
public MyCellRenderer() {
this.setBorder(new EmptyBorder(1, 1, 1, 1));
this.setOpaque(true);
}
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean hasFocus)
{
File file = new File(value.toString());
this
.setIcon(FileSystemView.getFileSystemView().getSystemIcon(
file));
this.setText(FileSystemView.getFileSystemView()
.getSystemDisplayName(file));
// if(text.endsWith(File.separator)) {
// this.setIcon(icon2);
// this.setText(text);
// }else{
// this.setIcon(icon);
// this.setText(text);
// }
if(isSelected|| hasFocus) {
this.setBackground(list.getSelectionBackground());
this.setForeground(list.getSelectionForeground());
}else{
this.setBackground(list.getBackground());
this.setForeground(list.getForeground());
}
this.setToolTipText(file.getAbsolutePath());
return this;
}
}
}