Exemplo n.º 1
0
 /** @param locale new locale to use. */
 private void localizeUI(final String locale) {
   File jarPath = Utils.getJarDir(LoginWindow.class);
   l10n = new L10n("localization_" + locale + ".properties");
   try {
     l10n = new L10n(jarPath.getCanonicalPath() + "/" + "localization_" + locale + ".properties");
   } catch (IOException e1) {
     e1.printStackTrace();
   }
   if (!l10n.load()) {
     l10n = new L10n("localization_" + locale + ".properties");
     if (!l10n.load()) {
       JOptionPane.showMessageDialog(
           LoginWindow.this, "Counld't load localization", "Error", JOptionPane.ERROR_MESSAGE);
       System.exit(0);
     }
   }
   this.setTitle(l10n.get("login.logintitle"));
   lbCaption.setText(l10n.get("login.loginWndTitle"));
   lbUsername.setText(l10n.get("login.lblusername"));
   lbPassword.setText(l10n.get("login.lblpassword"));
   btnLogin.setText(l10n.get("login.lbllog_in"));
   btnCancel.setText(l10n.get("login.lblcancel"));
   this.pack();
 }
Exemplo n.º 2
0
  /** Constructing login window. */
  public LoginWindow() {
    super();

    File jarPath = Utils.getJarDir(LoginWindow.class);
    try {
      String configFileStr = jarPath.getCanonicalPath() + "/" + "config.properties";
      File f = new File(configFileStr);
      if (f.exists() && !f.isDirectory()) {
        config = new Configuration(configFileStr);
      } else {
        config = new Configuration("config.properties");
      }
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    if (config == null || !this.config.load()) {
      JOptionPane.showMessageDialog(
          LoginWindow.this, "Counld't load config", "Error", JOptionPane.ERROR_MESSAGE);
      System.exit(0);
    }
    String locale = userPrefs.get("ui.language", null);
    if (locale == null) {
      locale = "en";
      userPrefs.put("ui.language", "en");
    }
    try {
      l10n = new L10n(jarPath.getCanonicalPath() + "/" + "localization_" + locale + ".properties");
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    if (!l10n.load()) {
      l10n = new L10n("localization_" + locale + ".properties");
      if (!l10n.load()) {
        JOptionPane.showMessageDialog(
            LoginWindow.this, "Counld't load localization", "Error", JOptionPane.ERROR_MESSAGE);
        System.exit(0);
      }
    }

    this.setTitle(l10n.get("login.logintitle"));

    try {
      this.setIconImage(ImageIO.read(new File(jarPath.getCanonicalPath() + "/" + "icon.png")));
    } catch (IOException | IllegalArgumentException e1) {
      System.out.println("failed to load icon");
    }

    mainPanel = new JPanel(new BorderLayout());
    JPanel l10nPanel = new JPanel();
    JRadioButton rdbtnEn = new JRadioButton("English");
    rdbtnEn.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            userPrefs.put("ui.language", "en");
            localizeUI("en");
          }
        });
    JRadioButton rdbtnKw = new JRadioButton("Kiswahili");
    rdbtnKw.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(final ActionEvent e) {
            userPrefs.put("ui.language", "kw");
            localizeUI("kw");
          }
        });
    l10nPanel.add(rdbtnEn);
    l10nPanel.add(rdbtnKw);
    ButtonGroup l10nGroup = new ButtonGroup();
    l10nGroup.add(rdbtnEn);
    l10nGroup.add(rdbtnKw);
    if (locale.equals("en")) {
      rdbtnEn.setSelected(true);
    } else {
      rdbtnKw.setSelected(true);
    }
    mainPanel.add(l10nPanel, BorderLayout.NORTH);

    final JPanel fldsPanel = new JPanel(new GridBagLayout());
    final GridBagConstraints gblConstr = new GridBagConstraints();
    gblConstr.fill = GridBagConstraints.HORIZONTAL;
    gblConstr.insets = new Insets(5, 5, 5, 5);
    lbCaption = new JLabel(l10n.get("login.loginWndTitle"));
    gblConstr.gridx = 0;
    gblConstr.gridy = 0;
    gblConstr.gridwidth = 3;
    fldsPanel.add(lbCaption, gblConstr);
    lbUsername = new JLabel(l10n.get("login.lblusername"));
    gblConstr.gridx = 0;
    gblConstr.gridy = 1;
    gblConstr.gridwidth = 1;
    fldsPanel.add(lbUsername, gblConstr);
    this.fldusername = new JTextField("");
    gblConstr.gridx = 1;
    gblConstr.gridy = 1;
    gblConstr.gridwidth = 2;
    fldsPanel.add(this.fldusername, gblConstr);
    lbPassword = new JLabel(l10n.get("login.lblpassword"));
    gblConstr.gridx = 0;
    gblConstr.gridy = 2;
    gblConstr.gridwidth = 1;
    fldsPanel.add(lbPassword, gblConstr);
    this.pfpassword = new JPasswordField(10);
    gblConstr.gridx = 1;
    gblConstr.gridy = 2;
    gblConstr.gridwidth = 2;
    fldsPanel.add(this.pfpassword, gblConstr);
    this.pfpassword.requestFocus();
    fldsPanel.setBorder(new LineBorder(Color.GRAY));
    btnLogin = new JButton(l10n.get("login.lbllog_in"));
    btnLogin.addActionListener(new LoginActLsnr());
    this.getRootPane().setDefaultButton(btnLogin);
    btnCancel = new JButton(l10n.get("login.lblcancel"));
    btnCancel.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent evt) {
            dispose();
          }
        });
    final JPanel btnp = new JPanel();
    btnp.add(btnLogin);
    btnp.add(btnCancel);
    mainPanel.add(fldsPanel, BorderLayout.CENTER);
    mainPanel.add(btnp, BorderLayout.PAGE_END);
    this.setContentPane(mainPanel);
  }