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 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.dbentity;
 import java.util.List;
Displays DbRelationships for the current DbEntity.
 public class DbEntityRelationshipTab extends JPanel implements DbEntityDisplayListener,
         DbEntityListener, DbRelationshipListener, ExistingSelectionProcessor,
     protected ProjectController mediator;
     protected CayenneTable table;
     protected 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;
Combo to edit 'target' field
     protected JComboBox targetCombo;
     public DbEntityRelationshipTab(ProjectController mediator) {
        this. = mediator;
        ActionListener resolver = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
    protected void init() {
        this.setLayout(new BorderLayout());
        JToolBar toolBar = new JToolBar();
        Application app = Application.getInstance();
         = new JButton();
        Icon ico = ModelerUtil.buildIcon("icon-info.gif");
        .setToolTipText("Database Mapping");
         = new CayenneTable();
        .setDefaultRenderer(DbEntity.class, CellRenderers.entityTableRendererWithIcons());
Create and install a popup
         = new JMenuItem("Database Mapping"ico);
        JPopupMenu popup = new JPopupMenu();
        add(PanelFactory.createTablePanel(null), .);
                CutRelationshipAction.getActionName(), CopyRelationshipAction.getActionName());
    public void valueChanged(ListSelectionEvent e) {
    public void tableChanged(TableModelEvent e) {
        DbRelationship rel = null;
        if (.getSelectedRow() >= 0) {
            DbRelationshipTableModel model = (DbRelationshipTableModel.getModel();
            rel = model.getRelationship(.getSelectedRow());
            .setEnabled(rel.getTargetEntity() != null);

Selects a specified relationship in the relationships table.
    public void selectRelationships(DbRelationship[] rels) {
        List listAttrs = model.getObjectList();
        int[] newSel = new int[rels.length];
        for (int i = 0; i < rels.length; i++) {
            newSel[i] = listAttrs.indexOf(rels[i]);
    public void processExistingSelection(EventObject e) {
        if (e instanceof ChangeEvent) {
        DbRelationship[] rels = new DbRelationship[0];
        if (.getSelectedRow() >= 0) {
            DbRelationshipTableModel model = (DbRelationshipTableModel.getModel();
            int[] sel = .getSelectedRows();
            rels = new DbRelationship[sel.length];
            for (int i = 0; i < sel.lengthi++) {
                rels[i] = model.getRelationship(sel[i]);
            .setEnabled(rels.length == 1 && rels[0].getTargetEntity() != null);
            if (sel.length == 1) {
                // scroll table
        RelationshipDisplayEvent ev = new RelationshipDisplayEvent(thisrels
                .getCurrentDbEntity(), .getCurrentDataMap(), 
    private void resolveRelationship() {
        int row = .getSelectedRow();
        if (row < 0) {
        // Get DbRelationship
        DbRelationship rel = model.getRelationship(row);
        ResolveDbRelationshipDialog dialog = new ResolveDbRelationshipDialog(rel);

Loads obj relationships into table.
    public void currentDbEntityChanged(EntityDisplayEvent e) {
        DbEntity entity = (DbEntity) e.getEntity();
        if (entity != null && e.isEntityChanged()) {
            // TODO: this line seems to slow down the Modeler significantly sometimes
            // (esp. noticable if selected entity has no relationships!),
            // even when this tab is not showing...maybe we should simply mark the view as
            // dirty and rebuild it when it becomes visible
        // if an entity was selected on a tree,
        // unselect currently selected row
        if (e.isUnselectAttributes()) {
    protected void rebuildTable(DbEntity entity) {
        DbRelationshipTableModel model = new DbRelationshipTableModel(
         = CayenneWidgetFactory.createComboBox();
    public void dbEntityChanged(EntityEvent e) {
    public void dbEntityAdded(EntityEvent e) {
    public void dbEntityRemoved(EntityEvent e) {
    public void dbRelationshipChanged(RelationshipEvent e) {
        if (e.getSource() != this) {
            if (!(.getModel() instanceof DbRelationshipTableModel)) {
                rebuildTable((DbEntity) e.getEntity());
            DbRelationshipTableModel model = (DbRelationshipTableModel.getModel();
    public void dbRelationshipAdded(RelationshipEvent e) {
        rebuildTable((DbEntity) e.getEntity());
    public void dbRelationshipRemoved(RelationshipEvent e) {
        int ind = model.getObjectList().indexOf(e.getRelationship());

Refresh the list of db entities (targets). Also refresh the table in case some db relationships were deleted.
    private void reloadEntityList(EntityEvent e) {
        if (e.getSource() == this)
        // If current model added/removed, do nothing.
        if (.getCurrentDbEntity() == e.getEntity())
        // If this is just loading new currentDbEntity, do nothing
        if (.getCurrentDbEntity() == null)

Creates a list of DbEntities.
    private ComboBoxModel createComboModel() {
        DataMap map = .getCurrentDataMap();
        Object[] objects = map.getNamespace().getDbEntities().toArray();
        return new DefaultComboBoxModel(objects);
New to GrepCode? Check out our FAQ X