|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.ui; |
|
38 |
| |
|
39 |
| import java.util.ArrayList; |
|
40 |
| |
|
41 |
| import javax.swing.*; |
|
42 |
| import javax.swing.event.*; |
|
43 |
| import javax.swing.text.*; |
|
44 |
| import java.awt.event.*; |
|
45 |
| import java.awt.*; |
|
46 |
| |
|
47 |
| import edu.rice.cs.drjava.config.OptionConstants; |
|
48 |
| import edu.rice.cs.drjava.model.IDocumentRegion; |
|
49 |
| import edu.rice.cs.drjava.model.SingleDisplayModel; |
|
50 |
| import edu.rice.cs.drjava.model.OpenDefinitionsDocument; |
|
51 |
| import edu.rice.cs.drjava.DrJava; |
|
52 |
| import edu.rice.cs.util.StringOps; |
|
53 |
| import edu.rice.cs.util.swing.RightClickMouseAdapter; |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| public abstract class RegionsListPanel<R extends IDocumentRegion> extends TabbedPanel { |
|
61 |
| protected JPanel _leftPane; |
|
62 |
| |
|
63 |
| protected JList _list; |
|
64 |
| protected DefaultListModel _listModel; |
|
65 |
| protected String _title; |
|
66 |
| |
|
67 |
| protected final SingleDisplayModel _model; |
|
68 |
| protected final MainFrame _frame; |
|
69 |
| |
|
70 |
| protected JPanel _buttonPanel; |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
0
| public RegionsListPanel(MainFrame frame, String title) {
|
|
78 |
0
| super(frame, title);
|
|
79 |
0
| _title = title;
|
|
80 |
0
| this.setLayout(new BorderLayout());
|
|
81 |
| |
|
82 |
0
| _frame = frame;
|
|
83 |
0
| _model = frame.getModel();
|
|
84 |
| |
|
85 |
0
| this.removeAll();
|
|
86 |
| |
|
87 |
| |
|
88 |
0
| _closePanel = new JPanel(new BorderLayout());
|
|
89 |
0
| _closePanel.add(_closeButton, BorderLayout.NORTH);
|
|
90 |
| |
|
91 |
0
| _leftPane = new JPanel(new BorderLayout());
|
|
92 |
0
| _setupRegionList();
|
|
93 |
| |
|
94 |
0
| this.add(_leftPane, BorderLayout.CENTER);
|
|
95 |
| |
|
96 |
0
| _buttonPanel = new JPanel(new BorderLayout());
|
|
97 |
0
| _setupButtonPanel();
|
|
98 |
0
| this.add(_buttonPanel, BorderLayout.EAST);
|
|
99 |
0
| updateButtons();
|
|
100 |
| |
|
101 |
| |
|
102 |
0
| _setColors(_list);
|
|
103 |
| |
|
104 |
0
| _list.addMouseListener(new RegionMouseAdapter());
|
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
0
| private static void _setColors(Component c) {
|
|
109 |
0
| new ForegroundColorListener(c);
|
|
110 |
0
| new BackgroundColorListener(c);
|
|
111 |
| } |
|
112 |
| |
|
113 |
| |
|
114 |
0
| @Override
|
|
115 |
| protected void _close() { |
|
116 |
0
| super._close();
|
|
117 |
0
| updateButtons();
|
|
118 |
| } |
|
119 |
| |
|
120 |
| |
|
121 |
0
| private void _setupRegionList() {
|
|
122 |
0
| _listModel = new DefaultListModel();
|
|
123 |
0
| _list = new JList(_listModel) {
|
|
124 |
0
| public String getToolTipText(MouseEvent evt) {
|
|
125 |
| |
|
126 |
0
| int index = locationToIndex(evt.getPoint());
|
|
127 |
| |
|
128 |
0
| @SuppressWarnings("unchecked") RegionListUserObj<R> node = (RegionListUserObj<R>)getModel().getElementAt(index);
|
|
129 |
0
| R r = node.region();
|
|
130 |
0
| String tooltip = null;
|
|
131 |
| |
|
132 |
0
| OpenDefinitionsDocument doc = r.getDocument();
|
|
133 |
0
| try {
|
|
134 |
0
| int lnr = doc.getLineOfOffset(r.getStartOffset())+1;
|
|
135 |
0
| int startOffset = doc._getOffset(lnr - 3);
|
|
136 |
0
| if (startOffset < 0) { startOffset = 0; }
|
|
137 |
0
| int endOffset = doc._getOffset(lnr + 3);
|
|
138 |
0
| if (endOffset < 0) { endOffset = doc.getLength()-1; }
|
|
139 |
| |
|
140 |
| |
|
141 |
0
| String s = doc.getText(startOffset, endOffset-startOffset);
|
|
142 |
| |
|
143 |
| |
|
144 |
0
| int rStart = r.getStartOffset() - startOffset;
|
|
145 |
0
| if (rStart < 0) { rStart = 0; }
|
|
146 |
0
| int rEnd = r.getEndOffset() - startOffset;
|
|
147 |
0
| if (rEnd>s.length()) { rEnd = s.length(); }
|
|
148 |
0
| if ((rStart <= s.length()) && (rEnd >= rStart)) {
|
|
149 |
0
| String t1 = StringOps.encodeHTML(s.substring(0, rStart));
|
|
150 |
0
| String t2 = StringOps.encodeHTML(s.substring(rStart, rEnd));
|
|
151 |
0
| String t3 = StringOps.encodeHTML(s.substring(rEnd));
|
|
152 |
0
| s = t1 + "<font color=#ff0000>" + t2 + "</font>" + t3;
|
|
153 |
| } |
|
154 |
| else { |
|
155 |
0
| s = StringOps.encodeHTML(s);
|
|
156 |
| } |
|
157 |
0
| tooltip = "<html><pre>" + s + "</pre></html>";
|
|
158 |
| } |
|
159 |
0
| catch(javax.swing.text.BadLocationException ble) { tooltip = null; }
|
|
160 |
0
| return tooltip;
|
|
161 |
| } |
|
162 |
| }; |
|
163 |
0
| _list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
|
164 |
0
| _list.addListSelectionListener(new ListSelectionListener() {
|
|
165 |
0
| public void valueChanged(ListSelectionEvent e) { updateButtons(); }
|
|
166 |
| }); |
|
167 |
0
| _list.addKeyListener(new KeyAdapter() {
|
|
168 |
0
| public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) performDefaultAction(); }
|
|
169 |
| }); |
|
170 |
0
| _list.setFont(DrJava.getConfig().getSetting(OptionConstants.FONT_DOCLIST));
|
|
171 |
| |
|
172 |
0
| _leftPane.add(new JScrollPane(_list));
|
|
173 |
0
| ToolTipManager.sharedInstance().registerComponent(_list);
|
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
0
| protected void updateButtons() {
|
|
178 |
| } |
|
179 |
| |
|
180 |
| |
|
181 |
0
| protected void performDefaultAction() {
|
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
0
| protected JComponent[] makeButtons() {
|
|
186 |
0
| return new JComponent[0];
|
|
187 |
| } |
|
188 |
| |
|
189 |
| |
|
190 |
0
| private void _setupButtonPanel() {
|
|
191 |
0
| JPanel mainButtons = new JPanel();
|
|
192 |
0
| JPanel emptyPanel = new JPanel();
|
|
193 |
0
| JPanel closeButtonPanel = new JPanel(new BorderLayout());
|
|
194 |
0
| GridBagLayout gbLayout = new GridBagLayout();
|
|
195 |
0
| GridBagConstraints c = new GridBagConstraints();
|
|
196 |
0
| mainButtons.setLayout(gbLayout);
|
|
197 |
| |
|
198 |
0
| JComponent[] buts = makeButtons();
|
|
199 |
| |
|
200 |
0
| closeButtonPanel.add(_closeButton, BorderLayout.NORTH);
|
|
201 |
0
| for (JComponent b: buts) { mainButtons.add(b); }
|
|
202 |
0
| mainButtons.add(emptyPanel);
|
|
203 |
| |
|
204 |
0
| c.fill = GridBagConstraints.HORIZONTAL;
|
|
205 |
0
| c.anchor = GridBagConstraints.NORTH;
|
|
206 |
0
| c.gridwidth = GridBagConstraints.REMAINDER;
|
|
207 |
0
| c.weightx = 1.0;
|
|
208 |
| |
|
209 |
0
| for (JComponent b: buts) { gbLayout.setConstraints(b, c); }
|
|
210 |
| |
|
211 |
0
| c.fill = GridBagConstraints.BOTH;
|
|
212 |
0
| c.anchor = GridBagConstraints.SOUTH;
|
|
213 |
0
| c.gridheight = GridBagConstraints.REMAINDER;
|
|
214 |
0
| c.weighty = 1.0;
|
|
215 |
| |
|
216 |
0
| gbLayout.setConstraints(emptyPanel, c);
|
|
217 |
| |
|
218 |
0
| _buttonPanel.add(mainButtons, BorderLayout.CENTER);
|
|
219 |
0
| _buttonPanel.add(closeButtonPanel, BorderLayout.EAST);
|
|
220 |
| } |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
0
| protected ArrayList<R> getSelectedRegions() {
|
|
226 |
0
| ArrayList<R> regs = new ArrayList<R>();
|
|
227 |
0
| int[] indices = _list.getSelectedIndices();
|
|
228 |
0
| if (indices != null) {
|
|
229 |
0
| for (int index: indices) {
|
|
230 |
0
| @SuppressWarnings("unchecked") RegionListUserObj<R> userObj = ((RegionListUserObj<R>)_listModel.elementAt(index));
|
|
231 |
0
| R r = userObj.region();
|
|
232 |
0
| regs.add(r);
|
|
233 |
| } |
|
234 |
| } |
|
235 |
0
| return regs;
|
|
236 |
| } |
|
237 |
| |
|
238 |
| |
|
239 |
0
| protected void goToRegion() {
|
|
240 |
0
| ArrayList<R> r = getSelectedRegions();
|
|
241 |
0
| if (r.size() == 1) {
|
|
242 |
0
| RegionListUserObj<R> userObj = getUserObjForRegion(r.get(0));
|
|
243 |
0
| if (userObj != null) { _list.ensureIndexIsVisible(_listModel.indexOf(userObj)); }
|
|
244 |
0
| _frame.scrollToDocumentAndOffset(r.get(0).getDocument(), r.get(0).getStartOffset(), false);
|
|
245 |
| } |
|
246 |
| } |
|
247 |
| |
|
248 |
| |
|
249 |
0
| protected RegionListUserObj<R> getUserObjForRegion(R r) {
|
|
250 |
0
| for(int i = 0; i < _listModel.size(); ++i) {
|
|
251 |
0
| @SuppressWarnings("unchecked")
|
|
252 |
| RegionListUserObj<R> userObj = (RegionListUserObj<R>)_listModel.get(i); |
|
253 |
0
| if ((userObj.region().getStartOffset() == r.getStartOffset()) &&
|
|
254 |
| (userObj.region().getEndOffset() == r.getEndOffset()) && |
|
255 |
| (userObj.region().getDocument().equals(r.getDocument()))) { |
|
256 |
0
| return userObj;
|
|
257 |
| } |
|
258 |
| } |
|
259 |
0
| return null;
|
|
260 |
| } |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
0
| public void addRegion(final R r, final int index) {
|
|
267 |
0
| assert EventQueue.isDispatchThread();
|
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
0
| RegionListUserObj<R> userObj = makeRegionListUserObj(r);
|
|
279 |
0
| _listModel.add(index, userObj);
|
|
280 |
0
| _list.ensureIndexIsVisible(_listModel.indexOf(userObj));
|
|
281 |
| |
|
282 |
0
| updateButtons();
|
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| } |
|
287 |
| |
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
0
| public void removeRegion(final R r) {
|
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
0
| for (int i = 0; i < _listModel.size(); ++i) {
|
|
304 |
0
| @SuppressWarnings("unchecked") RegionListUserObj<R> userObj = (RegionListUserObj<R>)_listModel.get(i);
|
|
305 |
0
| if (userObj.region() == r) {
|
|
306 |
0
| _listModel.removeElementAt(i);
|
|
307 |
0
| break;
|
|
308 |
| } |
|
309 |
| } |
|
310 |
| |
|
311 |
0
| updateButtons();
|
|
312 |
| |
|
313 |
| |
|
314 |
| |
|
315 |
| } |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
0
| protected RegionListUserObj<R> makeRegionListUserObj(R r) {
|
|
343 |
0
| return new RegionListUserObj<R>(r);
|
|
344 |
| } |
|
345 |
| |
|
346 |
| |
|
347 |
| protected static class RegionListUserObj<R extends IDocumentRegion> { |
|
348 |
| protected R _region; |
|
349 |
0
| public int lineNumber() { return _region.getDocument().getLineOfOffset(_region.getStartOffset())+1; }
|
|
350 |
0
| public R region() { return _region; }
|
|
351 |
0
| public RegionListUserObj(R r) { _region = r; }
|
|
352 |
0
| public String toString() {
|
|
353 |
0
| final StringBuilder sb = new StringBuilder();
|
|
354 |
0
| sb.append(_region.getDocument().toString());
|
|
355 |
0
| sb.append(':');
|
|
356 |
0
| sb.append(lineNumber());
|
|
357 |
0
| try {
|
|
358 |
0
| sb.append(": ");
|
|
359 |
0
| int length = Math.min(120, _region.getEndOffset() - _region.getStartOffset());
|
|
360 |
0
| sb.append(_region.getDocument().getText(_region.getStartOffset(), length).trim());
|
|
361 |
| } catch(BadLocationException bpe) { } |
|
362 |
0
| return sb.toString();
|
|
363 |
| } |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| } |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| protected class RegionMouseAdapter extends RightClickMouseAdapter { |
|
377 |
0
| protected void _popupAction(MouseEvent e) {
|
|
378 |
| |
|
379 |
| } |
|
380 |
| |
|
381 |
0
| public void mousePressed(MouseEvent e) {
|
|
382 |
0
| super.mousePressed(e);
|
|
383 |
0
| if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
|
|
384 |
0
| performDefaultAction();
|
|
385 |
| } |
|
386 |
| } |
|
387 |
| } |
|
388 |
| |
|
389 |
| } |