Package Controllers.Main

Source Code of Controllers.Main.MainWindowController

/**********************************************************************
    Virtual Cache is a program which helps to manage personal finances
    Copyright (C) 2009  by Rovinskiy Nikolay

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

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
package Controllers.Main;

import Controllers.CardPane.CardPaneController;
import Utils.Card;
import Interfaces.MainWindow;
import Messages.Legend;
import Messages.YesNoCancelMessage;
import Utils.IniHolder;
import Utils.Profile;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JTable;
/**
*
* @author nrovinskiy
*/
public class MainWindowController extends MainWindow
{
    private File fleOpened;
    public char[] chrPassword;
    private Profile prfMain;
    private Vector<String> vctReasons;
    private String strTitle;
    private Savelistener slSave;
    private SearchListener search;
    public MainWindowController()
    {
        super();
        fleOpened=null;
        chrPassword=null;
        miExit.addActionListener(new ExitListener(this));
        miOpen.addActionListener(new OpenListener(this));
        miCreateCard.addActionListener(new NewCardListener(this));
        miCreateDatabase.addActionListener(new CreateListener(this));
        slSave=new Savelistener(this);
        miSave.addActionListener(slSave);
        miSaveAs.addActionListener(slSave);
        vctReasons=new Vector<String>();
        strTitle=this.getTitle();
        miClose.addActionListener(new CloseFileListener(this));
        miDeleteCard.addActionListener(new DeleteCardListener(this, jtpCards));
        miDelete.addActionListener(new DeleteDatabaseListener(this));
        miMkTransfer.addActionListener(new TransferListener(this));
        miReport.addActionListener(new MkReportListener(this));
        miExport.addActionListener(new ExportListener(this));
        miImport.addActionListener(new ImoprtListener(this));
        miHelp.addActionListener(new LegendListener(this, new File("help.html"), Legend.CONTENT_TYPE_HTML, "Help"));
        miTotal.addActionListener(new TotalAmountListener(this));
        miChBalance.addActionListener(new ChBalanceListener(this));
        miLicense.addActionListener(new LegendListener(this, new File("license.txt"), Legend.CONTENT_TYPE_TEXT, "License"));
        miAbout.addActionListener(new AboutListener(this));
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowStateWriter());
        try
        {
            this.setLocation(Integer.parseInt(IniHolder.ReadValue("MainX")), Integer.parseInt(IniHolder.ReadValue("MainY")));
        }
        catch(NumberFormatException ex)
        {
            this.setLocation(400, 200);
        }
        search=new SearchListener(this);
        miSearch.addActionListener(search);
    }
    public void setOpenedFile(File file){fleOpened=file;}
    public File getOpenedFile(){return fleOpened;}
    public void setPassword(char[] password)
    {
        chrPassword=new char[password.length];
        for(int i=0; i<password.length; i++)
            chrPassword[i]=password[i];
    }
    public char[] getPassword(){return chrPassword;}
    private void ErasePassword()
    {
        if(chrPassword==null)
            return;
        for(int i=0; i<chrPassword.length; i++)
        {
            chrPassword[i]=(char)0;
        }
    }
    public void LoadCard(Card card, boolean newcard)
    {
        LoadReasons();
        CardPaneController cpcNew=new CardPaneController(this, card);
        if((!newcard)||jtpCards.getTabCount()==0)
            jtpCards.insertTab(card.getName(), null, cpcNew, card.getName(), 0);
        else
            jtpCards.insertTab(card.getName(), null, cpcNew, card.getName(), jtpCards.getTabCount());//addTab(card.getName(), cpcNew);
        if(!newcard)
            jtpCards.setSelectedIndex(0);
        else
            jtpCards.setSelectedIndex(jtpCards.getComponentCount()-1);
        cpcNew=null;
        miSearch.setEnabled(true);
        miDeleteCard.setEnabled(true);
        miExport.setEnabled(true);
        miImport.setEnabled(true);
        miTotal.setEnabled(true);
        miChBalance.setEnabled(true);
        miMkTransfer.setEnabled(true);
    }
    public void setProfile(Profile profile)
    {
        miCreateCard.setEnabled(true);
        miClose.setEnabled(true);
        miDelete.setEnabled(true);
        miReport.setEnabled(true);
        miCreateCard.setEnabled(true);
        miSave.setEnabled(true);
        miSaveAs.setEnabled(true);
        this.setTitle(virtualcache.Main.ApplicationName+" - "+profile.getName());
        prfMain=profile;
        for(int i=0; i<prfMain.getCardNumber(); i++)
        {
            Enumeration<Card> enCard=profile.getCards();
            while(enCard.hasMoreElements())
            {
                Card crdTemp=enCard.nextElement();
                if(crdTemp.getOrdNumber()==i)
                {
                    LoadCard(crdTemp, false);
                    break;
                }
            }
            enCard=null;
        }
        //reload Reasons on each card when all cards are loaded
        for(int i=0; i<jtpCards.getComponentCount(); i++)
        {
            ((CardPaneController)jtpCards.getComponent(i)).ReloadReasons();
        }
    }
    public Profile getProfile(){return prfMain;}
    public Vector<String> getReasons(){return vctReasons;}
    public void addReason(String reason)
    {
        if(!hasReason(reason))
        {
            vctReasons.add(reason.trim().toLowerCase());
            prfMain.addReason(reason);
        }
    }
    public void UpdateCardNumbers(boolean delete)
    {
        if(delete)
        {
            for(int i=0; i<jtpCards.getTabCount(); i++)
            {
                ((CardPaneController)jtpCards.getComponent(i)).getCard().setOrdNumber(i);//(jtpCards.getTabCount()-i-1);
            }
        }
        else
        {
            for(int i=0; i<jtpCards.getTabCount(); i++)
            {
                ((CardPaneController)jtpCards.getComponent(i)).getCard().setOrdNumber(((CardPaneController)jtpCards.getComponent(i)).getCard().getOrdNumber()+1);//(jtpCards.getTabCount()-i-1);
            }
        }
    }
    public void DeleteCard(String name)
    {
        //delete catrd
        prfMain.DeleteCard(name);
        //update user interface
        for(int i=0; i<jtpCards.getTabCount(); i++)
        {
            if(jtpCards.getTitleAt(i).equals(name))
            {
                jtpCards.removeTabAt(i);
                break;
            }
        }
       
        vctReasons.clear();
        //while(enCards.hasMoreElements())
        LoadReasons();
        for(int i=0; i<jtpCards.getTabCount(); i++)
            ((CardPaneController)jtpCards.getComponentAt(i)).ReloadReasons();
        UpdateCardNumbers(true);
        if(jtpCards.getComponentCount()==0)
        {
            miDeleteCard.setEnabled(false);
            miSearch.setEnabled(false);
            miExport.setEnabled(false);
            miImport.setEnabled(false);
            miTotal.setEnabled(false);
            miMkTransfer.setEnabled(false);
            miChBalance.setEnabled(false);
        }
    }
    public boolean ValidateCloseDatabase()
    {
        if((prfMain==null)||(!prfMain.isModifyed()))
            return true;
        else
        {
            YesNoCancelMessage ynmcMessage=new YesNoCancelMessage(this, virtualcache.Main.ApplicationName, "<html>The file was modifyed. Would you like to save changes before exit?</html>", true);
            ynmcMessage.setVisible(true);
            if(ynmcMessage.getResult()==YesNoCancelMessage.CANCEL)
                return false;
            else if(ynmcMessage.getResult()==YesNoCancelMessage.YES)
            {
                slSave.actionPerformed(new ActionEvent(miSave, 1, "", 1));
            }
            return true;
        }
       
    }
    public void CloseDatabase()
    {
        setOpenedFile(null);
        prfMain=null;
        ErasePassword();
        miCreateCard.setEnabled(false);
        miClose.setEnabled(false);
        miDelete.setEnabled(false);
        miReport.setEnabled(false);
        miSave.setEnabled(false);
        miSaveAs.setEnabled(false);
        miDeleteCard.setEnabled(false);
        miSearch.setEnabled(false);
        miExport.setEnabled(false);
        miImport.setEnabled(false);
        miTotal.setEnabled(false);
        miChBalance.setEnabled(false);
        miMkTransfer.setEnabled(false);
        this.setTitle(virtualcache.Main.ApplicationName);
        jtpCards.removeAll();
        vctReasons.clear();
        search.reset();
    }
    public void LoadReasons()
    {
        vctReasons.clear();
        Enumeration<String> enReasons=prfMain.getReasons().keys();
        while(enReasons.hasMoreElements())
            vctReasons.add(enReasons.nextElement());
    }
    private boolean hasReason(String reason)
    {
        for(Iterator<String> i=vctReasons.iterator(); i.hasNext();)
        {
            String strTemp=i.next();
            if(strTemp.trim().toLowerCase().equals(reason.trim().toLowerCase()))
                return true;
        }
        return false;
    }
    public JTable getCurrentTransactionTable()
    {
        if((prfMain==null)||(prfMain.getCardNumber()==0))
            return null;
        return ((CardPaneController)jtpCards.getSelectedComponent()).getTransactionTable();
    }
    public CardPaneController getSelectedTab()
    {
        return (CardPaneController)jtpCards.getSelectedComponent();
    }
    public CardPaneController getTab(String tabname)
    {
        for(int i=0; i<jtpCards.getComponentCount(); i++)
        {
            if(tabname.trim().equals(((CardPaneController)jtpCards.getComponent(i)).getCard().getName().trim()))
                return((CardPaneController)jtpCards.getComponent(i));
        }
        return null;
    }
}
TOP

Related Classes of Controllers.Main.MainWindowController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.