"Cześć, Amigo!"
„Witam, Kapitanie Wiewiórki, proszę pana!”
„Żołnierzu, mamy dla ciebie tajną misję. Opracujesz narzędzie do tworzenia stron internetowych”.
„Fajne, strony internetowe. Ale po co nam własny edytor?”
„Co masz na myśli mówiąc„ dlaczego ”? Żołnierzu, rozkazy nie podlegają dyskusji. Są przeznaczone do wykonania”.
"Tak proszę pana! Napisz narzędzie do tworzenia stron internetowych."
„Nie krzycz, żołnierzu. Czy nie mówili ci, że to ściśle tajna misja?”
„Skontaktuj się z agentem IntelliJ IDEA, aby rozpocząć. On cię wprowadzi w szczegóły”.
– Przekaże również wszelkie dalsze instrukcje.
– Czy mogę kontynuować, proszę pana?
"Przystępować."
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 1)
Today we'll write an HTML editor with a graphical user interface. We'll use Swing to
create the GUI. And we'll use the MVC pattern as the
architectural framework for our application.
1.1. Declare a Controller class and a View class.
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 2)
2.1. Add init() methods, empty for now, to the controller and view. They will
be responsible for initializing the controller and view.
2.2. Now we will write a main method in the Controller class. It must:
2.2.1. Create a view object.
2.2.2. Use the view to create a controller.
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 3)
The graphical interface will be a window with a menu and a pane with
two tabs.
The first tab will be a text pane that will render an HTML
page. Here it will be possible to format and edit the text of the page.
The second tab will have an editor that will display the page's HTML code.
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 4)
4.1. Declare initMenuBar() and initEditor() methods in the View class. They will be responsible for initializing the menus and editor panes.
4.2. Declare an initGui() method in the view. It will initialize the graphical interface.
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 5)
5.1. In the listeners package, declare a TabbedPaneChangeListener class that implements the ChangeListener
interface. This class will listen for and handle changes to the state of the tabbed pane.
32
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 6)
Implement the initEditor() method, which initializes the editor panes. It must:
6.1. Set "text/html" as the content type for the htmlTextPane component.
Find and use the appropriate method.
6.2. Create a new local JScrollPane component based on htmlTextPane.
32
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 7)
Add a MenuHelper class. This will be a helper class for initializing and configuring
the menu. The menu will have the following structure — File: New, Open, Save, Save as..., Exit; Edit: Undo, Redo, Cut, Copy, Paste.
32
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 8)
It's time to add all the necessary menu items and write classes for the actions that are performed when the menu items are selected. This is quite time-consuming, routine work. You're a great student and we wouldn't want to upset you. So, as a bonus, you're getting a ready-made MenuHelper class!
32
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 9)
9.1. Implement the initMenuBar() method. It must:
9.1.1. Create a new JMenuBar object. This will be our menu bar.
9.1.2. Use MenuHelper to initialize the menu in the following order: File, Edit, Style, Align, Color, Font, and Help.
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 10)
Our editor will support undo/redo of actions
performed in the editor.
Implement the UndoMenuListener class. This listener will listen to the menu, or more accurately,
it will listen for the moment when the edit menu is selected by the user.
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 11)
11.1. Add an UndoManager undoManager field to the view. Figure out what
this class is used for. Initialize the field with a new object.
11.2. In the listeners package, add an UndoListener class that implements the
UndoableEditListener interface.
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 12)
12.1. Implement the RedoAction class:
12.1.1. Add a View field to the class. Add its initialization in the constructor.
12.1.2. Implement the actionPerformed(ActionEvent actionEvent) method. It should call the redo() method on the view.
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 13)
Let's implement the TextEditMenuListener class in the listeners package. This class will work like the UndoMenuListener class, but for other menu items. The menu items responsible for style, font, color, etc. should only be available when the first tab is selected in our editor.
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 14)
14.1. Add a selectHtmlTab() method to the view class. It must:
14.1.1. Select the HTML tab (switch to it).
14.1.2. Reset all edits using the method you previously implemented.
14.2. Add a getter for the model to the controller class. In our case, this is the document field.
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 15)
Add a resetDocument() method to the controller. It should reset the current document. It must: 15.1. Remove the undo/redo edit listener from the current document (find the appropriate method inherited from AbstractDocument). You need to ask the view for the listener (getUndoListener() method).
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 16)
Add a setPlainText(String text) method to the controller. It will write the passed text with
HTML tags to document. Implement it as follows:
16.1. Reset the document.
16.2. Create a new StringReader based on the passed text.
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 17)
Add a String getPlainText() method to the controller. It should get text from the document with
all the HTML tags.
17.1. Create a StringWriter object.
17.2. Copy all of the document's contents to the created object using
the HTMLEditorKit class's write method.
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 18)
Implement the view's selectedTabChanged() method. This method is called when the selected
tab changes. Let's begin:
18.1. The method should check which tab is currently selected.
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 19)
Implement the actionPerformed(ActionEvent actionEvent) method on the view. This method is
inherited from the ActionListener interface and will be called upon selection of menu items
that our view has been added to as an event listener.
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 20)
20.1. Implement the createNewDocument() method in the controller. It
must:
20.1.1. Select the HTML tab on the view.
20.1.2. Reset the current document.
20.1.3. Set a new window title, e.g. "HTML editor". Use the
setTitle() method, which is inherited by our view.
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 21)
To open and save the file, we'll use JFileChooser from the swing library.
Objects of this type support filters that inherit FileFilter. Now we'll write
our own filter:
21.1. Create a public HTMLFileFilter class that inherits FileFilter.
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 22)
In the controller, let's implement the saveDocumentAs() method for saving the file under a new name.
The implementation should:
22.1. Switch the view to the HTML tab.
22.2. Create a new JFileChooser object.
22.3. Set an HTMLFileFilter object as its filter.
16
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 23)
23.1. Let's write the saveDocument() method for saving the current file. The method should
work the same as saveDocumentAs(), but not ask the user for a file. Instead, it should use
currentFile. If currentFile is null, then call the saveDocumentAs() method.
9
Zadanie
Java Multithreading, poziom 8, lekcja 15
HTML Editor (part 24)
Your HTML editor is ready!
You learned how to:
- Create applications with a graphical interface.
- Work with dialog boxes.
- Use classes from the Swing library.
- Implement interaction between program components with the help of events, listeners,
and actions.