1 /*
2 * Dictionary port to Java
3 * Copyright (C) 2000 Kaloian Doganov
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package net.sourceforge.dictport.ui;
20
21 import java.awt;
22 import java.awt.event;
23 import javax.swing;
24 import javax.swing.border;
25
26 /**
27 * Options UI Dialog
28 *
29 * @author Kaloian Doganov <kaloian@europe.com>
30 */
31
32 final class OptionsDialog extends JDialog {
33
34 private UserPrefsModel fPrefs;
35 private boolean fPluginMode;
36
37 private Action fOKAction;
38 private Action fCancelAction;
39
40 private PrefsPane fPrefsPane;
41
42 private boolean fApproved;
43
44 private static final int kGap = 5;
45
46 /**
47 * Create OptionsDialog dialog with current options.
48 *
49 * @param aOwner Dialog owner.
50 * @param aPrefs User preferences to use as default base.
51 * @param aPluginMode Disable editing application-related options.
52 */
53 OptionsDialog(Frame aOwner, UserPrefsModel aPrefs,
54 boolean aPluginMode) {
55
56 super(aOwner, "Dictionary: Options", true);
57 fPrefs = new UserPrefsModel(aPrefs);
58 fPluginMode = aPluginMode;
59 initActions();
60 initComponents();
61 pack();
62 setResizable(false);
63 }
64
65 private void initActions() {
66 fOKAction = new OKAction();
67 fCancelAction = new CancelAction();
68 }
69
70 private void initComponents() {
71 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
72
73 Border gap = new EmptyBorder(5, 5, 5, 5); // default ui component gap
74
75 JPanel root = new JPanel(new BorderLayout());
76 root.setBorder(gap);
77
78 fPrefsPane = new PrefsPane(fPrefs, fPluginMode);
79 root.add(fPrefsPane, "Center");
80
81 // South buttons
82 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
83 JButton okButton = new JButton(fOKAction);
84 buttonPanel.add(okButton);
85 buttonPanel.add(new JButton(fCancelAction));
86 root.add(buttonPanel, "South");
87
88 root.getActionMap().put("cancel", fCancelAction);
89 InputMap inputMap = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
90 KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
91 inputMap.put(esc, "cancel");
92
93 setContentPane(root);
94
95 getRootPane().setDefaultButton(okButton);
96 }
97
98 /** Center window when showing. */
99 public void show() {
100 fApproved = false;
101 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
102 Rectangle window = this.getBounds();
103 window.x = (screen.width - window.width) / 2;
104 window.y = (screen.height - window.height) / 2;
105 this.setBounds(window);
106 super.show();
107 }
108
109 /** Return <code>true</code> if dialog is approved. */
110 public boolean isApproved() {
111 return fApproved;
112 }
113
114 /** Return user preferences, if the dialog is approved. */
115 public UserPrefsModel getUserPrefs() {
116 return fPrefs;
117 }
118
119 /** Approve dialog action. */
120 private final class OKAction extends AbstractAction {
121 OKAction() {
122 super("OK");
123 }
124
125 public void actionPerformed(ActionEvent aEvent) {
126 fApproved = true;
127 fPrefs.assign(fPrefsPane.getUserPrefs());
128 dispose();
129 }
130 }
131
132 /** Cancel dialog action. */
133 private final class CancelAction extends AbstractAction {
134 CancelAction() {
135 super("Cancel");
136 }
137
138 public void actionPerformed(ActionEvent aEvent) {
139 fApproved = false;
140 dispose();
141 }
142 }
143 }