package com.didigame.acariaempire; import java.awt.Component; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Vector; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JLayeredPane; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; public class ToolbarController implements ActionListener { private final int TOOLBAR_INDEX = 1; private final int SHEET_BEGINNING_INDEX = 2; private final int TOOLBARELT_WIDTH = 50; private final int TOOLBARELT_HEIGHT = 50; private final int TOOLBARELT_SPACE = 5; JLayeredPane pane; int toolBarIndex; int currentIndex; Map iconMap; Map controllerMap; Vector displayedSheets; public ToolbarController( JLayeredPane pane ) { this.pane = pane; init(); } public void clearToolbar( ) { //Remove icons for( Iterator i = iconMap.values().iterator(); i.hasNext();) { JButton button = i.next(); pane.remove(button); } toolBarIndex = 0; //Remove displayed sheets for( int i = 0; i < displayedSheets.size(); i++ ) { pane.remove(displayedSheets.get(i)); } //Update display pane.validate(); //Reinit toolbar init(); } public void addSheet( SheetController controller ) { String paneName = controller.getPanel().getName(); //TODO JButton button = new JButton(); DataManager.setButtonIcons(button, controller.getPanel().getIcon() ); button.setVerticalAlignment(JLabel.CENTER); button.setHorizontalAlignment(JLabel.CENTER); button.setBounds( TOOLBARELT_SPACE, TOOLBARELT_SPACE + toolBarIndex * ( 2 * TOOLBARELT_SPACE + TOOLBARELT_HEIGHT), TOOLBARELT_WIDTH, TOOLBARELT_HEIGHT); //button.setMnemonic(KeyEvent.VK_D); button.setActionCommand( paneName ); button.addActionListener(this); iconMap.put( paneName , button ); controllerMap.put( paneName, controller); pane.add( button, TOOLBAR_INDEX); toolBarIndex++; } public void toggleSheet( String sheetName ) { //Precaution if( ! iconMap.containsKey( sheetName ) ) return; if( ! controllerMap.containsKey( sheetName ) ) return; JButton button = iconMap.get( sheetName ); SheetPanel sheet = controllerMap.get( sheetName ).getPanel(); //Check if the sheet is already displayed if( displayedSheets.indexOf( sheet ) != -1 ) //Pane already opened --> we hide it { button.setSelected( false ); pane.remove( sheet ); displayedSheets.remove( sheet ); } else // Pane not open --> We display it { button.setSelected( true ); setSheet( sheet ); displayedSheets.add( sheet ); currentIndex++; } //Update display pane.validate(); } @Override public void actionPerformed(ActionEvent evt) { String iconPressedName = evt.getActionCommand(); toggleSheet( iconPressedName ); } private void init() { currentIndex = SHEET_BEGINNING_INDEX; iconMap = new HashMap(); controllerMap = new HashMap(); displayedSheets = new Vector(); toolBarIndex = 0; } private void setSheet( SheetPanel sheet ) { // Center the element int sheetWidth = sheet.getWidth(); int sheetHeight = sheet.getHeight(); int widthReserved = TOOLBARELT_WIDTH + 2 * TOOLBARELT_SPACE; int windowWidth = ViewManager.getWindowsWidth(); int windowHeight = ViewManager.getWindowsHeight(); int x = widthReserved + ( windowWidth - sheetWidth ) / 2; int y = ( windowHeight - sheetHeight ) / 2; sheet.setBounds(x, y, sheetWidth, sheetHeight); pane.add( sheet, currentIndex ); sheet.repaint(); sheet.validate(); } }