AJP Microproject
AJP Microproject
COMPUTER ENGINEERING
Department Academic year: 2023-24
Microproject on
TEXT-EDITOR
Program Name: Computer Engineering Program
Code: CO5I
Course Name: Advanced Java Programming
Code: 22517
2. Sumit Nilvarn
3. Rohit Jadhav
1|Page
MAHARASHTRA STATE
BOARD OF TECHNICAL EDUCATION
Certificate
This is to certify that Mr. OMKAR DAHIWAL, SUMIT NILVARN, ROHIT JADHAV
Roll No. 341,334,338 of 5th Semester of Diploma in COMPUTER ENGINEERING of
Institute, GOVERNMENT POLYTECHNIC, JINTUR (0094) has completed the
Micro Project satisfactorily in Subject- AJP for the academic year 2023-24 as
prescribed in the curriculum.
Place : JINTUR
Date :
ANEXURE 1
2|Page
Evaluation Sheet for the Micro Project
Academic Year: 2023-24. Name of the Faculty: S.G. Munde
Course: AJP Course Code: 22517 Semester: V
Title of the Project: TEXT-EDITOR
(A)Practical outcome:
1) WAP to demonstrate use of components like
Label,TextField,TextArea,Button,Checkbox,RadioButton.
2)WAP to design a form using the components List and Choice .
Roll Student Name Marks out of 6 for Marks out of 4 for Total out
No. performance in group performance in of 10
activity oral/presentation
(D5 Col.8) (D5 Col.9)
(Signature of Faculty)
3|Page
“ TEXT -EDITOR”
WEEK ACTIVITY PERFORMED DATE SIGN OF
GUIDE
341,338,334-
CONTENTS
4|Page
Sr. Title Page No.
No.
1 INTRODUCTION 06
2 PROGRAM 07 to 10
3 OUTPUT 11
4 REFERENCES & SOURCE USED 12
5|Page
INTRODUCTION
6|Page
PROGRAM
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.FocusListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;
import javax.swing.SwingConstants;
import javax.swing.BoxLayout;
import javax.swing.UIManager;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.AbstractDocument;
import javax.swing.text.ElementIterator;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Element;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledEditorKit.AlignmentAction;
import javax.swing.text.StyledEditorKit.FontSizeAction;
import javax.swing.text.StyledEditorKit.FontFamilyAction;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.border.LineBorder;
import java.util.List;
import java.util.Vector;
import java.util.Arrays;
import java.util.Random;
import
7|Page
java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class MyEditor {
private JFrame frame__;
private JTextPane editor__;
private JComboBox<String> fontSizeComboBox__;
private JComboBox<String> textAlignComboBox__;
private JComboBox<String> fontFamilyComboBox__;
private String pictureButtonName__;
private File file__;
private static final String MAIN_TITLE = "My Editor - ";
private static final String DEFAULT_FONT_FAMILY = "SansSerif";
private static final int DEFAULT_FONT_SIZE = 18;
private static final List<String> FONT_LIST = Arrays.asList(new String [] {"Arial", "Calibri",
"Cambria", "Courier New", "Comic Sans MS", "Dialog", "Georgia", "Helevetica", "Lucida Sans",
"Monospaced", "Tahoma", "Times New Roman", "Verdana"});
private static final String [] FONT_SIZES = {"Font Size", "12", "14", "16", "18", "20", "22", "24",
"26", "28", "30"};
private static final String [] TEXT_ALIGNMENTS = {"Text Align", "Left", "Center", "Right",
"Justified"};
private static final String ELEM = AbstractDocument.ElementNameAttribute;
private static final String COMP = StyleConstants.ComponentElementName;
public static void main(String [] args)
throws Exception {
UIManager.put("TextPane.font",
new Font(DEFAULT_FONT_FAMILY, Font.PLAIN, DEFAULT_FONT_SIZE));
UIManager.setLookAndFeel(new NimbusLookAndFeel());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MyEditor().createAndShowGUI();
}
});
}
private void createAndShowGUI() {
frame__ = new JFrame();
setFrameTitleWithExtn("New file");
8|Page
editor__ = new JTextPane();
JScrollPane editorScrollPane = new JScrollPane(editor__);
editor__.setDocument(getNewDocument());
new EditButtonActionListener();
JButton colorButton = new JButton("Set Color");
colorButton.addActionListener(new ColorActionListener());
textAlignComboBox__ = new JComboBox<String>(TEXT_ALIGNMENTS);
textAlignComboBox__.setEditable(false);
textAlignComboBox__.addItemListener(new TextAlignItemListener());
fontSizeComboBox__ = new JComboBox<String>(FONT_SIZES);
fontSizeComboBox__.setEditable(false);
fontSizeComboBox__.addItemListener(new FontSizeItemListener());
Vector<String> editorFonts = getEditorFonts();
editorFonts.add(0, "Font Family");
fontFamilyComboBox__ = new JComboBox<String>(editorFonts);
fontFamilyComboBox__.setEditable(false);
fontFamilyComboBox__.addItemListener(new FontFamilyItemListener());
JButton insertPictureButton = new JButton("Picture Insert");
insertPictureButton.addActionListener(new PictureInsertActionListener());
JButton deletePictureButton = new JButton("Picture Delete");
deletePictureButton.addActionListener(new PictureDeleteActionListener());
JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel1.add(new JSeparator(SwingConstants.VERTICAL));
panel1.add(new JSeparator(SwingConstants.VERTICAL));
panel1.add(colorButton);
panel1.add(new JSeparator(SwingConstants.VERTICAL));
panel1.add(textAlignComboBox__);
panel1.add(new JSeparator(SwingConstants.VERTICAL));
panel1.add(fontSizeComboBox__);
panel1.add(new JSeparator(SwingConstants.VERTICAL));
panel1.add(fontFamilyComboBox__);
JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel2.add(insertPictureButton);
panel2.add(deletePictureButton);
panel2.add(new JSeparator(SwingConstants.VERTICAL));
panel2.add(new JSeparator(SwingConstants.VERTICAL));
panel2.add(new JSeparator(SwingConstants.VERTICAL));
JPanel toolBarPanel = new JPanel();
toolBarPanel.setLayout(new BoxLayout(toolBarPanel, BoxLayout.PAGE_AXIS));
toolBarPanel.add(panel1);
toolBarPanel.add(panel2);
frame__.add(toolBarPanel, BorderLayout.NORTH);
frame__.add(editorScrollPane, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
9|Page
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem newItem = new JMenuItem("New");
newItem.setMnemonic(KeyEvent.VK_N);
newItem.addActionListener(new NewFileListener());
JMenuItem openItem = new JMenuItem("Open...");
openItem.setMnemonic(KeyEvent.VK_O);
openItem.addActionListener(new OpenFileListener());
JMenuItem saveItem = new JMenuItem("Save (...)");
saveItem.setMnemonic(KeyEvent.VK_S);
saveItem.addActionListener(new SaveFileListener());
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.setMnemonic(KeyEvent.VK_X);
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
fileMenu.add(newItem);
fileMenu.addSeparator();
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
menuBar.add(fileMenu);
frame__.setJMenuBar(menuBar);
frame__.setSize(900, 500);
frame__.setLocation(150, 80);
frame__.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame__.setVisible(true);
editor__.requestFocusInWindow();
}
private void setFrameTitleWithExtn(String titleExtn) {
frame__.setTitle(MAIN_TITLE + titleExtn);
}
private StyledDocument getNewDocument() {
StyledDocument doc = new DefaultStyledDocument();
return doc;
}
private Vector<String> getEditorFonts() {
String [] availableFonts =
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
Vector<String> returnList = new Vector<>();
10 | P a g e
for (String font : availableFonts) {
if (FONT_LIST.contains(font)) {
returnList.add(font);
}
}
return returnList;
}
private class EditButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
editor__.requestFocusInWindow();
}
}
private class ColorActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Color newColor =
JColorChooser.showDialog(frame__, "Choose a color", Color.BLACK);
if (newColor == null) {
editor__.requestFocusInWindow();
return;
}
SimpleAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, newColor);
editor__.setCharacterAttributes(attr, false);
editor__.requestFocusInWindow();
}
}
private class TextAlignItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() != ItemEvent.SELECTED) ||
(textAlignComboBox__.getSelectedIndex() == 0)) {
return;
}
String alignmentStr = (String) e.getItem();
int newAlignment = textAlignComboBox__.getSelectedIndex() - 1;
textAlignComboBox__.setAction(new AlignmentAction(alignmentStr, newAlignment));
textAlignComboBox__.setSelectedIndex(0); // initialize to (default) select
editor__.requestFocusInWindow();
}
}
private class FontSizeItemListener implements ItemListener {
@Override
11 | P a g e
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() != ItemEvent.SELECTED) ||
(fontSizeComboBox__.getSelectedIndex() == 0)) {
return;
}
String fontSizeStr = (String) e.getItem();
int newFontSize = 0;
try {
newFontSize = Integer.parseInt(fontSizeStr);
}
catch (NumberFormatException ex) {
return;
}
fontSizeComboBox__.setAction(new FontSizeAction(fontSizeStr, newFontSize));
fontSizeComboBox__.setSelectedIndex(0); // initialize to (default) select
editor__.requestFocusInWindow();
}
}
private class FontFamilyItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() != ItemEvent.SELECTED) ||
(fontFamilyComboBox__.getSelectedIndex() == 0)) {
return;
}
String fontFamily = (String) e.getItem();
fontFamilyComboBox__.setAction(new FontFamilyAction(fontFamily, fontFamily));
fontFamilyComboBox__.setSelectedIndex(0); // initialize to (default) select
editor__.requestFocusInWindow();
}
}
private class PictureInsertActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
File pictureFile = choosePictureFile();
if (pictureFile == null) {
editor__.requestFocusInWindow();
return;
}
ImageIcon icon = new ImageIcon(pictureFile.toString());
JButton picButton = new JButton(icon);
picButton.setBorder(new LineBorder(Color.WHITE));
picButton.setMargin(new Insets(0,0,0,0));
picButton.setAlignmentY(.9f);
12 | P a g e
picButton.setAlignmentX(.9f);
picButton.addFocusListener(new PictureFocusListener());
picButton.setName("PICTURE_ID_" + new Random().nextInt());
editor__.insertComponent(picButton);
editor__.requestFocusInWindow();
}
private File choosePictureFile() {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"PNG, JPG & GIF Images", "png", "jpg", "gif");
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(frame__) == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
}
else {
return null;
}
}
}
private class PictureFocusListener implements FocusListener {
@Override
public void focusGained(FocusEvent e) {
JButton button = (JButton) e.getComponent();
button.setBorder(new LineBorder(Color.GRAY));
pictureButtonName__ = button.getName();
}
@Override
public void focusLost(FocusEvent e) {
((JButton) e.getComponent()).setBorder(new LineBorder(Color.WHITE));
}
}
private class PictureDeleteActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
StyledDocument doc = getEditorDocument();
ElementIterator iterator = new ElementIterator(doc);
Element element;
while ((element = iterator.next()) != null) {
AttributeSet attrs = element.getAttributes();
if (attrs.containsAttribute(ELEM, COMP)) {
JButton button = (JButton) StyleConstants.getComponent(attrs);
if (button.getName().equals(pictureButtonName__)) {
try {
doc.remove(element.getStartOffset(), 1); // length = 1
13 | P a g e
}
catch (BadLocationException ex_) {
throw new RuntimeException(ex_);
}
}
}
}
editor__.requestFocusInWindow();
pictureButtonName__ = null;
}
}
private StyledDocument getEditorDocument() {
StyledDocument doc = (DefaultStyledDocument) editor__.getDocument();
return doc;
}
private char getParaFirstCharacter(int paraEleStart) {
String firstChar = "";
try {
firstChar = editor__.getText(paraEleStart, 1);
}
catch (BadLocationException ex) {
throw new RuntimeException(ex);
}
return firstChar.charAt(0);
}
private void doLeftArrowKeyRoutine(int pos, boolean startTextPos) {
if (!startTextPos) {
return;
}
Element paraEle =
getEditorDocument().getParagraphElement(editor__.getCaretPosition());
int newPos = (paraEle.getStartOffset() == 0) ? 0 : pos;
SwingUtilities.invokeLater(new Runnable() {
public void run() {
editor__.setCaretPosition(newPos);
}
});
}
private class NewFileListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
initEditorAttributes();
editor__.setDocument(getNewDocument());
file__ = null;
14 | P a g e
setFrameTitleWithExtn("New file");
}
private void initEditorAttributes() {
AttributeSet attrs1 = editor__.getCharacterAttributes();
SimpleAttributeSet attrs2 = new SimpleAttributeSet(attrs1);
attrs2.removeAttributes(attrs1);
editor__.setCharacterAttributes(attrs2, true);
}
}
private class OpenFileListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
file__ = chooseFile();
if (file__ == null) {
return;
}
readFile(file__);
setFrameTitleWithExtn(file__.getName());
}
private File chooseFile() {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(frame__) == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
}
else {
return null;
}
}
private void readFile(File file) {
StyledDocument doc = null;
try (InputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis)) {
doc = (DefaultStyledDocument) ois.readObject();
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(frame__, "Input file was not found!");
return;
} catch (ClassNotFoundException | IOException ex) {
throw new RuntimeException(ex);
}
editor__.setDocument(doc);
applyFocusListenerToPictures(doc);
}
private void applyFocusListenerToPictures(StyledDocument doc) {
ElementIterator iterator = new ElementIterator(doc);
15 | P a g e
Element element;
while ((element = iterator.next()) != null) {
AttributeSet attrs = element.getAttributes();
if (attrs.containsAttribute(ELEM, COMP)) {
JButton picButton = (JButton) StyleConstants.getComponent(attrs);
picButton.addFocusListener(new PictureFocusListener());
}
}
}
}
private class SaveFileListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (file__ == null) {
file__ = chooseFile();
if (file__ == null) {
return;
}
}
DefaultStyledDocument doc = (DefaultStyledDocument) getEditorDocument();
try (OutputStream fos = new FileOutputStream(file__);
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(doc);
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
setFrameTitleWithExtn(file__.getName());
}
private File chooseFile() {
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(frame__) == JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile();
}
else {
return null;
}
}
}
}
16 | P a g e
OUTPUT
17 | P a g e
• REFERENCES
https://www.tutorialspoint.com/
https://www.geeksforgeeks.org/ https://www.javatpoint.com/
• SOURCE USED
(1) Laptop
(2) Mobile
(3) Internet
(4) Reference Book
18 | P a g e