Package vg.userInterface.tabCloser

Source Code of vg.userInterface.tabCloser.SimpleTabCloser

package vg.userInterface.tabCloser;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Observable;
import java.util.Set;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import vg.core.AMenuItem;
import vg.core.AUserInterface;
import vg.core.event.AUIEvent;
import vg.core.event.UIEventCloseTab;
import vg.core.event.UIEventOpenNewTab;
import vg.core.request.IUIRequestOwner;
import vg.core.request.UIRequestCloseCurrentTab;
import vg.core.request.UIRequestCloseTab;

public class SimpleTabCloser {
  // Data
  private final IUIRequestOwner owner;
  private final AUserInterface userInterface;
  private final Set<Integer> tabSet;
  // Components
  private final JMenuItem       closeTabMenuItem;
  private final JMenuItem       closeAllTabMenuItem;
  /**
   * Constructor.
   * @param userInterface - feedback between this plugin and system.
   */
  public SimpleTabCloser(AUserInterface userInterface) {
    // init data
    this.userInterface   = userInterface;
    this.tabSet = new HashSet<Integer>();
    // init components
    this.closeTabMenuItem = new JMenuItem("Close tab");
    this.closeTabMenuItem.setIcon(new ImageIcon("./data/resources/textures/closeTab.png"));
    this.closeTabMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.CTRL_DOWN_MASK));

    this.closeAllTabMenuItem = new JMenuItem("Close all tabs");
    //this.closeAllTabMenuItem.setIcon(new ImageIcon("./data/resources/textures/closeTab.png"));
    // init listeners
    this.owner = new IUIRequestOwner() {
      public void callRequestOwner(Object obj) {
      }
    };
    this.closeTabMenuItem.addActionListener(new ActionListener() {   
      public void actionPerformed(ActionEvent e) {
        closeTab();
      }
    });
    this.closeAllTabMenuItem.addActionListener(new ActionListener() {   
      public void actionPerformed(ActionEvent e) {
        closeAllTab();
      }
    });
   
    this.userInterface.addMenuItem(new AMenuItem(this.closeTabMenuItem) {
      public void update(Observable o, Object arg) {
        // empty, because we state update method for closing all tab menu item
      }
    }, "File");
    this.userInterface.addMenuItem(new AMenuItem(this.closeAllTabMenuItem) {
      public void update(Observable o, Object arg) {
        localUpdate(o, arg);
      }
    }, "File");

   
  }
  private void closeTab() {
    UIRequestCloseCurrentTab req = new UIRequestCloseCurrentTab(this.owner);
    this.userInterface.addRequest(req);
  }
  private void closeAllTab() {
    synchronized (this.tabSet) {
      Set<Integer> tmpTabSet = new HashSet<Integer>(this.tabSet);
      UIRequestCloseTab req = new UIRequestCloseTab(this.owner, tmpTabSet);
      this.userInterface.addRequest(req);     
    }
  }
  private void localUpdate(Observable o, Object arg) {
    if (arg instanceof AUIEvent) {
      AUIEvent event = (AUIEvent) arg;
      switch (event.getType()) {
        case DEF_OPEN_NEW_TAB: {
          UIEventOpenNewTab bufEvent = (UIEventOpenNewTab)event;
          synchronized (this.tabSet) {
            this.tabSet.add(bufEvent.getTabId());
          }
          break;
        }
        case DEF_CLOSE_TAB: {
          UIEventCloseTab bufEvent = (UIEventCloseTab)event;
          synchronized (this.tabSet) {
            this.tabSet.remove(bufEvent.getTabId());
          }
          break;         
        }
      }
    }
  }
}
TOP

Related Classes of vg.userInterface.tabCloser.SimpleTabCloser

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.