/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library 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 Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.ts;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import com.jstatcom.component.StdMessages;
import com.jstatcom.component.TopFrameReference;
import com.jstatcom.io.DataHandlerTypes;
import com.jstatcom.io.FileSupport;
import com.jstatcom.model.JSCConstants;

/**
 * This class holds default actions used by the TSList. The
 * defined actions override execute(TSList tsList) to provide the
 * a specific function on the underlying TSList, for example
 * deleting all selected entries, changing the types of selected entires, etc..
 * The actions can be invoked via keys and/or items in the
 * DefaultTSListPopup that is opened via a RIGHT click over the
 * TSList.
 * 

* New actions may be provided by subclassing this abstract class and setting * the new action instance to the default DefaultTSListPopup. * Also, existing actions may be removed from there. *

* If new actions should also be accessible via keys, they can be * added to the DefaultTSKeyListener. *

* This class implements the typesave enum pattern (Joshua Bloch, "Effective * Java", Item 21) . If you compare TSActionTypes, always * use the == operator. * * @author Markus Kraetzig */ public abstract class TSActionTypes extends AbstractAction implements TSListAccessor { private static final Logger log = Logger.getLogger(TSActionTypes.class); /** * The name of the function. */ private final String name; /** * Reference to the current TSList. */ private TSList tSList = null; /** * Deletes selected time series after asking to confirm. */ public static final TSActionTypes DELETE = new TSActionTypes("Delete") { /** * Delete selected TS, ask before. */ public void doAction(TSList tsList) { TS selection[] = tsList.getSelectedTS(); if (selection.length == 1) { String name = selection[0].name(); if (JOptionPane.YES_OPTION == JOptionPane .showConfirmDialog(TopFrameReference.getTopFrameRef(), "Really delete variable \"" + name + "\"?", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)) { TSHolder.getInstance().removeTS(name); } } if (selection.length > 1) { if (JOptionPane.YES_OPTION == JOptionPane .showConfirmDialog(TopFrameReference.getTopFrameRef(), "Really delete selected variables?", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)) { for (int j = 0; j < selection.length; j++) TSHolder.getInstance().removeTS(selection[j].name()); } } } }; /** * Renames time series by showing an input dialog. */ public static final TSActionTypes RENAME = new TSActionTypes("Rename") { /** * Rename selected TS. */ public void doAction(TSList tsList) { TS selection[] = tsList.getSelectedTS(); if (selection.length > 0) { TS ts = selection[0]; String name = ts.name(); Object obj = JOptionPane .showInputDialog(TopFrameReference.getTopFrameRef(), "Enter new name for \"" + name + "\"", "Rename", javax.swing.JOptionPane.PLAIN_MESSAGE, null, null, name); if (obj == null) return; String newName = obj.toString().trim(); String error = JSCConstants.isValidName(newName); if (error != null) { StdMessages.errorInput(error); return; } if (!newName.toString().equals(name)) { if (TSHolder.getInstance().contains(newName) && !newName.toString().equalsIgnoreCase(name)) { StdMessages .errorInput("Variable \"" + newName + "\" already exists.\nPlease choose a different name."); return; } // Ok, remove old reference and add new TSHolder.getInstance().removeTS(name); ts.setName(newName); TSHolder.getInstance().addTS(ts); } ; } } }; /** * Transforms time series by invoking a dialog. */ public static final TSActionTypes TRANSFORM = new TSActionTypes("Transform") { private TSTransDialog transDialog = null; /** * Transform selected TS. */ public void doAction(TSList tsList) { if (transDialog == null) transDialog = new TSTransDialog(TopFrameReference .getTopFrameRef(), true); transDialog.setTSList(tsList); transDialog.setVisible(true); } }; /** * Creates dummy variables by invoking a dialog. */ public static final TSActionTypes CREATE_DUMMY = new TSActionTypes( "Create Dummy") { private TSDummyDialog dummyDialog = null; /** * Create Dummy for selected TS. */ public void doAction(TSList tsList) { if (dummyDialog == null) dummyDialog = new TSDummyDialog(TopFrameReference .getTopFrameRef(), true); dummyDialog.setTSList(tsList); dummyDialog.setVisible(true); } }; /** * Shows the description for prohject of the 1st selected time series. */ public static final TSActionTypes SHOW_DESCRIPTION = new TSActionTypes( "Show Description") { /** * Description of 1st selected TS. */ public void doAction(TSList tsList) { TS selection[] = tsList.getSelectedTS(); if (selection.length > 0) { List list = new ArrayList(); for (int i = 0; i < selection.length; i++) { TSProject proj = selection[i].project(); if (!list.contains(proj)) list.add(proj); } StringBuffer des = new StringBuffer(); String marker = "\n------------------------------------\n"; for (TSProject proj : list) des.append(proj.getDescription().trim() + marker); des.delete(des.length() - marker.length(), des.length()); JOptionPane.showMessageDialog(TopFrameReference .getTopFrameRef(), des.toString(), "Project Description", JOptionPane.INFORMATION_MESSAGE); return; } } }; /** * Saves selected time series in a Dat file. */ public static final TSActionTypes SAVE_DAT = new TSActionTypes( "Save Data to File") { private TSDescDialog descDialog = null; /** * Save all selected TS as .dat file. */ public void doAction(TSList tsList) { if (descDialog == null) descDialog = new TSDescDialog(TopFrameReference .getTopFrameRef(), true); descDialog.setTSList(tsList); descDialog.setVisible(true); if (descDialog.isCanceled()) return; // select file and save generated buffer File file = FileSupport.getInstance().saveDataFile( TopFrameReference.getTopFrameRef(), null, DataHandlerTypes.EXCEL, DataHandlerTypes.MATLAB, DataHandlerTypes.GAUSS_DAT, DataHandlerTypes.GAUSS_FMT, DataHandlerTypes.DAT); if (file == null) // cancelled return; DataHandlerTypes handlerType = FileSupport.getInstance() .getSelectedSaveHandlerType(); try { handlerType.getHandler().exportTS(file, tsList.getSelectedTS(), descDialog.getDescription()); } catch (Throwable ex) { String msg = "Error exporting data to file \"" + file.getAbsolutePath() + "\"."; String message = msg + "\n" + ex.getMessage(); log.error(msg, ex); StdMessages.error(message, "Data Export Error"); } } }; /** * Invokes the TSEditorDialog. */ public static final TSActionTypes EDIT_TS = new TSActionTypes( "Edit Time Series") { private TSEditDialog editDialog = null; /** * Edit selected TS. */ public void doAction(TSList tsList) { if (editDialog == null) editDialog = new TSEditDialog(TopFrameReference .getTopFrameRef(), false); editDialog.setTSList(tsList); editDialog.setVisible(true); } }; /** * Sets type of selected TS to endogenous. */ public static final TSActionTypes ENDOGENOUS = new TSActionTypes( "Endogenous") { /** * Sets type. */ public void doAction(TSList tsList) { TS[] sel = tsList.getSelectedTS(); for (int i = 0; i < sel.length; i++) sel[i].setTSType(TSTypes.ENDOGENOUS); } }; /** * Sets type of selected TS to exogenous. */ public static final TSActionTypes EXOGENOUS = new TSActionTypes("Exogenous") { /** * Sets type. */ public void doAction(TSList tsList) { TS[] sel = tsList.getSelectedTS(); for (int i = 0; i < sel.length; i++) sel[i].setTSType(TSTypes.EXOGENOUS); } }; /** * Sets type of selected TS to deterministic. */ public static final TSActionTypes DETERMINISTIC = new TSActionTypes( "Deterministic") { /** * Sets type. */ public void doAction(TSList tsList) { TS[] sel = tsList.getSelectedTS(); for (int i = 0; i < sel.length; i++) sel[i].setTSType(TSTypes.DETERMINISTIC); } }; /** * TSActionTypes constructor with a descriptive string. */ protected TSActionTypes(String name) { this(name, null); } /** * TSActionTypes constructor with a descriptive string and an * icon. */ protected TSActionTypes(String name, Icon icon) { super(name, icon); this.name = name; } /** * Tries to retrieve TSList and triggers doAction. * * @param e * an action event */ public final void actionPerformed(ActionEvent e) { doAction(tSList); } /** * This defines the operation that is actually carried out by the action. * The method must be overridden if you define a new action. * * @param tsList * the TSList to operate on */ public abstract void doAction(TSList tsList); /** * Enables/disables the action according to the state of the underlying * list. The default is that the action is disabled if nothing is selected. * This can be changed by overriding the method. Apart from that, the * currently available TSList is set. * * @param tsList * the TSList to be referenced by the action */ public void enableForTSList(TSList tsList) { setEnabled(tsList.getSelectedIndices().length != 0); this.tSList = tsList; } /** * Override prevention method to enforce a.equals(b) if and * only if a==b. * * @param o * object to compare with * @return true if equal, false otherwise */ public final boolean equals(Object o) { return super.equals(o); } /** * Override prevention method invokes super method. * * @return hash code */ public final int hashCode() { return super.hashCode(); } /** * Returns the name of the actiontype, do not use for comparisons. * * @return a string representation */ public final String toString() { return name; } }

Ads help maintain this website.