/*
* ConverterView.java
*/
package com.icona.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.text.Document;
import javax.swing.tree.TreePath;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import com.icona.console.main.ConsoleMain;
import com.icona.console.main.ConsoleMain.ConversionObjects;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
/**
* The application's main frame.
*/
public class ConverterView extends FrameView {
public ConverterView(SingleFrameApplication app) {
super(app);
initComponents();
initView();
// status bar initialization - message timeout, idle icon and busy animation, etc
ResourceMap resourceMap = getResourceMap();
int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
messageTimer = new Timer(messageTimeout, new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusMessageLabel.setText("");
}
});
messageTimer.setRepeats(false);
int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
for (int i = 0; i < busyIcons.length; i++) {
busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
}
busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
public void actionPerformed(ActionEvent e) {
busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
}
});
idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
// connecting action tasks to status bar via TaskMonitor
TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
public void propertyChange(java.beans.PropertyChangeEvent evt) {
String propertyName = evt.getPropertyName();
if ("started".equals(propertyName)) {
if (!busyIconTimer.isRunning()) {
statusAnimationLabel.setIcon(busyIcons[0]);
busyIconIndex = 0;
busyIconTimer.start();
}
progressBar.setVisible(true);
progressBar.setIndeterminate(true);
} else if ("done".equals(propertyName)) {
busyIconTimer.stop();
statusAnimationLabel.setIcon(idleIcon);
progressBar.setVisible(false);
progressBar.setValue(0);
} else if ("message".equals(propertyName)) {
String text = (String)(evt.getNewValue());
statusMessageLabel.setText((text == null) ? "" : text);
messageTimer.restart();
} else if ("progress".equals(propertyName)) {
int value = (Integer)(evt.getNewValue());
progressBar.setVisible(true);
progressBar.setIndeterminate(false);
progressBar.setValue(value);
}
}
});
}
@Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = ConverterApp.getApplication().getMainFrame();
aboutBox = new ConverterAboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
ConverterApp.getApplication().show(aboutBox);
}
/** 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() {
mainPanel = new javax.swing.JPanel();
jToolBar1 = new javax.swing.JToolBar();
jButton1 = new javax.swing.JButton();
jSeparator1 = new javax.swing.JToolBar.Separator();
jButton3 = new javax.swing.JButton();
jSeparator2 = new javax.swing.JToolBar.Separator();
jButton2 = new javax.swing.JButton();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenu helpMenu = new javax.swing.JMenu();
javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
statusPanel = new javax.swing.JPanel();
javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
statusMessageLabel = new javax.swing.JLabel();
statusAnimationLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
mainPanel.setName("mainPanel"); // NOI18N
jToolBar1.setRollover(true);
jToolBar1.setName("jToolBar1"); // NOI18N
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ConverterApp.class).getContext().getResourceMap(ConverterView.class);
jButton1.setBackground(resourceMap.getColor("openButton.background")); // NOI18N
jButton1.setText(resourceMap.getString("openButton.text")); // NOI18N
jButton1.setFocusable(false);
jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton1.setName("openButton"); // NOI18N
jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jToolBar1.add(jButton1);
jSeparator1.setName("jSeparator1"); // NOI18N
jToolBar1.add(jSeparator1);
jButton3.setBackground(resourceMap.getColor("convertButton.background")); // NOI18N
jButton3.setText(resourceMap.getString("convertButton.text")); // NOI18N
jButton3.setFocusable(false);
jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton3.setName("convertButton"); // NOI18N
jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});
jToolBar1.add(jButton3);
jSeparator2.setName("jSeparator2"); // NOI18N
jToolBar1.add(jSeparator2);
jButton2.setBackground(resourceMap.getColor("jButton2.background")); // NOI18N
jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
jButton2.setFocusable(false);
jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton2.setName("jButton2"); // NOI18N
jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2MouseClicked(evt);
}
});
jToolBar1.add(jButton2);
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(229, Short.MAX_VALUE))
);
menuBar.setName("menuBar"); // NOI18N
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(ConverterApp.class).getContext().getActionMap(ConverterView.class, this);
exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
fileMenu.add(exitMenuItem);
menuBar.add(fileMenu);
helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
helpMenu.setName("helpMenu"); // NOI18N
aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
aboutMenuItem.setName("aboutMenuItem"); // NOI18N
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
statusPanel.setName("statusPanel"); // NOI18N
statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N
statusMessageLabel.setName("statusMessageLabel"); // NOI18N
statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N
progressBar.setName("progressBar"); // NOI18N
javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
statusPanel.setLayout(statusPanelLayout);
statusPanelLayout.setHorizontalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(statusMessageLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 230, Short.MAX_VALUE)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusAnimationLabel)
.addContainerGap())
);
statusPanelLayout.setVerticalGroup(
statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(statusPanelLayout.createSequentialGroup()
.addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(statusMessageLabel)
.addComponent(statusAnimationLabel)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(3, 3, 3))
);
setComponent(mainPanel);
setMenuBar(menuBar);
setStatusBar(statusPanel);
}// </editor-fold>//GEN-END:initComponents
// private JSplitPane jSplitT;
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
// TODO add your handling code here:
File dir = null;
JFileChooser jFile= new JFileChooser () ;
jFile.setFileSelectionMode(2); //jFile.setDialogTitle("Open project folder");
if (jFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
if (projectTabs.getTabCount() > 0) {
if (projectTabs.getTabTitleAt(0).equals("Empty Project")) {
projectTabs.removeAll();
}
}
dir = jFile.getSelectedFile();
} else {
return;
}
FileSystemModel modelI = new FileSystemModel(dir);
final JTree jTI = new JTree(modelI);
final ClosableTabbedPane jTab = new ClosableTabbedPane();
JScrollPane jScrollI = new JScrollPane(jTI);
JSplitPane jSplitT = new JSplitPane();
JSplitPane jSplitP = new JSplitPane();
jSplitP.setName(dir.getAbsolutePath());
jSplitT.setOrientation(JSplitPane.VERTICAL_SPLIT);
jSplitT.setDividerLocation(300);
jSplitP.setRightComponent(jTab);
jSplitT.setTopComponent(jScrollI);
jSplitT.setRightComponent(new JScrollPane(null));
jSplitP.setLeftComponent(jSplitT);
jTI.setEditable(true);
jTI.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent event) {
File file = (File) jTI.getLastSelectedPathComponent();
for (int i = 0; i < jTab.getTabCount(); i++) {
if (jTab.getTabTitleAt(i).equals(file.getName())) {
System.out.println("Already open-setting focus");
jTab.setSelectedIndex(i);
return;
}
}
System.out.println("You selected " + file.getPath());
if (file.isFile()) {
JEditorPane jEdit = new JEditorPane();
jTab.addTab(file.getName(), new JScrollPane(jEdit));
jTab.setSelectedIndex(jTab.getTabCount() - 1);
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file.getPath()));
} catch (FileNotFoundException ex) {
Logger.getLogger(ConverterView.class.getName()).log(Level.SEVERE, null, ex);
}
try {
jEdit.read(in, "readfile");
} catch (IOException ex) {
Logger.getLogger(ConverterView.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
projectTabs.addTab(dir.getName(), jSplitP);
JSplitPane jp = (JSplitPane) projectTabs.getComponent(0);
System.out.println("***Source*******");
System.out.println("ParentClass: " + jp.getClass());
System.out.println(jp.getComponent(0).getClass());
System.out.println(jp.getComponent(1).getClass());
System.out.println(jp.getComponent(2).getClass());
System.out.println("****************");
projectTabs.setSelectedIndex(projectTabs.getTabCount() - 1);
}//GEN-LAST:event_jButton1MouseClicked
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
// TODO add your handling code here:
JSplitPane jp = (JSplitPane) projectTabs.getSelectedComponent();
JSplitPane jSplitP = jp;
if(jp.getName()==null){
String message = "No project folder was found for conversion. Please open a project folder first.";
JOptionPane pane = new JOptionPane(message);
JDialog dialog = pane.createDialog(new JFrame(), "No Project Open!");
dialog.show();
return;
}
File dest = null;
JFileChooser jFile = new JFileChooser();
jFile.setFileSelectionMode(2);
if (jFile.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
dest = jFile.getSelectedFile();
}
else return;
/* run the conversion routine, convert files in dir object, place the converted in dest */
ConsoleMain cm = new ConsoleMain();
ConsoleMain.ConversionObjects cb = cm.new ConversionObjects();
String filename = null;
File dir = new File(jp.getName());
for(int i = 0; i < dir.listFiles().length; i++){
File file = dir.listFiles()[i];
if(file.isFile()){
filename = file.getName().substring(0, file.getName().lastIndexOf('.'));
cb.setFileName(filename);
cb.setInputStream( file.getPath() );
cb.doConversion();
cb.writeToFile(dest.getPath() + "/" + filename + ".java" );
}
}
/************************************/
FileSystemModel modelC = new FileSystemModel(dest);
final JTree jTC = new JTree(modelC);
System.out.println("***Destination****");
System.out.println("ParentClass: "+jp.getClass());
System.out.println(jp.getComponent(1).getClass());
System.out.println(jp.getComponent(2).getClass());
System.out.println("******************");
final ClosableTabbedPane jTab = (ClosableTabbedPane) jp.getComponent(1);
jSplitP.setRightComponent(jTab);
jTC.setEditable(true);
JSplitPane jSplitT = (JSplitPane) jp.getComponent(1);
JScrollPane jScrollC=new JScrollPane(jTC);
jScrollC.setSize(170, GroupLayout.DEFAULT_SIZE);
jSplitT.setRightComponent(jScrollC);
jSplitT.setDividerLocation(300);
jTC.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent event) {
File file = (File) jTC.getLastSelectedPathComponent();
for (int i = 0; i < jTab.getTabCount(); i++) {
if (jTab.getTabTitleAt(i).equals(file.getName())) {
System.out.println("Already open-setting focus");
jTab.setSelectedIndex(i);
return;
}
}
System.out.println("You selected " + file.getPath());
if (file.isFile()) {
JEditorPane jEdit = new JEditorPane();
jTab.addTab(file.getName(), new JScrollPane(jEdit));
jTab.setSelectedIndex(jTab.getTabCount() - 1);
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file.getPath()));
} catch (FileNotFoundException ex) {
Logger.getLogger(ConverterView.class.getName()).log(Level.SEVERE, null, ex);
}
try {
jEdit.read(in, "readfile");
} catch (IOException ex) {
Logger.getLogger(ConverterView.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
}//GEN-LAST:event_jButton3MouseClicked
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
// TODO add your handling code here:
ApiMapView ap = new ApiMapView((SingleFrameApplication) this.getApplication());
this.getApplication().show(ap);
}//GEN-LAST:event_jButton2MouseClicked
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JToolBar.Separator jSeparator1;
private javax.swing.JToolBar.Separator jSeparator2;
private javax.swing.JToolBar jToolBar1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JProgressBar progressBar;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
// End of variables declaration//GEN-END:variables
private ClosableTabbedPane projectTabs;
private final Timer messageTimer;
private final Timer busyIconTimer;
private final Icon idleIcon;
private final Icon[] busyIcons = new Icon[15];
private int busyIconIndex = 0;
private JDialog aboutBox;
private void initView() {
projectTabs = new ClosableTabbedPane();
projectTabs.setName("ProjectTabs"); // NOI18N
JSplitPane jSplitT=new JSplitPane();
JSplitPane jSplitP=new JSplitPane();
jSplitT.setOrientation(JSplitPane.VERTICAL_SPLIT);
jSplitT.setDividerLocation(300);
jSplitP.setRightComponent(new JScrollPane());
jSplitT.setTopComponent(new JScrollPane());
jSplitT.setRightComponent(new JScrollPane());
jSplitT.setMinimumSize(new Dimension(200,50));
jSplitP.setLeftComponent(jSplitT);
projectTabs.addTab("Empty Project", jSplitP );
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
.addComponent(projectTabs, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(projectTabs, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
.addContainerGap())
);
setComponent(mainPanel);
}
}