Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.modeler.editor;
 
 
 
 import  org.apache.cayenne.exp.Expression;
 import  org.apache.cayenne.exp.ExpressionException;
 import  org.apache.cayenne.map.Attribute;
 import  org.apache.cayenne.map.Entity;
 import  org.apache.cayenne.map.Relationship;
 import  org.apache.cayenne.map.event.QueryEvent;
 import  org.apache.cayenne.query.PrefetchTreeNode;

Subclass of the SelectQueryOrderingTab configured to work with prefetches.
 
 public class SelectQueryPrefetchTab extends SelectQueryOrderingTab {
 
     public SelectQueryPrefetchTab(ProjectController mediator) {
         super(mediator);
     }
 
     protected JComponent createToolbar() {
 
         JButton add = new JButton("Add Prefetch", ModelerUtil
                 .buildIcon("icon-move_up.gif"));
         
         add.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
             	String prefetch = getSelectedPath();
                 
             	if (prefetch == null) {
                     return;
                 }
             	
                 addPrefetch(prefetch);
                 
                 Application.getInstance().getUndoManager().addEdit(new AddPrefetchUndoableEdit(prefetchSelectQueryPrefetchTab.this));
             }
 
         });
 
         JButton remove = new JButton("Remove Prefetch", ModelerUtil
                 .buildIcon("icon-move_down.gif"));
         
         remove.addActionListener(new ActionListener() {
 
             public void actionPerformed(ActionEvent e) {
             	int selection = .getSelectedRow();
                 
             	if (selection < 0) {
                     return;
                 }
 
             	String prefetch = (String.getModel().getValueAt(selection, 0);
             	
                 removePrefetch(prefetch);
             }
 
         });
 
         JToolBar toolbar = new JToolBar();
         toolbar.add(add);
         toolbar.add(remove);
        return toolbar;
    }
    protected TreeModel createBrowserModel(Entity entity) {
        EntityTreeModel treeModel = new EntityTreeModel(entity);
        treeModel.setFilter(
                new EntityTreeFilter() {
                    public boolean attributeMatch(Object node, Attribute attr) {
                        return false;
                    }
                    public boolean relationshipMatch(Object node, Relationship rel) {
                        return true;
                    }
                });
        return treeModel;
    }
    protected TableModel createTableModel() {
        return new PrefetchModel();
    }
    public void addPrefetch(String prefetch) {
        
        // check if such prefetch already exists
        if (.getPrefetchTree() != null) {
            PrefetchTreeNode node = .getPrefetchTree().getNode(prefetch);
            if (node != null && !node.isPhantom()) {
                return;
            }
        }
        .addPrefetch(prefetch);
       
        // reset the model, since it is immutable
        .setModel(createTableModel());
        
        .fireQueryEvent(new QueryEvent(this));
    }
    public void removePrefetch(String prefetch) {
        .removePrefetch(prefetch);
        // reset the model, since it is immutable
        .setModel(createTableModel());
        .fireQueryEvent(new QueryEvent(this));
    }
    boolean isToMany(String prefetch) {
        if ( == null) {
            return false;
        }
        Object root = .getRoot();
        // totally invalid path would result in ExpressionException
        try {
            Expression exp = Expression.fromString(prefetch);
            Object object = exp.evaluate(root);
            if (object instanceof Relationship) {
                return ((Relationship) object).isToMany();
            }
            else {
                return false;
            }
        }
        catch (ExpressionException e) {
            return false;
        }
    }

    
A table model for the Ordering editing table.
    final class PrefetchModel extends AbstractTableModel {
        String[] prefetches;
        PrefetchModel() {
            if ( != null) {
                if (.getPrefetchTree() == null) {
                     = new String[0];
                }
                else {
                    Collection c = .getPrefetchTree().nonPhantomNodes();
                     = new String[c.size()];
                    Iterator it = c.iterator();
                    for (int i = 0; i < .i++) {
                        [i] = ((PrefetchTreeNode) it.next()).getPath();
                    }
                }
            }
        }
        public int getColumnCount() {
            return 2;
        }
        public int getRowCount() {
            return ( != null) ? . : 0;
        }
        public Object getValueAt(int rowint column) {
            switch (column) {
                case 0:
                    return [row];
                case 1:
                    return isToMany([row]) ? . : .;
                default:
                    throw new IndexOutOfBoundsException("Invalid column: " + column);
            }
        }
        public Class getColumnClass(int column) {
            switch (column) {
                case 0:
                    return String.class;
                case 1:
                    return Boolean.class;
                default:
                    throw new IndexOutOfBoundsException("Invalid column: " + column);
            }
        }
        public String getColumnName(int column) {
            switch (column) {
                case 0:
                    return "Prefetch Path";
                case 1:
                    return "To Many";
                default:
                    throw new IndexOutOfBoundsException("Invalid column: " + column);
            }
        }
        public boolean isCellEditable(int rowint column) {
            return false;
        }
    }
New to GrepCode? Check out our FAQ X