/*
* ConverterView.java
*/
package com.wiieditor.gui;
import com.wiieditor.other.ProgramConfig;
import com.wiieditor.other.RecognizedDataHandler;
import com.wiieditor.wiimote.WiiMoteController;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTree;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import jsyntaxpane.DefaultSyntaxKit;
import org.jdesktop.application.Action;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.TaskMonitor;
import org.slf4j.LoggerFactory;
/**
* The application's main frame.
*/
public class ConverterView extends FrameView implements RecognizedDataHandler{
public ConverterView(SingleFrameApplication app) {
super(app);
initComponents();
drawingPanel=new JSplitPane();
this.entryPanel=new Entry();
this.gridPanel=new GridPanel(ProgramConfig.GRID_WIDTH, ProgramConfig.GRID_HEIGHT);
Entry ep=(Entry)entryPanel;
ep.setGridPanel((GridPanel)gridPanel);
ep.registerListener(this);
drawingPanel.setTopComponent(entryPanel);
drawingPanel.setBottomComponent(gridPanel);
drawingPanel.setDividerLocation(110);
drawingPanel.setOrientation(JSplitPane.VERTICAL_SPLIT);
//drawingPanel.set
explorerPane.setRightComponent(drawingPanel);
// 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();
openButton = new javax.swing.JButton();
jSeparator1 = new javax.swing.JToolBar.Separator();
newButton = new javax.swing.JButton();
jSeparator2 = new javax.swing.JToolBar.Separator();
saveButton = new javax.swing.JButton();
jSeparator3 = new javax.swing.JToolBar.Separator();
this.trainButton = new javax.swing.JButton();
jSeparator4 = new javax.swing.JToolBar.Separator();
this.connectButton = 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.JMenuItem openMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenuItem saveMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenuItem newMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenuItem trainMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenuItem connectMenuItem = 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);
// openButton.setBackground(resourceMap.getColor("openButton.background")); // NOI18N
openButton.setText("Open"); // NOI18N
openButton.setFocusable(false);
openButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
openButton.setName("openButton"); // NOI18N
openButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
openButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
openButtonMouseClicked(evt);
}
});
jToolBar1.add(openButton);
jSeparator1.setName("jSeparator1"); // NOI18N
jToolBar1.add(jSeparator1);
// newButton.setBackground(resourceMap.getColor("convertButton.background")); // NOI18N
newButton.setText("New");//resourceMap.getString("convertButton.text")); // NOI18N
newButton.setFocusable(false);
newButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
newButton.setName("newButton"); // NOI18N
newButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
newButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
newButtonMouseClicked(evt);
}
});
jToolBar1.add(newButton);
jSeparator2.setName("jSeparator2"); // NOI18N
jToolBar1.add(jSeparator2);
saveButton.setBackground(resourceMap.getColor("saveButton.background")); // NOI18N
saveButton.setText("Save"); // NOI18N
saveButton.setFocusable(false);
saveButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
saveButton.setName("saveButton"); // NOI18N
saveButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
saveButtonMouseClicked(evt);
}
});
jToolBar1.add(saveButton);
jSeparator3.setName("jSeparator3"); // NOI18N
jToolBar1.add(jSeparator3);
trainButton.setBackground(resourceMap.getColor("saveButton.background")); // NOI18N
trainButton.setText("Train"); // NOI18N
trainButton.setFocusable(false);
trainButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
trainButton.setName("trainButton"); // NOI18N
trainButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
trainButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
trainButtonMouseClicked(evt);
}
});
jToolBar1.add(trainButton);
jSeparator4.setName("jSeparator4"); // NOI18N
jToolBar1.add(jSeparator4);
connectButton.setBackground(resourceMap.getColor("saveButton.background")); // NOI18N
connectButton.setText("Connect"); // NOI18N
connectButton.setFocusable(false);
connectButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
connectButton.setName("connectButton"); // NOI18N
connectButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
connectButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
connectButtonMouseClicked(evt);
}
});
jToolBar1.add(connectButton);
this.listModel = new DefaultListModel();
this.jFileList = new JList(listModel);
jFileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jFileList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
File file = (File) jFileList.getSelectedValue();
for (int i = 0; i < fileTabs.getTabCount(); i++) {
// System.out.print("*"+file.getName()+"*-*"+fileTabs.getTitleAt(i)+"*");
if (fileTabs.getTitleAt(i).trim().equals(file.getName())) {
System.out.println("Already open-setting focus");
fileTabs.setSelectedIndex(i);
return;
}
}
System.out.println("You selected " + file.getPath());
if (file.isFile()) {
DefaultSyntaxKit.initKit();
JEditorPane jEdit = new JEditorPane();
fileTabs.addTab(file.getName(), new JScrollPane(jEdit));
fileTabs.setSelectedIndex(fileTabs.getTabCount() - 1);
jEdit.setContentType("text/c");
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);
}
}
}
});
this.drawingArea = null;
this.explorerPane = new JSplitPane();
this.editorPane = new JSplitPane();
this.fileTabs = new ClosableTabbedPane();
projectTabs = new JTabbedPane();
projectTabs.setName("ProjectTabs"); // NOI18N
explorerPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
explorerPane.setDividerLocation(300);
DefaultSyntaxKit.initKit();
fileTabs.addTab("untitled1.txt", new JScrollPane(new JEditorPane()));
fileTabs.setSelectedIndex(fileTabs.getTabCount() - 1);
editorPane.setRightComponent(fileTabs);
explorerPane.setTopComponent(new JScrollPane(jFileList));
explorerPane.setRightComponent(new JScrollPane(null)); //drawing object will go here
explorerPane.setMinimumSize(new Dimension(200,50));
editorPane.setLeftComponent(explorerPane);
projectTabs.addTab("Explorer", editorPane );
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())
//.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);
openMenuItem.setAction(actionMap.get("open")); // NOI18N
openMenuItem.setName("openMenuItem"); // NOI18N
openMenuItem.setText("Open");
fileMenu.add(openMenuItem);
saveMenuItem.setAction(actionMap.get("save")); // NOI18N
saveMenuItem.setName("saveMenuItem"); // NOI18N
saveMenuItem.setText("Save");
fileMenu.add(saveMenuItem);
trainMenuItem.setAction(actionMap.get("train")); // NOI18N
trainMenuItem.setName("trainMenuItem"); // NOI18N
trainMenuItem.setText("Train");
fileMenu.add(trainMenuItem);
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 openButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_openButtonMouseClicked
// TODO add your handling code here:
File dir = null;
JFileChooser jFile= new JFileChooser () ;
// jFile.setFileSelectionMode(2); //jFile.setDialogTitle("Open project folder");
jFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if (jFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
dir = jFile.getSelectedFile();
} else {
return;
}
listModel.addElement(dir);
}//GEN-LAST:event_openButtonMouseClicked
private void saveButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
// TODO add your handling code here:
if(fileTabs.getComponentCount()>0){
JFileChooser jFile = new JFileChooser();
// jFile.setFileSelectionMode(JFileChooser.SAVE_DIALOG);
if (jFile.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
JScrollPane jp1 = (JScrollPane) fileTabs.getSelectedComponent();
JViewport jv1 = (JViewport) jp1.getComponent(0);
JEditorPane fileContent = (JEditorPane) jv1.getComponent(0);
listModel.addElement(jFile.getSelectedFile().getAbsoluteFile());
try {
FileWriter out = new FileWriter(jFile.getSelectedFile().getAbsolutePath());
fileTabs.setTitleAt(fileTabs.getSelectedIndex(), jFile.getSelectedFile().getName());
System.out.println(jFile.getSelectedFile().getAbsolutePath()+"\\"+fileTabs.getTitleAt(fileTabs.getSelectedIndex()));
out.write(fileContent.getText());
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else return;
}
else
return;
}//GEN-LAST:event_jButton3MouseClicked
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
//ApiMapView ap = new ApiMapView((SingleFrameApplication) this.getApplication());
FrameView ap = new FrameView(this.getApplication());
}
private void trainButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
// JFrame trainingUI = new TrainingUI();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TrainingUI().setVisible(true);
}
});
// trainingUI.show();
//FrameView ap = new FrameView(this.getApplication());
// ap.setFrame(trainingUI);
//ap.setComponent(trainingUI);
//this.getApplication().show(ap);
}
private void connectButtonMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
//ApiMapView ap = new ApiMapView((SingleFrameApplication) this.getApplication());
FrameView ap = new FrameView(this.getApplication());
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
WiiMoteController.createWiiMoteController().disconnect();
WiiMoteController.createWiiMoteController().unregisterListener((Entry)entryPanel);
((Entry)entryPanel).unregisterListener(this);
}
private void newButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
// TODO add your handling code here:
DefaultSyntaxKit.initKit();
fileTabs.addTab("untitled"+(fileTabs.getTabCount()+1)+".txt", new JScrollPane(new JEditorPane()));
fileTabs.setSelectedIndex(fileTabs.getTabCount() - 1);
}//GEN-LAST:event_jButton2MouseClicked
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton trainButton;
private javax.swing.JButton connectButton;
private javax.swing.JButton openButton;
private javax.swing.JButton saveButton;
private javax.swing.JButton newButton;
private javax.swing.JToolBar.Separator jSeparator1;
private javax.swing.JToolBar.Separator jSeparator2;
private javax.swing.JToolBar.Separator jSeparator3;
private javax.swing.JToolBar.Separator jSeparator4;
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 JTabbedPane projectTabs;
private ClosableTabbedPane fileTabs;
private JSplitPane explorerPane;
private Image drawingArea;
private JSplitPane editorPane;
private FileSystemModel fileTreeModel;// = new FileSystemModel(dir);
private JTree jTree;// = new JTree(modelI);
private JList jFileList;
private DefaultListModel listModel;
private JPanel entryPanel;
private JPanel gridPanel;
private JSplitPane drawingPanel;
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 org.slf4j.Logger log=LoggerFactory.getLogger(ConverterView.class);
@Override
public void onStopReceivingIRData(Entry entry) {
log.error("Recognized:"+entry.recognize());
}
}