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.dialog.autorelationship;
 
 
 
 import  org.apache.cayenne.map.DataMap;
 import  org.apache.cayenne.map.DbJoin;
 import  org.apache.cayenne.map.DbRelationship;
 import  org.apache.cayenne.map.Entity;
 import  org.apache.cayenne.map.event.MapEvent;
 import  org.apache.cayenne.map.event.RelationshipEvent;
 import  org.apache.cayenne.map.naming.NamingStrategy;
 
 
     public static final int SELECT = 1;
     public static final int CANCEL = 0;
 
     private static Log logObj = LogFactory.getLog(ErrorDebugDialog.class);
 
     protected InferRelationshipsDialog view;
 
 
     protected NamingStrategy strategy;
 
     public InferRelationshipsController(CayenneController parent, DataMap dataMap) {
         super(parentdataMap);
          = createNamingStrategy(NamingStrategyPreferences
                 .getInstance()
                 .getLastUsedStrategies()
                 .get(0));
         setNamingStrategy();
         setRelationships();
         this. = new InferRelationshipsTabController(this);
     }
 
     public NamingStrategy createNamingStrategy(String strategyClass) {
         try {
             ClassLoadingService classLoader = Application
                     .getInstance()
                     .getClassLoadingService();
 
             return (NamingStrategy) classLoader.loadClass(strategyClass).newInstance();
         }
         catch (Throwable th) {
             .error("Error in " + getClass().getName(), th);
 
             JOptionPane.showMessageDialog(
                     ,
                     "Naming Strategy Initialization Error: " + th.getMessage(),
                     "Naming Strategy Initialization Error",
                     .);
             return null;
         }
     }
 
     @Override
     public Component getView() {
         return ;
     }
 
     public void startup() {
         // show dialog even on empty DataMap, as custom generation may still take
         // advantage of it
 
          = new InferRelationshipsDialog(.getView());
         initBindings();
        .pack();
        .setModal(true);
        centerView();
        makeCloseableOnEscape();
        .setVisible(true);
    }
    protected void initBindings() {
        BindingBuilder builder = new BindingBuilder(
                getApplication().getBindingFactory(),
                this);
        builder.bindToAction(.getCancelButton(), "cancelAction()");
        builder.bindToAction(.getGenerateButton(), "generateAction()");
        builder.bindToAction(this"entitySelectedAction()");
        builder.bindToAction(.getStrategyCombo(), "strategyComboAction()");
    }
    public void entitySelectedAction() {
        int size = getSelectedEntitiesSize();
        String label;
        if (size == 0) {
            label = "No DbRelationships selected";
        }
        else if (size == 1) {
            label = "One DbRelationships selected";
        }
        else {
            label = size + " DbRelationships selected";
        }
        .getEntityCount().setText(label);
    }
    public void strategyComboAction() {
        try {
            String strategyClass = (String.getStrategyCombo().getSelectedItem();
            this. = createNamingStrategy(strategyClass);

            
Be user-friendly and update preferences with specified strategy
            if ( == null) {
                return;
            }
            NamingStrategyPreferences
                    .getInstance()
                    .addToLastUsedStrategies(strategyClass);
            .getStrategyCombo().setModel(
                    new DefaultComboBoxModel(NamingStrategyPreferences
                            .getInstance()
                            .getLastUsedStrategies()));
        }
        catch (Throwable th) {
            .error("Error in " + getClass().getName(), th);
            return;
        }
        setNamingStrategy();
        createName();
        .initBindings();
        .setChoice();
    }
    public NamingStrategy getNamingStrategy() {
        return ;
    }
    public void cancelAction() {
        .dispose();
    }
    public void generateAction() {
        
        ProjectController mediator = 
                .getFrameController()
                .getProjectController();
        
        InferRelationshipsUndoableEdit undoableEdit = new InferRelationshipsUndoableEdit();
        
        for (InferRelationships temp : ) {
            DbRelationship rel = new DbRelationship(uniqueRelName(temp.getSource(), temp
                    .getName()));
            RelationshipEvent e = new RelationshipEvent(Application.getFrame(), reltemp
                    .getSource(), MapEvent.ADD);
            mediator.fireDbRelationshipEvent(e);
            rel.setSourceEntity(temp.getSource());
            rel.setTargetEntity(temp.getTarget());
            DbJoin join = new DbJoin(reltemp.getJoinSource().getName(), temp
                    .getJoinTarget()
                    .getName());
            rel.addJoin(join);
            rel.setToMany(temp.isToMany());
            temp.getSource().addRelationship(rel);
            
            undoableEdit.addEdit(new CreateRelationshipUndoableEdit(temp.getSource(), new DbRelationship[] { rel }));
        }
        JOptionPane.showMessageDialog(this.getView(), getSelectedEntitiesSize()
                + " relationships generated");
        .dispose();
    }
    private String uniqueRelName(Entity entityString preferredName) {
        int currentSuffix = 1;
        String relName = preferredName;
        while (entity.getRelationship(relName) != null
                || entity.getAttribute(relName) != null) {
            relName = preferredName + currentSuffix;
            currentSuffix++;
        }
        return relName;
    }
New to GrepCode? Check out our FAQ X