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  org.apache.cayenne.dba.TypesMapping;
 import  com.jgoodies.forms.builder.DefaultFormBuilder;
 import  com.jgoodies.forms.layout.FormLayout;
 public class PKDBGeneratorPanel extends PKGeneratorPanel {
     private JComboBox attributes;
     public PKDBGeneratorPanel(ProjectController mediator) {
     private void initView() {
          = new JComboBox();
         .setRenderer(new AttributeRenderer());
         DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout(
                 "right:70dlu, 3dlu, fill:200dlu",
         builder.append("Auto Incremented:");
         setLayout(new BorderLayout());
         add(builder.getPanel(), .);
     public void setDbEntity(DbEntity entity) {
         // refresh only if this entity
         if (isVisible()) {
     public void onInit(DbEntity entity) {
         Collection pkAttributes = entity.getPrimaryKeys();
         // by default check the only numeric PK
         if (pkAttributes.size() == 1) {
             DbAttribute pk = (DbAttribute) pkAttributes.iterator().next();
             if (TypesMapping.isNumeric(pk.getType()) && !pk.isGenerated()) {
                 .fireDbEntityEvent(new EntityEvent(thisentity));
     void updateView(final DbEntity entity) {
         for (ItemListener listener : .getItemListeners()) {
         Collection<DbAttribute> pkAttributes = entity.getPrimaryKeys();
         if (pkAttributes.isEmpty()) {
            .addItem("<Entity has no PK columns>");
        else {
            MutableComboBoxModel model = new DefaultComboBoxModel(pkAttributes.toArray());
            String noSelection = "<Select Generated Column>";
            model.insertElementAt(noSelection, 0);
            for (DbAttribute a : pkAttributes) {
                if (a.isGenerated()) {
            // listen for selection changes of the new entity
            .addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent e) {
                    Object item = e.getItem();
                    if (item instanceof DbAttribute) {
                        boolean generated = e.getStateChange() == .;
                        DbAttribute a = (DbAttribute) item;
                        if (a.isGenerated() != generated) {
                            .fireDbEntityEvent(new EntityEvent(thisentity));
        // revalidate as children layout has changed...
    class AttributeRenderer extends BasicComboBoxRenderer {
        public Component getListCellRendererComponent(
                JList list,
                Object value,
                int index,
                boolean isSelected,
                boolean cellHasFocus) {
            if (value instanceof DbAttribute) {
                DbAttribute a = (DbAttribute) value;
                String type = TypesMapping.getSqlNameByType(a.getType());
                value = a.getName() + " (" + (type != null ? type : "?") + ")";
            return super.getListCellRendererComponent(
New to GrepCode? Check out our FAQ X