Print this page
    
4719 Common patchset for jdk1.7 support preparation
    
      
        | Split | Close | 
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/cmd/krb5/kadmin/gui/dchanger/DateTimeDialog.java
          +++ new/usr/src/cmd/krb5/kadmin/gui/dchanger/DateTimeDialog.java
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License, Version 1.0 only
   6    6   * (the "License").  You may not use this file except in compliance
   7    7   * with the License.
   8    8   *
   9    9   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10   10   * or http://www.opensolaris.org/os/licensing.
  11   11   * See the License for the specific language governing permissions
  12   12   * and limitations under the License.
  13   13   *
  14   14   * When distributing Covered Code, include this CDDL HEADER in each
  15   15   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16   16   * If applicable, add the following below this CDDL HEADER, with the
  17   17   * fields enclosed by brackets "[]" replaced with your own identifying
  18   18   * information: Portions Copyright [yyyy] [name of copyright owner]
  19   19   *
  20   20   * CDDL HEADER END
  21   21   */
  22   22  /*
  23   23   * ident        "%Z%%M% %I%     %E% SMI"
  24   24   *
  25   25   * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
  26   26   * All rights reserved.
  27   27   */
  28   28  
  29   29  import java.awt.*;
  30   30  import java.awt.event.*;
  31   31  import java.text.*;
  32   32  import java.util.*;
  33   33  
  34   34  /**
  35   35   * This class creates a dialog box that helps the user enter date and
  36   36   * time with mouse clicks.  The dialog box need only be created
  37   37   * once. The Ok and Cancel buttons merely call setVisible with an
  38   38   *  argument of false.
  39   39   */
  40   40  
  41   41  // The layout will consist of 3 panels: topPanel contains the
  42   42  // different labels and fields. middlePanel contains the buttons
  43   43  // midnight and now. bottomPanel contains the buttons ok, cancel and
  44   44  // help. The last two panels are separated by a LineSeparator.
  45   45  
  46   46  public class DateTimeDialog extends Dialog {
  47   47    
  48   48      private boolean save;
  49   49  
  50   50      private Frame parent;
  51   51    
  52   52      private DCPanel dateDCPanel;
  53   53      private DCPanel yearDCPanel;
  54   54      private DCPanel hourDCPanel;
  55   55      private DCPanel minuteDCPanel;
  56   56      private DCPanel secondDCPanel;
  57   57    
  58   58      private Choice month;
  59   59    
  60   60      private DCCircularTextField date;
  61   61      private DCCircularTextField hour;
  62   62      private DCCircularTextField second;
  63   63      private DCCircularTextField minute;
  64   64      private DCTextField year;
  65   65    
  66   66      private Button ok;
  67   67      private Button cancel;
  68   68      private Button help;
  69   69      private Button now;
  70   70      private Button midnight;
  71   71  
  72   72      private HelpDialog hd = null;
  73   73    
  74   74      private Panel topPanel;
  75   75      private Panel middlePanel;
  76   76      private Panel bottomPanel;
  77   77    
  78   78      private GregorianCalendar calendar = null;
  79   79      private static int MONTH_LEN[] = {31, 28, 31, 30, 31, 30, 31,
  80   80                                        31, 30, 31, 30, 31};
  81   81      private static DateFormat df =
  82   82      DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
  83   83                                   DateFormat.MEDIUM);
  84   84      private static Toolkit toolkit = Toolkit.getDefaultToolkit();
  85   85    
  86   86    // For I18N
  87   87      private static ResourceBundle rb =
  88   88      ResourceBundle.getBundle("GuiResource" /* NOI18N */); 
  89   89      private static ResourceBundle hrb =
  90   90      ResourceBundle.getBundle("HelpData" /* NOI18N */); 
  91   91  
  92   92      /**
  93   93       * Constructor that lays out the componeents and sets the different
  94   94       * event handlers. 
  95   95       */
  96   96      public DateTimeDialog(Frame parent, Color background, Color foreground) {
  97   97      super(parent, getString("SEAM Date/Time Helper"), true);
  98   98      
  99   99      this.parent = parent;
 100  100  
 101  101      setLayout(new GridBagLayout());
 102  102      addLabels();
 103  103      addFields(background, foreground);
 104  104      addDCPanels();
 105  105      addButtons();
 106  106      addFocusListeners();
 107  107      setCurrentTime();
 108  108      setSize(250, 300);
 109  109      setResizable(false);
 110  110      
 111  111      addWindowListener(new DCWindowListener());
 112  112      //      initializeFocusOnTextField();
 113  113      }
 114  114  
 115  115      /**
 116  116       * Adds the labels only
 117  117       */
 118  118      private void addLabels() {
 119  119      
 120  120      GridBagConstraints gbc = new GridBagConstraints();
 121  121      gbc.weighty = 1;
 122  122      
 123  123      topPanel = new Panel();
 124  124      topPanel.setLayout(new GridBagLayout());
 125  125      gbc.gridwidth = GridBagConstraints.REMAINDER;    
 126  126      gbc.fill = GridBagConstraints.BOTH;
 127  127      gbc.anchor = GridBagConstraints.CENTER;
 128  128      gbc.gridx = 0;
 129  129      gbc.gridy = 0;
 130  130      add(topPanel, gbc);
 131  131      
 132  132      gbc.fill = GridBagConstraints.NONE;
 133  133      gbc.anchor = GridBagConstraints.EAST;
 134  134      gbc.gridx = 0;
 135  135      gbc.gridwidth = 1;
 136  136      
 137  137      gbc.gridy = 0;
 138  138      topPanel.add(new Label(getString("Month")), gbc);
 139  139      
 140  140      gbc.gridy = 1;
 141  141      topPanel.add(new Label(getString("Date")), gbc);
 142  142      
 143  143      gbc.gridy = 2;
 144  144      topPanel.add(new Label(getString("Year")), gbc);
 145  145      
 146  146      gbc.gridy = 3;
 147  147      topPanel.add(new Label(getString("Hour")), gbc);
 148  148      
 149  149      gbc.gridy = 4;
 150  150      topPanel.add(new Label(getString("Minute")), gbc);
 151  151      
 152  152      gbc.gridy = 5;
 153  153      topPanel.add(new Label(getString("Second")), gbc);
 154  154      }
 155  155    
 156  156      /**
 157  157       * Adds the fields that will store the month, year, date, hour,
 158  158       * minute and second.
 159  159       */
 160  160      private void addFields(Color background, Color foreground) {
 161  161      
 162  162      GridBagConstraints gbc = new GridBagConstraints();
 163  163      gbc.weighty = 1;
 164  164      
 165  165      month = new Choice();
 166  166      initializeMonth();
 167  167      
 168  168      date = new DCCircularTextField("1", 2);
 169  169      date.setMinimum(1);
 170  170      date.setBackground(background);
 171  171      date.setForeground(foreground);
 172  172  
 173  173      hour = new DCCircularTextField("00", 2);
 174  174      hour.setMaximum(23);
 175  175      hour.setBackground(background);
 176  176      hour.setForeground(foreground);
 177  177      minute = new DCCircularTextField("00", 2);
 178  178      minute.setBackground(background);
 179  179      minute.setForeground(foreground);
 180  180      second = new DCCircularTextField("00", 2);
 181  181      second.setBackground(background);
 182  182      second.setForeground(foreground);
 183  183      
 184  184      year  = new DCTextField("2000", 4);
 185  185      year.setBackground(background);
 186  186      year.setForeground(foreground);
 187  187      
 188  188      Panel tempPanel = new Panel();
 189  189      tempPanel.add(month);
 190  190      gbc.gridwidth = GridBagConstraints.REMAINDER;
 191  191      gbc.fill = GridBagConstraints.HORIZONTAL;
 192  192      gbc.anchor = GridBagConstraints.WEST;
 193  193      gbc.gridx = 1;
 194  194      gbc.gridy = 0;
 195  195      topPanel.add(tempPanel, gbc);
 196  196      
 197  197      
 198  198      // Remaining fields are in topPanel
 199  199      gbc.gridwidth = 1;
 200  200      gbc.fill = GridBagConstraints.NONE;
 201  201      gbc.gridx = 1;
 202  202      
 203  203      gbc.gridy = 1;
 204  204      topPanel.add(date, gbc);
 205  205      
 206  206      gbc.gridy = 2;
 207  207      topPanel.add(year, gbc);
 208  208      
 209  209      gbc.gridy = 3;
 210  210      topPanel.add(hour, gbc);
 211  211      
 212  212      gbc.gridy = 4;
 213  213      topPanel.add(minute, gbc);
 214  214      
 215  215      gbc.gridy = 5;
 216  216      topPanel.add(second, gbc);
 217  217      
 218  218      }
 219  219  
 220  220    // Adds the panels with the +/- buttons for each DCField
 221  221      private void addDCPanels() {
 222  222      
 223  223      GridBagConstraints gbc = new GridBagConstraints();
 224  224      gbc.weighty = 1;
 225  225      
 226  226      gbc.gridx = 2;
 227  227      gbc.gridwidth = GridBagConstraints.REMAINDER;
 228  228      gbc.gridheight = 1;
 229  229      gbc.fill = GridBagConstraints.NONE;
 230  230      
 231  231      dateDCPanel = new DCPanel();
 232  232      yearDCPanel = new DCPanel();
 233  233      hourDCPanel = new DCPanel();
 234  234      minuteDCPanel = new DCPanel();
 235  235      secondDCPanel = new DCPanel();
 236  236      
 237  237      gbc.gridy = 1;
 238  238      topPanel.add(dateDCPanel, gbc);
 239  239      
 240  240      gbc.gridy = GridBagConstraints.RELATIVE;
 241  241      topPanel.add(yearDCPanel, gbc);
 242  242      topPanel.add(hourDCPanel, gbc);
 243  243      topPanel.add(minuteDCPanel, gbc);
 244  244      topPanel.add(secondDCPanel, gbc);
 245  245      
 246  246      dateDCPanel.setListener(date);
 247  247      yearDCPanel.setListener(year);
 248  248      hourDCPanel.setListener(hour);
 249  249      minuteDCPanel.setListener(minute);
 250  250      secondDCPanel.setListener(second);
 251  251      
 252  252      }
 253  253  
 254  254  
 255  255      /**
 256  256       * Sets the strings in the month pull-down menu. Also adds a listener
 257  257       * that will modify the maximum date allowed depending on the month.
 258  258       */
 259  259      private void initializeMonth() {
 260  260      DateFormatSymbols dfSymbols = new DateFormatSymbols();
 261  261      String[] monthStrings = dfSymbols.getMonths();
 262  262      
 263  263          month.removeAll();
 264  264      
 265  265          for (int i = 0; i < monthStrings.length; i++) {
 266  266          month.add(monthStrings[i]);
 267  267          }
 268  268  
 269  269          month.addItemListener(new DCMonthChangeListener());
 270  270      }
 271  271  
 272  272    // Adds all the buttons
 273  273      private void addButtons() {
 274  274  
 275  275          GridBagConstraints gbc = new GridBagConstraints();
 276  276          gbc.weighty = 1;
 277  277  
 278  278  
 279  279          middlePanel = new Panel();
 280  280          now  = new Button(getString("Now"));
 281  281          midnight        = new Button(getString("Midnight"));
 282  282          middlePanel.add(midnight);
 283  283          middlePanel.add(now);
 284  284          gbc.fill = GridBagConstraints.HORIZONTAL;
 285  285          gbc.gridwidth = GridBagConstraints.REMAINDER;
 286  286          gbc.gridx = 0;
 287  287          gbc.gridy = 1;
 288  288          add(middlePanel, gbc);
 289  289  
 290  290          gbc.gridwidth = GridBagConstraints.REMAINDER;
 291  291          gbc.gridheight = 1;
 292  292          gbc.fill = GridBagConstraints.BOTH;
 293  293          gbc.gridx = 0;
 294  294          gbc.gridy = 2;
 295  295          add(new LineSeparator(), gbc);
 296  296  
 297  297          bottomPanel = new Panel();
 298  298          ok = new Button(getString("OK"));
 299  299          cancel =        new Button(getString("Cancel"));
 300  300          help = new Button(getString("Help"));
 301  301          bottomPanel.add(ok);
 302  302          bottomPanel.add(cancel);
 303  303          bottomPanel.add(help);
 304  304          gbc.fill = GridBagConstraints.HORIZONTAL;
 305  305          gbc.gridwidth = GridBagConstraints.REMAINDER;
 306  306          gbc.gridx = 0;
 307  307          gbc.gridy = 3;
 308  308          add(bottomPanel, gbc);
 309  309  
 310  310          DCButtonListener bl = new DCButtonListener();
 311  311          ok.addActionListener(bl);
 312  312          cancel.addActionListener(bl);
 313  313          help.addActionListener(bl);
 314  314          now.addActionListener(bl);
 315  315          midnight.addActionListener(bl);
 316  316  
 317  317      }
 318  318  
 319  319      /**
 320  320       * Adds a listener to all the text fields so that when they go out
 321  321       * of focus (by tab or clicking), their values are checked for
 322  322       * errors.
 323  323       */
 324  324      private void addFocusListeners() {
 325  325      FocusListener fl = new DCFocusListener();
 326  326      date.addFocusListener(fl);
 327  327      year.addFocusListener(fl);
 328  328      hour.addFocusListener(fl);
 329  329      minute.addFocusListener(fl);
 330  330      second.addFocusListener(fl);
 331  331      }
 332  332  
 333  333      /**
 334  334       * Closes (hides) the dialog box when the user is done
 335  335       * @param save true if the box is being dismissed by clicking on
 336  336       * "ok" and the user wants to retain the modified value, false
 337  337       * otherwise. 
 338  338       */
 339  339      private void dateTimeDialogClose(boolean save) {
 340  340          if (save == true) {
 341  341          if (!updateFromGui())
 342  342             return;
 343  343      }
 344  344      this.save = save;
 345  345      setVisible(false);
 346  346      }
 347  347  
 348  348      /**
 349  349       * Checks to see is all text fields contain valid values.
 350  350       * @return true if all are valid, false otherwise.
 351  351       */
 352  352      private boolean updateFromGui() {
 353  353          return (checkErrorAndSet(date) && checkErrorAndSet(year) &&
 354  354                  checkErrorAndSet(hour) && checkErrorAndSet(minute) &&
 355  355                  checkErrorAndSet(second));
 356  356      }
 357  357  
 358  358      /**
 359  359       * Checks the value stored as text in the field and sets its numeric
 360  360       * value to that if it is legitimate.
 361  361       * @return true if the value was legitimate and got set, false
 362  362       * otherwise.
 363  363       */
 364  364      private boolean checkErrorAndSet(DCTextField tf) {
 365  365          int i = 0;
 366  366          boolean errorState = false;
 367  367          try {
 368  368          i = new Integer(tf.getText().trim()).intValue();
 369  369          errorState = !tf.checkValue(i);
 370  370          } catch (NumberFormatException e2) {
 371  371          errorState =  true;
 372  372          }       
 373  373          if (errorState) {
 374  374          tf.selectAll();
 375  375          toolkit.beep();
 376  376          }
 377  377          else
 378  378          tf.setValue(i);
 379  379          return !errorState;
 380  380      }
 381  381  
 382  382      /**
 383  383       * Checks if the user requested that the value in this
 384  384       * DateTimeDialog be used e.g., by clicking on "Ok" instead of
 385  385       * "Cancel."
 386  386       * @return true if the user wants to save the value in the
 387  387       * DateTimeDialog, false otherwise.
 388  388       */
 389  389  
 390  390      public boolean isSaved() {
 391  391          return save;
 392  392      }
 393  393  
 394  394      /**
 395  395       * Sets the date and time in fields to the current date and time.
 396  396       */
 397  397      public void setCurrentTime() {
 398  398          setDate(new Date());
 399  399      }
 400  400  
 401  401      /**
 402  402       * Sets the current date of the DateTimeDialog and updates the gui
 403  403       *   components to reflect that.
 404  404       * @param date the Date to set it to.
 405  405       */
 406  406      public void setDate(Date newDate) {
 407  407          calendar = new GregorianCalendar();
 408  408          calendar.setTime(newDate);
 409  409  
 410  410      // update gui components now
 411  411  
 412  412      year.setValue(calendar.get(Calendar.YEAR));
 413  413      month.select(calendar.get(Calendar.MONTH));
 414  414      date.setValue(calendar.get(Calendar.DATE));
 415  415      
 416  416      // Make sure the date is in the valid range for the given month
 417  417      fixDateField();
 418  418      
 419  419      hour.setValue(calendar.get(Calendar.HOUR_OF_DAY));
 420  420      minute.setValue(calendar.get(Calendar.MINUTE));
 421  421      second.setValue(calendar.get(Calendar.SECOND));
 422  422  
 423  423      }
 424  424  
 425  425      /**
 426  426       * Set the time fields to midnight, i.e., clears them.
 427  427       */
 428  428      private void setMidnight() {
 429  429              hour.setValue(0);
 430  430              minute.setValue(0);
 431  431              second.setValue(0);
 432  432      }
 433  433  
 434  434      /**
 435  435       * Make sure the date does not exceed the maximum allowable value
 436  436       * for the currently selected month.
 437  437       */
 438  438      private void fixDateField() {
 439  439          int monthIndex = month.getSelectedIndex();
 440  440          int max = MONTH_LEN[monthIndex];
 441  441          date.setMaximum(calendar.isLeapYear(year.getValue()) &&
 442  442                  monthIndex == 1 ? max + 1 : max);
 443  443      }
 444  444      
 445  445    // * **********************************************
 446  446    //     I N N E R    C L A S S E S   F O L L O W
 447  447    // ***********************************************
 448  448      
 449  449      /**
 450  450       * Listener for closing the dialog box through the window close
 451  451       * menu.
 452  452       */
 453  453      private class DCWindowListener extends WindowAdapter {
 454  454      public  void windowClosing(WindowEvent e) {
 455  455          dateTimeDialogClose(false);
 456  456          }
 457  457      }
 458  458    
 459  459      /**
 460  460       * Listener for any change in the month selected through the
 461  461       * pull down menu
 462  462       */
 463  463      private class DCMonthChangeListener implements ItemListener {
 464  464      public void itemStateChanged(ItemEvent e) {
 465  465          fixDateField();
 466  466      }
 467  467      }
 468  468  
 469  469      /**
 470  470       * Listener for all the buttons. The listener is shared for the sake
 471  471       * of reducing the number of overall listeners.
 472  472       * TBD: I18N the help
 473  473       */
 474  474      private class DCButtonListener implements ActionListener {
 475  475      public void actionPerformed(ActionEvent e) {
 476  476          if (e.getSource() == ok) {
 477  477          DateTimeDialog.this.dateTimeDialogClose(true);
 478  478          }
 479  479          else
 480  480          if (e.getSource() == cancel) {
 481  481            DateTimeDialog.this.dateTimeDialogClose(false);
 482  482          }
 483  483          else
  
    | ↓ open down ↓ | 483 lines elided | ↑ open up ↑ | 
 484  484            if (e.getSource() == now) {
 485  485              DateTimeDialog.this.setCurrentTime();
 486  486            }
 487  487            else
 488  488              if (e.getSource() == midnight) {
 489  489                  DateTimeDialog.this.setMidnight();
 490  490              }
 491  491              else
 492  492                 if (e.getSource() == help) {
 493  493                  if (hd != null)
 494      -                  hd.show();
      494 +                  hd.setVisible(true);
 495  495                  else {
 496  496                    hd = new
 497  497                      HelpDialog(DateTimeDialog.this.parent,
 498  498                          getString("Help for Date and Time Dialog"), false);
 499  499                    hd.setVisible(true);
 500  500                    hd.setText(getString(hrb, "DateTimeDialogHelp"));
 501  501                     }
 502  502                  }
 503  503          } // actionPerformed
 504  504      }
 505  505  
 506  506      /**
 507  507       * Listener for any change in focus with respect to the text
 508  508       * fields. When a text field is going out of focus, it detemines if the
 509  509       * text value in it is valid. If not, it returns focus to that text
 510  510       * field.
 511  511       */
 512  512      private class DCFocusListener extends FocusAdapter {
 513  513  
 514  514          public void focusLost(FocusEvent e) {
 515  515          if (!checkErrorAndSet((DCTextField)e.getSource()))
 516  516            ((DCTextField)e.getSource()).requestFocus();
 517  517          }
 518  518      }
 519  519  
 520  520      /**
 521  521       * The string representation of the dialog box.
 522  522       * @return a String which contians the date and time in locale
 523  523       * default format, but to MEDIUM length formatting style.
 524  524       */
 525  525      public String toString() {
 526  526          calendar = new GregorianCalendar(year.getValue(),
 527  527                                         month.getSelectedIndex(),
 528  528                                         date.getValue(),
 529  529                                         hour.getValue(),
 530  530                                         minute.getValue(),
 531  531                                         second.getValue());
 532  532          return df.format(calendar.getTime());
 533  533      }
 534  534  
 535  535      /**
 536  536       * Call rb.getString(), but catch exception and return English
 537  537       * key so that small spelling errors don't cripple the GUI
 538  538       *
 539  539       */
 540  540      private static final String getString(String key) {
 541  541      return (getString(rb, key));
 542  542      }
 543  543  
 544  544      private static final String getString(ResourceBundle rb, String key) {
 545  545      try {
 546  546          String res = rb.getString(key);
 547  547          return res;
 548  548      } catch (MissingResourceException e) {
 549  549          System.out.println("Missing resource "+key+", using English.");
 550  550          return key;
 551  551          }
 552  552      }
 553  553  
 554  554      /*
 555  555      public static final void main(String args[]) {
 556  556      Frame f = new Frame();
 557  557      //  while (true){
 558  558          DateTimeDialog d = new DateTimeDialog(f, Color.white, Color.black);
 559  559          d.setVisible(true);
 560  560          System.out.println(d.toString());
 561  561        //    }
 562  562      }
 563  563      */
 564  564  }
  
    | ↓ open down ↓ | 60 lines elided | ↑ open up ↑ | 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX