//# 0 errors, 86 messages
//#
/*
    //#ActionTypeModel.java:1:1: class: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel
    //#ActionTypeModel.java:1:1: method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing.dialogs.actioneditor;

import com.dmdirc.actions.ActionTypeComparator;
import com.dmdirc.actions.interfaces.ActionType;
import com.dmdirc.util.MapList;

import java.awt.FontMetrics;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.SwingUtilities;

/**
 * Model for the "trigger" list of the actions editor. Adds type group headers,
 * and ensures they can't be selected.
 */
public final class ActionTypeModel extends DefaultComboBoxModel {
    
    /**
     * A version number for this class. It should be changed whenever the class
     * structure is changed (or anything else that would prevent serialized
     * objects being unserialized with the new class).
     */
    private static final long serialVersionUID = 1;    
    /** Max Width. */
    private int maxWidth = -1;
    /** Font metrics. */
    private FontMetrics fm;
    
    /**
     * Creates a new instance of ActionTypeModel.
     * 
     * @param fm Font metrics
     * @param typeGroups The action type groups to use
     */
    public ActionTypeModel(final FontMetrics fm, final MapList<String, ActionType> typeGroups) {
        super();
    //#ActionTypeModel.java:61: method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)
    //#input(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): fm
    //#input(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): this
    //#input(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): typeGroups
    //#output(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): this.fm
    //#output(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): this.maxWidth
    //#pre[3] (void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): typeGroups != null
    //#presumption(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): com.dmdirc.util.MapList:entrySet(...)@65 != null
    //#presumption(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): java.util.Iterator:next(...)@65 != null
    //#presumption(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): java.util.Map_Entry:getValue(...)@68 != null
    //#post(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): this.fm == fm
    //#post(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): init'ed(this.fm)
    //#post(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): init'ed(this.maxWidth)
    //#unanalyzed(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): Effects-of-calling:javax.swing.DefaultComboBoxModel:addElement
    //#unanalyzed(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): Effects-of-calling:java.lang.String:instanceof
    //#unanalyzed(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): Effects-of-calling:javax.swing.SwingUtilities:computeStringWidth
    //#unanalyzed(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): Effects-of-calling:com.dmdirc.actions.interfaces.ActionType:instanceof
    //#unanalyzed(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): Effects-of-calling:com.dmdirc.actions.interfaces.ActionType:getName
    //#unanalyzed(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): Effects-of-calling:java.lang.Math:max
    //#test_vector(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): java.util.Iterator:hasNext(...)@65: {0}, {1}
    //#test_vector(void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)): java.util.Iterator:hasNext(...)@71: {0}, {1}
        
        this.fm = fm;
        
        for (Map.Entry<String, List<ActionType>> entry : typeGroups.entrySet()) {
    //#ActionTypeModel.java:65: Warning: method not available - call not analyzed
    //#    call on Set com.dmdirc.util.MapList:entrySet()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel
    //#    method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)
    //#    unanalyzed callee: Set com.dmdirc.util.MapList:entrySet()
            addElement(entry.getKey());
            
            final List<ActionType> types = entry.getValue();
            Collections.sort(types, new ActionTypeComparator());
    //#ActionTypeModel.java:69: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.actions.ActionTypeComparator()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel
    //#    method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)
    //#    unanalyzed callee: void com.dmdirc.actions.ActionTypeComparator()
            
            for (ActionType type : types) {
                addElement(type);
            }
        }
    }
    //#ActionTypeModel.java:75: end of method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel(FontMetrics, MapList)

    /** 
     * {@inheritDoc}
     * 
     * @param anObject Objerct to add
     */
    @Override
    public void addElement(final Object anObject) {
        super.addElement(anObject);
    //#ActionTypeModel.java:84: method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.addElement(Object)
    //#input(void addElement(Object)): anObject
    //#input(void addElement(Object)): this
    //#input(void addElement(Object)): this.fm
    //#input(void addElement(Object)): this.maxWidth
    //#output(void addElement(Object)): this.maxWidth
    //#pre[2] (void addElement(Object)): init'ed(this.maxWidth)
    //#pre[4] (void addElement(Object)): (soft) init'ed(this.fm)
    //#post(void addElement(Object)): init'ed(this.maxWidth)
        int width = maxWidth;
        if (anObject instanceof String) {
            width = SwingUtilities.computeStringWidth(fm, (String) anObject);
        } else if (anObject instanceof ActionType) {
            width = SwingUtilities.computeStringWidth(fm, ((ActionType) anObject).getName());
    //#ActionTypeModel.java:89: Warning: method not available - call not analyzed
    //#    call on String com.dmdirc.actions.interfaces.ActionType:getName()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel
    //#    method: void addElement(Object)
    //#    unanalyzed callee: String com.dmdirc.actions.interfaces.ActionType:getName()
        }
        maxWidth = Math.max(width, maxWidth);
    }
    //#ActionTypeModel.java:92: end of method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.addElement(Object)

    /** {@inheritDoc} */
    @Override
    public void setSelectedItem(final Object anObject) {
        if (!(anObject instanceof String)) {
    //#ActionTypeModel.java:97: method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.setSelectedItem(Object)
    //#input(void setSelectedItem(Object)): anObject
    //#input(void setSelectedItem(Object)): this
            super.setSelectedItem(anObject);
        }
    }
    //#ActionTypeModel.java:100: end of method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.setSelectedItem(Object)
    
    /**
     * Sets the type group for this model.
     * 
     * @param typeGroup New type group
     */
    public void setTypeGroup(final MapList<String, ActionType> typeGroup) {
        removeAllElements();
    //#ActionTypeModel.java:108: method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.setTypeGroup(MapList)
    //#ActionTypeModel.java:108: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel:removeAllElements()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel
    //#    method: void setTypeGroup(MapList)
    //#    unanalyzed callee: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel:removeAllElements()
    //#input(void setTypeGroup(MapList)): this
    //#input(void setTypeGroup(MapList)): this.fm
    //#input(void setTypeGroup(MapList)): this.maxWidth
    //#input(void setTypeGroup(MapList)): typeGroup
    //#output(void setTypeGroup(MapList)): this.maxWidth
    //#pre[4] (void setTypeGroup(MapList)): typeGroup != null
    //#pre[1] (void setTypeGroup(MapList)): (soft) init'ed(this.maxWidth)
    //#pre[3] (void setTypeGroup(MapList)): (soft) init'ed(this.fm)
    //#presumption(void setTypeGroup(MapList)): com.dmdirc.util.MapList:entrySet(...)@110 != null
    //#presumption(void setTypeGroup(MapList)): java.util.Iterator:next(...)@110 != null
    //#presumption(void setTypeGroup(MapList)): java.util.Map_Entry:getValue(...)@113 != null
    //#post(void setTypeGroup(MapList)): init'ed(this.maxWidth)
    //#unanalyzed(void setTypeGroup(MapList)): Effects-of-calling:javax.swing.DefaultComboBoxModel:addElement
    //#unanalyzed(void setTypeGroup(MapList)): Effects-of-calling:java.lang.String:instanceof
    //#unanalyzed(void setTypeGroup(MapList)): Effects-of-calling:javax.swing.SwingUtilities:computeStringWidth
    //#unanalyzed(void setTypeGroup(MapList)): Effects-of-calling:com.dmdirc.actions.interfaces.ActionType:instanceof
    //#unanalyzed(void setTypeGroup(MapList)): Effects-of-calling:com.dmdirc.actions.interfaces.ActionType:getName
    //#unanalyzed(void setTypeGroup(MapList)): Effects-of-calling:java.lang.Math:max
    //#test_vector(void setTypeGroup(MapList)): java.util.Iterator:hasNext(...)@110: {0}, {1}
    //#test_vector(void setTypeGroup(MapList)): java.util.Iterator:hasNext(...)@116: {0}, {1}
        
        for (Map.Entry<String, List<ActionType>> entry : typeGroup.entrySet()) {
    //#ActionTypeModel.java:110: Warning: method not available - call not analyzed
    //#    call on Set com.dmdirc.util.MapList:entrySet()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel
    //#    method: void setTypeGroup(MapList)
    //#    unanalyzed callee: Set com.dmdirc.util.MapList:entrySet()
            addElement(entry.getKey());
            
            final List<ActionType> types = entry.getValue();
            Collections.sort(types, new ActionTypeComparator());
    //#ActionTypeModel.java:114: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.actions.ActionTypeComparator()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel
    //#    method: void setTypeGroup(MapList)
    //#    unanalyzed callee: void com.dmdirc.actions.ActionTypeComparator()
            
            for (ActionType type : types) {
                addElement(type);
            }
        }
    }
    //#ActionTypeModel.java:120: end of method: void com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.setTypeGroup(MapList)
    
    /**
     * Returns the maximum width of a string in this model.
     * 
     * @return String max width
     */
    public int getMaxWidth() {
        return maxWidth;
    //#ActionTypeModel.java:128: method: int com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.getMaxWidth()
    //#input(int getMaxWidth()): this
    //#input(int getMaxWidth()): this.maxWidth
    //#output(int getMaxWidth()): return_value
    //#pre[2] (int getMaxWidth()): init'ed(this.maxWidth)
    //#post(int getMaxWidth()): return_value == this.maxWidth
    //#post(int getMaxWidth()): init'ed(return_value)
    //#ActionTypeModel.java:128: end of method: int com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.getMaxWidth()
    }
}
    //#output(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Descendant_Table[com/dmdirc/addons/ui_swing/dialogs/actioneditor/ActionTypeModel]
    //#output(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Dispatch_Table.addElement(Ljava/lang/Object;)V
    //#output(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Dispatch_Table.getMaxWidth()I
    //#output(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Dispatch_Table.setSelectedItem(Ljava/lang/Object;)V
    //#output(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Dispatch_Table.setTypeGroup(Lcom/dmdirc/util/MapList;)V
    //#post(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Descendant_Table[com/dmdirc/addons/ui_swing/dialogs/actioneditor/ActionTypeModel] == &__Dispatch_Table
    //#post(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Dispatch_Table.addElement(Ljava/lang/Object;)V == &addElement
    //#post(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Dispatch_Table.getMaxWidth()I == &getMaxWidth
    //#post(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Dispatch_Table.setSelectedItem(Ljava/lang/Object;)V == &setSelectedItem
    //#post(com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init): __Dispatch_Table.setTypeGroup(Lcom/dmdirc/util/MapList;)V == &setTypeGroup
    //#ActionTypeModel.java:: end of method: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel.com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel__static_init
    //#ActionTypeModel.java:: end of class: com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionTypeModel
