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.util.List;
 
 
 import  org.apache.cayenne.map.DataMap;
 import  org.apache.cayenne.map.Embeddable;
 import  org.apache.cayenne.map.EmbeddableAttribute;
 import  org.apache.cayenne.map.event.EmbeddableAttributeEvent;
 import  org.apache.cayenne.map.event.EmbeddableAttributeListener;
 import  org.apache.cayenne.map.event.EmbeddableEvent;
 import  org.apache.cayenne.map.event.EmbeddableListener;
 
 public class EmbeddableAttributeTab extends JPanel implements
         EmbeddableAttributeListener, EmbeddableDisplayListener, EmbeddableListener,
         ExistingSelectionProcessor {
 
     protected ProjectController mediator;
     protected CayenneTable table;
 
     JButton resolve;
 
     public EmbeddableAttributeTab(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.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();

        
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() {
        .addEmbeddableListener(this);
            public void valueChanged(ListSelectionEvent e) {
                processExistingSelection(e);
            }
        });
                ,
                CutAttributeAction.getActionName(),
                CopyAttributeAction.getActionName());
    }
    public void processExistingSelection(EventObject e) {
        if (e instanceof ChangeEvent) {
            .clearSelection();
        }
        EmbeddableAttribute[] attrs = new EmbeddableAttribute[0];
        if (.getSelectedRow() >= 0) {
                    .getModel();
            int[] sel = .getSelectedRows();
            attrs = new EmbeddableAttribute[sel.length];
            for (int i = 0; i < sel.lengthi++) {
                attrs[i] = model.getEmbeddableAttribute(sel[i]);
            }
            if (sel.length == 1) {
                UIUtil.scrollToSelectedRow();
            }
        }
                this,
                .getCurrentEmbeddable(),
                attrs,
                .getCurrentDataMap(),
                .getCurrentDataDomain());
    }
    private void rebuildTable(Embeddable emb) {
                emb,
                ,
                this);
        .setModel(model);
        .setRowHeight(25);
        .setRowMargin(3);
        setUpTableStructure(model);
    }
    private void setUpTableStructure(EmbeddableAttributeTableModel model) {
        TableColumn nameColumn = .getColumnModel().getColumn(
                .);
        nameColumn.setMinWidth(150);
        TableColumn typeColumn = .getColumnModel().getColumn(
                .);
        typeColumn.setMinWidth(150);
        JComboBox javaTypesCombo = CayenneWidgetFactory.createComboBox(ModelerUtil
                .getRegisteredTypeNames(), false);
        AutoCompletion.enable(javaTypesCombofalsetrue);
        typeColumn.setCellEditor(CayenneWidgetFactory.createCellEditor(javaTypesCombo));
        TableColumn dbAttrColumn = .getColumnModel().getColumn(
                .);
        dbAttrColumn.setMinWidth(150);
    }

    
Selects a specified attribute.
    public void selectAttributes(EmbeddableAttribute[] embAttrs) {
        ModelerUtil.updateActions(
                embAttrs.length,
                RemoveAttributeAction.getActionName(),
                CopyAttributeAction.getActionName(),
                CutAttributeAction.getActionName());
                .getModel();
        List listAttrs = model.getObjectList();
        int[] newSel = new int[embAttrs.length];
        for (int i = 0; i < embAttrs.length; i++) {
            newSel[i] = listAttrs.indexOf(embAttrs[i]);
        }
        .select(newSel);
    }
    public void embeddableAttributeAdded(EmbeddableAttributeEvent e) {
        rebuildTable((Embeddable) e.getEmbeddable());
        .select(e.getEmbeddableAttribute());
    }
    public void embeddableAttributeChanged(EmbeddableAttributeEvent e) {
        .select(e.getEmbeddableAttribute());
    }
    public void embeddableAttributeRemoved(EmbeddableAttributeEvent e) {
                .getModel();
        int ind = model.getObjectList().indexOf(e.getEmbeddableAttribute());
        model.removeRow(e.getEmbeddableAttribute());
        .select(ind);
    }
        if (e.getSource() == this) {
            return;
        }
        Embeddable embeddable = (Embeddable) e.getEmbeddable();
        if (embeddable != null) {
            rebuildTable(embeddable);
        }
    }
    public void embeddableAdded(EmbeddableEvent e, DataMap map) {
    }
    public void embeddableChanged(EmbeddableEvent e, DataMap map) {
        if (e.getOldName() != null) {
            ((Embeddable)map.getEmbeddable(e.getOldName())).setClassName(e.getEmbeddable().getClassName());
            if(map.getEmbeddableMap().containsKey(e.getOldName())){
                map.removeEmbeddable(e.getOldName());
                map.addEmbeddable(e.getEmbeddable());
            }
        }
    }
    public void embeddableRemoved(EmbeddableEvent e, DataMap map) {
    }
New to GrepCode? Check out our FAQ X