/* File: DasTimeRangeSelector.java * Copyright (C) 2002-2003 The University of Iowa * Created by: Jeremy Faden * Jessica Swanner * Edward E. West * * This file is part of the das2 library. * * das2 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. * * 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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.das2.components; import org.das2.datum.DatumRange; import org.das2.datum.Units; import org.das2.datum.Datum; import org.das2.datum.DatumRangeUtil; import org.das2.util.DasExceptionHandler; /** * * @author jbf */ import org.das2.datum.TimeUtil; import org.das2.event.TimeRangeSelectionEvent; import org.das2.event.TimeRangeSelectionListener; import org.das2.system.DasLogger; import java.awt.CardLayout; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.event.EventListenerList; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.prefs.*; import javax.swing.*; import org.das2.DasApplication; import org.das2.datum.DatumUtil; public class DasTimeRangeSelector extends JPanel implements TimeRangeSelectionListener { private DatumRange range= null; JTextField idStart= null; JTextField idStop= null; JButton viewButton= null; JPanel startStopModePane=null; CardLayout cardLayout= null; boolean updateRangeString= false; // true indicates use formatted range string in start time cell. /** Utility field used by event firing mechanism. */ private EventListenerList listenerList = null; /** Action that is associated with the previous button. * Access is given to subclasses so that other widgets can be associated * with this action (Popup menu, etc). */ protected final Action previousAction = new AbstractAction("<<") { public void actionPerformed(ActionEvent e) { fireTimeRangeSelectedPrevious(); } }; /** Action that is associated with the next button. * Access is given to subclasses so that other widgets can be associated * with this action (Popup menu, etc). */ protected final Action nextAction = new AbstractAction(">>") { public void actionPerformed(ActionEvent e) { fireTimeRangeSelectedNext(); } }; protected final Action rangeAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { fireTimeRangeSelected(); } }; private boolean favoritesEnabled= false; private List favoritesList= null; private JPopupMenu favoritesMenu= null; private final int FAVORITES_LIST_SIZE= 5; private String favoritesGroup; private JButton favoritesButton; private JPanel timesPane; private JComboBox rangeComboBox; /** Creates a new instance of DasTimeRangeSelector */ public DasTimeRangeSelector() { super(); updateRangeString= Preferences.userNodeForPackage(this.getClass()).getBoolean("updateRangeString", false); buildComponents(); Datum tnow= TimeUtil.prevMidnight( TimeUtil.now().convertTo(Units.us2000) ); this.range= new DatumRange( tnow, TimeUtil.next( TimeUtil.DAY, tnow ) ); update(); } private Action getModeAction() { return new AbstractAction("mode") { public void actionPerformed( ActionEvent e ) { updateRangeString= !updateRangeString; Preferences.userNodeForPackage(this.getClass()).putBoolean("updateRangeString", updateRangeString ); revalidateUpdateMode(); update(); } }; } private void revalidateUpdateMode() { if ( updateRangeString ) { //idStop.setColumns(8); idStart.setColumns(28); idStop.setVisible(false); viewButton.setVisible(true); //cardLayout.show( timesPane, "range" ); } else { idStart.setColumns(18); // idStop.setColumns(18); idStop.setVisible(true); //cardLayout.show( timesPane, "startStop" ); } startStopModePane.revalidate(); } private void buildComponents() { this.setLayout(new FlowLayout()); JButton b= new JButton(); b.setAction(previousAction); b.setActionCommand("previous"); b.setToolTipText("Scan back in time"); this.add(b); startStopModePane= new JPanel(new FlowLayout()); cardLayout= new CardLayout(); timesPane= new JPanel( cardLayout ); JPanel startStopPane2= new JPanel(new FlowLayout()); idStart= new JTextField(18); idStart.setAction(rangeAction); idStart.setActionCommand("startTime"); startStopPane2.add(idStart); idStop= new JTextField(18); idStop.addActionListener(rangeAction); idStop.setActionCommand("endTime"); startStopPane2.add(idStop); timesPane.add( startStopPane2, "startStop" ); startStopModePane.add( timesPane ); favoritesButton= new JButton("v"); favoritesButton.setToolTipText("recent entry times"); favoritesButton.setPreferredSize(new Dimension( 20,20 ) ); favoritesButton.setVisible(false); startStopModePane.add(favoritesButton); viewButton= new JButton(getModeAction()); viewButton.setToolTipText("input mode: start/end vs time range string"); viewButton.setPreferredSize(new Dimension( 20,20 ) ); startStopModePane.add(viewButton); this.add(startStopModePane); b= new JButton(); b.setAction(nextAction); b.setActionCommand("next"); b.setToolTipText("Scan forward in time"); this.add(b); revalidateUpdateMode(); } public DasTimeRangeSelector(Datum startTime, Datum endTime) { this(new DatumRange( startTime, endTime )); } public DasTimeRangeSelector( DatumRange range ) { this(); this.range= range; update(); } private void parseRange() { boolean updateRangeString0= updateRangeString; if ( idStop.getText().equals("") ) { DatumRange dr; try { String rangeString= idStart.getText(); if ( rangeString.equals("") ) { rangeString= (String)rangeComboBox.getEditor().getItem(); } dr= DatumRangeUtil.parseTimeRange(rangeString); DatumRange oldRange= range; range= dr; updateRangeString= true; firePropertyChange( "range", oldRange, range ); } catch ( ParseException e ) { DasApplication.getDefaultApplication().getExceptionHandler().handle(e); } } else { updateRangeString= false; try { Datum s1= TimeUtil.create(idStart.getText()); Datum s2= TimeUtil.create(idStop.getText()); DatumRange oldRange= range; range= new DatumRange(s1,s2); firePropertyChange( "range", oldRange, range ); } catch ( ParseException e ) { DasApplication.getDefaultApplication().getExceptionHandler().handle(e); } } if ( updateRangeString!=updateRangeString0 ) Preferences.userNodeForPackage(getClass()).putBoolean("updateRangeString", updateRangeString ); return; } private void refreshFavorites() { favoritesMenu.removeAll(); for ( Iterator i= favoritesList.iterator(); i.hasNext(); ) { final String fav= (String) i.next(); Action favAction= new AbstractAction( fav ) { public void actionPerformed( ActionEvent e ) { DasTimeRangeSelector.this.setRange( DatumRangeUtil.parseTimeRangeValid(fav) ); fireTimeRangeSelected(new TimeRangeSelectionEvent(this,range)); } }; favoritesMenu.add( favAction ); } } private void buildFavorites( ) { String favorites= Preferences.userNodeForPackage(getClass()).get( "timeRangeSelector.favorites."+favoritesGroup, "" ); String[] ss= favorites.split("\\|\\|"); favoritesList= new ArrayList(); for ( int i=0; i=0; i-=2) { if (listeners[i]==TimeRangeSelectionListener.class) { String logmsg= "fire event: "+this.getClass().getName()+"-->"+listeners[i+1].getClass().getName()+" "+event; DasLogger.getLogger( DasLogger.GUI_LOG ).fine(logmsg); ((org.das2.event.TimeRangeSelectionListener)listeners[i+1]).timeRangeSelected(event); ((TimeRangeSelectionListener)listeners[i+1]).timeRangeSelected(event); } } } public Dimension getMaximumSize() { return super.getPreferredSize(); } public Dimension getMinimumSize() { return super.getPreferredSize(); } private void saveFavorites() { if ( favoritesList.size()==0 ) return; StringBuffer favorites= new StringBuffer( (String)favoritesList.get(0) ); for ( int i=1; i