Line 0
Link Here
|
|
|
1 |
package org.netbeans.modules.git.ui.commit; |
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Font; |
5 |
import java.awt.Graphics; |
6 |
import java.awt.event.KeyEvent; |
7 |
import java.net.URL; |
8 |
import java.util.List; |
9 |
import javax.swing.Action; |
10 |
import javax.swing.ImageIcon; |
11 |
import javax.swing.text.BadLocationException; |
12 |
import javax.swing.text.Document; |
13 |
import javax.swing.text.JTextComponent; |
14 |
import org.netbeans.api.editor.mimelookup.MimeRegistration; |
15 |
import org.netbeans.modules.versioning.util.common.VCSCommitParameters; |
16 |
import org.netbeans.spi.editor.completion.CompletionDocumentation; |
17 |
import org.netbeans.spi.editor.completion.CompletionItem; |
18 |
import org.netbeans.spi.editor.completion.CompletionProvider; |
19 |
import org.netbeans.spi.editor.completion.CompletionResultSet; |
20 |
import org.netbeans.spi.editor.completion.CompletionTask; |
21 |
import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery; |
22 |
import org.netbeans.spi.editor.completion.support.AsyncCompletionTask; |
23 |
import org.netbeans.spi.editor.completion.support.CompletionUtilities; |
24 |
import org.openide.util.Exceptions; |
25 |
|
26 |
@MimeRegistration(mimeType = "text/plain", service = CompletionProvider.class) |
27 |
public class RecentMessagesCompletionProvider implements CompletionProvider { |
28 |
|
29 |
@Override |
30 |
public CompletionTask createTask(int queryType, JTextComponent jtc) { |
31 |
if (queryType != CompletionProvider.COMPLETION_QUERY_TYPE) { |
32 |
return null; |
33 |
} |
34 |
final List<String> prevCommitMessages = (List<String>) jtc.getClientProperty("previousCommitMessages"); |
35 |
return new AsyncCompletionTask(new AsyncCompletionQuery() { |
36 |
|
37 |
@Override |
38 |
protected void query(CompletionResultSet resultSet, Document doc, int caretOffset) { |
39 |
if (prevCommitMessages != null) { |
40 |
for (String commitMessage : prevCommitMessages) { |
41 |
resultSet.addItem(new RecentMessageCompletionItem(commitMessage, caretOffset)); |
42 |
} |
43 |
} |
44 |
|
45 |
resultSet.finish(); |
46 |
} |
47 |
}); |
48 |
} |
49 |
|
50 |
@Override |
51 |
public int getAutoQueryTypes(JTextComponent component, String typedText) { |
52 |
return 0; |
53 |
} |
54 |
|
55 |
private static class RecentMessageCompletionItem implements CompletionItem { |
56 |
|
57 |
private final String text; |
58 |
private static final ImageIcon icon = new ImageIcon(VCSCommitParameters.class.getResource("/org/netbeans/modules/versioning/util/resources/recent_messages.png")); |
59 |
|
60 |
private final int caretOffset; |
61 |
private final String firstLine; |
62 |
|
63 |
public RecentMessageCompletionItem(String text, int caretOffset) { |
64 |
this.text = text; |
65 |
this.firstLine = getFirstLineOfMessage(text); |
66 |
this.caretOffset = caretOffset; |
67 |
} |
68 |
|
69 |
private String getFirstLineOfMessage(String text) { |
70 |
String result; |
71 |
String[] split = text.split("[\n\r]"); |
72 |
if (null != split && split.length > 0) { |
73 |
result = split[0]; |
74 |
} else { |
75 |
result = text; |
76 |
} |
77 |
//prevent too long lines in CC |
78 |
if (result.length() > 200) { |
79 |
result = result.substring(0, 200); |
80 |
} |
81 |
return result; |
82 |
} |
83 |
|
84 |
@Override |
85 |
public void defaultAction(JTextComponent component) { |
86 |
//TODO insert at current caret |
87 |
// try { |
88 |
// component.getDocument().insertString(caretOffset, text, null); |
89 |
// } catch (BadLocationException ex) { |
90 |
// Exceptions.printStackTrace(ex); |
91 |
// } |
92 |
} |
93 |
|
94 |
@Override |
95 |
public void processKeyEvent(KeyEvent evt) { |
96 |
} |
97 |
|
98 |
@Override |
99 |
public int getPreferredWidth(Graphics g, Font defaultFont) { |
100 |
return CompletionUtilities.getPreferredWidth(firstLine, null, g, defaultFont); |
101 |
} |
102 |
|
103 |
@Override |
104 |
public void render(Graphics g, Font defaultFont, Color defaultColor, Color backgroundColor, int width, int height, boolean selected) { |
105 |
CompletionUtilities.renderHtml(icon, firstLine, null, g, defaultFont, defaultColor, width, height, selected); |
106 |
} |
107 |
|
108 |
@Override |
109 |
public CompletionTask createDocumentationTask() { |
110 |
return new AsyncCompletionTask(new AsyncCompletionQuery() { |
111 |
@Override |
112 |
protected void query(CompletionResultSet completionResultSet, Document document, int i) { |
113 |
completionResultSet.setDocumentation(new RecentMessageCompletionDocumentation(RecentMessageCompletionItem.this)); |
114 |
completionResultSet.finish(); |
115 |
} |
116 |
}); |
117 |
} |
118 |
|
119 |
@Override |
120 |
public CompletionTask createToolTipTask() { |
121 |
return null; |
122 |
} |
123 |
|
124 |
@Override |
125 |
public boolean instantSubstitution(JTextComponent component) { |
126 |
return false; |
127 |
} |
128 |
|
129 |
@Override |
130 |
public int getSortPriority() { |
131 |
//TODO use order of recent messages |
132 |
return 0; |
133 |
} |
134 |
|
135 |
@Override |
136 |
public CharSequence getSortText() { |
137 |
return firstLine; |
138 |
} |
139 |
|
140 |
@Override |
141 |
public CharSequence getInsertPrefix() { |
142 |
return firstLine; |
143 |
} |
144 |
|
145 |
} |
146 |
|
147 |
private static class RecentMessageCompletionDocumentation implements CompletionDocumentation { |
148 |
|
149 |
private final RecentMessageCompletionItem item; |
150 |
|
151 |
public RecentMessageCompletionDocumentation(RecentMessageCompletionItem item) { |
152 |
this.item = item; |
153 |
} |
154 |
|
155 |
@Override |
156 |
public String getText() { |
157 |
//TODO replace linebreaks with <br> |
158 |
return item.text; |
159 |
} |
160 |
|
161 |
@Override |
162 |
public URL getURL() { |
163 |
return null; |
164 |
} |
165 |
|
166 |
@Override |
167 |
public CompletionDocumentation resolveLink(String string) { |
168 |
return null; |
169 |
} |
170 |
|
171 |
@Override |
172 |
public Action getGotoSourceAction() { |
173 |
return null; |
174 |
} |
175 |
|
176 |
} |
177 |
} |