KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > license > AcceptLicense


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.license;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25 import java.awt.Frame JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.awt.event.ActionListener JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30 import javax.swing.AbstractButton JavaDoc;
31 import javax.swing.BorderFactory JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JDialog JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35
36 import org.netbeans.util.Util;
37 import org.openide.util.NbBundle;
38 import org.openide.util.Utilities;
39 /**
40  * Displays LicensePanel to user. User must accept license to continue.
41  * if user does not accept license UserCancelException is thrown.
42  *
43  * @author Marek Slama
44  */

45
46 public final class AcceptLicense {
47     
48     private static JDialog JavaDoc d;
49     private static String JavaDoc command;
50     
51     /** If License was not accepted during installation user must accept it here.
52      */

53     public static void showLicensePanel () throws Exception JavaDoc {
54         Util.setDefaultLookAndFeel();
55         URL JavaDoc url = AcceptLicense.class.getResource("LICENSE.txt"); // NOI18N
56
LicensePanel licensePanel = new LicensePanel(url);
57         ResourceBundle JavaDoc bundle = NbBundle.getBundle(AcceptLicense.class);
58         String JavaDoc yesLabel = bundle.getString("MSG_LicenseYesButton");
59         String JavaDoc noLabel = bundle.getString("MSG_LicenseNoButton");
60         JButton JavaDoc yesButton = new JButton JavaDoc();
61         JButton JavaDoc noButton = new JButton JavaDoc();
62         setLocalizedText(yesButton,yesLabel);
63         setLocalizedText(noButton,noLabel);
64         ActionListener JavaDoc listener = new ActionListener JavaDoc () {
65             public void actionPerformed (ActionEvent JavaDoc e) {
66                 command = e.getActionCommand();
67                 d.setVisible(false);
68                 d = null;
69             }
70         };
71         yesButton.addActionListener(listener);
72         noButton.addActionListener(listener);
73         
74         yesButton.setActionCommand("yes"); // NOI18N
75
noButton.setActionCommand("no"); // NOI18N
76

77         yesButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_AcceptButton"));
78         yesButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSD_AcceptButton"));
79         
80         noButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_RejectButton"));
81         noButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSD_RejectButton"));
82         
83         Dimension JavaDoc yesPF = yesButton.getPreferredSize();
84         Dimension JavaDoc noPF = noButton.getPreferredSize();
85         int maxWidth = Math.max(yesButton.getPreferredSize().width, noButton.getPreferredSize().width);
86         int maxHeight = Math.max(yesButton.getPreferredSize().height, noButton.getPreferredSize().height);
87         yesButton.setPreferredSize(new Dimension JavaDoc(maxWidth, maxHeight));
88         noButton.setPreferredSize(new Dimension JavaDoc(maxWidth, maxHeight));
89         
90         d = new JDialog JavaDoc((Frame JavaDoc) null,bundle.getString("MSG_LicenseDlgTitle"),true);
91         
92         d.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_LicenseDlg"));
93         d.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_LicenseDlg"));
94         
95         d.getContentPane().add(licensePanel,BorderLayout.CENTER);
96         JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
97         buttonPanel.setLayout(new FlowLayout JavaDoc(FlowLayout.RIGHT));
98         buttonPanel.setBorder(BorderFactory.createEmptyBorder(17,12,11,11));
99         buttonPanel.add(yesButton);
100         buttonPanel.add(noButton);
101         d.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
102         d.setSize(new Dimension JavaDoc(600,600));
103         d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
104         d.setModal(true);
105         d.setResizable(true);
106         //Center on screen
107
d.setLocationRelativeTo(null);
108         d.setVisible(true);
109         
110         if ("yes".equals(command)) { // NOI18N
111
return;
112         } else {
113             throw new org.openide.util.UserCancelException();
114         }
115     }
116     
117     /**
118      * Actual setter of the text & mnemonics for the AbstractButton or
119      * their subclasses. We must copy necessary code from org.openide.awt.Mnemonics
120      * because org.openide.awt module is not available yet when this code is called.
121      * @param item AbstractButton
122      * @param text new label
123      */

124     private static void setLocalizedText (AbstractButton JavaDoc button, String JavaDoc text) {
125         if (text == null) {
126             button.setText(null);
127             return;
128         }
129
130         int i = findMnemonicAmpersand(text);
131
132         if (i < 0) {
133             // no '&' - don't set the mnemonic
134
button.setText(text);
135             button.setMnemonic(0);
136         } else {
137             button.setText(text.substring(0, i) + text.substring(i + 1));
138             
139             if (Utilities.isMac()) {
140                 // there shall be no mnemonics on macosx.
141
//#55864
142
return;
143             }
144
145             char ch = text.charAt(i + 1);
146
147             // it's latin character or arabic digit,
148
// setting it as mnemonics
149
button.setMnemonic(ch);
150
151             // If it's something like "Save &As", we need to set another
152
// mnemonic index (at least under 1.4 or later)
153
// see #29676
154
button.setDisplayedMnemonicIndex(i);
155         }
156     }
157     
158     /**
159      * Searches for an ampersand in a string which indicates a mnemonic.
160      * Recognizes the following cases:
161      * <ul>
162      * <li>"Drag & Drop", "Ampersand ('&')" - don't have mnemonic ampersand.
163      * "&" is not found before " " (space), or if enclosed in "'"
164      * (single quotation marks).
165      * <li>"&File", "Save &As..." - do have mnemonic ampersand.
166      * <li>"Rock & Ro&ll", "Underline the '&' &character" - also do have
167      * mnemonic ampersand, but the second one.
168      * </ul>
169      * @param text text to search
170      * @return the position of mnemonic ampersand in text, or -1 if there is none
171      */

172     public static int findMnemonicAmpersand(String JavaDoc text) {
173         int i = -1;
174
175         do {
176             // searching for the next ampersand
177
i = text.indexOf('&', i + 1);
178
179             if ((i >= 0) && ((i + 1) < text.length())) {
180                 // before ' '
181
if (text.charAt(i + 1) == ' ') {
182                     continue;
183
184                     // before ', and after '
185
} else if ((text.charAt(i + 1) == '\'') && (i > 0) && (text.charAt(i - 1) == '\'')) {
186                     continue;
187                 }
188
189                 // ampersand is marking mnemonics
190
return i;
191             }
192         } while (i >= 0);
193
194         return -1;
195     }
196 }
197
Popular Tags