Package org.jdesktop.wonderland.modules.xappsconfig.client

Source Code of org.jdesktop.wonderland.modules.xappsconfig.client.EditXAppsJFrame$XAppTableModel

/**
* Project Wonderland
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., All Rights Reserved
*
* Redistributions in source code form must reproduce the above
* copyright and this condition.
*
* The contents of this file are subject to the GNU General Public
* License, Version 2 (the "License"); you may not use this file
* except in compliance with the License. A copy of the License is
* available at http://www.opensource.org/licenses/gpl-license.php.
*
* Sun designates this particular file as subject to the "Classpath"
* exception as provided by Sun in the License file that accompanied
* this code.
*/
package org.jdesktop.wonderland.modules.xappsconfig.client;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import org.jdesktop.wonderland.client.cell.registry.CellRegistry;
import org.jdesktop.wonderland.modules.xappsconfig.common.XAppRegistryItem;

/**
* A JFrame to allow editing of the list of X Apps registered to appear in the
* Cell Palettes.
*
* @author Jordan Slott <jslott@dev.java.net>
*/
public class EditXAppsJFrame extends javax.swing.JFrame {

    private Logger logger = Logger.getLogger(EditXAppsJFrame.class.getName());
    private XAppTableModel xAppTableModel = null;
    private JTable userXAppsTable = null;

    /** Creates new form EditXAppsJFrame */
    public EditXAppsJFrame() {
        initComponents();

        // Create the user table to display the system-wide X App items
        List<XAppRegistryItem> userList =
                XAppRegistryItemUtils.getUserXAppRegistryItemList();
        xAppTableModel = new XAppTableModel(userList);
        userXAppsTable = new JTable(xAppTableModel);
        userXAppsTable.setColumnSelectionAllowed(false);
        userXAppsTable.setRowSelectionAllowed(true);
        userXAppsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        userScrollPane.setViewportView(userXAppsTable);

        // Listen for changes to the select on the user table and enable/
        // disable the Remove button as a result.
        ListSelectionListener userListener = new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                userRemoveButton.setEnabled(
                        userXAppsTable.getSelectedRow() != -1);
            }
        };
        userXAppsTable.getSelectionModel().addListSelectionListener(userListener);
    }

    /**
     * A table model that displays a list of X Apps registered to appear in the
     * Cell Palettes.
     */
    private class XAppTableModel extends AbstractTableModel {

        private List<XAppRegistryItem> itemList = null;

        /** Constructor, takes the list of registry items to display */
        public XAppTableModel(List<XAppRegistryItem> items) {
            itemList = items;
        }

        /**
         * @inheritDoc()
         */
        public int getRowCount() {
            return itemList.size();
        }

        /**
         * @inheritDoc()
         */
        public int getColumnCount() {
            return 2;
        }

        /**
         * @inheritDoc()
         */
        @Override
        public String getColumnName(int column) {
           switch (column) {
               case 0:
                   return "App Name";
               case 1:
                   return "Command";
               default:
                   return "";
           }
        }

        /**
         * @inheritDoc()
         */
        public Object getValueAt(int rowIndex, int columnIndex) {
            XAppRegistryItem item = itemList.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return item.getAppName();
                case 1:
                    return item.getCommand();
                default:
                    return "";
            }
        }

        /**
         * Adds the given X App registry item from the table and tells the
         * table to update itself.
         */
        public void addItem(XAppRegistryItem item) {
            itemList.add(item);
            fireTableDataChanged();
        }

        /**
         * Removes the given X App registry item from the table and tells the
         * table to update itself.
         */
        public void removeItem(XAppRegistryItem item) {
            itemList.remove(item);
            fireTableDataChanged();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        mainPanel = new javax.swing.JPanel();
        userMainPanel = new javax.swing.JPanel();
        userScrollPane = new javax.swing.JScrollPane();
        userButtonPanel = new javax.swing.JPanel();
        userAddButton = new javax.swing.JButton();
        userRemoveButton = new javax.swing.JButton();

        setTitle("Edit X Apps");
        getContentPane().setLayout(new java.awt.GridLayout(1, 1));

        mainPanel.setLayout(new java.awt.GridLayout(1, 0));

        userMainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        userMainPanel.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        userMainPanel.add(userScrollPane, gridBagConstraints);

        userButtonPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        userAddButton.setText("Add...");
        userAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userAddButtonActionPerformed(evt);
            }
        });
        userButtonPanel.add(userAddButton);

        userRemoveButton.setText("Remove");
        userRemoveButton.setEnabled(false);
        userRemoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userRemoveButtonActionPerformed(evt);
            }
        });
        userButtonPanel.add(userRemoveButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        userMainPanel.add(userButtonPanel, gridBagConstraints);

        mainPanel.add(userMainPanel);

        getContentPane().add(mainPanel);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void userAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userAddButtonActionPerformed
        // When the Add... button is pressed popup a dialog asking for all of
        // the information. Add it to the repository and table upon OK.
        AddXAppJDialog dialog = new AddXAppJDialog(this, true);
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);

        if (dialog.getReturnStatus() == AddXAppJDialog.RET_OK) {
            String appName = dialog.getAppName();
            String command = dialog.getCommand();
            XAppRegistryItem item = new XAppRegistryItem(appName, command);
            try {
                XAppRegistryItemUtils.addUserXAppRegistryItem(item);
                xAppTableModel.addItem(item);
            } catch (Exception ex) {
                logger.log(Level.WARNING, "Unable to add " + appName + " to " +
                        " user's x-apps store", ex);
            }

            // Register the new app in the Cell registry. The listener mechanism
            // on CellRegistry will notify everyone else of the update.
            CellRegistry registry = CellRegistry.getCellRegistry();
            appName = appName + " (User)";
            XAppCellFactory factory = new XAppCellFactory(appName, command);
            registry.registerCellFactory(factory);
        }
    }//GEN-LAST:event_userAddButtonActionPerformed

    private void userRemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userRemoveButtonActionPerformed
        // Fetch the selected row and remove the item from the Cell Registry.
        // Since XAppCellFactory defines equals() and hashCode(), we can simply
        // construct a new object with the proper app name and command
        int row = userXAppsTable.getSelectedRow();
        if (row == -1) {
            return;
        }

        // Fetch the selected appName and command from the table row
        String appName = (String)xAppTableModel.getValueAt(row, 0);
        String command = (String)xAppTableModel.getValueAt(row, 1);

        // Remove it permenantly from the user's repository
        XAppRegistryItem item = new XAppRegistryItem(appName, command);
        try {
            XAppRegistryItemUtils.removeUserXAppRegistryItem(item);
        } catch (Exception ex) {
                logger.log(Level.WARNING, "Unable to remove " + appName + " to " +
                        " user's x-apps store", ex);
        }

        // Remove it from the table model too.
        xAppTableModel.removeItem(item);

        // Remove it from the Cell registry, make sure we add " (User)" to the
        // app name, since that is how it appears in the Cell registry
        CellRegistry registry = CellRegistry.getCellRegistry();
        appName = appName + " (User)";
        XAppCellFactory factory = new XAppCellFactory(appName, command);
        registry.unregisterCellFactory(factory);
    }//GEN-LAST:event_userRemoveButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel mainPanel;
    private javax.swing.JButton userAddButton;
    private javax.swing.JPanel userButtonPanel;
    private javax.swing.JPanel userMainPanel;
    private javax.swing.JButton userRemoveButton;
    private javax.swing.JScrollPane userScrollPane;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of org.jdesktop.wonderland.modules.xappsconfig.client.EditXAppsJFrame$XAppTableModel

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.