PageRenderTime 74ms CodeModel.GetById 65ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/structure/TypeAheadPopupListCellRenderer.java

#
Java | 85 lines | 52 code | 11 blank | 22 comment | 12 complexity | d9c80bf49c3c88fe099ecfdee1395fd6 MD5 | raw file
 1/*
 2 * TypeAheadPopupListCellRenderer.java - 
 3 *
 4 * Copyright 2005 Robert McKinnon
 5 *
 6 * This program is free software; you can redistribute it and/or
 7 * modify it under the terms of the GNU General Public License
 8 * as published by the Free Software Foundation; either version 2
 9 * of the License, or any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 */
20package org.jedit.ruby.structure;
21
22import org.jedit.ruby.ast.Member;
23import org.jedit.ruby.icons.MemberIcon;
24
25import javax.swing.DefaultListCellRenderer;
26import javax.swing.JList;
27import java.awt.Component;
28import java.util.List;
29
30/**
31 * @author robmckinnon at users.sourceforge.net
32 */
33final class TypeAheadPopupListCellRenderer extends DefaultListCellRenderer {
34
35    private final Member gotoParentMember;
36    private boolean allHaveSameName;
37    private final boolean showAll;
38
39    public TypeAheadPopupListCellRenderer(Member toParentMember, boolean showAllMembers) {
40        gotoParentMember = toParentMember;
41        showAll = showAllMembers;
42        allHaveSameName = false;
43    }
44
45    public final Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
46        super.getListCellRendererComponent(list, null, index, isSelected, cellHasFocus);
47
48        Member member = (Member)value;
49        StringBuffer buffer = new StringBuffer();
50
51        if (member == gotoParentMember) {
52            buffer.append("[ " + org.jedit.ruby.structure.TypeAheadPopup.UP_TO_PARENT_TEXT + " ]");
53        } else {
54            if (showAll) {
55                for (int i = 0; i < member.getParentCount(); i++) {
56                    buffer.append("  ");
57                }
58                buffer.append(member.getDisplayName());
59            } else {
60                buffer.append(member.getDisplayName());
61            }
62
63            if (allHaveSameName && member.hasParentMember()) {
64                String diplayName = member.getParentMember().getDisplayName();
65                buffer.append(" (").append(diplayName).append(")");
66            }
67        }
68
69        setText(buffer.toString());
70        MemberIcon memberIcon = new MemberIcon(member);
71        setIcon(memberIcon.getIcon());
72        return this;
73    }
74
75    final void resetAllHaveSameName(List<Member> members) {
76        allHaveSameName = members.size() > 1;
77        String name = null;
78        for (Member member : members) {
79            if (name != null) {
80                allHaveSameName = allHaveSameName && name.equals(member.getFullName());
81            }
82            name = member.getFullName();
83        }
84    }
85}