//////////////////////////////////////////////////////////////////////////////////////////// //PIMS: Personal Invstment Management System //Class : AskPortfolioName //This contains the GUI for asking the user the //name of the portfolio while creating a new portfolio. //////////////////////////////////////////////////////////////////////////////////////////// import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AskPortfolioName extends JFrame implements ActionListener{ private Investment INV; private TextField name; private Label namep; private TextField name1; private Label namep1; private String pfName=""; private String task=""; private GUI g; public AskPortfolioName(Investment INV, String str,GUI gui){ super("Portfolio Details"); this.INV = INV; g=gui; task=str; makeGUI(); } private void makeGUI(){ Container contentPane=getContentPane(); contentPane.setBackground(Color.white); contentPane.setLayout(new GridLayout(3,2)); name=new TextField(15); //name.setBackground(Color.white); namep=new Label("Portfolio: "); namep.setBackground(Color.white); name1=new TextField(15); //name1.setBackground(Color.white); namep1=new Label("New Portfolio: "); namep1.setBackground(Color.white); Button b1=new Button("Ok"); Button b2=new Button("Cancel"); b1.setBackground(Color.white); b2.setBackground(Color.white); contentPane.add(namep); contentPane.add(name); contentPane.add(namep1); contentPane.add(name1); if(task.equals("Rename Portfolio")){ namep1.setVisible(true); name1.setVisible(true); } else{ namep1.setVisible(false); name1.setVisible(false); } contentPane.add(b1); contentPane.add(b2); name.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); setSize(250,150); setLocation(120,150); setVisible(true); } public void actionPerformed(ActionEvent ae){ try{ String str =ae.getActionCommand(); switch(task.charAt(0)){ case 'A' : if(str.equals("Ok")){ pfName=name.getText(); if(INV.CheckPortfolioName(pfName)){ new Error("This portfolio name already exists"); name.setText(""); setVisible(true); } else{ INV.AddPortfolio(pfName); setVisible(false); g.buildTree(g.cp); g.buildPortfolioList(g.cp); } } else setVisible(false); break; case 'D' : if(str.equals("Ok")){ pfName=name.getText(); if(!INV.CheckPortfolioName(pfName)){ new Error("This portfolio name does not exist"); name.setText(""); setVisible(true); } else{ INV.DeletePortfolio(pfName); setVisible(false); g.buildTree(g.cp); g.buildPortfolioList(g.cp); } } else setVisible(false); break; case 'R' : if(str.equals("Ok")){ pfName=name.getText(); String pfName1=name1.getText(); if(!INV.CheckPortfolioName(pfName) || INV.CheckPortfolioName(pfName1)){ if(INV.CheckPortfolioName(pfName1)) new Error("The portfolio to be renamed does not exist"); else new Error("Find a different New Portfolio name"); name.setText(""); name1.setText(""); setVisible(true); } else{ INV.RenamePortfolio(pfName,pfName1); setVisible(false); g.buildTree(g.cp); g.buildPortfolioList(g.cp); } } else setVisible(false); break; default: break; } g.cp.setVisible(true); }catch(Exception e){ new Error("Problem in handling portfolio"); return; } } }