001    /**
002     * planningtool - A Planning Tool with Critiquing Support.
003     * 
004     * Copyright (C) 2006 olale
005    
006     * This program is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU General Public License
008     * as published by the Free Software Foundation; either version 2
009     * of the License, or (at your option) any later version.
010    
011     * This program is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015    
016     * You should have received a copy of the GNU General Public License
017     * along with this program; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019    
020     * Contact information: 
021     * E-mail: olale@ida.liu.se
022     *         olale@lysator.liu.se
023     */
024    package se.liu.ida.critiquer.activities.parameters;
025    
026    import java.awt.event.ActionEvent;
027    import java.awt.event.ActionListener;
028    import java.util.HashSet;
029    
030    import javax.swing.BorderFactory;
031    import javax.swing.JLabel;
032    import javax.swing.JOptionPane;
033    import javax.swing.JPanel;
034    import javax.swing.JTextField;
035    
036    import se.liu.ida.critiquer.activities.Activity;
037    import se.liu.ida.critiquer.mics.ReferenceHolder;
038    
039    
040    public class NameParameter extends AbstractTaskViewParameter<JPanel,String> {
041    
042            /**
043             * 
044             */
045            private static final long       serialVersionUID        = 8072678938385535268L;
046    
047            public NameParameter(String name,Activity activity) {
048                    super(name,activity);
049                    this.value=activity.type+" "+activity.getNumActivities();
050            }
051            
052            public NameParameter(Activity activity) {
053                    super("name",activity);
054                    this.value=activity.type+" "+activity.getNumActivities();
055            }
056            private transient JTextField nameField;
057            
058            @Override
059            public String toString() {
060                    return value;
061            }
062    
063            public void initComponent() {
064                    JPanel pane = new JPanel();
065                    pane.setBorder(BorderFactory.createTitledBorder("Name:"));
066                    nameField=new JTextField(this.value);
067                    selectText();
068                    nameField.addActionListener(new ActionListener(){
069    
070                            public void actionPerformed(ActionEvent action) {
071                            
072                                    if (uniqueName(nameField.getText())) {
073                                            setValue(nameField.getText());
074                                            selectText();
075                                    } else {
076                                            JOptionPane.showMessageDialog(component,"Activity with that name already exists");
077                                    }
078                            }
079                            
080                    });
081                    pane.add(new JLabel("Edit activity name:"));
082                    pane.add(nameField);
083                    component=pane;
084            }
085    
086    
087            protected boolean uniqueName(String text) {
088                    boolean ok=true;
089                    HashSet<Activity> activities = ReferenceHolder.allActivities;
090                    for (Activity activity : activities) {
091                            if (activity.toString().equals(text)) {
092                                    ok=false;
093                                    break;
094                            }
095                    }
096                    return ok;
097            }
098    
099            private void selectText() {
100                    nameField.setSelectionStart(0);
101                    nameField.setSelectionEnd(nameField.getText().length());
102            }
103    
104    }