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.util.combo;
 
 
 
CustomTypeComboBoxEditor is used as an editor of a combobox, when custom type (such as Entity) is to be used. BasicComboBoxEditor cannot be used, because it converts String values to other types incorrectly (in fact, only classes with valueOf(String) methods are supported).
 
 public class CustomTypeComboBoxEditor extends BasicComboBoxEditor {
    
'oldValue' property is private somewhy, so we make our local copy
 
     protected Object localOldValue;
    
    
The combobox being edited
 
     protected final JComboBox combo;
    
    
Whether non-present items are allowed
 
     protected final boolean allowsUserValues;
    
    
Creates new editor

Parameters:
combo ComboBox being edited
 
     public CustomTypeComboBoxEditor(JComboBox comboboolean allowsUserValues) {
          = new EditorTextField(combo);
         this. = combo;
         this. = allowsUserValues;
     }
    
    
Sets the item that should be edited.

Parameters:
anObject the displayed value of the editor
 
     @Override
     public void setItem(Object anObject) {
          = anObject;
         super.setItem(anObject == null ? null : CellRenderers.asString(anObject));
     }
    
    

Returns:
edited item
 
     @Override
     public Object getItem() {
         Object newValue = .getText();
         
         if ( != null && !( instanceof String))  {
             // The original value is not a string. Should return the value in it's
             // original type.
             if (newValue.equals(.toString())) {
                 return ;
             }
             else {
                 // Must take the value from the editor and get the value and cast it to the new type.
                 Class cls = .getClass();
                 try {
                     newValue = convert((String)newValuecls);
                 }
                 catch (Exception ignored) {}
             }
         }
         
         if (! && newValue != null) {
             boolean contains = false;
            
            for (int i = 0; i < .getItemCount(); i++) {
                if (newValue.equals(.getItemAt(i))) {
                    contains = true;
                    break;
                }
            }
                   
            if (!contains) {
                return null;
            }
        }
        
        return newValue;
    }
    
    
Converts String value to specified type

Parameters:
value String value of textfield
classTo type of result item
Returns:
value of classTo type, or null if conversion is impossible
    protected Object convert(String valueClass classTo) {
        if (classTo == String.class) {
            return value;
        }
        
        
We still try to it in BasicComboBox's way, so that primary object types (such as numbers) would still be supported
        try {
            Method method = classTo.getMethod("valueOf"new Class[]{String.class});
            return method.invoke(nullvalue);
        }
        catch (Exception ignored) {}
        
        
We could manually convert strings to dbentities, attrs and other, but in this implementation we use reverse operation instead, and convert combobox model's items to String. All string values are assumed unique is one model.
        ComboBoxModel model = .getModel();
        for (int i = 0; i < model.getSize(); i++) {
            if (value.equals(CellRenderers.asString(model.getElementAt(i)))) {
                return model.getElementAt(i);
            }
        }
        
        //we return null, since String will not be appreciated
        return null;
    }
New to GrepCode? Check out our FAQ X