Skip to Content.
Sympa Menu

perfsonar-dev - perfsonar: r4893 - branches/perfsonarui/src/org/perfsonar/perfsonarui/ui/panels

Subject: perfsonar development work

List archive

perfsonar: r4893 - branches/perfsonarui/src/org/perfsonar/perfsonarui/ui/panels


Chronological Thread 
  • From:
  • To:
  • Subject: perfsonar: r4893 - branches/perfsonarui/src/org/perfsonar/perfsonarui/ui/panels
  • Date: Mon, 19 Jan 2009 10:08:46 -0500

Author: nina
Date: 2009-01-19 10:08:46 -0500 (Mon, 19 Jan 2009)
New Revision: 4893

Modified:
branches/perfsonarui/src/org/perfsonar/perfsonarui/ui/panels/DatePanel.java
Log:
replacing custom DatePanel with swingx implementation

Modified:
branches/perfsonarui/src/org/perfsonar/perfsonarui/ui/panels/DatePanel.java
===================================================================
---
branches/perfsonarui/src/org/perfsonar/perfsonarui/ui/panels/DatePanel.java
2009-01-19 13:38:44 UTC (rev 4892)
+++
branches/perfsonarui/src/org/perfsonar/perfsonarui/ui/panels/DatePanel.java
2009-01-19 15:08:46 UTC (rev 4893)
@@ -1,530 +1,58 @@
-/*
-Copyright (C) 2005-2007
-
-Contact:

-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-All we ask is that proper credit is given for our work, which includes
-- but is not limited to - adding the above copyright notice to the beginning
-of your source code files, and to any copyright notice that you may
distribute
-with programs based on this work.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU Lesser General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
-*/
-
package org.perfsonar.perfsonarui.ui.panels;

-import java.awt.BorderLayout;
-import java.awt.Color;
-import java.awt.Dimension;
-import java.awt.GridLayout;
-import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
-import java.awt.event.FocusEvent;
-import java.awt.event.FocusListener;
-import java.awt.event.ItemEvent;
-import java.awt.event.ItemListener;
-import java.awt.event.KeyAdapter;
-import java.awt.event.KeyEvent;
-import java.awt.event.KeyListener;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
-import java.awt.event.MouseListener;
+import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
-import java.util.GregorianCalendar;
import java.util.TimeZone;

-import javax.swing.AbstractAction;
-import javax.swing.BorderFactory;
-import javax.swing.JButton;
-import javax.swing.JComboBox;
-import javax.swing.JFormattedTextField;
-import javax.swing.JLabel;
-import javax.swing.JPanel;
-import javax.swing.JToolBar;
-import javax.swing.SwingConstants;
-import javax.swing.border.Border;
-
+import org.jdesktop.swingx.JXMonthView;
import org.perfsonar.perfsonarui.ui.actions.ITimeSet;
import org.perfsonar.perfsonarui.ui.actions.ITimeSetAction;

-/**
- * Calendar. Use
{@link
#addTimeAction} to assign actions that will be executed when selecting a new
date.
- * @author Nina Jeliazkova
- *
- */
-public class DatePanel extends JPanel implements FocusListener {
+public class DatePanel extends JXMonthView {
protected ArrayList<ITimeSet> timeActions = null;
- /**
+ /**
*
*/
- private static final long serialVersionUID = -8106105090087165065L;
-
- /** Names of the months. */
- private static final String[] MONTHS =
- new String[] {
- "January",
- "February",
- "March",
- "April",
- "May",
- "June",
- "July",
- "August",
- "September",
- "October",
- "November",
- "December"
- };
-
- /** Names of the days of the week. */
- private static final String[] DAYS =
- new String[] {
- "Sun",
- "Mon",
- "Tue",
- "Wed",
- "Thu",
- "Fri",
- "Sat"
- };
-
- /** Text color of the days of the weeks, used as column headers in
- the calendar. */
- private static final Color WEEK_DAYS_FOREGROUND = Color.black;
-
- /** Text color of the days' numbers in the calendar. */
- private static final Color DAYS_FOREGROUND = Color.blue;
-
- /** Background color of the selected day in the calendar. */
- private static final Color SELECTED_DAY_FOREGROUND = Color.white;
-
- /** Text color of the selected day in the calendar. */
- private static final Color SELECTED_DAY_BACKGROUND = Color.blue;
-
- /** Empty border, used when the calendar does not have the focus. */
- private static final Border EMPTY_BORDER =
BorderFactory.createEmptyBorder(1,1,1,1);
-
- /** Border used to highlight the selected day when the calendar
- has the focus. */
- private static final Border FOCUSED_BORDER =
BorderFactory.createLineBorder(Color.yellow,1);
-
- /** First year that can be selected. */
- private static final int FIRST_YEAR = 1900;
-
- /** Last year that can be selected. */
- private static final int LAST_YEAR = 2100;
-
- /** Auxiliary variable to compute dates. */
- private GregorianCalendar calendar;
-
- /** Calendar, as a matrix of labels. The first row represents the
- first week of the month, the second row, the second week, and
- so on. Each column represents a day of the week, the first is
- Sunday, and the last is Saturday. The label's text is the
- number of the corresponding day. */
- private JLabel[][] days;
-
- /** Day selection control. It is just a panel that can receive the
- focus. The actual user interaction is driven by the
- <code>DateChooser</code> class. */
- private FocusablePanel daysGrid;
-
- /** Month selection control. */
- private JComboBox month;
-
- /** Year selection control. */
- private JComboBox year;
-
- /** Day of the week (0=Sunday) corresponding to the first day of
- the selected month. Used to calculate the position, in the
- calendar
({@link
#days}), corresponding to a given day. */
- private int offset;
-
- /** Last day of the selected month. */
- private int lastDay;
-
- /** Selected day. */
- private JLabel day;
-
- protected JFormattedTextField hoursEditor;
- protected JFormattedTextField minutesEditor;
-
- protected JButton prevDay;
- protected JButton nextDay;
-
-
-
- /**
- * Custom panel that can receive the focus. Used to implement the
- * calendar control.
- **/
- private static class FocusablePanel extends JPanel
- {
- /**
- *
- */
- private static final long serialVersionUID =
2125401462387373003L;
-
-
- /**
- * Constructs a new <code>FocusablePanel</code> with the given
- * layout manager.
- *
- * @param layout layout manager
- **/
- public FocusablePanel( LayoutManager layout ) {
- super( layout );
- }
-
-
- /**
- * Always returns <code>true</code>, since
- * <code>FocusablePanel</code> can receive the focus.
- *
- * @return <code>true</code>
- **/
- @Override
- public boolean isFocusTraversable() {
- return true;
- }
- }
-
- public DatePanel(Date date) {
- super();
- addWidgets();
- select(date);
- }
+ private static final long serialVersionUID = -9154737925821794260L;
public DatePanel() {
this(Calendar.getInstance(TimeZone.getDefault()).getTime());
}
-
-
- /**
- * Initializes this <code>DateChooser</code> object. Creates the
- * controls, registers listeners and initializes the dialog box.
- **/
- private void addWidgets()
- {
- timeActions = new ArrayList<ITimeSet>();
- setLayout(new BorderLayout());
- calendar = new GregorianCalendar();
-
- month = new JComboBox(MONTHS);
- month.addItemListener( new ItemListener() {
- public void itemStateChanged(ItemEvent arg0) {
- update();
- for (int i=0; i < timeActions.size();i++)
setTime(timeActions.get(i));
- }
- });
-
- year = new JComboBox();
- for ( int i=FIRST_YEAR; i<=LAST_YEAR; i++ )
- year.addItem( Integer.toString(i) );
- year.addItemListener( new ItemListener() {
- public void itemStateChanged(ItemEvent arg0) {
- update();
- for (int i=0; i < timeActions.size();i++)
setTime(timeActions.get(i));
- }
- });
-
- days = new JLabel[7][7];
- for ( int i=0; i<7; i++ ) {
- days[0][i] = new JLabel(DAYS[i],SwingConstants.RIGHT);
- days[0][i].setForeground( WEEK_DAYS_FOREGROUND );
+ public DatePanel(Date date) {
+ super();
+ setSelectionDate(date);
+ setTraversable(true);
+ timeActions = new ArrayList<ITimeSet>();
+ addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+
setFlaggedDates(((JXMonthView)e.getSource()).getSelectionDate());
+
runActions(((JXMonthView)e.getSource()).getSelectionDate());
+ }
+ });
}
- MouseListener mouseAdapter = new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- JLabel day = (JLabel)e.getSource();
- if ( !day.getText().equals(" ") ) {
- setSelected( day );
- if (timeActions != null)
- for (int i=0; i <
timeActions.size();i++) {
- ITimeSet action =
timeActions.get(i);
- setTime(timeActions.get(i));
- if (action instanceof ITimeSetAction)
- ((ITimeSetAction)
action).actionPerformed(null);
- }
- }
- daysGrid.requestFocus();
- };
- };
- for ( int i=1; i<7; i++ )
- for ( int j=0; j<7; j++ )
- {
- days[i][j] = new JLabel(" ",SwingConstants.RIGHT);
- days[i][j].setForeground( DAYS_FOREGROUND );
- days[i][j].setBackground( SELECTED_DAY_BACKGROUND );
- days[i][j].setBorder( EMPTY_BORDER );
- days[i][j].addMouseListener( mouseAdapter );
- }
-
- KeyListener keyAdaptor = new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- int iday = getSelectedDay();
- switch ( e.getKeyCode() ) {
- case KeyEvent.VK_LEFT:
- if ( iday > 1 )
- setSelected( iday-1 );
- break;
- case KeyEvent.VK_RIGHT:
- if ( iday < lastDay )
- setSelected( iday+1 );
- break;
- case KeyEvent.VK_UP:
- if ( iday > 7 )
- setSelected( iday-7 );
- break;
- case KeyEvent.VK_DOWN:
- if ( iday <= lastDay-7 )
- setSelected( iday+7 );
- break;
- }
- };
- };
-
- daysGrid = new FocusablePanel(new GridLayout(7,7,5,0));
- daysGrid.addFocusListener( this );
- daysGrid.addKeyListener( keyAdaptor );
- for ( int i=0; i<7; i++ )
- for ( int j=0; j<7; j++ )
- daysGrid.add( days[i][j] );
- daysGrid.setBackground( Color.white );
- daysGrid.setBorder( BorderFactory.createLoweredBevelBorder() );
-
- JPanel daysPanel = new JPanel();
- daysPanel.add( daysGrid );
-
-
- prevDay = new JButton(new
AbstractAction("",ActionPanel.createImageIcon("org/perfsonar/perfsonarui/ui/nav_prev.gif"))
{
- public void actionPerformed(ActionEvent arg0) {
- int iday = getSelectedDay();
- if ( iday > 1 ) {
- setSelected( iday-1 );
- } else {
- int m = month.getSelectedIndex()-1;
- if (m >= 0) { month.setSelectedIndex(m); }
- else {
-
month.setSelectedIndex(month.getItemCount()-1);
-
year.setSelectedIndex(year.getSelectedIndex()-1);
- }
- day.setText("32");
- update();
- }
- for (int i=0; i < timeActions.size();i++)
setTime(timeActions.get(i));
-
- }
- });
- prevDay.setToolTipText("previous day");
-
- nextDay = new JButton(new
AbstractAction("",ActionPanel.createImageIcon("org/perfsonar/perfsonarui/ui/nav_next.gif"))
{
- public void actionPerformed(ActionEvent arg0) {
- int iday = getSelectedDay();
- if ( iday < lastDay ) setSelected( iday+1 );
- else {
- int m = month.getSelectedIndex()+1;
- if (m < month.getItemCount()) {
month.setSelectedIndex(m); }
- else {
- month.setSelectedIndex(0);
-
year.setSelectedIndex(year.getSelectedIndex()+1);
- }
- day.setText("1");
- update();
- }
- for (int i=0; i < timeActions.size();i++)
setTime(timeActions.get(i));
-
- }
- });
- nextDay.setToolTipText("next day");
-
- JToolBar monthYear = new JToolBar();
- monthYear.setFloatable(false);
- monthYear.add(prevDay);
- monthYear.add( month );
- monthYear.add( year );
- monthYear.add(nextDay);
-
- add(monthYear,BorderLayout.NORTH);
- add(daysPanel,BorderLayout.CENTER);
-
- setPreferredSize(new Dimension(250,200));
- setMinimumSize(new Dimension(150,150));
-
- }
-
- protected void setTime(ITimeSet action) {
- action.setStartTime(getDate().getTime()/1000+24*60*60);
- action.setTimeInterval(24*60*60);
- }
-
-
- /**
- * Gets the selected day, as an <code>int</code>. Parses the text
- * of the selected label in the calendar to get the day.
- *
- * @return the selected day or -1 if there is no day selected
- **/
- private int getSelectedDay()
- {
- if ( day == null )
- return -1 ;
- try {
- return Integer.parseInt(day.getText());
- } catch ( NumberFormatException e ) {
+ protected void runActions(Date date) {
+ if (timeActions != null)
+ for (int i=0; i < timeActions.size();i++) {
+ ITimeSet action = timeActions.get(i);
+ setTime(timeActions.get(i),date);
+ if (action instanceof ITimeSetAction)
+ ((ITimeSetAction)
action).actionPerformed(null);
+ }
}
- return -1;
- }
-
-
-
- /**
- * Sets the selected day. The day is specified as the label
- * control, in the calendar, corresponding to the day to select.
- *
- * @param newDay day to select
- **/
- private void setSelected( JLabel newDay )
- {
- if ( day != null ) {
- day.setForeground( DAYS_FOREGROUND );
- day.setOpaque( false );
- day.setBorder( EMPTY_BORDER );
- }
- day = newDay;
- day.setForeground( SELECTED_DAY_FOREGROUND );
- day.setOpaque( true );
- if ( daysGrid.hasFocus() )
- day.setBorder( FOCUSED_BORDER );
- }
-
-
-
- /**
- * Sets the selected day. The day is specified as the number of
- * the day, in the month, to selected. The function compute the
- * corresponding control to select.
- *
- * @param newDay day to select
- **/
- private void setSelected( int newDay )
- {
- setSelected( days[(newDay+offset-1)/7+1][(newDay+offset-1)%7] );
- }
-
-
-
- /**
- * Updates the calendar. This function updates the calendar panel
- * to reflect the month and year selected. It keeps the same day
- * of the month that was selected, except if it is beyond the last
- * day of the month. In this case, the last day of the month is
- * selected.
- **/
- private void update()
- {
- int iday = getSelectedDay();
- for ( int i=0; i<7; i++ ) {
- days[1][i].setText( " " );
- days[5][i].setText( " " );
- days[6][i].setText( " " );
- }
- calendar.set( Calendar.DATE, 1 );
- calendar.set( Calendar.MONTH,
month.getSelectedIndex()+Calendar.JANUARY );
- calendar.set( Calendar.YEAR, year.getSelectedIndex()+FIRST_YEAR );
- calendar.set(Calendar.HOUR, 0);
- calendar.set(Calendar.MINUTE, 0);
- calendar.set(Calendar.SECOND, 0);
- calendar.set(Calendar.MILLISECOND, 0);
-
- offset = calendar.get(Calendar.DAY_OF_WEEK)-Calendar.SUNDAY;
- lastDay = calendar.getActualMaximum(Calendar.DATE);
- for ( int i=0; i<lastDay; i++ )
- days[(i+offset)/7+1][(i+offset)%7].setText( String.valueOf(i+1) );
- if ( iday != -1 ) {
- if ( iday > lastDay )
- iday = lastDay;
- setSelected( iday );
- }
-
-
- }
-
-
- /**
- * Called when the calendar gains the focus. Just re-sets the
- * selected day so that it is redrawn with the border that
- * indicate focus.
- **/
- public void focusGained( FocusEvent e ) {
- setSelected( day );
- }
-
- /**
- * Called when the calendar loses the focus. Just re-sets the
- * selected day so that it is redrawn without the border that
- * indicate focus.
- **/
- public void focusLost( FocusEvent e ) {
- setSelected( day );
- }
-
-
- /**
- * Selects a date. Displays the dialog box, with a given date as
- * the selected date, and allows the user select a new date.
- *
- * @param date initial date
- *
- * @return the new date selected or <code>null</code> if the user
- * press "Cancel" or closes the dialog box
- **/
- public void select( Date date )
- {
- calendar.setTime( date );
- int _day = calendar.get(Calendar.DATE);
- int _month = calendar.get(Calendar.MONTH);
- int _year = calendar.get(Calendar.YEAR);
-
- year.setSelectedIndex( _year-FIRST_YEAR );
- month.setSelectedIndex( _month-Calendar.JANUARY );
- setSelected( _day );
-
- /*
- okClicked = false;
- show();
- if ( !okClicked )
- return null;
- */
-
- }
- public Date getDate() {
- calendar.set( Calendar.DATE, getSelectedDay() );
- calendar.set( Calendar.MONTH,
month.getSelectedIndex()+Calendar.JANUARY );
- calendar.set( Calendar.YEAR,
year.getSelectedIndex()+FIRST_YEAR );
- calendar.set(Calendar.HOUR_OF_DAY,0);
- return calendar.getTime();
- }
public ArrayList<ITimeSet> getTimeActions() {
return timeActions;
}
public void addTimeAction(ITimeSet timeAction) {
timeActions.add(timeAction);
- setTime(timeAction);
+ setTime(timeAction, getSelectionDate());
}
-
+ protected void setTime(ITimeSet action, Date date) {
+ action.setStartTime(date.getTime()/1000+24*60*60);
+ action.setTimeInterval(24*60*60);
+ }
+ public Date getDate() {
+ return super.getSelectionDate();
+ }
}
-



  • perfsonar: r4893 - branches/perfsonarui/src/org/perfsonar/perfsonarui/ui/panels, svnlog, 01/19/2009

Archive powered by MHonArc 2.6.16.

Top of Page