// File: SGRenameDialog.java // Classes: SGRenameDialog // Author: Kedar Patankar package edu.neu.ccs.demeter.tools.apstudio.graphedit; import java.awt.Label; import java.awt.Button; import java.awt.TextField; import java.awt.Frame; import java.awt.Panel; import java.awt.BorderLayout; import java.awt.Event; import java.awt.Color; import java.awt.Point; import java.awt.Dimension; import java.awt.Dialog; import java.util.Vector; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; /* Purpose : Rename strategy graph name */ public class SGRenameDialog extends Dialog implements ActionListener { private TextField _graphName; private Button _execute, _cancel; private Vector _names; private String _oldName,_newName; private Label _statusInfo; public void setDefaultMessage() { showMessage(" "); } public void showMessage(String message) { _statusInfo.setText(message); invalidate(); validate(); } // public SGRenameDialog(Frame frame, String title,DefaultMutableTreeNode node,Vector names) public SGRenameDialog(Frame frame, String title,String oldName,Vector names) { super(frame,title,true); _names = names; _oldName = oldName; Label label1 = new Label("Strategy Graph Name "); _graphName = new TextField(_oldName,25); _execute = new Button("OK"); _cancel = new Button("Cancel"); // Addition for jdk 1.1.3 _graphName.addActionListener(this); _execute.addActionListener(this); _cancel.addActionListener(this); // Addition over Panel label = new Panel(); label.setLayout(new BorderLayout()); label.add("West",label1); label.add("East",_graphName); label.setBackground(Color.lightGray); Panel buttonPanel = new Panel(); buttonPanel.add(_execute); buttonPanel.add(_cancel); buttonPanel.setBackground(Color.lightGray); label.add("South",buttonPanel); setLayout(new BorderLayout()); add("Center",label); Panel _statusPanel = new Panel(); _statusPanel.setLayout(new BorderLayout()); _statusInfo = new Label(); setDefaultMessage(); _statusPanel.add("West",_statusInfo); _statusPanel.setBackground(Color.lightGray); add("South",_statusPanel); pack(); // jdk 1.1 type window event handling addWindowListener(new SGRDAdapter()); // using inner class for window handling. } public void setVisible(boolean status) { if(status) { _graphName.select(0,_oldName.length()); _graphName.requestFocus(); Dimension frameSize = getParent().getSize(); Point frameLoc = getParent().getLocation(); Dimension mySize = getSize(); int x,y; x = frameLoc.x + (frameSize.width/2) -(mySize.width/2); y = frameLoc.y + (frameSize.height/2)-(mySize.height/2); setBounds(x,y,getSize().width,getSize().height); } super.setVisible(status); } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if(source == _cancel) close(); else if (_graphName.getText().trim().length() != 0) executeCommand(); } public String getNewName() { if(_newName!=null) return _newName; return _oldName; } private void close() { setVisible(false);dispose(); } private void executeCommand() { String name =_graphName.getText().trim(); if(_oldName.equals(name)) close(); if(name.length()==0) { _graphName.setText(_oldName); _graphName.requestFocus(); _graphName.select(0,_oldName.length()); showMessage("Can't leave it empty"); return; } StrategyName tn; try{tn = StrategyName.parse(name);}catch(RuntimeException pe) { showMessage("Not a valid strategy graph name."); _graphName.requestFocus(); _graphName.select(0,name.length()); return; } if(!name.equals(tn.get_name().toString())) { showMessage("Parser thinks differently than you."); _graphName.requestFocus(); _graphName.select(0,name.length()); return; } if(_names.indexOf(name)!=-1) { showMessage("Name is already present"); _graphName.requestFocus(); _graphName.select(0,name.length()); return; } _newName = name; close(); } // inner class VDAdapter class SGRDAdapter extends WindowAdapter { public void windowClosing(WindowEvent event) { close(); } } } /* end class SGRenameDialog */