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.awt.Font;
 import java.util.List;
 
 
 import  org.apache.cayenne.map.DataMap;
 import  org.apache.cayenne.map.Embeddable;
 import  org.apache.cayenne.map.ObjAttribute;
 import  org.apache.cayenne.map.ObjEntity;
 import  org.apache.cayenne.map.event.AttributeEvent;
 import  org.apache.cayenne.map.event.EntityEvent;
 import  org.apache.cayenne.map.event.ObjAttributeListener;
 import  org.apache.cayenne.map.event.ObjEntityListener;
Detail view of the ObjEntity attributes.
 
 public class ObjEntityAttributeTab extends JPanel implements ObjEntityDisplayListener,
         ObjEntityListener, ObjAttributeListener, ExistingSelectionProcessor {
 
     protected ProjectController mediator;
     protected CayenneTable table;
 
     JButton resolve;
 
     public ObjEntityAttributeTab(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(CreateAttributeAction.getActionName()).buildButton());
        toolBar.add(app.getAction(ObjEntitySyncAction.getActionName()).buildButton());
        toolBar.addSeparator();
        Icon ico = ModelerUtil.buildIcon("icon-info.gif");
         = new JButton();
        .setIcon(ico);
        .setToolTipText("Edit Attribute");
        toolBar.add();
        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.);
         = new CayenneTable();
        .setDefaultRenderer(String.classnew CellRenderer());

        
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), .);
    }
    private void initController() {
        .addObjEntityListener(this);
        .addObjAttributeListener(this);
            public void valueChanged(ListSelectionEvent e) {
                processExistingSelection(e);
            }
        });
        ActionListener resolver = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int row = .getSelectedRow();
                if (row < 0) {
                    return;
                }
                ObjAttributeTableModel model = (ObjAttributeTableModel.getModel();
                // ... show dialog...
                new ObjAttributeInfoDialog(rowmodel).startupAction();

                
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);
                ,
                CutAttributeAction.getActionName(),
                CopyAttributeAction.getActionName());
    }
    public void initComboBoxes(ObjAttributeTableModel model) {
        List<StringembeddableNames = new ArrayList<String>();
        List<StringtypeNames = new ArrayList<String>();
        Iterator it = .getCurrentDataDomain().getDataMaps().iterator();
        while (it.hasNext()) {
            DataMap dataMap = (DataMap) it.next();
            Iterator<Embeddable> embs = dataMap.getEmbeddables().iterator();
            while (embs.hasNext()) {
                Embeddable emb = (Embeddable) embs.next();
                embeddableNames.add(emb.getClassName());
            }
        }
        
        String[] registeredTypes = ModelerUtil.getRegisteredTypeNames();
        for(int i=0; iregisteredTypes.lengthi++){
            typeNames.add(registeredTypes[i]);
        }
        typeNames.addAll(embeddableNames);
     
        TableColumn typeColumn = .getColumnModel().getColumn(
                .);
        JComboBox javaTypesCombo = CayenneWidgetFactory.createComboBox(typeNames.toArray(), false);
        AutoCompletion.enable(javaTypesCombofalsetrue);
        typeColumn.setCellEditor(CayenneWidgetFactory.createCellEditor(javaTypesCombo));
        TableColumn dbNameColumn = .getColumnModel().getColumn(
                .);
        dbNameColumn.setMinWidth(150);
        if (model.getEntity().getDbEntity() != null) {
            Collection<StringnameAttr = ModelerUtil.getDbAttributeNames(model
                    .getEntity()
                    .getDbEntity());
            model.setCellEditor(nameAttr);
            model.setComboBoxes(nameAttr.);
        }
    }

    
Selects a specified attribute.
    public void selectAttributes(ObjAttribute[] 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();
        }
        ObjAttribute[] attrs = new ObjAttribute[0];
        if (.getSelectedRow() >= 0) {
            ObjAttributeTableModel model = (ObjAttributeTableModel.getModel();
            int[] sel = .getSelectedRows();
            attrs = new ObjAttribute[sel.length];
            for (int i = 0; i < sel.lengthi++) {
                attrs[i] = model.getAttribute(sel[i]);
            }
            if (sel.length == 1) {
                // scroll table
                UIUtil.scrollToSelectedRow();
                .setEnabled(true);
            }
            else {
                .setEnabled(false);
            }
        }
        else {
            .setEnabled(false);
        }
        AttributeDisplayEvent ev = new AttributeDisplayEvent(thisattrs
                .getCurrentObjEntity(), .getCurrentDataMap(), 
                .getCurrentDataDomain());
    }
    public void objAttributeChanged(AttributeEvent e) {
        rebuildTable((ObjEntity) e.getEntity());
        .select(e.getAttribute());
    }
    public void objAttributeAdded(AttributeEvent e) {
        rebuildTable((ObjEntity) e.getEntity());
        .select(e.getAttribute());
    }
    public void objAttributeRemoved(AttributeEvent e) {
        int ind = model.getObjectList().indexOf(e.getAttribute());
        model.removeRow(e.getAttribute());
        .select(ind);
    }
    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();
        }
    }
    protected void rebuildTable(ObjEntity entity) {
        ObjAttributeTableModel model = new ObjAttributeTableModel(entitythis);
        .setModel(model);
        .setRowHeight(25);
        .setRowMargin(3);
        setUpTableStructure(model);
    }
    protected void setUpTableStructure(ObjAttributeTableModel model) {
        TableColumn inheritanceColumn = .getColumnModel().getColumn(
                .);
        inheritanceColumn.setMinWidth(20);
        inheritanceColumn.setMaxWidth(20);
        TableColumn nameColumn = .getColumnModel().getColumn(
                .);
        nameColumn.setMinWidth(150);
        TableColumn typeColumn = .getColumnModel().getColumn(
                .);
        typeColumn.setMinWidth(150);
        TableColumn lockColumn = .getColumnModel().getColumn(
                .);
        lockColumn.setMinWidth(100);
        TableColumn dbTypeColumn = .getColumnModel().getColumn(
                .);
        dbTypeColumn.setMinWidth(120);
        initComboBoxes(model);
    }

    
Refreshes attributes view for the updated entity
    public void objEntityChanged(EntityEvent e) {
        if (e.getSource() == this) {
            return;
        }
        if (!(.getModel() instanceof ObjAttributeTableModel)) {
            // probably means this panel hasn't been loaded yet...
            return;
        }
        if (model.getDbEntity() != ((ObjEntity) e.getEntity()).getDbEntity()) {
            model.resetDbEntity();
            setUpTableStructure(model);
        }
    }
    public void objEntityAdded(EntityEvent e) {
    }
    public void objEntityRemoved(EntityEvent e) {
    }
    // custom renderer used for inherited attributes highlighting
    final class CellRenderer extends DefaultTableCellRenderer {
        @Override
                JTable table,
                Object value,
                boolean isSelected,
                boolean hasFocus,
                int row,
                int column) {
            super.getTableCellRendererComponent(
                    table,
                    value,
                    isSelected,
                    hasFocus,
                    row,
                    column);
            ObjAttributeTableModel model = (ObjAttributeTableModeltable.getModel();
            ObjAttribute attribute = model.getAttribute(row);
            if (column != .) {
                if (!model.isCellEditable(rowcolumn)) {
                    setForeground(.);
                }
                else {
                    setForeground(isSelected && !hasFocus ? table
                            .getSelectionForeground() : table.getForeground());
                }
                if (attribute.isInherited()) {
                    Font font = getFont();
                    Font newFont = font.deriveFont(.);
                    setFont(newFont);
                }
                setIcon(null);
            }
            else {
                if (attribute.isInherited()) {
                    ImageIcon objEntityIcon = ModelerUtil.buildIcon("icon-override.gif");
                    setIcon(objEntityIcon);
                }
                setText("");
            }
            setBackground(isSelected && !hasFocus
                    ? table.getSelectionBackground()
                    : table.getBackground());
            return this;
        }
    }