Example usage for java.awt.event ActionEvent getID

List of usage examples for java.awt.event ActionEvent getID

Introduction

In this page you can find the example usage for java.awt.event ActionEvent getID.

Prototype

public int getID() 

Source Link

Document

Returns the event type.

Usage

From source file:org.openuat.apps.BedaApp.java

/**
 * Creates a new instance of this class and launches the
 * actual application./*from   w w w.  j av  a  2 s. c o  m*/
 */
public BedaApp() {
    random = new SecureRandom();
    devices = new RemoteDevice[0];
    currentPeerUrl = null;
    currentChannel = null;
    isInitiator = false;

    // Initialize the logger. Use a wrapper around the log4j framework.
    /*LogFactory.init(new Log4jFactory());
    logger = LogFactory.getLogger(BedaApp.class.getName());
    logger.finer("Logger initiated!");*/

    mainWindow = new JFrame("Beda App");
    mainWindow.setSize(new Dimension(FRAME_WIDTH, FRAME_HIGHT));
    mainWindow.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HIGHT));
    mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWindow.getContentPane().setLayout(new FlowLayout());
    URL imgURL = getClass().getResource("/resources/Button_Icon_Blue_beda.png");
    ImageIcon icon = imgURL != null ? new ImageIcon(imgURL)
            : new ImageIcon("resources/Button_Icon_Blue_beda.png");
    mainWindow.setIconImage(icon.getImage());

    // prepare the button channels
    ActionListener abortHandler = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (e.getID() == ActionEvent.ACTION_PERFORMED) {
                logger.warning("Protocol run aborted by user");
                BluetoothRFCOMMChannel.shutdownAllChannels();
                alertError("Protocol run aborted.");
            }
        }
    };
    buttonChannels = new HashMap<String, OOBChannel>();
    ButtonChannelImpl impl = new AWTButtonChannelImpl(mainWindow.getContentPane(), abortHandler);
    OOBChannel c = new ButtonToButtonChannel(impl);
    buttonChannels.put(c.toString(), c);
    c = new FlashDisplayToButtonChannel(impl, false);
    buttonChannels.put(c.toString(), c);
    c = new TrafficLightToButtonChannel(impl);
    buttonChannels.put(c.toString(), c);
    c = new ProgressBarToButtonChannel(impl);
    buttonChannels.put(c.toString(), c);
    c = new PowerBarToButtonChannel(impl);
    buttonChannels.put(c.toString(), c);
    c = new ShortVibrationToButtonChannel(impl);
    buttonChannels.put(c.toString(), c);
    c = new LongVibrationToButtonChannel(impl);
    buttonChannels.put(c.toString(), c);

    // set up the menu bar
    menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    final JMenuItem homeEntry = new JMenuItem("Home");
    final JMenuItem testEntry = new JMenuItem("Test channels");

    ActionListener menuListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            JMenuItem menuItem = (JMenuItem) event.getSource();
            if (menuItem == homeEntry) {
                showHomeScreen();
            } else if (menuItem == testEntry) {
                showTestScreen();
            }
        }
    };
    homeEntry.addActionListener(menuListener);
    testEntry.addActionListener(menuListener);

    menu.add(homeEntry);
    menu.add(testEntry);
    menuBar.add(menu);
    mainWindow.setJMenuBar(menuBar);

    // set up channel list
    OOBChannel[] channels = { new ButtonToButtonChannel(impl), new FlashDisplayToButtonChannel(impl, false),
            new TrafficLightToButtonChannel(impl), new ProgressBarToButtonChannel(impl),
            new PowerBarToButtonChannel(impl), new ShortVibrationToButtonChannel(impl),
            new LongVibrationToButtonChannel(impl) };
    channelList = new JList(channels);
    channelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    channelList.setVisibleRowCount(15);
    channelList.setPreferredSize(new Dimension(LIST_WIDTH, LIST_HIGHT));

    // set up device list
    deviceList = new JList();
    deviceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    deviceList.setVisibleRowCount(15);
    deviceList.setPreferredSize(new Dimension(LIST_WIDTH, LIST_HIGHT));

    // enable double clicks on the two lists
    doubleClickListener = new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent event) {
            // react on double clicks
            if (event.getButton() == MouseEvent.BUTTON1 && event.getClickCount() == 2) {
                JList source = (JList) event.getSource();
                if (source == channelList && channelList.isEnabled()) {
                    currentChannel = (OOBChannel) channelList.getSelectedValue();
                    startAuthentication();
                } else if (source == deviceList) {
                    int index = deviceList.getSelectedIndex();
                    if (index > -1) {
                        String peerAddress = devices[index].getBluetoothAddress();
                        searchForService(peerAddress);
                    }
                }
            }
            event.consume();
        }
    };
    deviceList.addMouseListener(doubleClickListener);
    // note: this listener will be set on the channelList in the showHomeScreen method

    ListSelectionListener selectionListener = new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent event) {
            channelList.setEnabled(false);
        }
    };
    deviceList.addListSelectionListener(selectionListener);

    // create refresh button
    refreshButton = new JButton("Refresh list");
    ActionListener buttonListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent event) {
            if ((JButton) event.getSource() == refreshButton) {
                refreshButton.setEnabled(false);
                statusLabel.setText("Please wait... scanning for devices...");
                peerManager.startInquiry(false);
            }
        }
    };
    refreshButton.addActionListener(buttonListener);

    // set up the bluetooth peer manager
    BluetoothPeerManager.PeerEventsListener listener = new BluetoothPeerManager.PeerEventsListener() {
        public void inquiryCompleted(Vector newDevices) {
            refreshButton.setEnabled(true);
            statusLabel.setText("");
            updateDeviceList();
        }

        public void serviceSearchCompleted(RemoteDevice remoteDevice, Vector services, int errorReason) {
            // ignore
        }
    };

    try {
        peerManager = new BluetoothPeerManager();
        peerManager.addListener(listener);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Could not initiate BluetoothPeerManager.", e);
    }

    // set up the bluetooth rfcomm server
    try {
        btServer = new BluetoothRFCOMMServer(null, SERVICE_UUID, SERVICE_NAME, 30000, true, false);
        btServer.addAuthenticationProgressHandler(this);
        btServer.start();
        if (logger.isLoggable(Level.INFO)) {
            logger.info("Finished starting SDP service at " + btServer.getRegisteredServiceURL());
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Could not create bluetooth server.", e);
    }
    ProtocolCommandHandler inputProtocolHandler = new ProtocolCommandHandler() {
        @Override
        public boolean handleProtocol(String firstLine, RemoteConnection remote) {
            if (logger.isLoggable(Level.FINER)) {
                logger.finer("Handle protocol command: " + firstLine);
            }
            if (firstLine.equals(UACAPProtocolConstants.PRE_AUTH)) {
                inputProtocol(false, remote, null);
                return true;
            }
            return false;
        }
    };
    btServer.addProtocolCommandHandler(UACAPProtocolConstants.PRE_AUTH, inputProtocolHandler);

    // build staus bar
    statusLabel = new JLabel("");
    statusLabel.setPreferredSize(new Dimension(FRAME_WIDTH - 40, LABEL_HIGHT));

    // build initial screen (the home screen)
    showHomeScreen();

    // launch window
    mainWindow.pack();
    mainWindow.setVisible(true);
}

From source file:org.rdv.datapanel.DigitalTabularDataPanel.java

private void addColumn() {
    if (columnGroupCount == MAX_COLUMN_GROUP_COUNT) {
        return;//from   ww  w.ja  v  a  2s.c  o m
    }

    if (columnGroupCount != 0) {
        panelBox.add(Box.createHorizontalStrut(7));
    }

    final int tableIndex = columnGroupCount;

    final DataTableModel tableModel = new DataTableModel();
    final JTable table = new JTable(tableModel);

    table.setDragEnabled(true);
    table.setName(DigitalTabularDataPanel.class.getName() + " JTable #" + Integer.toString(columnGroupCount));

    if (showThresholdCheckBoxGroup.isSelected()) {
        tableModel.setThresholdVisible(true);
    }

    if (showMinMaxCheckBoxGroup.isSelected()) {
        tableModel.setMaxMinVisible(true);

        table.getColumn("Min").setCellRenderer(doubleCellRenderer);
        table.getColumn("Max").setCellRenderer(doubleCellRenderer);
    }

    table.getColumn("Value").setCellRenderer(dataCellRenderer);

    tables.add(table);
    tableModels.add(tableModel);

    JScrollPane tableScrollPane = new JScrollPane(table);
    panelBox.add(tableScrollPane);

    // popup menu for panel
    JPopupMenu popupMenu = new JPopupMenu();

    final JMenuItem copyMenuItem = new JMenuItem("Copy");
    copyMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            TransferHandler.getCopyAction().actionPerformed(
                    new ActionEvent(table, ae.getID(), ae.getActionCommand(), ae.getWhen(), ae.getModifiers()));
        }
    });
    popupMenu.add(copyMenuItem);

    popupMenu.addSeparator();

    JMenuItem printMenuItem = new JMenuItem("Print...");
    printMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                table.print(JTable.PrintMode.FIT_WIDTH);
            } catch (PrinterException pe) {
            }
        }
    });
    popupMenu.add(printMenuItem);
    popupMenu.addSeparator();

    final JCheckBoxMenuItem showMaxMinMenuItem = new JCheckBoxMenuItem("Show min/max columns", false);
    showMaxMinMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            setMaxMinVisible(showMaxMinMenuItem.isSelected());
        }
    });
    showMinMaxCheckBoxGroup.addCheckBox(showMaxMinMenuItem);
    popupMenu.add(showMaxMinMenuItem);

    final JCheckBoxMenuItem showThresholdMenuItem = new JCheckBoxMenuItem("Show threshold columns", false);
    showThresholdMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            setThresholdVisible(showThresholdMenuItem.isSelected());
        }
    });
    showThresholdCheckBoxGroup.addCheckBox(showThresholdMenuItem);
    popupMenu.add(showThresholdMenuItem);

    popupMenu.addSeparator();

    JMenuItem blankRowMenuItem = new JMenuItem("Insert blank row");
    blankRowMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tableModel.addBlankRow();
        }
    });
    popupMenu.add(blankRowMenuItem);

    final JMenuItem removeSelectedRowsMenuItem = new JMenuItem("Remove selected rows");
    removeSelectedRowsMenuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            removeSelectedRows(tableIndex);
        }
    });
    popupMenu.add(removeSelectedRowsMenuItem);

    popupMenu.addSeparator();

    JMenu numberOfColumnsMenu = new JMenu("Number of columns");
    numberOfColumnsMenu.addMenuListener(new MenuListener() {
        public void menuSelected(MenuEvent me) {
            JMenu menu = (JMenu) me.getSource();
            for (int j = 0; j < MAX_COLUMN_GROUP_COUNT; j++) {
                JMenuItem menuItem = menu.getItem(j);
                boolean selected = (j == (columnGroupCount - 1));
                menuItem.setSelected(selected);
            }
        }

        public void menuDeselected(MenuEvent me) {
        }

        public void menuCanceled(MenuEvent me) {
        }
    });

    for (int i = 0; i < MAX_COLUMN_GROUP_COUNT; i++) {
        final int number = i + 1;
        JRadioButtonMenuItem item = new JRadioButtonMenuItem(Integer.toString(number));
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                setNumberOfColumns(number);
            }
        });
        numberOfColumnsMenu.add(item);
    }
    popupMenu.add(numberOfColumnsMenu);

    popupMenu.addPopupMenuListener(new PopupMenuListener() {
        public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
            boolean anyRowsSelected = table.getSelectedRowCount() > 0;
            copyMenuItem.setEnabled(anyRowsSelected);
            removeSelectedRowsMenuItem.setEnabled(anyRowsSelected);
        }

        public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
        }

        public void popupMenuCanceled(PopupMenuEvent arg0) {
        }
    });

    // set component popup and mouselistener to trigger it
    table.setComponentPopupMenu(popupMenu);
    tableScrollPane.setComponentPopupMenu(popupMenu);

    panelBox.revalidate();

    columnGroupCount++;

    properties.setProperty("numberOfColumns", Integer.toString(columnGroupCount));
}