1+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+ /*
3+ * This file is part of Arduino.
4+ *
5+ * Copyright 2015 Ricardo JL Rufino (ricardo@criativasoft.com.br)
6+ * Copyright 2015 Arduino LLC
7+ *
8+ * Arduino is free software; you can redistribute it and/or modify
9+ * it under the terms of the GNU General Public License as published by
10+ * the Free Software Foundation; either version 2 of the License, or
11+ * (at your option) any later version.
12+ *
13+ * This program is distributed in the hope that it will be useful,
14+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+ * GNU General Public License for more details.
17+ *
18+ * You should have received a copy of the GNU General Public License
19+ * along with this program; if not, write to the Free Software
20+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+ *
22+ * As a special exception, you may use this file as part of a free software
23+ * library without restriction. Specifically, if other files instantiate
24+ * templates or use macros or inline functions from this file, or you compile
25+ * this file and link it with other files to produce an executable, this
26+ * file does not by itself cause the resulting executable to be covered by
27+ * the GNU General Public License. This exception does not however
28+ * invalidate any other reasons why the executable file might be covered by
29+ * the GNU General Public License.
30+ */
31+ package cc .arduino .autocomplete ;
32+
33+ import java .awt .Color ;
34+ import java .awt .Component ;
35+ import java .awt .Font ;
36+ import java .util .HashMap ;
37+ import java .util .Map ;
38+
39+ import javax .swing .DefaultListCellRenderer ;
40+ import javax .swing .Icon ;
41+ import javax .swing .ImageIcon ;
42+ import javax .swing .JList ;
43+
44+ import org .fife .ui .autocomplete .BasicCompletion ;
45+ import org .fife .ui .autocomplete .FunctionCompletion ;
46+ import org .fife .ui .autocomplete .ShorthandCompletion ;
47+ import org .fife .ui .autocomplete .TemplateCompletion ;
48+ import org .fife .ui .rsyntaxtextarea .RSyntaxTextArea ;
49+
50+ //import br.com.criativasoft.cpluslibparser.metadata.TElement;
51+ //import br.com.criativasoft.cpluslibparser.metadata.TFunction;
52+
53+ /**
54+ * Class responsible for formatting the elements of the autocomplete list
55+ */
56+ public class CompletionsRenderer extends DefaultListCellRenderer {
57+
58+ private static final long serialVersionUID = 1L ;
59+
60+ private static final Color HIGHLIGHT_COLOR = new Color (74 , 144 , 217 );
61+
62+ private static final String GRAY = "#A0A0A0" ;
63+ private static final String LIGHT_BLUE = "#008080" ;
64+
65+ /**
66+ * Keeps the HTML descriptions from "wrapping" in the list, which cuts off
67+ * words.
68+ */
69+ private static final String PREFIX = "<html><nobr>" ;
70+
71+ private static Map <CompletionType , Icon > iconsTypes = new HashMap <CompletionType , Icon >();
72+
73+ static {
74+ iconsTypes .put (CompletionType .CLASS , getIcon ("class.gif" ));
75+ iconsTypes .put (CompletionType .ENUM , getIcon ("enum.gif" ));
76+ iconsTypes .put (CompletionType .VARIABLE , getIcon ("variable.gif" ));
77+ iconsTypes .put (CompletionType .LOCAL_VAR , getIcon ("variable_local.gif" ));
78+ iconsTypes .put (CompletionType .STATIC_VAR , getIcon ("variable_static.gif" ));
79+ iconsTypes .put (CompletionType .FUNCTION , getIcon ("function.gif" ));
80+ iconsTypes .put (CompletionType .ERROR , getIcon ("error.gif" ));
81+ iconsTypes .put (CompletionType .TEMPLATE , getIcon ("template_obj.gif" ));
82+ }
83+
84+ private static Icon getIcon (String image ) {
85+ return new ImageIcon (
86+ CompletionsRenderer .class .getResource ("icons/" + image ));
87+ }
88+
89+ public static Icon getIcon (CompletionType tokenType ) {
90+ return iconsTypes .get (tokenType );
91+ }
92+
93+ public CompletionsRenderer () {
94+ setOpaque (true );
95+ // Font f = Theme.getDefaultFont();
96+ Font f = RSyntaxTextArea .getDefaultFont ();
97+ setFont (f .deriveFont (f .getStyle () & ~Font .BOLD )); // remove bold.
98+ }
99+
100+ @ Override
101+ public Component getListCellRendererComponent (JList list , Object value ,
102+ int index , boolean isSelected ,
103+ boolean cellHasFocus ) {
104+
105+ String text = null ;
106+ CompletionType tokenType = null ;
107+
108+ // if (value instanceof TElementCompletion) {
109+ //
110+ // TElementCompletion completion = (TElementCompletion) value;
111+ // TElement element = completion.getElement();
112+ // tokenType = completion.getType();
113+ // text = element.getHtmlRepresentation();
114+ //
115+ // } else if (value instanceof TFunctionCompletion) {
116+ //
117+ // TFunctionCompletion completion = (TFunctionCompletion) value;
118+ // TFunction function = completion.getFunction();
119+ // text = function.getHtmlRepresentation();
120+ // tokenType = CompletionType.FUNCTION;
121+ //
122+ // } else
123+ if (value instanceof ShorthandCompletion ) {
124+ text = ((ShorthandCompletion ) value ).getShortDescription ();
125+ tokenType = CompletionType .TEMPLATE ;
126+ } else if (value instanceof FunctionCompletion ) {
127+ text = ((FunctionCompletion ) value ).getShortDescription ();
128+ tokenType = CompletionType .FUNCTION ;
129+ } else if (value instanceof BasicCompletion ) {
130+ text = ((BasicCompletion ) value ).getInputText ();
131+ if (((BasicCompletion ) value ).getShortDescription () != null ) {
132+ text = ((BasicCompletion ) value ).getShortDescription ();
133+ }
134+ } else if (value instanceof TemplateCompletion ) {
135+ TemplateCompletion template = (TemplateCompletion ) value ;
136+ text = font (template .getInputText (), LIGHT_BLUE )
137+ + font (" - " + template .getDefinitionString (), GRAY );
138+ }
139+
140+ if (text == null ) {
141+ text = value .toString ();
142+ }
143+
144+ // Find Icon
145+ if (tokenType != null ) {
146+ setIcon (iconsTypes .get (tokenType ));
147+ } else {
148+ setIcon (iconsTypes .get (CompletionType .TEMPLATE ));
149+ }
150+
151+ if (isSelected ) {
152+ setText (text .replaceAll ("\\ <[^>]*>" , "" ));
153+ setBackground (HIGHLIGHT_COLOR );
154+ setForeground (Color .white );
155+ } else {
156+ setText (PREFIX + text );
157+ setBackground (Color .white );
158+ setForeground (Color .black );
159+ }
160+
161+ return this ;
162+ }
163+
164+ private String font (String text , String color ) {
165+ return "<font color='" + color + "'>" + text + "</font>" ;
166+ }
167+
168+ }
0 commit comments