Author: ruschein
Date: 2011-01-03 16:34:36 -0800 (Mon, 03 Jan 2011)
New Revision: 23290

Added:
   
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/AttributeListModel.java
Modified:
   
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/BrowserTableModel.java
   
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/TableBrowser.java
   
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/ui/AttributeBrowserToolBar.java
Log:
Made the select button work.

Copied: 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/AttributeListModel.java
 (from rev 21185, 
coreplugins/trunk/browser/src/main/java/browser/AttributeModel.java)
===================================================================
--- 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/AttributeListModel.java
                         (rev 0)
+++ 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/AttributeListModel.java
 2011-01-04 00:34:36 UTC (rev 23290)
@@ -0,0 +1,181 @@
+/*
+ Copyright (c) 2006, 2007, 2010, The Cytoscape Consortium (www.cytoscape.org)
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ any later version.
+
+ This library 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.  The software and
+ documentation provided hereunder is on an "as is" basis, and the
+ Institute for Systems Biology and the Whitehead Institute
+ have no obligations to provide maintenance, support,
+ updates, enhancements or modifications.  In no event shall the
+ Institute for Systems Biology and the Whitehead Institute
+ be liable to any party for direct, indirect, special,
+ incidental or consequential damages, including lost profits, arising
+ out of the use of this software and its documentation, even if the
+ Institute for Systems Biology and the Whitehead Institute
+ have been advised of the possibility of such damage.  See
+ the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+package org.cytoscape.browser.internal;
+
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.Vector;
+
+import javax.swing.ComboBoxModel;
+import javax.swing.ListModel;
+import javax.swing.event.ListDataEvent;
+import javax.swing.event.ListDataListener;
+
+import org.cytoscape.model.CyTable;
+import org.cytoscape.model.events.ColumnCreatedEvent;
+import org.cytoscape.model.events.ColumnCreatedListener;
+import org.cytoscape.model.events.ColumnDeletedEvent;
+import org.cytoscape.model.events.ColumnDeletedListener;
+import org.cytoscape.service.util.CyServiceRegistrar;
+
+
+public class AttributeListModel
+       implements ListModel, ComboBoxModel, ColumnCreatedListener, 
ColumnDeletedListener
+{
+       private Vector listeners = new Vector();
+       private BrowserTableModel browserTableModel;
+       private List<String> attributeNames;
+       private Object selection = null;
+       private Set<Class<?>> validAttrTypes;
+
+       /**
+        * Creates a new AttributeListModel object.
+        */
+       public AttributeListModel(final BrowserTableModel browserTableModel,
+                                 final Set<Class<?>> validAttrTypes)
+       {
+               this.browserTableModel = browserTableModel;
+               this.validAttrTypes = validAttrTypes;
+               updateAttributes();
+//             serviceRegistrar.registerAllServices(this, new Properties());
+       }
+
+       //@SuppressWarnings("unchecked")
+       public AttributeListModel(final BrowserTableModel browserTableModel) {
+               this(browserTableModel,
+                    new HashSet<Class<?>>((List<Class<?>>)(Arrays.asList(new 
Class<?>[] {
+                       String.class,
+                       Boolean.class,
+                       Double.class,
+                       Integer.class,
+                       Long.class,
+                       List.class
+                    }))));
+       }
+
+       public void setBrowserTableModel(final BrowserTableModel 
newBrowserTableModel) {
+               browserTableModel = newBrowserTableModel;
+               updateAttributes();
+       }
+
+       /**
+        *  Sets "attributeNames" to the sorted list of user-visible attribute 
names with supported data types.
+        */
+       public void updateAttributes() {
+               if (browserTableModel == null)
+                       return;
+
+               attributeNames = new ArrayList<String>();
+               final CyTable attributes = browserTableModel.getAttributes();
+               for (final String attrName : 
browserTableModel.getVisibleAttributeNames()) {
+                       if 
(validAttrTypes.contains(attributes.getType(attrName)))
+                               attributeNames.add(attrName);
+               }
+               Collections.sort(attributeNames);
+               browserTableModel.setVisibleAttributeNames(attributeNames);
+
+               notifyListeners(new ListDataEvent(this, 
ListDataEvent.CONTENTS_CHANGED, 0,
+                                                 attributeNames.size()));
+       }
+
+       /**
+        *  @return the i-th attribute name
+        */
+       @Override
+       public Object getElementAt(int i) {
+               if (i > attributeNames.size())
+                       return null;
+
+               return attributeNames.get(i);
+       }
+
+       /**
+        *  @return the number of attribute names
+        */
+       @Override
+       public int getSize() {
+               return attributeNames.size();
+       }
+
+       @Override
+       public void setSelectedItem(Object anItem) {
+               selection = anItem;
+       }
+
+       @Override
+       public Object getSelectedItem() {
+               return selection;
+       }
+
+       @Override
+       public void handleEvent(final ColumnCreatedEvent e) {
+               if (browserTableModel == null)
+                       return;
+
+               if (e.getSource() == browserTableModel.getAttributes())
+                       updateAttributes();
+       }
+
+       @Override
+       public void handleEvent(final ColumnDeletedEvent e) {
+               if (browserTableModel == null)
+                       return;
+
+               if (e.getSource() == browserTableModel.getAttributes())
+                       updateAttributes();
+       }
+
+       @Override
+       public void addListDataListener(ListDataListener l) {
+               listeners.add(l);
+       }
+
+       @Override
+       public void removeListDataListener(ListDataListener l) {
+               listeners.remove(l);
+       }
+
+       private void notifyListeners(ListDataEvent e) {
+               for (Iterator listenIt = listeners.iterator(); 
listenIt.hasNext();) {
+                       if (e.getType() == ListDataEvent.CONTENTS_CHANGED) {
+                               ((ListDataListener) 
listenIt.next()).contentsChanged(e);
+                       } else if (e.getType() == ListDataEvent.INTERVAL_ADDED) 
{
+                               ((ListDataListener) 
listenIt.next()).intervalAdded(e);
+                       } else if (e.getType() == 
ListDataEvent.INTERVAL_REMOVED) {
+                               ((ListDataListener) 
listenIt.next()).intervalRemoved(e);
+                       }
+               }
+       }
+}

Modified: 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/BrowserTableModel.java
===================================================================
--- 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/BrowserTableModel.java
  2011-01-03 21:15:14 UTC (rev 23289)
+++ 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/BrowserTableModel.java
  2011-01-04 00:34:36 UTC (rev 23290)
@@ -3,7 +3,9 @@
 
 import java.io.IOException;
 import java.io.StringReader;
+
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -75,6 +77,45 @@
                }
        }
 
+       public CyTable getAttributes() { return attrs; }
+
+       // Note: return value excludes the primary key of the associated 
CyTable!
+       public List<String> getVisibleAttributeNames() {
+               final List<String> visibleAttrNames = new ArrayList<String>();
+
+               final String primaryKey = attrs.getPrimaryKey();
+               for (final AttrNameAndVisibility nameAndVisibility : 
attrNamesAndVisibilities) {
+                       if (nameAndVisibility.isVisible())// && 
!nameAndVisibility.getName().equals(primaryKey))
+                               
visibleAttrNames.add(nameAndVisibility.getName());
+               }
+
+               return visibleAttrNames;
+       }
+
+       // Note: primary key visibility will not be affected by this, whether 
"visibleAttributes"
+       //       contains the primary key or not!
+       public void setVisibleAttributeNames(final Collection<String> 
visibleAttributes) {
+               boolean changed = false;
+               final String primaryKey = attrs.getPrimaryKey();
+               for (final AttrNameAndVisibility nameAndVisibility : 
attrNamesAndVisibilities) {
+                       if (nameAndVisibility.getName().equals(primaryKey))
+                               continue;
+
+                       if 
(visibleAttributes.contains(nameAndVisibility.getName())) {
+                               if (!nameAndVisibility.isVisible()) {
+                                       nameAndVisibility.setVisibility(true);
+                                       changed = true;
+                               }
+                       } else if (nameAndVisibility.isVisible()) {
+                               nameAndVisibility.setVisibility(false);
+                               changed = true;
+                       }
+               }
+
+               if (changed)
+                       fireTableStructureChanged();
+       }
+
        @Override
        public int getRowCount() {
                final Map<String, Class<?>> columnNameToTypeMap = 
attrs.getColumnTypeMap();
@@ -191,7 +232,14 @@
 
        @Override
        public void handleEvent(final ColumnDeletedEvent e) {
-//             XXX
+               final String columnName = e.getColumnName();
+               for (int i = 0; i < attrNamesAndVisibilities.size(); ++i) {
+                       if 
(attrNamesAndVisibilities.get(i).getName().equals(columnName)) {
+                               attrNamesAndVisibilities.remove(i);
+                               break;
+                       }
+               }
+
                fireTableStructureChanged();
        }
 

Modified: 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/TableBrowser.java
===================================================================
--- 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/TableBrowser.java
       2011-01-03 21:15:14 UTC (rev 23289)
+++ 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/internal/TableBrowser.java
       2011-01-04 00:34:36 UTC (rev 23290)
@@ -44,7 +44,7 @@
                this.eventHelper = eventHelper;
                this.compiler = compiler;
                this.browserTable = new BrowserTable();
-               this.attributeBrowserToolBar = new 
AttributeBrowserToolBar(browserTable);
+               this.attributeBrowserToolBar = new 
AttributeBrowserToolBar(serviceRegistrar);
                this.setLayout(new BorderLayout());
 
                browserTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
@@ -98,7 +98,7 @@
                        serviceRegistrar.registerAllServices(browserTableModel, 
new Properties());
                        browserTable.setModel(browserTableModel);
                        browserTable.setRowSorter(new 
TableRowSorter(browserTableModel));
-                       attributeBrowserToolBar.setAttrs(table);
+                       
attributeBrowserToolBar.setBrowserTableModel(browserTableModel);
                }
        }
 }
\ No newline at end of file

Modified: 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/ui/AttributeBrowserToolBar.java
===================================================================
--- 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/ui/AttributeBrowserToolBar.java
  2011-01-03 21:15:14 UTC (rev 23289)
+++ 
core3/table-browser-impl/trunk/src/main/java/org/cytoscape/browser/ui/AttributeBrowserToolBar.java
  2011-01-04 00:34:36 UTC (rev 23290)
@@ -32,10 +32,14 @@
 import java.awt.Dimension;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
+
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
 
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
@@ -60,16 +64,17 @@
 
 import java.util.HashMap;
 
-import org.cytoscape.browser.internal.BrowserTable;
+import org.cytoscape.browser.internal.AttributeListModel;
+import org.cytoscape.browser.internal.BrowserTableModel;
 import org.cytoscape.model.CyTable;
+import org.cytoscape.service.util.CyServiceRegistrar;
 import org.cytoscape.util.swing.CheckBoxJList;
 
 
 public class AttributeBrowserToolBar extends JPanel implements 
PopupMenuListener {
        private static final long serialVersionUID = -508393701912596399L;
 
-       private CyTable attrs;
-       private final JTable table;
+       private BrowserTableModel browserTableModel = null;
        private String attributeType = null;
        private List<String> orderedCol;
 
@@ -103,20 +108,25 @@
        private JButton matrixButton = null;
        private JButton importButton = null;
 
+       private AttributeListModel attrListModel;
+
 //     private ModDialog modDialog;
 //     private FormulaBuilderDialog formulaBuilderDialog;
 
-       public AttributeBrowserToolBar(final BrowserTable table) {
-               this.table = table;
+       public AttributeBrowserToolBar(final CyServiceRegistrar 
serviceRegistrar) {
                this.orderedCol = orderedCol;
+               this.attrListModel = new AttributeListModel(null);
+               serviceRegistrar.registerAllServices(attrListModel, new 
Properties());
 
                initializeGUI();
        }
 
-       public void setAttrs(final CyTable attrs) {
-               this.attrs = attrs;
-               createNewAttributeButton.setEnabled(attrs != null);
-               deleteAttributeButton.setEnabled(attrs != null);
+       public void setBrowserTableModel(final BrowserTableModel 
browserTableModel) {
+               this.browserTableModel = browserTableModel;
+               attrListModel.setBrowserTableModel(browserTableModel);
+               selectButton.setEnabled(browserTableModel != null);
+               createNewAttributeButton.setEnabled(browserTableModel != null);
+               deleteAttributeButton.setEnabled(browserTableModel != null);
        }
 
        private void initializeGUI() {
@@ -125,7 +135,7 @@
                this.setPreferredSize(new Dimension(210, 32));
                this.add(getJToolBar(), java.awt.BorderLayout.CENTER);
 
-//             getAttributeSelectionPopupMenu();
+               getAttributeSelectionPopupMenu();
                getJPopupMenu();
 
 //             modDialog = new ModDialog(tableModel, objectType, 
Cytoscape.getDesktop());
@@ -149,7 +159,7 @@
        private JPopupMenu getAttributeSelectionPopupMenu() {
                if (attributeSelectionPopupMenu == null) {
                        attributeSelectionPopupMenu = new JPopupMenu();
-//                     attributeSelectionPopupMenu.add(getJScrollPane());
+                       attributeSelectionPopupMenu.add(getJScrollPane());
                        attributeSelectionPopupMenu.addPopupMenuListener(this);
                }
 
@@ -161,7 +171,7 @@
         *
         * @return javax.swing.JScrollPane
         */
-/*     private JScrollPane getJScrollPane() {
+       private JScrollPane getJScrollPane() {
                if (jScrollPane == null) {
                        jScrollPane = new JScrollPane();
                        jScrollPane.setPreferredSize(new Dimension(600, 300));
@@ -170,7 +180,6 @@
 
                return jScrollPane;
        }
-*/
 
        /**
         * This method initializes jPopupMenu
@@ -411,9 +420,9 @@
                                                           
.addGroup(buttonBarLayout.createSequentialGroup()
                                                                
.addPreferredGap(ComponentPlacement.RELATED)
 
-/*                                                             
.addComponent(getSelectButton())
+                                                               
.addComponent(getSelectButton())
                                                                
.addPreferredGap(ComponentPlacement.RELATED)
-*/
+
                                                                
.addComponent(getNewButton())
                                                                
.addPreferredGap(ComponentPlacement.RELATED)
 /*                                                             
.add(getSelectAllButton())
@@ -445,12 +454,11 @@
                                                                
.addComponent(getMatrixButton())
                                                                
.addPreferredGap(ComponentPlacement.RELATED)*/));
                        
buttonBarLayout.setVerticalGroup(buttonBarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-/*                                                      
.addComponent(selectButton,
+                                                        
.addComponent(selectButton,
                                                                       
javax.swing.GroupLayout.Alignment.CENTER,
                                                                       
javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                       27,
                                                                       
javax.swing.GroupLayout.PREFERRED_SIZE)
-*/
                                                         
.addComponent(createNewAttributeButton,
                                                                       
javax.swing.GroupLayout.Alignment.CENTER,
                                                                       
javax.swing.GroupLayout.DEFAULT_SIZE,
@@ -506,11 +514,14 @@
 
                        selectButton.addMouseListener(new MouseAdapter() {
                                        public void 
mouseClicked(java.awt.event.MouseEvent e) {
-                                               
attributeList.setSelectedItems(orderedCol);
+                                               if (browserTableModel == null)
+                                                       return;
+                                               
attributeList.setSelectedItems(browserTableModel.getVisibleAttributeNames());
                                                
attributeSelectionPopupMenu.show(e.getComponent(), e.getX(), e.getY());
                                        }
                                });
                }
+               selectButton.setEnabled(false);
 
                return selectButton;
        }
@@ -703,7 +714,7 @@
                                                final List<String> emptyList = 
new ArrayList<String>();
                                                updateList(emptyList);
                                                try {
-                                                       
getUpdatedSelectedList();
+//                                                     
getUpdatedSelectedList();
 //                                                     
tableModel.setTableData(null, orderedCol);
                                                } catch (Exception ex) {
                                                        
attributeList.clearSelection();
@@ -719,18 +730,17 @@
                final String[] attrArray = getAttributeArray();
 
                final JFrame frame = (JFrame)SwingUtilities.getRoot(this);
-               final DeletionDialog dDialog = new DeletionDialog(frame, attrs);
+               final DeletionDialog dDialog = new DeletionDialog(frame, 
browserTableModel.getAttributes());
 
                dDialog.pack();
                dDialog.setLocationRelativeTo(browserToolBar);
                dDialog.setVisible(true);
        }
 
-/*
        private JList getSelectedAttributeList() {
                if (attributeList == null) {
                        attributeList = new CheckBoxJList();
-                       attributeList.setModel(attrModel);
+                       attributeList.setModel(attrListModel);
                        
attributeList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
                        attributeList.addMouseListener(new MouseAdapter() {
                                        public void mouseClicked(MouseEvent e) {
@@ -743,9 +753,9 @@
 
                return attributeList;
        }
-*/
 
        private String[] getAttributeArray() {
+               final CyTable attrs = browserTableModel.getAttributes();
                final String primaryKey = attrs.getPrimaryKey();
                final Map<String, Class<?>> nameToTypeMap = 
attrs.getColumnTypeMap();
                final String[] attributeArray = new String[nameToTypeMap.size() 
- 1];
@@ -776,7 +786,7 @@
                        createNewAttributeButton.setIcon(new 
javax.swing.ImageIcon(getClass().getClassLoader().getResource("images/stock_new.png")));
                        createNewAttributeButton.addMouseListener(new 
java.awt.event.MouseAdapter() {
                                        public void 
mouseClicked(java.awt.event.MouseEvent e) {
-                                               if (attrs != null)
+                                               if (browserTableModel != null)
                                                        
jPopupMenu.show(e.getComponent(), e.getX(), e.getY());
                                        }
                                });
@@ -806,6 +816,7 @@
                        }
                } while (newAttribName == null);
 
+               final CyTable attrs = browserTableModel.getAttributes();
                if (type.equals("String"))
                        attrs.createColumn(newAttribName, String.class);
                else if (type.equals("Floating Point"))
@@ -847,82 +858,19 @@
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                // Update actual table
                try {
-                       getUpdatedSelectedList();
+                       if (attributeList != null) {
+                               final Object[] selectedValues = 
attributeList.getSelectedValues();
+                               final Set<String> visibleAttributes = new 
HashSet<String>();
+                               for (final Object selectedValue : 
selectedValues)
+                                       
visibleAttributes.add((String)selectedValue);
 
-//                     tableModel.setTableData(null, orderedCol);
+                               
browserTableModel.setVisibleAttributeNames(visibleAttributes);
+                       }
                } catch (Exception ex) {
                        attributeList.clearSelection();
                }
        }
 
-       public List<String> getUpdatedSelectedList() {
-/*
-               final Object[] selected = attributeList.getSelectedValues();
-
-               orderedCol.remove("ID");
-
-               // determine if orderedCol is ordered (drag and drop column may 
change the order)
-               boolean isColOrdered = true;
-               for (int i=0; i< orderedCol.size()-1; i++){
-                       if 
(orderedCol.get(i).compareToIgnoreCase(orderedCol.get(i+1)) > 0){
-                               isColOrdered = false;
-                       }
-               }
-
-               if (isColOrdered){
-                       // The original columns are in order, leave as is
-                       orderedCol.clear();
-                       for (Object colName : selected)
-                               orderedCol.add(colName.toString());
-                       return orderedCol;
-               }
-
-               // The original columns are out of order
-
-               // Determine the cols to be added
-               ArrayList<String> colsToBeAdded = new ArrayList<String>();
-               HashMap<String, String> hashMap_orig = new HashMap<String, 
String>();
-
-               for (int i=0; i< orderedCol.size(); i++){
-                       hashMap_orig.put(orderedCol.get(i), null);
-               }
-
-               for (Object colName : selected) {
-                       if (!hashMap_orig.containsKey(colName.toString())){
-                               colsToBeAdded.add(colName.toString());
-                       }
-               }
-
-               // Determine the cols to be deleted
-               HashMap<String, String> hashMap_new = new HashMap<String, 
String>();
-               ArrayList<String> colsToBeDeleted = new ArrayList<String>();
-
-               for (Object colName : selected) {
-                       hashMap_new.put(colName.toString(), null);
-               }
-
-               for (int i=0; i< orderedCol.size(); i++){
-                       if (!hashMap_new.containsKey(orderedCol.get(i))){
-                               colsToBeDeleted.add(orderedCol.get(i));
-                       }
-               }
-
-               // delete the cols to be deleted from orderedCol
-               for (int i=0; i< colsToBeDeleted.size(); i++){
-                       orderedCol.remove(colsToBeDeleted.get(i));
-               }
-
-               // Append the new columns to the end
-               for (int i=0; i< colsToBeAdded.size(); i++){
-                       orderedCol.add(colsToBeAdded.get(i));
-               }
-
-               return orderedCol;
-*/
-               return null;
-       }
-
-
        public void updateList(List<String> newSelection) {
                orderedCol = newSelection;
                attributeList.setSelectedItems(orderedCol);

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to