comparison src/viewer_swing/java/com/glavsoft/viewer/swing/gui/OptionsDialog.java @ 0:4689cc86d6cb

create TreeViewer2 Repository
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 03 Jul 2012 13:20:49 +0900
parents
children 17b702648079
comparison
equal deleted inserted replaced
-1:000000000000 0:4689cc86d6cb
1 // Copyright (C) 2010, 2011 GlavSoft LLC.
2 // All rights reserved.
3 //
4 //-------------------------------------------------------------------------
5 // This file is part of the TightVNC software. Please visit our Web site:
6 //
7 // http://www.tightvnc.com/
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License along
20 // with this program; if not, write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 //-------------------------------------------------------------------------
23 //
24
25 package com.glavsoft.viewer.swing.gui;
26
27 import com.glavsoft.rfb.encoding.EncodingType;
28 import com.glavsoft.rfb.protocol.LocalPointer;
29 import com.glavsoft.rfb.protocol.ProtocolSettings;
30
31 import javax.swing.*;
32 import java.awt.*;
33 import java.awt.event.*;
34 import java.util.HashMap;
35 import java.util.Map;
36
37 /**
38 * Options dialog
39 */
40 @SuppressWarnings("serial")
41 public class OptionsDialog extends JDialog {
42 private JSlider jpegQuality;
43 private JSlider compressionLevel;
44 private JCheckBox viewOnlyCheckBox;
45 private ProtocolSettings settings;
46 private JCheckBox sharedSession;
47
48 private RadioButtonSelectedState<LocalPointer> mouseCursorTrackSelected;
49 private Map<LocalPointer, JRadioButton> mouseCursorTrackMap;
50
51 // private RadioButtonSelectedState<Integer> colorDepthSelected;
52 // private Map<Integer, JRadioButton> colorDepthMap;
53
54 private JCheckBox useCompressionLevel;
55 private JCheckBox useJpegQuality;
56 private JLabel jpegQualityPoorLabel;
57 private JLabel jpegQualityBestLabel;
58 private JLabel compressionLevelFastLabel;
59 private JLabel compressionLevelBestLabel;
60 private JCheckBox allowCopyRect;
61 private JComboBox encodings;
62 private JCheckBox disableClipboardTransfer;
63 private JComboBox colorDepth;
64
65 public OptionsDialog(Window owner) {
66 super(owner, "Connection Options", ModalityType.MODELESS);
67 final WindowAdapter onClose = new WindowAdapter() {
68 @Override
69 public void windowClosing(WindowEvent e) {
70 setVisible(false);
71 }
72 };
73 addWindowListener(onClose);
74
75 JPanel optionsPane = new JPanel(new GridLayout(0, 2));
76 add(optionsPane, BorderLayout.CENTER);
77
78 optionsPane.add(createLeftPane());
79 optionsPane.add(createRightPane());
80
81 addButtons(onClose);
82
83 pack();
84 }
85
86 public void initControlsFromSettings(ProtocolSettings settings, boolean isOnConnect) {
87 this.settings = settings;
88
89 viewOnlyCheckBox.setSelected(settings.isViewOnly());
90
91 // String scaling = String.valueOf((int)(settings.scaling * 100));
92 // int i = 0; boolean isNotSetScale = true;
93 // while ( scaleCombo.getItemAt(i) != null) {
94 // String item = (String)scaleCombo.getItemAt(i);
95 // if (item.equals(scaling)) {
96 // scaleCombo.setSelectedIndex(i);
97 // isNotSetScale = false;
98 // break;
99 // }
100 // ++i;
101 // }
102 // if (isNotSetScale) {
103 // scaleCombo.setSelectedItem(SCALE_100);
104 // }
105
106 int i = 0; boolean isNotSetEncoding = true;
107 while ( encodings.getItemAt(i) != null) {
108 EncodingType item = ((EncodingSelectItem)encodings.getItemAt(i)).type;
109 if (item.equals(settings.getPreferredEncoding())) {
110 encodings.setSelectedIndex(i);
111 isNotSetEncoding = false;
112 break;
113 }
114 ++i;
115 }
116 if (isNotSetEncoding) {
117 encodings.setSelectedItem(0);
118 }
119
120 sharedSession.setSelected(settings.isShared());
121 sharedSession.setEnabled(isOnConnect);
122
123 mouseCursorTrackMap.get(settings.getMouseCursorTrack()).setSelected(true);
124 mouseCursorTrackSelected.setSelected(settings.getMouseCursorTrack());
125
126 // settings.setBitsPerPixel(settings.getBitsPerPixel()); // when 0 set default bpp value
127 // final JRadioButton colorDepthRadioButton = colorDepthMap.get(settings.getBitsPerPixel());
128 // if (colorDepthRadioButton != null) {
129 // colorDepthRadioButton.setSelected(true);
130 // }
131 // colorDepthSelected.setSelected(settings.getBitsPerPixel());
132
133 int bpp = settings.getBitsPerPixel();
134 i = 0; boolean isNotSet = true;
135 while ( colorDepth.getItemAt(i) != null) {
136 int itemBpp = ((ColorDepthSelectItem)colorDepth.getItemAt(i)).bpp;
137 if (itemBpp == bpp) {
138 colorDepth.setSelectedIndex(i);
139 isNotSet = false;
140 break;
141 }
142 ++i;
143 }
144 if (isNotSet) {
145 colorDepth.setSelectedItem(0);
146 }
147
148 useCompressionLevel.setSelected(settings.getCompressionLevel() > 0);
149 compressionLevel.setValue(Math.abs(settings.getCompressionLevel()));
150 setCompressionLevelPaneEnable();
151
152 useJpegQuality.setSelected(settings.getJpegQuality() > 0);
153 jpegQuality.setValue(Math.abs(settings.getJpegQuality()));
154 setJpegQualityPaneEnable();
155
156 allowCopyRect.setSelected(settings.isAllowCopyRect());
157 disableClipboardTransfer.setSelected( ! settings.isAllowClipboardTransfer());
158 }
159
160 private void setSettingsFromControls() {
161 settings.setViewOnly(viewOnlyCheckBox.isSelected());
162 settings.setPreferredEncoding(((EncodingSelectItem)encodings.getSelectedItem()).type);
163
164 settings.setSharedFlag(sharedSession.isSelected());
165 settings.setMouseCursorTrack(mouseCursorTrackSelected.getSelected());
166
167 settings.setBitsPerPixel(((ColorDepthSelectItem)colorDepth.getSelectedItem()).bpp);
168
169 settings.setCompressionLevel(useCompressionLevel.isSelected() ?
170 compressionLevel.getValue() :
171 - Math.abs(settings.getCompressionLevel()));
172 settings.setJpegQuality(useJpegQuality.isSelected() ?
173 jpegQuality.getValue() :
174 - Math.abs(settings.getJpegQuality()));
175 settings.setAllowCopyRect(allowCopyRect.isSelected());
176 settings.setAllowClipboardTransfer( ! disableClipboardTransfer.isSelected());
177 settings.fireListeners();
178 }
179
180 private Component createLeftPane() {
181 Box box = Box.createVerticalBox();
182 box.setAlignmentX(LEFT_ALIGNMENT);
183
184 box.add(createEncodingsPanel());
185
186 box.add(Box.createVerticalGlue());
187 return box;
188 }
189
190 private Component createRightPane() {
191 Box box = Box.createVerticalBox();
192 box.setAlignmentX(LEFT_ALIGNMENT);
193
194 box.add(createRestrictionsPanel());
195 // box.add(createDisplayPanel());
196 box.add(createMouseCursorPanel());
197 // box.add(createLocalShapePanel());
198
199 sharedSession = new JCheckBox("Request shared session");
200 box.add(new JPanel(new FlowLayout(FlowLayout.LEFT)).add(sharedSession));
201
202 box.add(Box.createVerticalGlue());
203 return box;
204 }
205
206 private JPanel createRestrictionsPanel() {
207 JPanel restrictionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
208 // restrictionsPanel.setAlignmentX(LEFT_ALIGNMENT);
209 restrictionsPanel.setBorder(
210 BorderFactory.createTitledBorder(
211 BorderFactory.createEtchedBorder(), "Restrictions"));
212
213 Box restrictionsBox = Box.createVerticalBox();
214 restrictionsBox.setAlignmentX(LEFT_ALIGNMENT);
215 restrictionsPanel.add(restrictionsBox);
216 viewOnlyCheckBox = new JCheckBox("View only (inputs ignored)");
217 viewOnlyCheckBox.setAlignmentX(LEFT_ALIGNMENT);
218 restrictionsBox.add(viewOnlyCheckBox);
219
220 disableClipboardTransfer = new JCheckBox("Disable clipboard transfer");
221 disableClipboardTransfer.setAlignmentX(LEFT_ALIGNMENT);
222 restrictionsBox.add(disableClipboardTransfer);
223
224 return restrictionsPanel;
225 }
226
227 private JPanel createEncodingsPanel() {
228 JPanel encodingsPanel = new JPanel();
229 encodingsPanel.setAlignmentX(LEFT_ALIGNMENT);
230 encodingsPanel.setLayout(new BoxLayout(encodingsPanel, BoxLayout.Y_AXIS));
231 encodingsPanel.setBorder(
232 BorderFactory.createTitledBorder(
233 BorderFactory.createEtchedBorder(), "Format and Encodings"));
234
235 JPanel encPane = new JPanel(new FlowLayout(FlowLayout.LEFT));
236 encPane.setAlignmentX(LEFT_ALIGNMENT);
237 encPane.add(new JLabel("Preferred encoding: "));
238
239 encodings = new JComboBox();
240 encodings.addItem(new EncodingSelectItem(EncodingType.TIGHT));
241 encodings.addItem(new EncodingSelectItem(EncodingType.HEXTILE));
242
243 // encodings.addItem(new EncodingSelectItem(EncodingType.RRE));
244 // encodings.addItem(new EncodingSelectItem(EncodingType.ZLIB));
245
246 encodings.addItem(new EncodingSelectItem(EncodingType.ZRLE));
247 encodings.addItem(new EncodingSelectItem(EncodingType.RAW_ENCODING));
248 encPane.add(encodings);
249 encodingsPanel.add(encPane);
250
251 encodingsPanel.add(createColorDepthPanel());
252
253 addCompressionLevelPane(encodingsPanel);
254 addJpegQualityLevelPane(encodingsPanel);
255
256 allowCopyRect = new JCheckBox("Allow CopyRect encoding");
257 allowCopyRect.setAlignmentX(LEFT_ALIGNMENT);
258 encodingsPanel.add(allowCopyRect);
259
260 return encodingsPanel;
261 }
262
263 private static class EncodingSelectItem {
264 final EncodingType type;
265 public EncodingSelectItem(EncodingType type) {
266 this.type = type;
267 }
268 @Override
269 public String toString() {
270 return type.getName();
271 }
272 }
273
274 private static class ColorDepthSelectItem {
275 final int bpp;
276 final String title;
277 public ColorDepthSelectItem(int bpp, String title) {
278 this.bpp = bpp;
279 this.title = title;
280 }
281 @Override
282 public String toString() {
283 return title;
284 }
285 }
286
287 private JPanel createColorDepthPanel() {
288 JPanel colorDepthPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
289 colorDepthPanel.setAlignmentX(LEFT_ALIGNMENT);
290 colorDepthPanel.add(new JLabel("Color format: "));
291
292 colorDepth = new JComboBox();
293 colorDepth.addItem(new ColorDepthSelectItem(ProtocolSettings.BPP_SERVER_SETTINGS,
294 "Server's default"));
295 colorDepth.addItem(new ColorDepthSelectItem(ProtocolSettings.BPP_32,
296 "16 777 216 colors"));
297 colorDepth.addItem(new ColorDepthSelectItem(ProtocolSettings.BPP_16,
298 "65 536 colors"));
299 colorDepth.addItem(new ColorDepthSelectItem(ProtocolSettings.BPP_8,
300 "256 colors"));
301 colorDepth.addItem(new ColorDepthSelectItem(ProtocolSettings.BPP_6,
302 "64 colors"));
303 colorDepth.addItem(new ColorDepthSelectItem(ProtocolSettings.BPP_3,
304 "8 colors"));
305
306 colorDepthPanel.add(colorDepth);
307 colorDepth.addItemListener(new ItemListener() {
308 @Override
309 public void itemStateChanged(ItemEvent e) {
310 final ColorDepthSelectItem selectedItem =
311 (ColorDepthSelectItem) colorDepth.getSelectedItem();
312 setEnabled(selectedItem.bpp > ProtocolSettings.BPP_8 ||
313 selectedItem.bpp == ProtocolSettings.BPP_SERVER_SETTINGS,
314 useJpegQuality);
315 setEnabled(useJpegQuality.isSelected() &&
316 (selectedItem.bpp > ProtocolSettings.BPP_8 ||
317 selectedItem.bpp == ProtocolSettings.BPP_SERVER_SETTINGS),
318 jpegQuality, jpegQualityPoorLabel, jpegQualityBestLabel);
319 }
320 });
321 return colorDepthPanel;
322 }
323
324 private void addJpegQualityLevelPane(JPanel encodingsPanel) {
325 useJpegQuality = new JCheckBox("Allow JPEG, set quality level:");
326 useJpegQuality.setAlignmentX(LEFT_ALIGNMENT);
327 encodingsPanel.add(useJpegQuality);
328
329 JPanel jpegQualityPane = new JPanel();
330 jpegQualityPane.setAlignmentX(LEFT_ALIGNMENT);
331 jpegQualityPoorLabel = new JLabel("poor");
332 jpegQualityPane.add(jpegQualityPoorLabel);
333 jpegQuality = new JSlider(1, 9, 9);
334 jpegQualityPane.add(jpegQuality);
335 jpegQuality.setPaintTicks(true);
336 jpegQuality.setMinorTickSpacing(1);
337 jpegQuality.setMajorTickSpacing(1);
338 jpegQuality.setPaintLabels(true);
339 jpegQuality.setSnapToTicks(true);
340 jpegQuality.setFont(
341 jpegQuality.getFont().deriveFont((float) 8));
342 jpegQualityBestLabel = new JLabel("best");
343 jpegQualityPane.add(jpegQualityBestLabel);
344 encodingsPanel.add(jpegQualityPane);
345
346 jpegQualityPoorLabel.setFont(jpegQualityPoorLabel.getFont().deriveFont((float) 10));
347 jpegQualityBestLabel.setFont(jpegQualityBestLabel.getFont().deriveFont((float) 10));
348
349 useJpegQuality.addActionListener(new ActionListener() {
350 @Override
351 public void actionPerformed(ActionEvent e) {
352 setJpegQualityPaneEnable();
353 }
354 });
355 }
356
357 protected void setJpegQualityPaneEnable() {
358 if (useJpegQuality != null && colorDepth != null) {
359 int bpp = ((ColorDepthSelectItem)colorDepth.getSelectedItem()).bpp;
360 setEnabled(
361 bpp > ProtocolSettings.BPP_8 || bpp == ProtocolSettings.BPP_SERVER_SETTINGS,
362 useJpegQuality);
363 setEnabled(useJpegQuality.isSelected() &&
364 (bpp > ProtocolSettings.BPP_8 || bpp == ProtocolSettings.BPP_SERVER_SETTINGS),
365 jpegQuality, jpegQualityPoorLabel, jpegQualityBestLabel);
366 }
367 }
368
369 private void addCompressionLevelPane(JPanel encodingsPanel) {
370 useCompressionLevel = new JCheckBox("Custom compression level:");
371 useCompressionLevel.setAlignmentX(LEFT_ALIGNMENT);
372 encodingsPanel.add(useCompressionLevel);
373
374 JPanel compressionLevelPane = new JPanel();
375 compressionLevelPane.setAlignmentX(LEFT_ALIGNMENT);
376 compressionLevelFastLabel = new JLabel("fast");
377 compressionLevelPane.add(compressionLevelFastLabel);
378 compressionLevel = new JSlider(1, 9, 1);
379 compressionLevelPane.add(compressionLevel);
380 compressionLevel.setPaintTicks(true);
381 compressionLevel.setMinorTickSpacing(1);
382 compressionLevel.setMajorTickSpacing(1);
383 compressionLevel.setPaintLabels(true);
384 compressionLevel.setSnapToTicks(true);
385 compressionLevel.setFont(compressionLevel.getFont().deriveFont((float) 8));
386 compressionLevelBestLabel = new JLabel("best");
387 compressionLevelPane.add(compressionLevelBestLabel);
388 encodingsPanel.add(compressionLevelPane);
389
390 compressionLevelFastLabel.setFont(compressionLevelFastLabel.getFont().deriveFont((float) 10));
391 compressionLevelBestLabel.setFont(compressionLevelBestLabel.getFont().deriveFont((float) 10));
392
393 useCompressionLevel.addActionListener(new ActionListener() {
394 @Override
395 public void actionPerformed(ActionEvent e) {
396 setEnabled(useCompressionLevel.isSelected(),
397 compressionLevel, compressionLevelFastLabel, compressionLevelBestLabel);
398 }
399 });
400 setCompressionLevelPaneEnable();
401 }
402
403 protected void setCompressionLevelPaneEnable() {
404 setEnabled(useCompressionLevel.isSelected(),
405 compressionLevel, compressionLevelFastLabel, compressionLevelBestLabel);
406 }
407 private void setEnabled(boolean isEnabled, JComponent ... comp) {
408 for (JComponent c : comp) {
409 c.setEnabled(isEnabled);
410 }
411 }
412
413 /*
414 private JPanel createLocalShapePanel() {
415 JPanel localCursorShapePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
416 // localCursorShapePanel.setLayout(new BoxLayout(localCursorShapePanel, BoxLayout.Y_AXIS));
417 localCursorShapePanel.setBorder(
418 BorderFactory.createTitledBorder(
419 BorderFactory.createEtchedBorder(), "Local cursor shape"));
420 Box localCursorShapeBox = Box.createVerticalBox();
421 localCursorShapePanel.add(localCursorShapeBox);
422
423 JRadioButton dotCursorRadio = new JRadioButton("Dot cursor");
424 JRadioButton smallDotCursorRadio = new JRadioButton("Small dot cursor");
425 JRadioButton arrowCursorRadio = new JRadioButton("Default cursor");
426 JRadioButton noCursorRadio = new JRadioButton("No local cursor");
427 localCursorShapeBox.add(dotCursorRadio);
428 localCursorShapeBox.add(smallDotCursorRadio);
429 localCursorShapeBox.add(arrowCursorRadio);
430 localCursorShapeBox.add(noCursorRadio);
431 ButtonGroup localCursorButtonGroup = new ButtonGroup();
432 localCursorButtonGroup.add(dotCursorRadio);
433 localCursorButtonGroup.add(smallDotCursorRadio);
434 localCursorButtonGroup.add(arrowCursorRadio);
435 localCursorButtonGroup.add(noCursorRadio);
436 return localCursorShapePanel;
437 }
438 */
439
440 private JPanel createMouseCursorPanel() {
441 JPanel mouseCursorPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
442 mouseCursorPanel.setBorder(
443 BorderFactory.createTitledBorder(
444 BorderFactory.createEtchedBorder(), "Mouse Cursor"));
445 Box mouseCursorBox = Box.createVerticalBox();
446 mouseCursorPanel.add(mouseCursorBox);
447
448 ButtonGroup mouseCursorTrackGroup = new ButtonGroup();
449
450 mouseCursorTrackSelected = new RadioButtonSelectedState<LocalPointer>();
451 mouseCursorTrackMap = new HashMap<LocalPointer, JRadioButton>();
452
453 addRadioButton("Track remote cursor locally", LocalPointer.ON,
454 mouseCursorTrackSelected, mouseCursorTrackMap, mouseCursorBox,
455 mouseCursorTrackGroup);
456 addRadioButton("Let remote server deal with mouse cursor",
457 LocalPointer.OFF,
458 mouseCursorTrackSelected, mouseCursorTrackMap, mouseCursorBox,
459 mouseCursorTrackGroup);
460 addRadioButton("Don't show remote cursor", LocalPointer.HIDE,
461 mouseCursorTrackSelected, mouseCursorTrackMap, mouseCursorBox,
462 mouseCursorTrackGroup);
463 return mouseCursorPanel;
464 }
465
466 private static class RadioButtonSelectedState<T> {
467 private T state;
468
469 public void setSelected(T state) {
470 this.state = state;
471 }
472
473 public T getSelected() {
474 return state;
475 }
476
477 }
478
479 private <T> JRadioButton addRadioButton(String text, final T state,
480 final RadioButtonSelectedState<T> selected,
481 Map<T, JRadioButton> state2buttonMap, JComponent component, ButtonGroup group) {
482 JRadioButton radio = new JRadioButton(text);
483 radio.addActionListener(new ActionListener() {
484 @Override
485 public void actionPerformed(ActionEvent e) {
486 selected.setSelected(state);
487 }
488 });
489 component.add(radio);
490 group.add(radio);
491 state2buttonMap.put(state, radio);
492 return radio;
493 }
494
495 private void addButtons(final WindowListener onClose) {
496 JPanel buttonPanel = new JPanel();
497 JButton loginButton = new JButton("Ok");
498 buttonPanel.add(loginButton);
499 loginButton.addActionListener(new ActionListener() {
500 @Override
501 public void actionPerformed(ActionEvent e) {
502 setSettingsFromControls();
503 setVisible(false);
504 }
505 });
506
507 JButton closeButton = new JButton("Cancel");
508 buttonPanel.add(closeButton);
509 closeButton.addActionListener(new ActionListener() {
510 @Override
511 public void actionPerformed(ActionEvent e) {
512 onClose.windowClosing(null);
513 }
514 });
515 add(buttonPanel, BorderLayout.SOUTH);
516 }
517
518 }