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 java.awt.Color;
 import java.util.List;
 
 
 import  org.apache.cayenne.map.DataMap;
 import  org.apache.cayenne.map.DeleteRule;
 import  org.apache.cayenne.map.ObjEntity;
 import  org.apache.cayenne.map.ObjRelationship;
 import  org.apache.cayenne.map.event.EntityEvent;
 import  org.apache.cayenne.map.event.ObjEntityListener;
 import  org.apache.cayenne.map.event.ObjRelationshipListener;
 import  org.apache.cayenne.map.event.RelationshipEvent;
Displays ObjRelationships for the edited ObjEntity.
 
 public class ObjEntityRelationshipTab extends JPanel implements ObjEntityDisplayListener,
         ObjEntityListener, ObjRelationshipListener, ExistingSelectionProcessor {
 
     private static Log logObj = LogFactory.getLog(ObjEntityRelationshipTab.class);
 
     private static final Object[] deleteRules = new Object[] {
             DeleteRule.deleteRuleName(DeleteRule.NO_ACTION),
             DeleteRule.deleteRuleName(DeleteRule.NULLIFY),
             DeleteRule.deleteRuleName(DeleteRule.CASCADE),
             DeleteRule.deleteRuleName(DeleteRule.DENY),
     };
 
 
     CayenneTable table;
    JButton resolve;
    
    
By now popup menu item is made similiar to toolbar button. (i.e. all functionality is here) This should be probably refactored as Action.
    protected JMenuItem resolveMenu;
    public ObjEntityRelationshipTab(ProjectController mediator) {
        this. = mediator;
        init();
        initController();
    }
    private void init() {
        this.setLayout(new BorderLayout());
        JToolBar toolBar = new JToolBar();
        Application app = Application.getInstance();
        toolBar
                .add(app
                        .getAction(CreateRelationshipAction.getActionName())
                        .buildButton());
        toolBar.add(app.getAction(ObjEntitySyncAction.getActionName()).buildButton());
        toolBar.addSeparator();
        
        Icon ico = ModelerUtil.buildIcon("icon-info.gif");
         = new JButton();
        .setIcon(ico);
        .setToolTipText("Edit Relationship");
        toolBar.add();
        toolBar.addSeparator();
        toolBar.add(app.getAction(RemoveRelationshipAction.getActionName()).buildButton());
        
        toolBar.addSeparator();
        toolBar.add(app.getAction(CutRelationshipAction.getActionName()).buildButton());
        toolBar.add(app.getAction(CopyRelationshipAction.getActionName()).buildButton());
        toolBar.add(app.getAction(PasteAction.getActionName()).buildButton());
        
        add(toolBar.);
         = new CayenneTable();
        .setDefaultRenderer(String.classnew StringRenderer());
        .setDefaultRenderer(ObjEntity.classnew EntityRenderer());
        
        
Create and install a popup
         = new JMenuItem("Database Mapping"ico);
        
        JPopupMenu popup = new JPopupMenu();
        popup.add();
        popup.add(app.getAction(RemoveRelationshipAction.getActionName()).buildMenu());
        
        popup.addSeparator();
        popup.add(app.getAction(CutRelationshipAction.getActionName()).buildMenu());
        popup.add(app.getAction(CopyRelationshipAction.getActionName()).buildMenu());
        popup.add(app.getAction(PasteAction.getActionName()).buildMenu());
        
        TablePopupHandler.install(popup);
        add(PanelFactory.createTablePanel(null), .);
    }
    private void initController() {
        .addObjEntityListener(this);
        ActionListener resolver = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int row = .getSelectedRow();
                if (row < 0) {
                    return;
                }
                ObjRelationshipTableModel model = (ObjRelationshipTableModel
                        .getModel();
                new ObjRelationshipInfoController(model.getRelationship(row))
                        .startup();
                
                
This is required for a table to be updated properly
                .cancelEditing();
                // need to refresh selected row... do this by unselecting/selecting the
                // row
                .getSelectionModel().clearSelection();
                .select(row);
            }
        };
        
        .addActionListener(resolver);
        .addActionListener(resolver);
            public void valueChanged(ListSelectionEvent e) {
                processExistingSelection(e);
            }
        });
        
                CutRelationshipAction.getActionName(), CopyRelationshipAction.getActionName());
    }

    
Selects a specified relationship in the relationships table.
    public void selectRelationships(ObjRelationship[] rels) {
        ModelerUtil.updateActions(rels.length,  
                RemoveRelationshipAction.getActionName(),
                CutRelationshipAction.getActionName(),
                CopyRelationshipAction.getActionName());
        List listAttrs = model.getObjectList();
        int[] newSel = new int[rels.length];
        
        for (int i = 0; i < rels.length; i++) {
            newSel[i] = listAttrs.indexOf(rels[i]);
        }
        
        .select(newSel);
    }
    public void processExistingSelection(EventObject e) {
        if (e instanceof ChangeEvent) {
            .clearSelection();
        }
        ObjRelationship[] rels = new ObjRelationship[0];
        if (.getSelectedRow() >= 0) {
            ObjRelationshipTableModel model = (ObjRelationshipTableModel
                    .getModel();
            
            int[] sel = .getSelectedRows();
            rels = new ObjRelationship[sel.length];
            
            for (int i = 0; i < sel.lengthi++) {
                rels[i] = model.getRelationship(sel[i]);
            }
            
            .setEnabled(true);
            // scroll table
            UIUtil.scrollToSelectedRow();
        }
        else {
            .setEnabled(false);
        }
        
        RelationshipDisplayEvent ev = new RelationshipDisplayEvent(thisrels
                .getCurrentObjEntity(), .getCurrentDataMap(), 
                .getCurrentDataDomain());
    }

    
Loads obj relationships into table.
    public void currentObjEntityChanged(EntityDisplayEvent e) {
        if (e.getSource() == this) {
            return;
        }
        ObjEntity entity = (ObjEntity) e.getEntity();
        // Important: process event even if this is the same entity,
        // since the inheritance structure might have changed
        if (entity != null) {
            rebuildTable(entity);
        }
        // if an entity was selected on a tree,
        // unselect currently selected row
        if (e.isUnselectAttributes()) {
            .clearSelection();
        }
    }

    
Creates a list of ObjEntity names.
    private Object[] createObjEntityComboModel() {
        DataMap map = .getCurrentDataMap();
        // this actually happens per CAY-221... can't reproduce though
        if (map == null) {
            .warn("createObjEntityComboModel:: Null DataMap.");
            return new Object[0];
        }
        if (map.getNamespace() == null) {
            .warn("createObjEntityComboModel:: Null DataMap namespace - " + map);
            return new Object[0];
        }
        Collection objEntities = map.getNamespace().getObjEntities();
        return objEntities.toArray();
    }
    public void objEntityChanged(EntityEvent e) {
    }
    public void objEntityAdded(EntityEvent e) {
        reloadEntityList(e);
    }
    public void objEntityRemoved(EntityEvent e) {
        reloadEntityList(e);
    }
    public void objRelationshipChanged(RelationshipEvent e) {
        .select(e.getRelationship());
    }
    public void objRelationshipAdded(RelationshipEvent e) {
        rebuildTable((ObjEntity) e.getEntity());
        .select(e.getRelationship());
    }
    public void objRelationshipRemoved(RelationshipEvent e) {
        int ind = model.getObjectList().indexOf(e.getRelationship());
        model.removeRow(e.getRelationship());
        .select(ind);
    }

    
Refresh the list of ObjEntity targets. Also refresh the table in case some ObjRelationships were deleted.
    private void reloadEntityList(EntityEvent e) {
        if (e.getSource() != this) {
            return;
        }
        // If current model added/removed, do nothing.
        ObjEntity entity = .getCurrentObjEntity();
        if (entity == e.getEntity() || entity == null) {
            return;
        }
        TableColumn col = .getColumnModel().getColumn(
                .);
        DefaultCellEditor editor = (DefaultCellEditorcol.getCellEditor();
        JComboBox combo = (JComboBoxeditor.getComponent();
        combo.setRenderer(CellRenderers.entityListRendererWithIcons(entity.getDataMap()));
        model.fireTableDataChanged();
    }
    protected void rebuildTable(ObjEntity entity) {
        final ObjRelationshipTableModel model = new ObjRelationshipTableModel(
                entity,
                ,
                this);
        model.addTableModelListener(new TableModelListener() {
            public void tableChanged(TableModelEvent e) {
                if (.getSelectedRow() >= 0) {
                    ObjRelationship rel = model.getRelationship(.getSelectedRow());
                    if (((ObjEntity) rel.getSourceEntity()).getDbEntity() != null) {
                        .setEnabled(true);
                    }
                    else
                        .setEnabled(false);
                    
                    .setEnabled(.isEnabled());
                }
            }
        });
        .setModel(model);
        .setRowHeight(25);
        .setRowMargin(3);
        TableColumn lockColumn = .getColumnModel().getColumn(
                .);
        lockColumn.setMinWidth(100);
        TableColumn col = .getColumnModel().getColumn(
                .);
        col.setMinWidth(150);
        col.setMinWidth(150);
        JComboBox targetCombo = CayenneWidgetFactory.createComboBox(
                createObjEntityComboModel(),
                false);
        AutoCompletion.enable(targetCombo);        
        
        targetCombo.setRenderer(CellRenderers.entityListRendererWithIcons(entity
                .getDataMap()));
        targetCombo.setSelectedIndex(-1);
        col.setCellEditor(CayenneWidgetFactory.createCellEditor(targetCombo));
        col.setMinWidth(150);
        col.setMinWidth(60);
        JComboBox deleteRulesCombo = CayenneWidgetFactory.createComboBox(
                ,
                false);
        deleteRulesCombo.setEditable(false);
        deleteRulesCombo.setSelectedIndex(0); // Default to the first value
        col.setCellEditor(CayenneWidgetFactory.createCellEditor(deleteRulesCombo));
    }
    class EntityRenderer extends StringRenderer {
        @Override
                JTable table,
                Object value,
                boolean isSelected,
                boolean hasFocus,
                int row,
                int column) {
            Object oldValue = value;
            value = CellRenderers.asString(value);
            super.getTableCellRendererComponent(
                    table,
                    value,
                    isSelected,
                    hasFocus,
                    row,
                    column);
            
            setIcon(CellRenderers.iconForObject(oldValue));
            return this;
        }
    }
    class StringRenderer extends DefaultTableCellRenderer {
        @Override
                JTable table,
                Object value,
                boolean isSelected,
                boolean hasFocus,
                int row,
                int column) {
            // center cardinality column
            int align = column == .
                    ? .
                    : .;
            super.setHorizontalAlignment(align);
            super.getTableCellRendererComponent(
                    table,
                    value,
                    isSelected,
                    hasFocus,
                    row,
                    column);
            ObjRelationshipTableModel model = (ObjRelationshipTableModeltable
                    .getModel();
            ObjRelationship relationship = model.getRelationship(row);
            if (relationship != null
                    && relationship.getSourceEntity() != model.getEntity()) {
                setForeground(.);
            }
            else {
                setForeground(isSelected && !hasFocus
                        ? table.getSelectionForeground()
                        : table.getForeground());
            }
            return this;
        }
    }