/*
* IOTextView.java
*/
package br.com.mvbos.iotext;
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 java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
/**
* The application's main frame.
*/
public class IOTextView extends FrameView {
private JFileChooser chooser = new JFileChooser();
private File selectedFile = new File("iotext.txt");
private final String TITLE;
private long last;
private Set<String> encodeList = new HashSet<String>(3);
public IOTextView(SingleFrameApplication app) {
super(app);
initComponents();
TITLE = this.getFrame().getTitle();
encodeList.add(Charset.defaultCharset().toString());
encodeList.add("UTF-8");
encodeList.add("ISO-8859-1");
encodeList.add("ASCII");
for (String s : encodeList) {
selectEncode.addItem(s);
}
// 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 = IOTextApp.getApplication().getMainFrame();
aboutBox = new IOTextAboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
IOTextApp.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() {
bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
mainPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
textContent = new javax.swing.JTextPane();
selectEncode = new javax.swing.JComboBox();
cbReload = new javax.swing.JCheckBox();
btnChangeEncode = new javax.swing.JButton();
btnUpdate = new javax.swing.JButton();
btnClear = new javax.swing.JButton();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem();
jmReload = new javax.swing.JMenuItem();
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();
lblEncode = new javax.swing.JLabel();
mainPanel.setName("mainPanel"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
textContent.setName("textContent"); // NOI18N
jScrollPane1.setViewportView(textContent);
selectEncode.setName("selectEncode"); // NOI18N
selectEncode.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
selectEncodeItemStateChanged(evt);
}
});
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(br.com.mvbos.iotext.IOTextApp.class).getContext().getResourceMap(IOTextView.class);
cbReload.setText(resourceMap.getString("cbReload.text")); // NOI18N
cbReload.setName("cbReload"); // NOI18N
cbReload.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cbReloadActionPerformed(evt);
}
});
btnChangeEncode.setText(resourceMap.getString("btnChangeEncode.text")); // NOI18N
btnChangeEncode.setToolTipText(resourceMap.getString("btnChangeEncode.toolTipText")); // NOI18N
btnChangeEncode.setName("btnChangeEncode"); // NOI18N
org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cbReload, org.jdesktop.beansbinding.ELProperty.create("${!selected}"), btnChangeEncode, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
bindingGroup.addBinding(binding);
btnChangeEncode.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnChangeEncodeActionPerformed(evt);
}
});
btnUpdate.setText(resourceMap.getString("btnUpdate.text")); // NOI18N
btnUpdate.setToolTipText(resourceMap.getString("btnUpdate.toolTipText")); // NOI18N
btnUpdate.setName("btnUpdate"); // NOI18N
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
btnClear.setText(resourceMap.getString("btnClear.text")); // NOI18N
btnClear.setToolTipText(resourceMap.getString("btnClear.toolTipText")); // NOI18N
btnClear.setName("btnClear"); // NOI18N
btnClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
mainPanel.setLayout(mainPanelLayout);
mainPanelLayout.setHorizontalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 605, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
.addGap(24, 24, 24)
.addComponent(cbReload)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(selectEncode, 0, 337, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnUpdate)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnChangeEncode)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnClear)))
.addContainerGap())
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cbReload)
.addComponent(selectEncode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnClear)
.addComponent(btnChangeEncode)
.addComponent(btnUpdate))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
.addContainerGap())
);
menuBar.setName("menuBar"); // NOI18N
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
openMenuItem.setText(resourceMap.getString("openMenuItem.text")); // NOI18N
openMenuItem.setName("openMenuItem"); // NOI18N
openMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openMenuItemActionPerformed(evt);
}
});
fileMenu.add(openMenuItem);
saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
saveMenuItem.setText(resourceMap.getString("saveMenuItem.text")); // NOI18N
saveMenuItem.setName("saveMenuItem"); // NOI18N
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cbReload, org.jdesktop.beansbinding.ELProperty.create("${!selected}"), saveMenuItem, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
bindingGroup.addBinding(binding);
saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveMenuItemActionPerformed(evt);
}
});
fileMenu.add(saveMenuItem);
jmReload.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
jmReload.setText(resourceMap.getString("jmReload.text")); // NOI18N
jmReload.setName("jmReload"); // NOI18N
jmReload.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jmReloadActionPerformed(evt);
}
});
fileMenu.add(jmReload);
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(br.com.mvbos.iotext.IOTextApp.class).getContext().getActionMap(IOTextView.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
lblEncode.setText(resourceMap.getString("lblEncode.text")); // NOI18N
lblEncode.setName("lblEncode"); // 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, 625, Short.MAX_VALUE)
.addGroup(statusPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(statusMessageLabel)
.addComponent(lblEncode, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.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)
.addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(statusPanelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, 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))
.addGroup(statusPanelLayout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblEncode)
.addContainerGap())))
);
setComponent(mainPanel);
setMenuBar(menuBar);
setStatusBar(statusPanel);
bindingGroup.bind();
}// </editor-fold>//GEN-END:initComponents
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (chooser.showOpenDialog(this.getFrame()) == JFileChooser.APPROVE_OPTION) {
selectedFile = chooser.getSelectedFile();
loadFile();
}
}//GEN-LAST:event_openMenuItemActionPerformed
private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
saveFile(textContent.getText());
}//GEN-LAST:event_saveMenuItemActionPerformed
private void selectEncodeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_selectEncodeItemStateChanged
//loadFile();
}//GEN-LAST:event_selectEncodeItemStateChanged
private void cbReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbReloadActionPerformed
if (cbReload.isSelected()) {
last = 0;
new Thread() {
@Override
public void run() {
while (cbReload.isSelected()) {
try {
Thread.sleep(800);
if (last == 0 || last != selectedFile.lastModified()) {
loadFile();
}
} catch (InterruptedException e) {
}
}
}
}.start();
}
}//GEN-LAST:event_cbReloadActionPerformed
private void btnChangeEncodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChangeEncodeActionPerformed
Charset charset = Charset.forName(selectEncode.getSelectedItem().toString());
CharsetDecoder decoder = charset.newDecoder();
CharsetEncoder encoder = charset.newEncoder();
try {
ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(textContent.getText()));
CharBuffer cbuf = decoder.decode(bbuf);
textContent.setText(cbuf.toString());
lblEncode.setText("Change encode complete. Do not forget to save.");
} catch (CharacterCodingException e) {
lblEncode.setText(e.getMessage());
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
}
}//GEN-LAST:event_btnChangeEncodeActionPerformed
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
loadFile();
}//GEN-LAST:event_btnUpdateActionPerformed
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
saveFile("");
loadFile();
}//GEN-LAST:event_btnClearActionPerformed
private void jmReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmReloadActionPerformed
loadFile();
}//GEN-LAST:event_jmReloadActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnChangeEncode;
private javax.swing.JButton btnClear;
private javax.swing.JButton btnUpdate;
private javax.swing.JCheckBox cbReload;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JMenuItem jmReload;
private javax.swing.JLabel lblEncode;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem openMenuItem;
private javax.swing.JProgressBar progressBar;
private javax.swing.JMenuItem saveMenuItem;
private javax.swing.JComboBox selectEncode;
private javax.swing.JLabel statusAnimationLabel;
private javax.swing.JLabel statusMessageLabel;
private javax.swing.JPanel statusPanel;
private javax.swing.JTextPane textContent;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration//GEN-END:variables
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 final StringBuilder conteudo = new StringBuilder(500);
private synchronized void loadFile() {
try {
if (conteudo.length() > 0) {
conteudo.delete(0, conteudo.length());
}
if (selectedFile.exists()) {
last = selectedFile.lastModified();
//String defEncode = new InputStreamReader(new FileInputStream(selectedFile)).getEncoding();
//lblEncode.setText("File encode: " + defEncode);//Don`t work...
lblEncode.setText(new Date(last).toString());
InputStreamReader is = new InputStreamReader(
new FileInputStream(selectedFile),
Charset.forName(selectEncode.getSelectedItem().toString()));
BufferedReader reader = new BufferedReader(is);
String linha = null;
while ((linha = reader.readLine()) != null) {
conteudo.append(linha).append(System.getProperty("line.separator"));
}
reader.close();
}
textContent.setText(conteudo.toString());
this.getFrame().setTitle(TITLE + " - " + selectedFile.getAbsoluteFile());
} catch (FileNotFoundException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
private void saveFile(String txt) {
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(selectedFile), Charset.forName(selectEncode.getSelectedItem().toString()));
osw.write(txt);
last = selectedFile.lastModified();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (osw != null) {
osw.close();
}
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}