Lines 53-58
Link Here
|
53 |
import java.beans.PropertyChangeListener; |
53 |
import java.beans.PropertyChangeListener; |
54 |
import java.lang.ref.Reference; |
54 |
import java.lang.ref.Reference; |
55 |
import java.lang.ref.WeakReference; |
55 |
import java.lang.ref.WeakReference; |
|
|
56 |
import java.lang.reflect.InvocationTargetException; |
57 |
import java.lang.reflect.Method; |
56 |
import java.util.ArrayList; |
58 |
import java.util.ArrayList; |
57 |
import java.util.Collection; |
59 |
import java.util.Collection; |
58 |
import java.util.Collections; |
60 |
import java.util.Collections; |
Lines 65-70
Link Here
|
65 |
import javax.swing.AbstractAction; |
67 |
import javax.swing.AbstractAction; |
66 |
import javax.swing.AbstractButton; |
68 |
import javax.swing.AbstractButton; |
67 |
import javax.swing.Action; |
69 |
import javax.swing.Action; |
|
|
70 |
import javax.swing.ButtonModel; |
68 |
import javax.swing.ImageIcon; |
71 |
import javax.swing.ImageIcon; |
69 |
import javax.swing.InputMap; |
72 |
import javax.swing.InputMap; |
70 |
import javax.swing.JButton; |
73 |
import javax.swing.JButton; |
Lines 74-79
Link Here
|
74 |
import javax.swing.JToolBar; |
77 |
import javax.swing.JToolBar; |
75 |
import javax.swing.KeyStroke; |
78 |
import javax.swing.KeyStroke; |
76 |
import javax.swing.UIManager; |
79 |
import javax.swing.UIManager; |
|
|
80 |
import javax.swing.event.ChangeEvent; |
81 |
import javax.swing.event.ChangeListener; |
77 |
import javax.swing.plaf.TextUI; |
82 |
import javax.swing.plaf.TextUI; |
78 |
import javax.swing.plaf.ToolBarUI; |
83 |
import javax.swing.plaf.ToolBarUI; |
79 |
import javax.swing.text.DefaultEditorKit; |
84 |
import javax.swing.text.DefaultEditorKit; |
Lines 99-104
Link Here
|
99 |
import org.openide.loaders.DataObject; |
104 |
import org.openide.loaders.DataObject; |
100 |
import org.openide.nodes.Node; |
105 |
import org.openide.nodes.Node; |
101 |
import org.openide.util.ContextAwareAction; |
106 |
import org.openide.util.ContextAwareAction; |
|
|
107 |
import org.openide.util.Exceptions; |
102 |
import org.openide.util.ImageUtilities; |
108 |
import org.openide.util.ImageUtilities; |
103 |
import org.openide.util.Lookup; |
109 |
import org.openide.util.Lookup; |
104 |
import org.openide.util.LookupEvent; |
110 |
import org.openide.util.LookupEvent; |
Lines 523-539
Link Here
|
523 |
if (item instanceof Component) { |
529 |
if (item instanceof Component) { |
524 |
add((Component)item); |
530 |
add((Component)item); |
525 |
} else if (item instanceof Action) { |
531 |
} else if (item instanceof Action) { |
526 |
// Wrap action to execute on the proper text component |
532 |
Action a = (Action) item; |
527 |
// because the default fallback in TextAction.getTextComponent() |
533 |
ButtonModel model = (ButtonModel) a.getValue("buttonModel"); // NOI18N |
528 |
// might not work properly if the focus was switched |
534 |
if (model!=null) { // NOI18N |
529 |
// to e.g. a JTextField and then toolbar was clicked. |
535 |
JToggleButton button = new GenericButton(a); |
530 |
Action a = new WrapperAction(componentRef, (Action) item); |
536 |
button.setModel(model); |
531 |
|
537 |
updateIcon(button, a); |
532 |
// Try to find an icon if not present |
538 |
String textComponentMethodName = (String) a.getValue("textComponentMethod"); //NOI18N |
533 |
updateIcon(a); |
539 |
updateTextComponent(c, model, textComponentMethodName); |
|
|
540 |
item = button; |
541 |
add(button); |
542 |
} else { |
543 |
// Wrap action to execute on the proper text component |
544 |
// because the default fallback in TextAction.getTextComponent() |
545 |
// might not work properly if the focus was switched |
546 |
// to e.g. a JTextField and then toolbar was clicked. |
547 |
a = new WrapperAction(componentRef, a); |
534 |
|
548 |
|
535 |
// Add the action and let the JToolbar to creat a presenter for it |
549 |
// Try to find an icon if not present |
536 |
item = add(a); |
550 |
updateIcon(a); |
|
|
551 |
|
552 |
// Add the action and let the JToolbar to create a presenter for it |
553 |
item = add(a); |
554 |
} |
537 |
} else { |
555 |
} else { |
538 |
// Some sort of crappy item -> ignore |
556 |
// Some sort of crappy item -> ignore |
539 |
continue; |
557 |
continue; |
Lines 569-574
Link Here
|
569 |
} |
587 |
} |
570 |
} |
588 |
} |
571 |
|
589 |
|
|
|
590 |
private static void updateIcon(JToggleButton button, Action a) { |
591 |
String iconImageName = (String)a.getValue("iconBase"); // NOI18N |
592 |
if (iconImageName!=null) { |
593 |
Image img = ImageUtilities.loadImage(iconImageName); |
594 |
if (img!=null) { |
595 |
button.setIcon(new ImageIcon(img)); |
596 |
} |
597 |
} |
598 |
} |
599 |
|
600 |
private static void updateTextComponent(JTextComponent c, ButtonModel model, String textComponentMethodName) { |
601 |
if (textComponentMethodName!=null && textComponentMethodName.length()>0) { |
602 |
try { |
603 |
Method componentSettingMethod = model.getClass().getMethod(textComponentMethodName, JTextComponent.class); |
604 |
componentSettingMethod.invoke(model, c); |
605 |
} catch (IllegalAccessException ex) { |
606 |
Exceptions.printStackTrace(ex); |
607 |
} catch (IllegalArgumentException ex) { |
608 |
Exceptions.printStackTrace(ex); |
609 |
} catch (InvocationTargetException ex) { |
610 |
Exceptions.printStackTrace(ex); |
611 |
} catch (NoSuchMethodException ex) { |
612 |
Exceptions.printStackTrace(ex); |
613 |
} catch (SecurityException ex) { |
614 |
Exceptions.printStackTrace(ex); |
615 |
} |
616 |
} |
617 |
} |
618 |
|
572 |
private static void updateTooltip(AbstractButton b, List<? extends MultiKeyBinding> keybindings) { |
619 |
private static void updateTooltip(AbstractButton b, List<? extends MultiKeyBinding> keybindings) { |
573 |
Action a = b.getAction(); |
620 |
Action a = b.getAction(); |
574 |
String actionName = a == null ? null : (String) a.getValue(Action.NAME); |
621 |
String actionName = a == null ? null : (String) a.getValue(Action.NAME); |
Lines 701-706
Link Here
|
701 |
} |
748 |
} |
702 |
|
749 |
|
703 |
public Object getValue(String key) { |
750 |
public Object getValue(String key) { |
|
|
751 |
if (key==null||delegate==null) return null; |
704 |
return delegate.getValue(key); |
752 |
return delegate.getValue(key); |
705 |
} |
753 |
} |
706 |
|
754 |
|
Lines 732-735
Link Here
|
732 |
delegate.actionPerformed(e); |
780 |
delegate.actionPerformed(e); |
733 |
} |
781 |
} |
734 |
} // End of WrapperAction class |
782 |
} // End of WrapperAction class |
|
|
783 |
|
784 |
private static final class GenericButton extends JToggleButton implements ChangeListener { |
785 |
|
786 |
GenericButton(Action a) { |
787 |
setAction(a); |
788 |
setHideActionText(true); |
789 |
} |
790 |
|
791 |
@Override |
792 |
public void setModel(ButtonModel model) { |
793 |
ButtonModel oldModel = getModel(); |
794 |
if (oldModel != null) { |
795 |
oldModel.removeChangeListener(this); |
796 |
} |
797 |
|
798 |
super.setModel(model); |
799 |
|
800 |
ButtonModel newModel = getModel(); |
801 |
if (newModel != null) { |
802 |
newModel.addChangeListener(this); |
803 |
} |
804 |
|
805 |
stateChanged(null); |
806 |
} |
807 |
|
808 |
@Override |
809 |
public void stateChanged(ChangeEvent evt) { |
810 |
boolean selected = isSelected(); |
811 |
super.setContentAreaFilled(selected); |
812 |
super.setBorderPainted(selected); |
813 |
} |
814 |
|
815 |
@Override |
816 |
public void setBorderPainted(boolean paint) { |
817 |
if (!isSelected()) { |
818 |
super.setBorderPainted(paint); |
819 |
} |
820 |
} |
821 |
|
822 |
@Override |
823 |
public void setContentAreaFilled(boolean fill) { |
824 |
if (!isSelected()) { |
825 |
super.setContentAreaFilled(fill); |
826 |
} |
827 |
} |
828 |
} |
735 |
} |
829 |
} |