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.exp.parser.ASTPath;
 import  org.apache.cayenne.map.DataMap;
 import  org.apache.cayenne.map.Entity;
 import  org.apache.cayenne.map.event.QueryEvent;
 import  org.apache.cayenne.query.AbstractQuery;
 import  org.apache.cayenne.query.Query;
 import  org.apache.cayenne.query.SelectQuery;
 import  org.apache.cayenne.util.CayenneMapEntry;
 import  org.apache.cayenne.util.Util;
 import  org.apache.cayenne.validation.ValidationException;
 
 import  com.jgoodies.forms.builder.PanelBuilder;
 import  com.jgoodies.forms.layout.CellConstraints;
 import  com.jgoodies.forms.layout.FormLayout;

A tabbed pane that contains editors for various SelectQuery parts.
 
 public class SelectQueryMainTab extends JPanel {
 
     protected ProjectController mediator;
 
     protected TextAdapter name;
     protected JComboBox queryRoot;
     protected TextAdapter qualifier;
     protected JCheckBox distinct;
 
     public SelectQueryMainTab(ProjectController mediator) {
         this. = mediator;
 
         initView();
         initController();
     }
 
     private void initView() {
         // create widgets
          = new TextAdapter(new JTextField()) {
 
             @Override
             protected void updateModel(String text) {
                 setQueryName(text);
             }
         };
 
          = CayenneWidgetFactory.createComboBox();
         AutoCompletion.enable();
         .setRenderer(CellRenderers.listRendererWithIcons());
 
          = new ValidatorTextAdapter(new JTextField()) {
 
             @Override
            protected void updateModel(String text) {
                setQueryQualifier(text);
            }
            @Override
            protected void validate(String textthrows ValidationException {
                createQualifier(text);
            }
        };
         = new JCheckBox();
        // assemble
        CellConstraints cc = new CellConstraints();
        FormLayout layout = new FormLayout(
                "right:max(80dlu;pref), 3dlu, fill:200dlu",
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p");
        PanelBuilder builder = new PanelBuilder(layout);
        builder.setDefaultDialogBorder();
        builder.addSeparator("SelectQuery Settings"cc.xywh(1, 1, 3, 1));
        builder.addLabel("Query Name:"cc.xy(1, 3));
        builder.add(.getComponent(), cc.xy(3, 3));
        builder.addLabel("Query Root:"cc.xy(1, 5));
        builder.add(cc.xy(3, 5));
        builder.addLabel("Qualifier:"cc.xy(1, 7));
        builder.add(.getComponent(), cc.xy(3, 7));
        builder.addLabel("Distinct:"cc.xy(1, 9));
        builder.add(cc.xy(3, 9));
        this.setLayout(new BorderLayout());
        this.add(builder.getPanel(), .);
        this.add(.);
    }
    private void initController() {
        RootSelectionHandler rootHandler = new RootSelectionHandler();
        
        .addActionListener(rootHandler);
        .addFocusListener(rootHandler);
        .getEditor().getEditorComponent().addFocusListener(rootHandler);
        .addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                SelectQuery query = getQuery();
                if (query != null) {
                    query.setDistinct(.isSelected());
                    .fireQueryEvent(new QueryEvent(thisquery));
                }
            }
        });
    }

    
Updates the view from the current model state. Invoked when a currently displayed query is changed.
    void initFromModel() {
        Query query = .getCurrentQuery();
        if (!(query instanceof SelectQuery)) {
            setVisible(false);
            return;
        }
        SelectQuery selectQuery = (SelectQuery) query;
        .setText(query.getName());
        .setSelected(selectQuery.isDistinct());
        .setText(selectQuery.getQualifier() != null ? selectQuery
                .getQualifier()
                .toString() : null);
        // init root choices and title label..
        // - SelectQuery supports ObjEntity roots
        // TODO: now we only allow roots from the current map,
        // since query root is fully resolved during map loading,
        // making it impossible to reference other DataMaps.
        DataMap map = .getCurrentDataMap();
        Object[] roots = map.getObjEntities().toArray();
        if (roots.length > 1) {
            Arrays.sort(roots, Comparators.getDataMapChildrenComparator());
        }
        DefaultComboBoxModel model = new DefaultComboBoxModel(roots);
        model.setSelectedItem(selectQuery.getRoot());
        .setModel(model);
        .initFromModel(selectQuery);
        setVisible(true);
    }
    protected SelectQuery getQuery() {
        return (.getCurrentQuery() instanceof SelectQuery)
                ? (SelectQuery) .getCurrentQuery()
                : null;
    }

    
Initializes Query qualifier from string.
    void setQueryQualifier(String text) {
        if (text != null && text.trim().length() == 0) {
            text = null;
        }
        Expression qualifier = createQualifier(text);
        if (qualifier != null)
        {
            //getQuery() is not null if we reached here
            getQuery().setQualifier(qualifier);
            .fireQueryEvent(new QueryEvent(thisgetQuery()));
        }
        
    }
    
    
Method to create and check an expression

Parameters:
text String to be converted as Expression
Returns:
Expression if a new expression was created, null otherwise.
Throws:
ValidationException if text can't be converted
    Expression createQualifier(String textthrows ValidationException
    {
        SelectQuery query = getQuery();
        if (query == null) {
            return null;
        }
        
        ExpressionConvertor convertor = new ExpressionConvertor();
        try {
            String oldQualifier = convertor.valueAsString(query.getQualifier());
            if (!Util.nullSafeEquals(oldQualifiertext)) {
                Expression exp = (Expression) convertor.stringAsValue(text);
                
                
Advanced checking. See CAY-888 #1
                if (query.getRoot() instanceof Entity) {
                    checkExpression((Entity) query.getRoot(), exp);
                }
                
                return exp;
            }
            
            return null;
        }
        catch (IllegalArgumentException ex) {
            // unparsable qualifier
            throw new ValidationException(ex.getMessage());
        }
    }

    
Initializes Query name from string.
    void setQueryName(String newName) {
        if (newName != null && newName.trim().length() == 0) {
            newName = null;
        }
        AbstractQuery query = getQuery();
        if (query == null) {
            return;
        }
        if (Util.nullSafeEquals(newNamequery.getName())) {
            return;
        }
        if (newName == null) {
            throw new ValidationException("SelectQuery name is required.");
        }
        DataMap map = .getCurrentDataMap();
        Query matchingQuery = map.getQuery(newName);
        if (matchingQuery == null) {
            // completely new name, set new name for entity
            QueryEvent e = new QueryEvent(thisqueryquery.getName());
            ProjectUtil.setQueryName(mapquerynewName);
            .fireQueryEvent(e);
        }
        else if (matchingQuery != query) {
            // there is a query with the same name
            throw new ValidationException("There is another query named '"
                    + newName
                    + "'. Use a different name.");
        }
    }
    
    
Advanced checking of an expression, needed because Expression.fromString() might terminate normally, but returned Expression will not be appliable for real Entities. Current implementation assures all attributes in expression are present in Entity

Parameters:
root Root of a query
ex Expression to check
Throws:
ValidationException when something's wrong
    static void checkExpression(Entity root, Expression exthrows ValidationException {
        try {
            if (ex instanceof ASTPath) {
                
Try to iterate through path, if some attributes are not present, exception will be raised
                
                Iterator<CayenneMapEntry> path = root.resolvePathComponents(ex);
                while (path.hasNext()) {
                    path.next();
                }
            }
            
            if (ex != null) {
                for (int i = 0; i < ex.getOperandCount(); i++) {
                    if (ex.getOperand(iinstanceof Expression) {
                        checkExpression(root, (Expression)ex.getOperand(i));
                    }
                }
            }
        }
        catch (ExpressionException eex) {
            throw new ValidationException(eex.getUnlabeledMessage());
        }
    }
    
    
Handler to user's actions with root selection combobox
    class RootSelectionHandler implements FocusListenerActionListener {
        String newName = null;
        boolean needChangeName;
        public void actionPerformed(ActionEvent ae) {
            SelectQuery query = getQuery();
            if (query != null) {
                Entity root = (Entity) .getModel().getSelectedItem();
                if (root != null) {
                    query.setRoot(root);
                    
                    if () { //not changed by user
                        
Doing auto name change, following CAY-888 #2
                        String newPrefix = root.getName() + "Query";
                         = newPrefix;
                        
                        DataMap map = .getCurrentDataMap();
                        long postfix = 1;
                        
                        while (map.getQuery() != null) {
                             = newPrefix + (postfix++);
                        }
                        
                        .setText();
                    }
                }
            }
        }
        public void focusGained(FocusEvent e) {
            //reset new name tracking
             = null;
            
            SelectQuery query = getQuery();
            if (query != null) {
                 = hasDefaultName(query);
            }
            else {
                 = false;
            }
        }
        public void focusLost(FocusEvent e) {
            if ( != null) {
                setQueryName();
            }
            
             = null;
             = false;
        }

        

Returns:
whether specified's query name is 'default' i.e. Cayenne generated A query's name is 'default' if it starts with 'UntitledQuery' or with root name. We cannot follow user input because tab might be opened many times
        boolean hasDefaultName(SelectQuery query) {
            String prefix = query.getRoot() == null ? "UntitledQuery" :
                CellRenderers.asString(query.getRoot()) + "Query";
            
            return .getComponent().getText().startsWith(prefix);
        }
    }
New to GrepCode? Check out our FAQ X