Repository navigation
Expand file tree
/
Copy pathMultiSelectDialog.java
More file actions
413 lines (363 loc) · 15.5 KB
/
Copy pathMultiSelectDialog.java
File metadata and controls
413 lines (363 loc) · 15.5 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Copyright 2014-2015 Wesley Lin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ui;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.wm.IdeFrame;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.ui.BrowserHyperlinkListener;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.mac.foundation.MacUtil;
import com.intellij.util.Alarm;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.html.HTMLEditorKit;
import data.Log;
import data.StorageDataKey;
import language_engine.TranslationEngineType;
import module.SupportedLanguages;
/**
* Created by Wesley Lin on 11/29/14.
* 选择要翻译的语言,和是否覆盖翻译
*/
public class MultiSelectDialog extends DialogWrapper {
public static final double GOLDEN_RATIO = 0.618;
public static final double REVERSE_GOLDEN_RATIO = 1 - GOLDEN_RATIO;
public interface OnOKClickedListener {
void onClick(List<SupportedLanguages> selectedLanguages, boolean overrideChecked, boolean inputChecked, boolean outputChecked);
}
private PropertiesComponent propertiesComponent;
protected String mPrompt;
private MyBorderLayout myLayout;
private JCheckBox overrideCheckBox;
private JCheckBox inputCheckBox;
private JCheckBox outputCheckBox;
private String overrideCheckboxText;
private String inputBoxText;
private String outputBoxText;
private boolean overrideCheckedStatus;
/**
* 可以翻译的语种
*/
private List<SupportedLanguages> translatableLanguage;
private List<SupportedLanguages> selectedLanguages = new ArrayList<SupportedLanguages>();
private OnOKClickedListener onOKClickedListener;
public void setOnOKClickedListener(OnOKClickedListener onOKClickedListener) {
this.onOKClickedListener = onOKClickedListener;
}
/**
* @param project
* @param prompt 提示
* @param title 窗口标题
* @param overrideCheckBoxText 覆盖翻译选择框的标题
* @param overrideCheckboxStatus 覆盖翻译选择框的状态
* @param inputBoxText 导入box的标题
* @param outputBoxText 导出box的标题
* @param translationEngineType 翻译引擎的种类,默认是google翻译
* @param canBeParent
*/
public MultiSelectDialog(@Nullable Project project,
String prompt,
String title,
@Nullable String overrideCheckBoxText,
boolean overrideCheckboxStatus,
String inputBoxText,
String outputBoxText,
TranslationEngineType translationEngineType,
boolean canBeParent) {
super(project, canBeParent);
translatableLanguage = SupportedLanguages.getAllSupportedLanguages(translationEngineType);
setTitle(title);
if (Messages.isMacSheetEmulation()) {
setUndecorated(true);
}
propertiesComponent = PropertiesComponent.getInstance(project);
mPrompt = prompt;
overrideCheckboxText = overrideCheckBoxText;
overrideCheckedStatus = overrideCheckboxStatus;
this.inputBoxText = inputBoxText;
this.outputBoxText = outputBoxText;
setButtonsAlignment(SwingConstants.RIGHT);
setDoNotAskOption(null);
init();
if (Messages.isMacSheetEmulation()) {
MacUtil.adjustFocusTraversal(myDisposable);
}
}
@Override
protected void doOKAction() {
super.doOKAction();
if (onOKClickedListener != null) {
Log.i("selected Translation Languages: " + selectedLanguages.toString());
onOKClickedListener.onClick(selectedLanguages,
overrideCheckBox.isSelected(),
inputCheckBox.isSelected(),
outputCheckBox.isSelected());
}
}
@NotNull
@Override
protected Action[] createActions() {
Action[] actions;
if (SystemInfo.isMac) {
actions = new Action[]{myCancelAction, myOKAction};
} else {
actions = new Action[]{myOKAction, myCancelAction};
}
return actions;
}
@Override
public void doCancelAction() {
close(-1);
}
@Override
protected JComponent createCenterPanel() {
return doCreateCenterPanel();
}
@NotNull
LayoutManager createRootLayout() {
return Messages.isMacSheetEmulation() ? myLayout = new MyBorderLayout() : new BorderLayout();
}
@Override
protected void dispose() {
if (Messages.isMacSheetEmulation()) {
animate();
} else {
super.dispose();
}
}
@Override
public void show() {
if (Messages.isMacSheetEmulation()) {
setInitialLocationCallback(new Computable<Point>() {
@Override
public Point compute() {
JRootPane rootPane = SwingUtilities.getRootPane(getWindow().getParent());
if (rootPane == null) {
rootPane = SwingUtilities.getRootPane(getWindow().getOwner());
}
Point p = rootPane.getLocationOnScreen();
p.x += (rootPane.getWidth() - getWindow().getWidth()) / 2;
return p;
}
});
animate();
if (SystemInfo.isJavaVersionAtLeast("1.7")) {
try {
Method method = Class.forName("java.awt.Window").getDeclaredMethod("setOpacity", float.class);
if (method != null) method.invoke(getPeer().getWindow(), .8f);
} catch (Exception exception) {
}
}
setAutoAdjustable(false);
setSize(getPreferredSize().width, 0);//initial state before animation, zero height
}
super.show();
}
private void animate() {
final int height = getPreferredSize().height;
final int frameCount = 10;
final boolean toClose = isShowing();
final AtomicInteger i = new AtomicInteger(-1);
final Alarm animator = new Alarm(myDisposable);
final Runnable runnable = new Runnable() {
@Override
public void run() {
int state = i.addAndGet(1);
double linearProgress = (double) state / frameCount;
if (toClose) {
linearProgress = 1 - linearProgress;
}
myLayout.myPhase = (1 - Math.cos(Math.PI * linearProgress)) / 2;
Window window = getPeer().getWindow();
Rectangle bounds = window.getBounds();
bounds.height = (int) (height * myLayout.myPhase);
window.setBounds(bounds);
if (state == 0 && !toClose && window.getOwner() instanceof IdeFrame) {
WindowManager.getInstance().requestUserAttention((IdeFrame) window.getOwner(), true);
}
if (state < frameCount) {
animator.addRequest(this, 10);
} else if (toClose) {
MultiSelectDialog.super.dispose();
}
}
};
animator.addRequest(runnable, 10, ModalityState.stateForComponent(getRootPane()));
}
protected JComponent doCreateCenterPanel() {
JPanel panel = new JPanel(new BorderLayout(15, 0));
if (mPrompt != null) {
final JTextPane messageComponent = createMessageComponent(mPrompt);
final Dimension screenSize = messageComponent.getToolkit().getScreenSize();
final Dimension textSize = messageComponent.getPreferredSize();
if (mPrompt.length() > 100) {
final JScrollPane pane = ScrollPaneFactory.createScrollPane(messageComponent);
pane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
final int scrollSize = (int) new JScrollBar(Adjustable.VERTICAL).getPreferredSize().getWidth() + 12;
final Dimension preferredSize =
new Dimension(Math.min(textSize.width, (int) (screenSize.width * REVERSE_GOLDEN_RATIO)) + scrollSize,
Math.min(textSize.height, screenSize.height / 3) + scrollSize);
pane.setPreferredSize(preferredSize);
panel.add(pane, BorderLayout.NORTH);
} else {
panel.add(messageComponent, BorderLayout.NORTH);
}
}
if (!translatableLanguage.isEmpty()) {
Container container = new Container();
int gridCol = 2;
int gridRow = (translatableLanguage.size() % gridCol == 0) ? translatableLanguage.size() / gridCol : translatableLanguage.size() / gridCol + 1;
container.setLayout(new GridLayout(gridRow, gridCol));
for (final SupportedLanguages language : translatableLanguage) {
JCheckBox checkbox = new JCheckBox(language.getLanguageEnglishDisplayName()
+ " (" + language.getLanguageDisplayName() + ") ");
checkbox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (!selectedLanguages.contains(language)) {
selectedLanguages.add(language);
}
} else if (e.getStateChange() == ItemEvent.DESELECTED) {
if (selectedLanguages.contains(language)) {
selectedLanguages.remove(language);
}
}
}
});
checkbox.setSelected(
propertiesComponent.getBoolean(StorageDataKey.SupportedLanguageCheckStatusPrefix + language.getLanguageCode(), false));
container.add(checkbox);
}
if (inputBoxText != null) {
inputCheckBox = new JCheckBox(inputBoxText);
container.add(inputCheckBox);
}
if (outputBoxText != null) {
outputCheckBox = new JCheckBox(outputBoxText);
container.add(outputCheckBox);
}
panel.add(container, BorderLayout.CENTER);
}
if (overrideCheckboxText != null) {
overrideCheckBox = new JCheckBox(overrideCheckboxText);
overrideCheckBox.setSelected(overrideCheckedStatus);
overrideCheckBox.setMargin(new Insets(2, -4, 0, 0));
panel.add(overrideCheckBox, BorderLayout.SOUTH);
}
return panel;
}
protected static JTextPane createMessageComponent(final String message) {
final JTextPane messageComponent = new JTextPane();
return configureMessagePaneUi(messageComponent, message);
}
@Override
protected void doHelpAction() {
// do nothing
}
@NotNull
public static JTextPane configureMessagePaneUi(JTextPane messageComponent, String message) {
return configureMessagePaneUi(messageComponent, message, true);
}
@NotNull
public static JTextPane configureMessagePaneUi(JTextPane messageComponent,
String message,
final boolean addBrowserHyperlinkListener) {
messageComponent.setFont(UIUtil.getLabelFont());
if (BasicHTML.isHTMLString(message)) {
final HTMLEditorKit editorKit = new HTMLEditorKit();
editorKit.getStyleSheet().addRule(UIUtil.displayPropertiesToCSS(UIUtil.getLabelFont(), UIUtil.getLabelForeground()));
messageComponent.setEditorKit(editorKit);
messageComponent.setContentType(UIUtil.HTML_MIME);
if (addBrowserHyperlinkListener) {
messageComponent.addHyperlinkListener(BrowserHyperlinkListener.INSTANCE);
}
}
messageComponent.setText(message);
messageComponent.setEditable(false);
if (messageComponent.getCaret() != null) {
messageComponent.setCaretPosition(0);
}
if (UIUtil.isUnderNimbusLookAndFeel()) {
messageComponent.setOpaque(false);
messageComponent.setBackground(UIUtil.TRANSPARENT_COLOR);
} else {
messageComponent.setBackground(UIUtil.getOptionPaneBackground());
}
messageComponent.setForeground(UIUtil.getLabelForeground());
return messageComponent;
}
private static class MyBorderLayout extends BorderLayout {
private double myPhase = 0;//it varies from 0 (hidden state) to 1 (fully visible)
private MyBorderLayout() {
}
@Override
public void layoutContainer(Container target) {
final Dimension realSize = target.getSize();
target.setSize(target.getPreferredSize());
super.layoutContainer(target);
target.setSize(realSize);
synchronized (target.getTreeLock()) {
int yShift = (int) ((1 - myPhase) * target.getPreferredSize().height);
Component[] components = target.getComponents();
for (Component component : components) {
Point point = component.getLocation();
point.y -= yShift;
component.setLocation(point);
}
}
}
}
}