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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*******************************************************************************
* Copyright (c) 2015 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public
* License v3.0 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma.gui.dialog;
import cuchaz.enigma.gui.util.GuiUtil;
import cuchaz.enigma.utils.I18n;
import cuchaz.enigma.gui.util.ScaleUtil;
import javax.swing.*;
import javax.swing.text.html.HTML;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class JavadocDialog {
private static JavadocDialog instance = null;
private JFrame frame;
private JavadocDialog(JFrame parent, JTextArea text, Callback callback) {
// init frame
frame = new JFrame(I18n.translate("javadocs.edit"));
final Container pane = frame.getContentPane();
pane.setLayout(new BorderLayout());
// editor panel
text.setTabSize(2);
pane.add(new JScrollPane(text), BorderLayout.CENTER);
text.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.VK_ENTER:
if (event.isControlDown())
callback.closeUi(frame, true);
break;
case KeyEvent.VK_ESCAPE:
callback.closeUi(frame, false);
break;
default:
break;
}
}
});
// buttons panel
JPanel buttonsPanel = new JPanel();
FlowLayout buttonsLayout = new FlowLayout();
buttonsLayout.setAlignment(FlowLayout.RIGHT);
buttonsPanel.setLayout(buttonsLayout);
buttonsPanel.add(GuiUtil.unboldLabel(new JLabel(I18n.translate("javadocs.instruction"))));
JButton cancelButton = new JButton(I18n.translate("javadocs.cancel"));
cancelButton.addActionListener(event -> {
// close (hide) the dialog
callback.closeUi(frame, false);
});
buttonsPanel.add(cancelButton);
JButton saveButton = new JButton(I18n.translate("javadocs.save"));
saveButton.addActionListener(event -> {
// exit enigma
callback.closeUi(frame, true);
});
buttonsPanel.add(saveButton);
pane.add(buttonsPanel, BorderLayout.SOUTH);
// tags panel
JMenuBar tagsMenu = new JMenuBar();
// add javadoc tags
for (JavadocTag tag : JavadocTag.values()) {
JButton tagButton = new JButton(tag.getText());
tagButton.addActionListener(action -> {
boolean textSelected = text.getSelectedText() != null;
String tagText = tag.isInline() ? "{" + tag.getText() + " }" : tag.getText() + " ";
if (textSelected) {
if (tag.isInline()) {
tagText = "{" + tag.getText() + " " + text.getSelectedText() + "}";
} else {
tagText = tag.getText() + " " + text.getSelectedText();
}
text.replaceSelection(tagText);
} else {
text.insert(tagText, text.getCaretPosition());
}
if (tag.isInline()) {
text.setCaretPosition(text.getCaretPosition() - 1);
}
text.grabFocus();
});
tagsMenu.add(tagButton);
}
// add html tags
JComboBox<String> htmlList = new JComboBox<String>();
htmlList.setPreferredSize(new Dimension());
for (HTML.Tag htmlTag : HTML.getAllTags()) {
htmlList.addItem(htmlTag.toString());
}
htmlList.addActionListener(action -> {
String tagText = "<" + htmlList.getSelectedItem().toString() + ">";
text.insert(tagText, text.getCaretPosition());
text.grabFocus();
});
tagsMenu.add(htmlList);
pane.add(tagsMenu, BorderLayout.NORTH);
// show the frame
frame.setSize(ScaleUtil.getDimension(600, 400));
frame.setLocationRelativeTo(parent);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void init(JFrame parent, JTextArea area, Callback callback) {
instance = new JavadocDialog(parent, area, callback);
instance.frame.doLayout();
instance.frame.setVisible(true);
}
public interface Callback {
void closeUi(JFrame frame, boolean save);
}
private enum JavadocTag {
CODE(true),
LINK(true),
LINKPLAIN(true),
RETURN(false),
SEE(false),
THROWS(false);
private boolean inline;
private JavadocTag(boolean inline) {
this.inline = inline;
}
public String getText() {
return "@" + this.name().toLowerCase();
}
public boolean isInline() {
return this.inline;
}
}
}
|