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 java.io;
24 import java.net.URL;
25 import javax.swing;
26 import javax.swing.border;
27
28 /**
29 * License UI Dialog
30 *
31 * @author Kaloian Doganov <kaloian@europe.com>
32 */
33
34 final class LicenseDialog extends JDialog {
35
36 private Action fCloseAction;
37
38 /**
39 * Create LicenseDialog dialog with GNU General Public License.
40 *
41 * @param aOwner Dialog owner.
42 */
43 LicenseDialog(Dialog aOwner) {
44 super(aOwner, "Dictionary: License", true);
45 initActions();
46 initComponents();
47 setResizable(false);
48 pack();
49 }
50
51 private void initActions() {
52 fCloseAction = new CloseAction();
53 }
54
55 private void initComponents() {
56 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
57
58 JPanel root = new JPanel(new BorderLayout());
59 root.setBorder(new EmptyBorder(Main.kGap, Main.kGap, 0, Main.kGap));
60
61 // Text area
62 JTextArea licenseArea = new JTextArea();
63 StringBuffer license = new StringBuffer(18000);
64 int cols = resourceToString("gpl.txt", license);
65 licenseArea.setColumns(cols);
66 licenseArea.setRows(25);
67 licenseArea.setEditable(false);
68 licenseArea.setText(license.toString());
69 licenseArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
70 JScrollPane scroll = new JScrollPane(licenseArea,
71 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
72 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
73 licenseArea.setCaretPosition(0);
74
75 root.add(scroll, "Center");
76
77 // Button
78 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
79 JButton closeButton = new JButton(fCloseAction);
80 buttonPanel.add(closeButton);
81 getRootPane().setDefaultButton(closeButton);
82
83 root.add(buttonPanel, "South");
84
85 root.getActionMap().put("close", fCloseAction);
86 InputMap inputMap = root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
87 KeyStroke esc = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
88 inputMap.put(esc, "close");
89
90 setContentPane(root);
91 }
92
93 /** Center window when showing. */
94 public void show() {
95 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
96 Rectangle window = this.getBounds();
97 window.x = (screen.width - window.width) / 2;
98 window.y = (screen.height - window.height) / 2;
99 setBounds(window);
100 super.show();
101 }
102
103 /**
104 * Read resource content to StringBuffer.
105 *
106 * @param aFile resource name.
107 * @param aBuffer buffer where to load the contents of the resource.
108 * @return Maxumum columns.
109 */
110 private int resourceToString(String aFile, StringBuffer aBuffer) {
111 int cols = 0;
112 URL url = getClass().getResource(aFile);
113
114 try {
115 BufferedReader in = new BufferedReader(
116 new InputStreamReader(url.openStream()));
117
118 String line = "";
119 do {
120 line = in.readLine();
121 if (line != null) {
122 if (line.length() > cols) cols = line.length();
123 aBuffer.append(line);
124 aBuffer.append('\n');
125 }
126 }
127 while (line != null);
128 }
129 catch (IOException e) {
130 e.printStackTrace();
131 }
132
133 return cols;
134 }
135
136 private final class CloseAction extends AbstractAction {
137 CloseAction() {
138 super("Close");
139 }
140
141 public void actionPerformed(ActionEvent aEvent) {
142 dispose();
143 }
144 }
145 }