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.dbentity;
 
 import java.util.List;
 
 
 import  org.apache.cayenne.dba.TypesMapping;
 import  org.apache.cayenne.map.DbAttribute;
 import  org.apache.cayenne.map.DbEntity;
 import  org.apache.cayenne.map.event.AttributeEvent;
 import  org.apache.cayenne.map.event.DbAttributeListener;
Detail view of the DbEntity attributes.
 
 public class DbEntityAttributeTab extends JPanel implements DbEntityDisplayListener,
         ListSelectionListener, DbAttributeListener, ExistingSelectionProcessor {
 
     protected ProjectController mediator;
     protected CayenneTable table;
 
     public DbEntityAttributeTab(ProjectController temp_mediator) {
         super();
          = temp_mediator;
         .addDbEntityDisplayListener(this);
         .addDbAttributeListener(this);
 
         // Create and layout components
         init();
     }
 
     private void init() {
         this.setLayout(new BorderLayout());
 
         JToolBar toolBar = new JToolBar();
         Application app = Application.getInstance();
         toolBar.add(app.getAction(CreateObjEntityAction.getActionName()).buildButton());
         toolBar.add(app.getAction(CreateAttributeAction.getActionName()).buildButton());
         toolBar.add(app.getAction(DbEntitySyncAction.getActionName()).buildButton());
 
         toolBar.addSeparator();
 
         toolBar.addSeparator();
         toolBar.add(app.getAction(RemoveAttributeAction.getActionName()).buildButton());
         
         toolBar.addSeparator();
         toolBar.add(app.getAction(CutAttributeAction.getActionName()).buildButton());
         toolBar.add(app.getAction(CopyAttributeAction.getActionName()).buildButton());
         toolBar.add(app.getAction(PasteAction.getActionName()).buildButton());
 
        add(toolBar.);
        
        // Create table with two columns and no rows.
         = new CayenneTable();
        
        
Create and install a popup
        JPopupMenu popup = new JPopupMenu();
        popup.add(app.getAction(RemoveAttributeAction.getActionName()).buildMenu());
        
        popup.addSeparator();
        popup.add(app.getAction(CutAttributeAction.getActionName()).buildMenu());
        popup.add(app.getAction(CopyAttributeAction.getActionName()).buildMenu());
        popup.add(app.getAction(PasteAction.getActionName()).buildMenu());
        
        TablePopupHandler.install(popup);
        
        add(PanelFactory.createTablePanel(null), .);
        
                CutAttributeAction.getActionName(), CopyAttributeAction.getActionName());
    }
    public void valueChanged(ListSelectionEvent e) {
        processExistingSelection(e);
    }

    
Selects specified attributes.
    public void selectAttributes(DbAttribute[] attrs) {
        ModelerUtil.updateActions(attrs.length,  
                RemoveAttributeAction.getActionName(),
                CutAttributeAction.getActionName(),
                CopyAttributeAction.getActionName());
        
        List<?> listAttrs = model.getObjectList();
        int[] newSel = new int[attrs.length];
        
        for (int i = 0; i < attrs.length; i++) {
            newSel[i] = listAttrs.indexOf(attrs[i]);
        }
        
        .select(newSel);
    }
    public void processExistingSelection(EventObject e) {
        if (e instanceof ChangeEvent) {
            .clearSelection();
        }
            
        DbAttribute[] attrs = new DbAttribute[0];
        if (.getSelectedRow() >= 0) {
           DbAttributeTableModel model = (DbAttributeTableModel.getModel();
           
           int[] sel = .getSelectedRows();
           attrs = new DbAttribute[sel.length];
           
           for (int i = 0; i < sel.lengthi++) {
               attrs[i] = model.getAttribute(sel[i]);
           }
    
           if (sel.length == 1) {
               // scroll table
               UIUtil.scrollToSelectedRow();
           }
        }
    
               this,
               attrs,
               .getCurrentDbEntity(),
               .getCurrentDataMap(),
               .getCurrentDataDomain()));
    }
    public void dbAttributeChanged(AttributeEvent e) {
        .select(e.getAttribute());
    }
    public void dbAttributeAdded(AttributeEvent e) {
        rebuildTable((DbEntity) e.getEntity());
        .select(e.getAttribute());
    }
    public void dbAttributeRemoved(AttributeEvent e) {
        int ind = model.getObjectList().indexOf(e.getAttribute());
        model.removeRow(e.getAttribute());
        .select(ind);
    }
    public void currentDbEntityChanged(EntityDisplayEvent e) {
        DbEntity entity = (DbEntity) e.getEntity();
        if (entity != null && e.isEntityChanged()) {
            rebuildTable(entity);
        }
        // if an entity was selected on a tree,
        // unselect currently selected row
        if (e.isUnselectAttributes()) {
            .clearSelection();
        }
    }
    protected void rebuildTable(DbEntity ent) {
        DbAttributeTableModel model = new DbAttributeTableModel(entthis);
        .setModel(model);
        .setRowHeight(25);
        .setRowMargin(3);
        TableColumn col = .getColumnModel().getColumn(model.nameColumnInd());
        col.setMinWidth(150);
        col = .getColumnModel().getColumn(model.typeColumnInd());
        col.setMinWidth(90);
        String[] types = TypesMapping.getDatabaseTypes();
        JComboBox comboBox = CayenneWidgetFactory.createComboBox(typestrue);
        
        // Types.NULL makes no sense as a column type
        comboBox.removeItem("NULL");
        
        AutoCompletion.enable(comboBox);
        
        col.setCellEditor(CayenneWidgetFactory.createCellEditor(comboBox));
    }
New to GrepCode? Check out our FAQ X