001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: aschultz $'
006 * '$Date: 2010-12-23 19:01:04 +0000 (Thu, 23 Dec 2010) $' 
007 * '$Revision: 26600 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gui;
031
032import java.awt.Component;
033import java.awt.event.ActionEvent;
034
035import javax.swing.JOptionPane;
036
037import org.apache.commons.logging.Log;
038import org.apache.commons.logging.LogFactory;
039import org.kepler.util.RenameUtil;
040
041import ptolemy.actor.gui.TableauFrame;
042import ptolemy.kernel.ComponentEntity;
043import ptolemy.kernel.util.NamedObj;
044import ptolemy.vergil.toolbox.FigureAction;
045
046/**
047 * 
048 * @author Aaron Schultz
049 * 
050 */
051public class RenameComponentEntityAction extends FigureAction {
052
053        private static final Log log = LogFactory
054                        .getLog(RenameComponentEntityAction.class.getName());
055        private static final boolean isDebugging = log.isDebugEnabled();
056
057        private final static String LABEL = "Rename";
058
059        private TableauFrame _parent;
060        private NamedObj _obj;
061
062        public RenameComponentEntityAction(TableauFrame parent) {
063                super(LABEL);
064
065                _parent = parent;
066
067                if (parent == null) {
068                        IllegalArgumentException iae = new IllegalArgumentException(
069                                        "RenameComponentEntityAction constructor received NULL argument for TableauFrame");
070                        iae.fillInStackTrace();
071                        throw iae;
072                }
073
074                this.putValue("tooltip", "Open the Rename Component Entity Dialog.");
075        }
076
077        public void setObject(NamedObj no) {
078                _obj = no;
079        }
080
081        public void actionPerformed(ActionEvent e) {
082                super.actionPerformed(e);
083
084                if (isDebugging)
085                        log.debug("RenameComponentEntityAction.actionPerformed()");
086
087                NamedObj object = null;
088                if (_obj == null) {
089                        object = getTarget();
090                } else {
091                        object = _obj;
092                }
093
094                if (isDebugging)
095                        log.debug(object.getClass().getName());
096
097                if (object instanceof ComponentEntity) {
098
099                        // TODO very similar to code in RenameComponentEntityAction,
100                        // find a way to merge?
101                                
102                        Component parentComponent = (Component) _parent;
103                        String message = "Enter a new name: ";
104                        String warnMessage = "ERROR name cannot contain the < sign";
105                        String title = "Rename";
106                        int messageType = JOptionPane.QUESTION_MESSAGE;
107                        String initialValue = object.getName();
108
109                        String newName = (String) JOptionPane.showInputDialog(
110                                        parentComponent, message, title, messageType, null, null,
111                                        initialValue);
112                        if (newName == null) {
113                                // user hit the cancel button
114                                return;
115                        }
116                        
117                int lessThan = newName.indexOf("<");
118                if (lessThan >= 0){
119                        JOptionPane.showMessageDialog(parentComponent, warnMessage, "Error",
120                                JOptionPane.ERROR_MESSAGE);
121                        return;
122                }
123                        
124                        try {
125
126                                RenameUtil.renameComponentEntity((ComponentEntity) object,
127                                                newName);
128                                _parent.setTitle(object.getName());
129
130                        } catch (Exception ex) {
131                                ex.printStackTrace();
132                                JOptionPane.showMessageDialog(_parent, "A problem occured.");
133                        }
134                }
135
136        }
137
138}