/*
* DesktopApplication3View.java
*/
package desktopapplication3;
import java.util.logging.Level;
import java.util.logging.Logger;
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 javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.lang.*;
import java.text.*;
import java.awt.event.*;
import java.io.*;
import java.awt.Color;
import java.io.InputStreamReader;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.GroupLayout.*;
import java.util.regex.*;
public class DesktopApplication3View extends FrameView {
String selectedItem;
public DesktopApplication3View(SingleFrameApplication app) {
super(app);
initComponents();
hilit = new DefaultHighlighter();
painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
MainTextArea.setHighlighter(hilit);
if(selectedItem == null)
{
MainTextArea.append("No File Selected\n");
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
mainPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
MainTextArea = new javax.swing.JTextArea();
StringSearchTextArea = new javax.swing.JTextField();
SearchButton = new javax.swing.JButton();
StatusMessage = new javax.swing.JLabel();
StatusLabel = new javax.swing.JLabel();
StringSearchLabel = new javax.swing.JLabel();
menuBar = new javax.swing.JMenuBar();
javax.swing.JMenu fileMenu = new javax.swing.JMenu();
javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenuItem exitMenuItem1 = new javax.swing.JMenuItem();
javax.swing.JMenu helpMenu = new javax.swing.JMenu();
javax.swing.JMenuItem HiglightKeyMenuItem = new javax.swing.JMenuItem();
javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
Satus = new javax.swing.JLabel();
mainPanel.setName("mainPanel"); // NOI18N
jScrollPane1.setName("jScrollPane1"); // NOI18N
MainTextArea.setColumns(20);
MainTextArea.setEditable(false);
MainTextArea.setRows(5);
MainTextArea.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getResourceMap(DesktopApplication3View.class);
MainTextArea.setDisabledTextColor(resourceMap.getColor("MainTextArea.disabledTextColor")); // NOI18N
MainTextArea.setDoubleBuffered(true);
MainTextArea.setDragEnabled(true);
MainTextArea.setName("MainTextArea"); // NOI18N
jScrollPane1.setViewportView(MainTextArea);
StringSearchTextArea.setText(resourceMap.getString("StringSearchTextArea.text")); // NOI18N
StringSearchTextArea.setName("StringSearchTextArea"); // NOI18N
StringSearchTextArea.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
StringSearchTextAreaMouseClicked(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
StringSearchTextAreaMouseClicked(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
StringSearchTextAreaMouseClicked(evt);
}
});
StringSearchTextArea.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
StringSearchTextAreaMouseClicked(evt);
}
});
SearchButton.setText(resourceMap.getString("SearchButton.text")); // NOI18N
SearchButton.setName("SearchButton"); // NOI18N
SearchButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
SearchForString(evt);
}
public void mousePressed(java.awt.event.MouseEvent evt) {
SearchForString(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt) {
SearchForString(evt);
}
});
StatusMessage.setBackground(resourceMap.getColor("StatusMessage.background")); // NOI18N
StatusMessage.setText(resourceMap.getString("StatusMessage.text")); // NOI18N
StatusMessage.setBorder(javax.swing.BorderFactory.createEtchedBorder());
StatusMessage.setName("StatusMessage"); // NOI18N
StatusLabel.setText(resourceMap.getString("StatusLabel.text")); // NOI18N
StatusLabel.setName("StatusLabel"); // NOI18N
StringSearchLabel.setText(resourceMap.getString("StringSearchLabel.text")); // NOI18N
StringSearchLabel.setName("StringSearchLabel"); // NOI18N
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.DEFAULT_SIZE, 791, Short.MAX_VALUE)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGap(1, 1, 1)
.addComponent(StringSearchLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(StringSearchTextArea, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(SearchButton)
.addGap(18, 18, 18)
.addComponent(StatusLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(StatusMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)))
.addContainerGap())
);
mainPanelLayout.setVerticalGroup(
mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 465, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(mainPanelLayout.createSequentialGroup()
.addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(StringSearchTextArea)
.addComponent(SearchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(StringSearchLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(StatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(87, 87, 87))
.addGroup(mainPanelLayout.createSequentialGroup()
.addComponent(StatusMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())))
);
menuBar.setName("menuBar"); // NOI18N
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getActionMap(DesktopApplication3View.class, this);
fileMenu.setAction(actionMap.get("OpenFileDialoge")); // NOI18N
fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
fileMenu.setName("fileMenu"); // NOI18N
exitMenuItem.setAction(actionMap.get("OpenFileDialoge")); // NOI18N
exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
exitMenuItem.setName("exitMenuItem"); // NOI18N
fileMenu.add(exitMenuItem);
exitMenuItem1.setAction(actionMap.get("quit")); // NOI18N
exitMenuItem1.setName("exitMenuItem1"); // NOI18N
fileMenu.add(exitMenuItem1);
menuBar.add(fileMenu);
helpMenu.setAction(actionMap.get("showHiglightBox")); // NOI18N
helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
helpMenu.setName("helpMenu"); // NOI18N
HiglightKeyMenuItem.setAction(actionMap.get("showHiglightBox")); // NOI18N
HiglightKeyMenuItem.setText(resourceMap.getString("HiglightKeyMenuItem.text")); // NOI18N
HiglightKeyMenuItem.setToolTipText(resourceMap.getString("HiglightKeyMenuItem.toolTipText")); // NOI18N
HiglightKeyMenuItem.setName("HiglightKeyMenuItem"); // NOI18N
helpMenu.add(HiglightKeyMenuItem);
aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
aboutMenuItem.setName("aboutMenuItem"); // NOI18N
helpMenu.add(aboutMenuItem);
menuBar.add(helpMenu);
Satus.setText(resourceMap.getString("Satus.text")); // NOI18N
Satus.setName("Satus"); // NOI18N
setComponent(mainPanel);
setMenuBar(menuBar);
}// </editor-fold>
private void SearchForString(java.awt.event.MouseEvent evt) {
int index, end, counter=0;
boolean loop = true;
String s = StringSearchTextArea.getText();
String content = MainTextArea.getText();
hilit.removeAllHighlights();
if (s.length() <= 0) {
StatusMessage.setText("Nothing to search");
return;
}
while(loop == true)
{
index = content.indexOf(s, counter);
if (index >= 0) { // match found
++counter;
try {
end = index + s.length();
hilit.addHighlight(index, end, painter);
}
catch (BadLocationException e) {
StatusMessage.setText("Error: " + e.getMessage());
}
loop = true;
StatusMessage.setText("'" + s + "' found.");
}
else if (index < 0){
loop = false;
if(counter == 0)StatusMessage.setText("'" + s + "' not found.");
}
}
}
private void StringSearchTextAreaMouseClicked(java.awt.event.MouseEvent evt) {
if (StringSearchTextArea.getText().equals("**Search is Case Sensitive**"))
{
StringSearchTextArea.setText(""); // clears the fiedl if equal to **Search is Case Sensitive**
}
}
public void DispalyFile() {
Runnable runnable = new Thread(){
@Override
public void run(){
FileInputStream fstream = null;
try {
fstream = new FileInputStream(selectedItem);
} catch (FileNotFoundException ex) {
Logger.getLogger(DesktopApplication3View.class.getName()).log(Level.SEVERE, null, ex);
}
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
try {
while ((strLine = br.readLine()) != null) {
//System.out.println(strLine);
MainTextArea.append(strLine+"\n"); // print out each line in the text file in jTextArea1
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
@Action
public void showAboutBox() {
if (aboutBox == null) {
JFrame mainFrame = DesktopApplication3.getApplication().getMainFrame();
aboutBox = new DesktopApplication3AboutBox(mainFrame);
aboutBox.setLocationRelativeTo(mainFrame);
}
DesktopApplication3.getApplication().show(aboutBox);
}
@Action
public void showHiglightBox() {
if (HighlightBox == null) {
JFrame mainFrame = DesktopApplication3.getApplication().getMainFrame();
HighlightBox = new DesktopApplication3HighlightKey(mainFrame);
HighlightBox.setLocationRelativeTo(mainFrame);
}
DesktopApplication3.getApplication().show(HighlightBox);
}
@Action
public void OpenFileDialoge() {
Frame parent = new Frame();
FileDialog fd = new FileDialog(parent, "Please choose a file:",FileDialog.LOAD);
fd.show();
selectedItem = fd.getDirectory() + fd.getFile(); // get the full directory of the file
DispalyFile();
}
// Variables declaration - do not modify
private javax.swing.JTextArea MainTextArea;
private javax.swing.JLabel Satus;
private javax.swing.JButton SearchButton;
private javax.swing.JLabel StatusLabel;
private javax.swing.JLabel StatusMessage;
private javax.swing.JLabel StringSearchLabel;
private javax.swing.JTextField StringSearchTextArea;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JPanel mainPanel;
private javax.swing.JMenuBar menuBar;
// End of variables declaration
final Highlighter hilit;
final Highlighter.HighlightPainter painter;
final static Color HILIT_COLOR = Color.YELLOW;
private JDialog HighlightBox;
private JDialog aboutBox;
}