Lines 43-48
Link Here
|
43 |
package org.netbeans.modules.quicksearch; |
43 |
package org.netbeans.modules.quicksearch; |
44 |
|
44 |
|
45 |
import java.awt.Color; |
45 |
import java.awt.Color; |
|
|
46 |
import java.awt.Component; |
47 |
import java.awt.Container; |
46 |
import java.awt.FontMetrics; |
48 |
import java.awt.FontMetrics; |
47 |
import java.awt.KeyboardFocusManager; |
49 |
import java.awt.KeyboardFocusManager; |
48 |
import java.awt.event.ActionEvent; |
50 |
import java.awt.event.ActionEvent; |
Lines 51-63
Link Here
|
51 |
import java.awt.event.MouseAdapter; |
53 |
import java.awt.event.MouseAdapter; |
52 |
import java.awt.event.MouseEvent; |
54 |
import java.awt.event.MouseEvent; |
53 |
import java.lang.ref.WeakReference; |
55 |
import java.lang.ref.WeakReference; |
|
|
56 |
import java.util.Collections; |
57 |
import java.util.HashSet; |
58 |
import java.util.Iterator; |
59 |
import java.util.Set; |
60 |
import javax.swing.JCheckBoxMenuItem; |
54 |
import javax.swing.JComponent; |
61 |
import javax.swing.JComponent; |
55 |
import javax.swing.JList; |
62 |
import javax.swing.JList; |
|
|
63 |
import javax.swing.JMenuItem; |
56 |
import javax.swing.JPopupMenu; |
64 |
import javax.swing.JPopupMenu; |
57 |
import javax.swing.JRadioButtonMenuItem; |
|
|
58 |
import javax.swing.KeyStroke; |
65 |
import javax.swing.KeyStroke; |
|
|
66 |
import javax.swing.MenuElement; |
67 |
import javax.swing.MenuSelectionManager; |
59 |
import javax.swing.SwingUtilities; |
68 |
import javax.swing.SwingUtilities; |
60 |
import javax.swing.UIManager; |
69 |
import javax.swing.UIManager; |
|
|
70 |
import javax.swing.event.ChangeEvent; |
71 |
import javax.swing.event.ChangeListener; |
61 |
import javax.swing.event.DocumentEvent; |
72 |
import javax.swing.event.DocumentEvent; |
62 |
import javax.swing.event.DocumentListener; |
73 |
import javax.swing.event.DocumentListener; |
63 |
import javax.swing.text.AbstractDocument; |
74 |
import javax.swing.text.AbstractDocument; |
Lines 78-86
Link Here
|
78 |
* Quick search toolbar component |
89 |
* Quick search toolbar component |
79 |
* @author Jan Becicka |
90 |
* @author Jan Becicka |
80 |
*/ |
91 |
*/ |
81 |
public abstract class AbstractQuickSearchComboBar extends javax.swing.JPanel implements ActionListener { |
92 |
public abstract class AbstractQuickSearchComboBar extends javax.swing.JPanel { |
82 |
|
|
|
83 |
private static final String CATEGORY = "cat"; |
84 |
|
93 |
|
85 |
QuickSearchPopup displayer = new QuickSearchPopup(this); |
94 |
QuickSearchPopup displayer = new QuickSearchPopup(this); |
86 |
WeakReference<TopComponent> caller; |
95 |
WeakReference<TopComponent> caller; |
Lines 253-259
Link Here
|
253 |
setShowHint(false); |
262 |
setShowHint(false); |
254 |
if (CommandEvaluator.isCatTemporary()) { |
263 |
if (CommandEvaluator.isCatTemporary()) { |
255 |
CommandEvaluator.setCatTemporary(false); |
264 |
CommandEvaluator.setCatTemporary(false); |
256 |
CommandEvaluator.setEvalCat(null); |
265 |
CommandEvaluator.setEvalCats(null); |
257 |
} |
266 |
} |
258 |
} |
267 |
} |
259 |
|
268 |
|
Lines 263-283
Link Here
|
263 |
} |
272 |
} |
264 |
|
273 |
|
265 |
JPopupMenu pm = new JPopupMenu(); |
274 |
JPopupMenu pm = new JPopupMenu(); |
266 |
ProviderModel.Category evalCat = null; |
275 |
final Set<ProviderModel.Category> evalCats = |
|
|
276 |
new HashSet<ProviderModel.Category>(); |
267 |
if (!CommandEvaluator.isCatTemporary()) { |
277 |
if (!CommandEvaluator.isCatTemporary()) { |
268 |
evalCat = CommandEvaluator.getEvalCat(); |
278 |
evalCats.addAll(CommandEvaluator.getEvalCats()); |
269 |
} |
279 |
} |
270 |
|
280 |
JMenuItem allCats = new AllMenuItem(evalCats); |
271 |
JRadioButtonMenuItem allCats = new JRadioButtonMenuItem( |
|
|
272 |
NbBundle.getMessage(getClass(), "LBL_AllCategories"), evalCat == null); |
273 |
allCats.addActionListener(this); |
274 |
pm.add(allCats); |
281 |
pm.add(allCats); |
275 |
|
282 |
|
276 |
for (ProviderModel.Category cat : ProviderModel.getInstance().getCategories()) { |
283 |
for (ProviderModel.Category cat : ProviderModel.getInstance().getCategories()) { |
277 |
if (!CommandEvaluator.RECENT.equals(cat.getName())) { |
284 |
if (!CommandEvaluator.RECENT.equals(cat.getName())) { |
278 |
JRadioButtonMenuItem item = new JRadioButtonMenuItem(cat.getDisplayName(), cat == evalCat); |
285 |
JCheckBoxMenuItem item = new CategoryCheckBoxMenuItem(cat, |
279 |
item.putClientProperty(CATEGORY, cat); |
286 |
evalCats); |
280 |
item.addActionListener(this); |
|
|
281 |
pm.add(item); |
287 |
pm.add(item); |
282 |
} |
288 |
} |
283 |
} |
289 |
} |
Lines 285-304
Link Here
|
285 |
pm.show(getInnerComponent(), 0, getInnerComponent().getHeight() - 1); |
291 |
pm.show(getInnerComponent(), 0, getInnerComponent().getHeight() - 1); |
286 |
} |
292 |
} |
287 |
|
293 |
|
288 |
/** ActionListener implementation, reaction to popup menu item invocation */ |
294 |
private void updateCats(Set<Category> evalCats) { |
289 |
public void actionPerformed(ActionEvent e) { |
295 |
CommandEvaluator.setEvalCats(evalCats); |
290 |
JRadioButtonMenuItem item = (JRadioButtonMenuItem)e.getSource(); |
|
|
291 |
CommandEvaluator.setEvalCat((Category) item.getClientProperty(CATEGORY)); |
292 |
CommandEvaluator.setCatTemporary(false); |
296 |
CommandEvaluator.setCatTemporary(false); |
293 |
// refresh hint |
297 |
// refresh hint |
294 |
setShowHint(!command.isFocusOwner()); |
298 |
setShowHint(!command.isFocusOwner()); |
295 |
} |
299 |
} |
296 |
|
300 |
|
|
|
301 |
private void updateCheckBoxes(Container container, Set<Category> evalCats) { |
302 |
Container parent = container.getParent(); |
303 |
for (Component c : parent.getComponents()) { |
304 |
if (c instanceof CategoryCheckBoxMenuItem) { |
305 |
CategoryCheckBoxMenuItem ci = (CategoryCheckBoxMenuItem) c; |
306 |
ci.setSelected(evalCats.contains(ci.category)); |
307 |
} |
308 |
} |
309 |
} |
310 |
|
297 |
/** Runs evaluation narrowed to specified category |
311 |
/** Runs evaluation narrowed to specified category |
298 |
* |
312 |
* |
299 |
*/ |
313 |
*/ |
300 |
public void evaluateCategory (Category cat, boolean temporary) { |
314 |
public void evaluateCategory (Category cat, boolean temporary) { |
301 |
CommandEvaluator.setEvalCat(cat); |
315 |
CommandEvaluator.setEvalCats( |
|
|
316 |
cat == null ? null : Collections.singleton(cat)); |
302 |
CommandEvaluator.setCatTemporary(temporary); |
317 |
CommandEvaluator.setCatTemporary(temporary); |
303 |
displayer.maybeEvaluate(command.getText()); |
318 |
displayer.maybeEvaluate(command.getText()); |
304 |
} |
319 |
} |
Lines 322-330
Link Here
|
322 |
} |
337 |
} |
323 |
if (showHint) { |
338 |
if (showHint) { |
324 |
command.setForeground(command.getDisabledTextColor()); |
339 |
command.setForeground(command.getDisabledTextColor()); |
325 |
Category evalCat = CommandEvaluator.getEvalCat(); |
340 |
Set<Category> evalCats = CommandEvaluator.getEvalCats(); |
326 |
if (evalCat != null && !CommandEvaluator.isCatTemporary()) { |
341 |
if (evalCats.size() < 3 && !CommandEvaluator.isCatTemporary()) { |
327 |
command.setText(getHintText(evalCat)); |
342 |
Category bestFound = null; |
|
|
343 |
for (Category c : evalCats) { |
344 |
if (bestFound == null || CommandEvaluator.RECENT.equals( |
345 |
bestFound.getName())) { |
346 |
bestFound = c; |
347 |
} |
348 |
} |
349 |
command.setText(getHintText(bestFound)); |
328 |
} else { |
350 |
} else { |
329 |
command.setText(getHintText(null)); |
351 |
command.setText(getHintText(null)); |
330 |
} |
352 |
} |
Lines 493-496
Link Here
|
493 |
DialogDisplayer.getDefault().notifyLater(nd); |
515 |
DialogDisplayer.getDefault().notifyLater(nd); |
494 |
} |
516 |
} |
495 |
} |
517 |
} |
|
|
518 |
|
519 |
/** |
520 |
* Show and select menu at a given path. Used to restore a menu after click. |
521 |
*/ |
522 |
private void showMenuPath(MenuElement[] selectedPath) { |
523 |
if (selectedPath != null && selectedPath.length > 1) { |
524 |
if (selectedPath[0] instanceof JPopupMenu) { |
525 |
((JPopupMenu) selectedPath[0]).setVisible(true); |
526 |
MenuSelectionManager.defaultManager().setSelectedPath( |
527 |
selectedPath); |
528 |
} |
529 |
} |
530 |
} |
531 |
|
532 |
/** |
533 |
* Menu item representing a single category. |
534 |
*/ |
535 |
private class CategoryCheckBoxMenuItem extends JCheckBoxMenuItem |
536 |
implements ActionListener { |
537 |
|
538 |
private MenuElement[] selectedPath = null; |
539 |
private Category category; |
540 |
private final Set<Category> evalCats; |
541 |
|
542 |
public CategoryCheckBoxMenuItem(final Category category, |
543 |
final Set<Category> evalCats) { |
544 |
super(category.getDisplayName(), evalCats.contains(category)); |
545 |
this.category = category; |
546 |
this.evalCats = evalCats; |
547 |
setOpaque(false); |
548 |
getModel().addChangeListener(new ChangeListener() { |
549 |
@Override |
550 |
public void stateChanged(ChangeEvent e) { |
551 |
if (isShowing() && model.isArmed()) { |
552 |
selectedPath = MenuSelectionManager.defaultManager() |
553 |
.getSelectedPath(); |
554 |
} |
555 |
} |
556 |
}); |
557 |
addActionListener(this); |
558 |
addMouseListener(new MouseAdapter() { |
559 |
|
560 |
@Override |
561 |
public void mouseClicked(MouseEvent e) { |
562 |
mouseClickedOnItem(e); |
563 |
} |
564 |
}); |
565 |
} |
566 |
|
567 |
@Override |
568 |
public void doClick(int pressTime) { |
569 |
super.doClick(pressTime); |
570 |
showMenuPath(selectedPath); |
571 |
} |
572 |
|
573 |
private void mouseClickedOnItem(MouseEvent e) { |
574 |
if (SwingUtilities.isRightMouseButton(e)) { |
575 |
e.consume(); |
576 |
if (isSelected()) { |
577 |
Iterator<Category> iterator = evalCats.iterator(); |
578 |
while (iterator.hasNext()) { |
579 |
Category c = iterator.next(); |
580 |
if (!CommandEvaluator.RECENT.equals(c.getName())) { |
581 |
iterator.remove(); |
582 |
} |
583 |
} |
584 |
evalCats.add(category); |
585 |
} else { |
586 |
evalCats.addAll( |
587 |
ProviderModel.getInstance().getCategories()); |
588 |
evalCats.remove(category); |
589 |
} |
590 |
updateCheckBoxes(CategoryCheckBoxMenuItem.this, evalCats); |
591 |
updateCats(evalCats); |
592 |
} |
593 |
} |
594 |
|
595 |
@Override |
596 |
public void actionPerformed(ActionEvent e) { |
597 |
if (this.isSelected()) { |
598 |
evalCats.add(category); |
599 |
} else { |
600 |
evalCats.remove(category); |
601 |
} |
602 |
updateCats(evalCats); |
603 |
} |
604 |
} |
605 |
|
606 |
/** |
607 |
* Menu item for enabling or disabling all categories. |
608 |
*/ |
609 |
private class AllMenuItem extends JMenuItem implements ActionListener { |
610 |
|
611 |
private Set<Category> evalCats; |
612 |
private int totalCount; |
613 |
private MenuElement[] selectedPath = null; |
614 |
|
615 |
public AllMenuItem(Set<Category> evalCats) { |
616 |
this.evalCats = evalCats; |
617 |
this.totalCount = ProviderModel.getInstance() |
618 |
.getCategories().size(); |
619 |
getModel().addChangeListener(new ChangeListener() { |
620 |
@Override |
621 |
public void stateChanged(ChangeEvent e) { |
622 |
if (isShowing() && model.isArmed()) { |
623 |
selectedPath = MenuSelectionManager.defaultManager() |
624 |
.getSelectedPath(); |
625 |
} |
626 |
} |
627 |
}); |
628 |
addActionListener(this); |
629 |
} |
630 |
|
631 |
@Override |
632 |
public void actionPerformed(ActionEvent e) { |
633 |
if (evalCats.size() == totalCount) { |
634 |
Iterator<Category> iterator = evalCats.iterator(); |
635 |
while (iterator.hasNext()) { |
636 |
Category c = iterator.next(); |
637 |
if (!CommandEvaluator.RECENT.equals(c.getName())) { |
638 |
iterator.remove(); |
639 |
} |
640 |
} |
641 |
} else { |
642 |
evalCats.addAll(ProviderModel.getInstance().getCategories()); |
643 |
} |
644 |
updateCats(evalCats); |
645 |
updateCheckBoxes(this, evalCats); |
646 |
} |
647 |
|
648 |
@Override |
649 |
public void doClick(int pressTime) { |
650 |
super.doClick(pressTime); |
651 |
showMenuPath(selectedPath); |
652 |
} |
653 |
|
654 |
@Override |
655 |
public String getText() { |
656 |
if (evalCats == null || evalCats.size() != totalCount) { |
657 |
return NbBundle.getMessage(getClass(), |
658 |
"LBL_AllCategories"); //NOI18N |
659 |
} else { |
660 |
return NbBundle.getMessage(getClass(), |
661 |
"LBL_NoCategory"); //NOI18N |
662 |
} |
663 |
} |
664 |
} |
496 |
} |
665 |
} |