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