1 19 20 package org.netbeans.license; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Dimension ; 24 import java.awt.FlowLayout ; 25 import java.awt.Frame ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.net.URL ; 29 import java.util.ResourceBundle ; 30 import javax.swing.AbstractButton ; 31 import javax.swing.BorderFactory ; 32 import javax.swing.JButton ; 33 import javax.swing.JDialog ; 34 import javax.swing.JPanel ; 35 36 import org.netbeans.util.Util; 37 import org.openide.util.NbBundle; 38 import org.openide.util.Utilities; 39 45 46 public final class AcceptLicense { 47 48 private static JDialog d; 49 private static String command; 50 51 53 public static void showLicensePanel () throws Exception { 54 Util.setDefaultLookAndFeel(); 55 URL url = AcceptLicense.class.getResource("LICENSE.txt"); LicensePanel licensePanel = new LicensePanel(url); 57 ResourceBundle bundle = NbBundle.getBundle(AcceptLicense.class); 58 String yesLabel = bundle.getString("MSG_LicenseYesButton"); 59 String noLabel = bundle.getString("MSG_LicenseNoButton"); 60 JButton yesButton = new JButton (); 61 JButton noButton = new JButton (); 62 setLocalizedText(yesButton,yesLabel); 63 setLocalizedText(noButton,noLabel); 64 ActionListener listener = new ActionListener () { 65 public void actionPerformed (ActionEvent 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"); noButton.setActionCommand("no"); 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 yesPF = yesButton.getPreferredSize(); 84 Dimension 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 (maxWidth, maxHeight)); 88 noButton.setPreferredSize(new Dimension (maxWidth, maxHeight)); 89 90 d = new JDialog ((Frame ) 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 buttonPanel = new JPanel (); 97 buttonPanel.setLayout(new FlowLayout (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 (600,600)); 103 d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 104 d.setModal(true); 105 d.setResizable(true); 106 d.setLocationRelativeTo(null); 108 d.setVisible(true); 109 110 if ("yes".equals(command)) { return; 112 } else { 113 throw new org.openide.util.UserCancelException(); 114 } 115 } 116 117 124 private static void setLocalizedText (AbstractButton button, String text) { 125 if (text == null) { 126 button.setText(null); 127 return; 128 } 129 130 int i = findMnemonicAmpersand(text); 131 132 if (i < 0) { 133 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 return; 143 } 144 145 char ch = text.charAt(i + 1); 146 147 button.setMnemonic(ch); 150 151 button.setDisplayedMnemonicIndex(i); 155 } 156 } 157 158 172 public static int findMnemonicAmpersand(String text) { 173 int i = -1; 174 175 do { 176 i = text.indexOf('&', i + 1); 178 179 if ((i >= 0) && ((i + 1) < text.length())) { 180 if (text.charAt(i + 1) == ' ') { 182 continue; 183 184 } else if ((text.charAt(i + 1) == '\'') && (i > 0) && (text.charAt(i - 1) == '\'')) { 186 continue; 187 } 188 189 return i; 191 } 192 } while (i >= 0); 193 194 return -1; 195 } 196 } 197 | Popular Tags |