-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuHelper.java
132 lines (103 loc) · 5.8 KB
/
MenuHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.javarush.task.task32.task3209;
import com.javarush.task.task32.task3209.actions.*;
import com.javarush.task.task32.task3209.listeners.TextEditMenuListener;
import com.javarush.task.task32.task3209.listeners.UndoMenuListener;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import java.awt.*;
import java.awt.event.ActionListener;
public class MenuHelper {
public static JMenuItem addMenuItem(JMenu parent, String text, ActionListener actionListener) {
JMenuItem menuItem = new JMenuItem(text);
menuItem.addActionListener(actionListener);
parent.add(menuItem);
return menuItem;
}
public static JMenuItem addMenuItem(JMenu parent, String text, Action action) {
JMenuItem menuItem = addMenuItem(parent, action);
menuItem.setText(text);
return menuItem;
}
public static JMenuItem addMenuItem(JMenu parent, Action action) {
JMenuItem menuItem = new JMenuItem(action);
parent.add(menuItem);
return menuItem;
}
public static void initHelpMenu(View view, JMenuBar menuBar) {
JMenu helpMenu = new JMenu("Помощь");
menuBar.add(helpMenu);
addMenuItem(helpMenu, "О программе", view);
}
public static void initFontMenu(View view, JMenuBar menuBar) {
JMenu fontMenu = new JMenu("Шрифт");
menuBar.add(fontMenu);
JMenu fontTypeMenu = new JMenu("Шрифт");
fontMenu.add(fontTypeMenu);
String[] fontTypes = {Font.SANS_SERIF, Font.SERIF, Font.MONOSPACED, Font.DIALOG, Font.DIALOG_INPUT};
for (String fontType : fontTypes) {
addMenuItem(fontTypeMenu, fontType, new StyledEditorKit.FontFamilyAction(fontType, fontType));
}
JMenu fontSizeMenu = new JMenu("Размер шрифта");
fontMenu.add(fontSizeMenu);
String[] fontSizes = {"6", "8", "10", "12", "14", "16", "20", "24", "32", "36", "48", "72"};
for (String fontSize : fontSizes) {
addMenuItem(fontSizeMenu, fontSize, new StyledEditorKit.FontSizeAction(fontSize, Integer.parseInt(fontSize)));
}
fontMenu.addMenuListener(new TextEditMenuListener(view));
}
public static void initColorMenu(View view, JMenuBar menuBar) {
JMenu colorMenu = new JMenu("Цвет");
menuBar.add(colorMenu);
addMenuItem(colorMenu, new StyledEditorKit.ForegroundAction("Красный", Color.red));
addMenuItem(colorMenu, new StyledEditorKit.ForegroundAction("Оранжевый", Color.orange));
addMenuItem(colorMenu, new StyledEditorKit.ForegroundAction("Желтый", Color.yellow));
addMenuItem(colorMenu, new StyledEditorKit.ForegroundAction("Зеленый", Color.green));
addMenuItem(colorMenu, new StyledEditorKit.ForegroundAction("Синий", Color.blue));
addMenuItem(colorMenu, new StyledEditorKit.ForegroundAction("Голубой", Color.cyan));
addMenuItem(colorMenu, new StyledEditorKit.ForegroundAction("Пурпурный", Color.magenta));
addMenuItem(colorMenu, new StyledEditorKit.ForegroundAction("Черный", Color.black));
colorMenu.addMenuListener(new TextEditMenuListener(view));
}
public static void initAlignMenu(View view, JMenuBar menuBar) {
JMenu alignMenu = new JMenu("Выравнивание");
menuBar.add(alignMenu);
addMenuItem(alignMenu, new StyledEditorKit.AlignmentAction("По левому краю", StyleConstants.ALIGN_LEFT));
addMenuItem(alignMenu, new StyledEditorKit.AlignmentAction("По центру", StyleConstants.ALIGN_CENTER));
addMenuItem(alignMenu, new StyledEditorKit.AlignmentAction("По правому краю", StyleConstants.ALIGN_RIGHT));
alignMenu.addMenuListener(new TextEditMenuListener(view));
}
public static void initStyleMenu(View view, JMenuBar menuBar) {
JMenu styleMenu = new JMenu("Стиль");
menuBar.add(styleMenu);
addMenuItem(styleMenu, "Полужирный", new StyledEditorKit.BoldAction());
addMenuItem(styleMenu, "Подчеркнутый", new StyledEditorKit.UnderlineAction());
addMenuItem(styleMenu, "Курсив", new StyledEditorKit.ItalicAction());
styleMenu.addSeparator();
addMenuItem(styleMenu, "Подстрочный знак", new SubscriptAction());
addMenuItem(styleMenu, "Надстрочный знак", new SuperscriptAction());
addMenuItem(styleMenu, "Зачеркнутый", new StrikeThroughAction());
styleMenu.addMenuListener(new TextEditMenuListener(view));
}
public static void initEditMenu(View view, JMenuBar menuBar) {
JMenu editMenu = new JMenu("Редактировать");
menuBar.add(editMenu);
JMenuItem undoItem = addMenuItem(editMenu, "Отменить", new UndoAction(view));
JMenuItem redoItem = addMenuItem(editMenu, "Вернуть", new RedoAction(view));
addMenuItem(editMenu, "Вырезать", new DefaultEditorKit.CutAction());
addMenuItem(editMenu, "Копировать", new DefaultEditorKit.CopyAction());
addMenuItem(editMenu, "Вставить", new DefaultEditorKit.PasteAction());
editMenu.addMenuListener(new UndoMenuListener(view, undoItem, redoItem));
}
public static void initFileMenu(View view, JMenuBar menuBar) {
JMenu fileMenu = new JMenu("Файл");
menuBar.add(fileMenu);
addMenuItem(fileMenu, "Новый", view);
addMenuItem(fileMenu, "Открыть", view);
addMenuItem(fileMenu, "Сохранить", view);
addMenuItem(fileMenu, "Сохранить как...", view);
fileMenu.addSeparator();
addMenuItem(fileMenu, "Выход", view);
}
}