/** Constructs cell editor. */ public CellEditor() { super(new JFormattedTextField()); final JFormattedTextField ftf = (JFormattedTextField) getComponent(); // Set GUI behaviour of text field ftf.setValue(null); ftf.setHorizontalAlignment(JTextField.LEADING); ftf.setFocusLostBehavior(JFormattedTextField.PERSIST); // Set that one click on cell is enough for editing setClickCountToStart(1); // Special handling code for ENTER ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check"); ftf.getActionMap() .put( "check", new AbstractAction() { public void actionPerformed(ActionEvent e) { if (!ftf.isEditValid()) { if (askEditOrRevert(ftf, null)) { ftf.setValue(ftf.getValue()); ftf.postActionEvent(); } } else try { ftf.commitEdit(); ftf.postActionEvent(); } catch (java.text.ParseException exc) { // nothing to do } } }); }
ButtonAction( String name, String mnemonic, String accelerator, String image, String shortDescription, String longDescription, int actionId) { if (name != null) { putValue(Action.NAME, bundle.getString(name)); } if (mnemonic != null) { String mnemonicString = bundle.getString(mnemonic); if (mnemonicString != null && mnemonicString.length() > 0) { putValue(Action.MNEMONIC_KEY, new Integer(bundle.getString(mnemonic).charAt(0))); } } if (accelerator != null) { String acceleratorString = bundle.getString(accelerator); if (accelerator != null && acceleratorString.length() > 0) { putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(acceleratorString)); } } if (image != null) { String imageName = bundle.getString(image); if (imageName != null && imageName.length() > 0) { imageName = "images/" + imageName + SMALL; URL url = this.getClass().getResource(imageName); if (url != null) { putValue(Action.SMALL_ICON, new ImageIcon(url)); } } } if (shortDescription != null) { String shortString = bundle.getString(shortDescription); if (shortString != null & shortString.length() > 0) { putValue(Action.SHORT_DESCRIPTION, shortString); } } if (longDescription != null) { String longString = bundle.getString(longDescription); if (longString != null && longString.length() > 0) { putValue(Action.LONG_DESCRIPTION, longString); } } putValue("buttonAction", new Integer(actionId)); }
static { // Set up the input map that will be shared by all instances inputMap.put(KeyStroke.getKeyStroke("BACK_SPACE"), "setNullDate"); inputMap.put(KeyStroke.getKeyStroke("DELETE"), "setNullDate"); inputMap.put(KeyStroke.getKeyStroke("shift LEFT"), "yearBackward"); inputMap.put(KeyStroke.getKeyStroke("shift RIGHT"), "yearForward"); inputMap.put(KeyStroke.getKeyStroke("LEFT"), "monthBackward"); inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "monthForward"); actionMap.put("setNullDate", setNullDate); actionMap.put("yearBackward", yearBackward); actionMap.put("yearForward", yearForward); actionMap.put("monthBackward", monthBackward); actionMap.put("monthForward", monthForward); }
/** * Set up the calendar panel with the basic layout and components. These are not date specific. */ private void createCalendarComponents() { // The date panel will hold the calendar and/or the time spinner JPanel datePanel = new JPanel(new BorderLayout(2, 2)); // Create the calendar if we are displaying a calendar if ((selectedComponents & DISPLAY_DATE) > 0) { formatMonth = new SimpleDateFormat("MMM", locale); formatWeekDay = new SimpleDateFormat("EEE", locale); // Set up the shared keyboard bindings setInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, inputMap); setActionMap(actionMap); // Set up the decrement buttons yearDecrButton = new JButton( new ButtonAction( "YearDecrButton", "YearDecrButtonMnemonic", "YearDecrButtonAccelerator", "YearDecrButtonImage", "YearDecrButtonShort", "YearDecrButtonLong", YEAR_DECR_BUTTON)); monthDecrButton = new JButton( new ButtonAction( "MonthDecrButton", "MonthDecrButtonMnemonic", "MonthDecrButtonAccelerator", "MonthDecrButtonImage", "MonthDecrButtonShort", "MonthDecrButtonLong", MONTH_DECR_BUTTON)); JPanel decrPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0)); decrPanel.add(yearDecrButton); decrPanel.add(monthDecrButton); // Set up the month/year label monthYearLabel = new JLabel(); monthYearLabel.setHorizontalAlignment(JLabel.CENTER); // Set up the increment buttons monthIncrButton = new JButton( new ButtonAction( "MonthIncrButton", "MonthIncrButtonMnemonic", "MonthIncrButtonAccelerator", "MonthIncrButtonImage", "MonthIncrButtonShort", "MonthIncrButtonLong", MONTH_INCR_BUTTON)); yearIncrButton = new JButton( new ButtonAction( "YearIncrButton", "YearIncrButtonMnemonic", "YearIncrButtonAccelerator", "YearIncrButtonImage", "YearIncrButtonShort", "YearIncrButtonLong", YEAR_INCR_BUTTON)); JPanel incrPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0)); incrPanel.add(monthIncrButton); incrPanel.add(yearIncrButton); // Put them all together JPanel monthYearNavigator = new JPanel(new BorderLayout(2, 2)); monthYearNavigator.add(decrPanel, BorderLayout.WEST); monthYearNavigator.add(monthYearLabel); monthYearNavigator.add(incrPanel, BorderLayout.EAST); // Set up the day panel JPanel dayPanel = new JPanel(new GridLayout(7, 7)); int firstDay = displayCalendar.getFirstDayOfWeek(); // Get the week day labels. The following technique is used so // that we can start the calendar on the right day of the week and // we can get the week day labels properly localized Calendar temp = Calendar.getInstance(locale); temp.set(2000, Calendar.MARCH, 15); while (temp.get(Calendar.DAY_OF_WEEK) != firstDay) { temp.add(Calendar.DATE, 1); } dayOfWeekLabels = new JLabel[7]; for (int i = 0; i < 7; i++) { Date date = temp.getTime(); String dayOfWeek = formatWeekDay.format(date); dayOfWeekLabels[i] = new JLabel(dayOfWeek); dayOfWeekLabels[i].setHorizontalAlignment(JLabel.CENTER); dayPanel.add(dayOfWeekLabels[i]); temp.add(Calendar.DATE, 1); } // Add all the day buttons dayButtons = new JToggleButton[6][7]; dayGroup = new ButtonGroup(); DayListener dayListener = new DayListener(); for (int row = 0; row < 6; row++) { for (int day = 0; day < 7; day++) { dayButtons[row][day] = new JToggleButton(); dayButtons[row][day].addItemListener(dayListener); dayPanel.add(dayButtons[row][day]); dayGroup.add(dayButtons[row][day]); } } // We add this special button to the button group, so we have a // way of unselecting all the visible buttons offScreenButton = new JToggleButton("X"); dayGroup.add(offScreenButton); // Combine the navigators and days datePanel.add(monthYearNavigator, BorderLayout.NORTH); datePanel.add(dayPanel); } // Create the time spinner field if we are displaying the time if ((selectedComponents & DISPLAY_TIME) > 0) { // Create the time component spinnerDateModel = new SpinnerDateModel(); spinnerDateModel.addChangeListener(new TimeListener()); spinner = new JSpinner(spinnerDateModel); JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(spinner, timePattern); dateEditor.getTextField().setEditable(false); dateEditor.getTextField().setHorizontalAlignment(JTextField.CENTER); spinner.setEditor(dateEditor); // Set the input/action maps for the spinner. (Only BACK_SPACE // seems to work!) InputMap sim = new InputMap(); sim.put(KeyStroke.getKeyStroke("BACK_SPACE"), "setNullDate"); sim.put(KeyStroke.getKeyStroke("DELETE"), "setNullDate"); sim.setParent(spinner.getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)); ActionMap sam = new ActionMap(); sam.put( "setNullDate", new AbstractAction("setNullDate") { public void actionPerformed(ActionEvent e) { JCalendar.this.setDate(null); } }); sam.setParent(spinner.getActionMap()); spinner.setInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, sim); spinner.setActionMap(sam); // Create a special panel for the time display JPanel timePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 2, 2)); timePanel.add(spinner); // Now add it to the bottom datePanel.add(timePanel, BorderLayout.SOUTH); } setLayout(new BorderLayout(2, 2)); add(datePanel); // Add today's date at the bottom of the calendar/time, if needed if (isTodayDisplayed) { Object[] args = {new Date()}; String todaysDate = MessageFormat.format(bundle.getString("Today"), args); todaysLabel = new JLabel(todaysDate); todaysLabel.setHorizontalAlignment(JLabel.CENTER); // Add today's date at the very bottom add(todaysLabel, BorderLayout.SOUTH); } }