How to be notified that a composite's child received/lost focus? - java

I have an SWT Composite that I need to pass to some other code which will add children to it at will. Is there any way to be notified that a child of the composite received and lost focus?
Just to make sure it's clear, I cannot add listeners to each child, because I'm not in charge of creating those controls. A child could be added at any time.

As noted by Favonius, you can hook layout events like SWT.Resize to determine when you're being painted and recompute your child hierarchy, adding listeners as appropriate. Another option is simply to listen for all focus events and only pay attention to those that are for controls that you're interested in.
Displays have filters which, like listeners, are notified of events, however filters differ in that they are run before listeners, they have the opportunity to cancel events, and they are notified for all of a type of event on the entire Display.
You could thus use a Filter to examine all focus events and determine if it's one that you're interested in. For example:
public class MyControl extends Composite
{
private final Listener focusListener;
public MyControl(final Composite parent, final int style)
{
/* initialize the control... */
focusListener = new Listener()
{
public void handleEvent(Event event)
{
if (!(event.widget instanceof Control))
{
return;
}
boolean isOurChild = false;
for (Control c = (Control) event.widget; c != null; c = c.getParent())
{
if (c == container)
{
isOurChild = true;
break;
}
}
if (isOurChild)
{
System.out.println("Our child is " + (event.type == SWT.FocusIn ? "focused" : "unfocused"));
}
}
};
getDisplay().addFilter(SWT.FocusIn, focusListener);
getDisplay().addFilter(SWT.FocusOut, focusListener);
addDisposeListener(new DisposeListener()
{
public void widgetDisposed(DisposeEvent e)
{
getDisplay().removeFilter(SWT.FocusIn, focusListener);
getDisplay().removeFilter(SWT.FocusOut, focusListener);
}
});
}
}
Do note the javadoc for Display's warnings about using filters:
They should generally be avoided for performance, debugging and code maintenance reasons.
Obviously you're looking at performance trade-offs in either solution - depending on what type of application you're delivering and your users' workflow, it may make more sense to add focus listeners when you resize, or it may make more sense to simply listen to all focus events and ignore the ones you're not interested in.

Have you checked this link: SWT: notify a composite that it has a new child
As per the proposed solution in the above link the only possible solution is to use the resize event. Based on that see the following code, which adds a focus listener on all the immediate child nodes. Though the solution itself is not very elegant.
Test Code
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTApplication
{
public static void main(String[] args) {
new SWTApplication().initSystem("Children Notification");
}
private Display display;
private Shell shell;
public void initSystem(String windowName)
{
display = new Display();
shell = new Shell(display);
shell.setText(windowName);
shell.setLayout(new GridLayout(6, true));
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final TestFocusListener listener = new TestFocusListener();
shell.addControlListener(new ControlListener() {
public void controlResized(ControlEvent e) {
if(e.getSource() instanceof Shell)
{
Shell s = (Shell)e.getSource();
Control[] children = s.getChildren();
for (int i = 0; i < children.length; i++)
{
Control c = children[i];
c.removeFocusListener(listener);
c.addFocusListener(listener);
}
}
}
public void controlMoved(ControlEvent e) {
}
});
createControls();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private void createControls()
{
String[] name = {"a", "b", "c", "d", "e", "f"};
for(int i=0; i<6; i++)
{
Button button = new Button(shell, SWT.PUSH);
button.setText(name[i] + " button");
button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
}
class TestFocusListener implements FocusListener
{
public void focusGained(FocusEvent e) {
Object src = e.getSource();
if(src instanceof Button)
{
System.out.println("Focus gained: " + ((Button)src).getText());
}
}
public void focusLost(FocusEvent e) {
Object src = e.getSource();
if(src instanceof Button)
{
System.out.println("Focus lost: " + ((Button)src).getText());
}
}
}
}

Related

Font not taking effect during initial construction of new Composite

I wanted to have a Text widget that could display a message in it when the user has not entered a value into the field yet. I extended composite and essentially wrapped a text field in it. Added a focus listener to remove the message on focus, and to replace the message when the focus is lost if the field is empty. That all works as expected.
The issue I am having is I wanted the prompt to be styled differently when it is placed in the text field. The font does not seem to be being used initially. Once the field has had focus and loses focus it looks correct.
For example this is how it looks when it initially loads:
And this is how it should look on initial load and how it looks after having and lost focus:
It gets a little stranger, as when I run this inside a simple shell, it works how it should. When I run it as an Eclipse application is when it does not get styled correctly.
Here is the code for my composite:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
/**
* The <code>PromptingTextInput</code> component is a small enhancement to
* standard <code>Text</code>. It adds the ability to specify a prompt value
* that displays when the text is empty and the field does not have focus.
*/
public class PromptingTextInput extends Composite {
private String prompt;
private Text input;
private boolean textEmpty;
Font emptyFont;
Font inputFont;
public PromptingTextInput(String prompt, Composite parent, int style, boolean passwordField) {
super(parent, style);
this.prompt = prompt;
setLayout(new FillLayout());
this.textEmpty = true;
this.input = new Text(this, (passwordField ? SWT.PASSWORD : SWT.NONE));
setEmptyInputStyle();
this.input.setText(this.prompt);
this.input.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
PromptingTextInput.this.focusGained();
}
public void focusLost(FocusEvent e) {
PromptingTextInput.this.focusLost();
}
});
addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
disposeFonts();
}
});
}
protected void focusGained() {
if (this.textEmpty) {
this.input.setText("");
setInputStyle();
}
}
protected void focusLost() {
if (input.getText() == null || input.getText().trim().length() == 0) {
this.input.setText(this.prompt);
setEmptyInputStyle();
this.textEmpty = true;
} else {
this.textEmpty = false;
}
}
protected void setInputStyle() {
if (this.inputFont == null){
this.inputFont = new Font(Display.getCurrent(), "Verdana", 8, SWT.DEFAULT);
}
this.input.setFont(this.inputFont);
this.input.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
}
protected void setEmptyInputStyle() {
if (this.emptyFont == null){
this.emptyFont = new Font(Display.getCurrent(), "Verdana", 6, SWT.ITALIC);
}
this.input.setFont(this.emptyFont);
this.input.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
}
public String getPrompt() {
return prompt;
}
public void setPrompt(String prompt) {
this.prompt = prompt;
if(!this.input.isFocusControl()){
this.input.setText(this.prompt);
setEmptyInputStyle();
}
}
public Text getInput() {
return input;
}
public boolean isTextEmpty() {
return textEmpty;
}
public String getText() {
return this.input.getText();
}
public void addModifyListener (ModifyListener listener) {
this.input.addModifyListener(listener);
}
public void disposeFonts(){
if (this.inputFont != null){
this.inputFont.dispose();
}
if (this.emptyFont != null){
this.emptyFont.dispose();
}
}
}
UPDATE: As Baz has shown this is not an issue in Indigo and only seems to be an issue with an E4 app in Juno.
Building upon sambi's answer, I got the following working. Maybe this works in Juno:
private static Font italic;
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1,false));
italic = new Font(Display.getCurrent(), "Verdana", 6, SWT.ITALIC);
final Text text = new Text(shell, SWT.BORDER);
text.addListener(SWT.Paint, new Listener() {
#Override
public void handleEvent(Event event) {
if(text.getText().length() < 1 && !text.isFocusControl())
{
GC gc = event.gc;
gc.setFont(italic);
gc.setForeground(display.getSystemColor(SWT.COLOR_GRAY));
Point size = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
/* Strangely the y positioning doesn't work correctly */
//gc.drawText("Please enter text", 1, (size.y / 2) - (italic.getFontData()[0].getHeight() / 2));
gc.drawText("Please enter text", 1, 4);
}
}
});
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
Button button = new Button(shell, SWT.PUSH);
button.setText("Dummy");
button.forceFocus();
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
italic.dispose();
display.dispose();
}
Without focus and empty text:
With focus or text:
You could use Text.setMessage (String message). The problem with this is you will not be able to customize much with font size and fore ground...etc.
To customize it, you can actually draw message with in the bounds of Text.

SWT remove all listener from StyledText

How do I remove all listener from a SWT StyledText if I'm missing the instances? I tried to use the StyledText.getListeners(int) method to get all the instances, being able to remove them afterwards. But it's exhausting to find all the required int values. Is this the most straightforward way? Thank you!
Here is my temporary solution:
public void removeAllListener(StyledText st) {
int[] eventTypes = { 3007, 3011, SWT.Resize, SWT.Move, SWT.Dispose,
SWT.DragDetect, 3000, SWT.FocusIn, SWT.FocusOut, SWT.Gesture,
SWT.Help, SWT.KeyUp, SWT.KeyDown, 3001, 3002, SWT.MenuDetect,
SWT.Modify, SWT.MouseDown, SWT.MouseUp, SWT.MouseDoubleClick,
SWT.MouseMove, SWT.MouseEnter, SWT.MouseExit, SWT.MouseHover,
SWT.MouseWheel, SWT.Paint, 3008, SWT.Selection, SWT.Touch,
SWT.Traverse, 3005, SWT.Verify, 3009, 3010 };
for (int eventType : eventTypes) {
Listener[] listeners = st.getListeners(eventType);
for (Listener listener : listeners) {
st.removeListener(eventType, listener);
}
}
}
I had to copy some of the values since they are part of StyledText class declared with the default modifier. So I cannot access them.
I hope, I didn't miss any int values ;)
In general, there is no mechanism to do this. However, I managed to get it working by subclassing StyledText. The new class is called StyledTextWithListeners. You can just rename it if you want ;) . However, you will have to use
styledText.addListenerByUser(int eventType, Listener listener);
to add you Listeners rather than
styledText.addListener(int eventTyle, Listener listener);
This is necessary to discriminate between Listeners added by you and those added by SWT upon creation.
To remove all listeners added by the user (you) just call
styledText.removeAllListeners();
Here is the code:
import java.util.HashMap;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class MiniExample {
public static void main(String[] args) {
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
StyledTextWithListeners text = new StyledTextWithListeners(shell, SWT.BORDER);
text.setText("TEXT");
text.addListenerByUser(SWT.Verify, new Listener() {
#Override
public void handleEvent(Event arg0) {
}
});
text.removeAllListeners();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public static class StyledTextWithListeners extends StyledText
{
HashMap<Integer, Listener> listeners;
public StyledTextWithListeners(Composite parent, int style) {
super(parent, style);
}
public void addListenerByUser(int eventType, Listener listener)
{
addListener(eventType, listener);
System.out.println("Adding: " + listener.getClass().toString());
if(listeners == null)
listeners = new HashMap<Integer, Listener>();
listeners.put(eventType, listener);
}
public void removeAllListeners()
{
for(Integer type : listeners.keySet())
{
System.out.println("Removing: " + listeners.get(type).getClass().toString());
removeListener(type.intValue(), listeners.get(type));
}
listeners = new HashMap<Integer, Listener>();
}
}
}
It basically saves all the Listeners you add in a HashMap and removes them when you call removeAllListeners.
Keep in mind that I didn't take care of the case where you add a second Listener with the same eventType to the StyledText as done before. In this case, the old Listener will be replaced in the HashMap, but not removed from the StyledText. If this case can occur in your scenario, just add some code yourself.

Communication between JOptionPane buttons and a custom panel

I have made a multiple input dialog by building a JPanel with the fields I want and adding it to a JOption pane
JMainPanel mainPanel = new JMainPanel(mensaje, parametros, mgr);
int i = JOptionPane.showOptionDialog(null, mainPanel, "Sirena",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
new String[] {"Aceptar", "Cancelar"}, "Aceptar");
However I'm having trouble with the buttons, because some of the fields are required. How can I make the "Ok" button to be enabled once every required field is up, or making the click on the button to make the validations and do not close the pane until every required field is filled?
From the Java API, I found this:
options - an array of objects indicating the possible choices the user
can make; if the objects are components, they are rendered properly;
non-String objects are rendered using their toString methods; if this
parameter is null, the options are determined by the Look and Feel
So, can't I pass custom buttons as parameter?
Looks like I will have to make my own JDialog? for which case, I don't know how to make it return an int just like JOptionPane does, any recommended tutorial?
In the example options is {"Aceptar", "Cancelar"} which are the displayed buttons,
PS. I have full controll over the fields I added to the JPanel.
This is a screenshot of the JOptionPane:
I don't think that you can de-activate a JOptionPane's selections buttons, but one way to still use the JOptionPane is to simply re-display it if the required fields have not been set. You could display an error message JOptionPane first describing the error, and then display a new JOptionPane that holds the same JPanel as its second parameter -- so that the data already entered has not been lost. Otherwise, you may want to create your own JDialog which by the way isn't that hard to do.
Edit
I'm wrong. You can enable and disable the dialog buttons if you use a little recursion.
For example:
import java.awt.Component;
import java.awt.Container;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Set;
import javax.swing.*;
public class Foo extends JPanel {
private static final String[] DIALOG_BUTTON_TITLES = new String[] { "Aceptar", "Cancelar" };
private JCheckBox checkBox = new JCheckBox("Buttons Enabled", true);
private Set<AbstractButton> exemptButtons = new HashSet<AbstractButton>();
public Foo() {
JButton exemptBtn = new JButton("Exempt Button");
JButton nonExemptBtn = new JButton("Non-Exempt Button");
add(checkBox);
add(exemptBtn);
add(nonExemptBtn);
exemptButtons.add(checkBox);
exemptButtons.add(exemptBtn);
checkBox.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
allBtnsSetEnabled(checkBox.isSelected());
}
});
}
private void allBtnsSetEnabled(boolean enabled) {
JRootPane rootPane = SwingUtilities.getRootPane(checkBox);
if (rootPane != null) {
Container container = rootPane.getContentPane();
recursiveBtnEnable(enabled, container);
}
}
private void recursiveBtnEnable(boolean enabled, Container container) {
Component[] components = container.getComponents();
for (Component component : components) {
if (component instanceof AbstractButton && !exemptButtons.contains(component)) {
((AbstractButton) component).setEnabled(enabled);
} else if (component instanceof Container) {
recursiveBtnEnable(enabled, (Container) component);
}
}
}
public int showDialog() {
return JOptionPane.showOptionDialog(null, this, "Sirena",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
DIALOG_BUTTON_TITLES, "Aceptar");
}
private static void createAndShowGui() {
Foo foo = new Foo();
int result = foo.showDialog();
System.out.println(DIALOG_BUTTON_TITLES[result]);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
This code uses listeners to check the state of a JCheckBox, but you can have listeners (DocumentListeners) listening to text field documents if you desire to know if they have data or not. The code then gets the JRootPane that holds the JCheckBox, then the root pane's contentPane, and all components of the dialog are held by this. It then recurses through all the components held by the dialog. If a component is a Container, it recurses through that container. If the component is an AbstractButton (such any JButton or checkbox), it enables or disables -- except for buttons held in the exempt buttons set.
A better example with document listeners
import java.awt.*;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Set;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Foo2 extends JPanel {
private static final String[] DIALOG_BUTTON_TITLES = new String[] {
"Aceptar", "Cancelar" };
private static final int FIELD_COUNT = 10;
private Set<AbstractButton> exemptButtons = new HashSet<AbstractButton>();
private JTextField[] fields = new JTextField[FIELD_COUNT];
public Foo2() {
setLayout(new GridLayout(0, 5, 5, 5));
DocumentListener myDocListener = new MyDocumentListener();
for (int i = 0; i < fields.length; i++) {
fields[i] = new JTextField(10);
add(fields[i]);
fields[i].getDocument().addDocumentListener(myDocListener);
}
// cheating here
int timerDelay = 200;
Timer timer = new Timer(timerDelay , new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
checkDocsForText();
}
});
timer.setRepeats(false);
timer.setInitialDelay(timerDelay);
timer.start();
}
private void checkDocsForText() {
for (JTextField field : fields) {
if (field.getText().trim().isEmpty()) {
allBtnsSetEnabled(false);
return;
}
}
allBtnsSetEnabled(true);
}
private void allBtnsSetEnabled(boolean enabled) {
JRootPane rootPane = SwingUtilities.getRootPane(this);
if (rootPane != null) {
Container container = rootPane.getContentPane();
recursiveBtnEnable(enabled, container);
}
}
private void recursiveBtnEnable(boolean enabled, Container container) {
Component[] components = container.getComponents();
for (Component component : components) {
if (component instanceof AbstractButton && !exemptButtons.contains(component)) {
((AbstractButton) component).setEnabled(enabled);
} else if (component instanceof Container) {
recursiveBtnEnable(enabled, (Container) component);
}
}
}
public int showDialog() {
return JOptionPane.showOptionDialog(null, this, "Sirena",
JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
DIALOG_BUTTON_TITLES, "Aceptar");
}
private class MyDocumentListener implements DocumentListener {
public void removeUpdate(DocumentEvent arg0) {
checkDocsForText();
}
public void insertUpdate(DocumentEvent arg0) {
checkDocsForText();
}
public void changedUpdate(DocumentEvent arg0) {
checkDocsForText();
}
}
private static void createAndShowGui() {
Foo2 foo = new Foo2();
int result = foo.showDialog();
if (result >= 0) {
System.out.println(DIALOG_BUTTON_TITLES[result]);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
I suggest you to define some properties into your JPanel extended class, and use PropertyChangeListener to listen the occured changes and enable/disable relative buttons.
Here's an article.
Another issue maybe finding the ok/cancel buttons in the hierarchy of components, since the JDialog is created through JOptionPane and you haven't a reference to the buttons. Here's a useful thread .
You can add a property to a JComponent using putClientProperty method.
When changes occurs to a given property a PropertyChanged event is raised.
So in your example you can define a boolean property indicating that required that are inserted into the JDialog. Then add a PropertyChangeListener that when is notified enable/disable the ok button.

JInternalFrame selection

I have a JDesktopPane containing some JInternalFrames. I want some menus on the menubar to be activated only when one of the JInternalFrames is selected. I've tried using VetoableChangeListener, with the following code in it:
JInternalFrame selectedFrame = desk.getSelectedFrame();
if ((selectedFrame != null)) {
imageMenu.setEnabled(Boolean.TRUE);
} else {
imageMenu.setEnabled(Boolean.FALSE);
}
But the results are not what I expected - for example, the menu is enabled only the second time I add a frame. when I close all frames, it remains enabled.
How can I make this work?
you have to read basic tutorial about JInternalFrames with link to the InternalFrameListener,
but another and look like as better way is programatically to know those event in all cases and evety times is by adding PropertyChangeListener as shows examples Getting All Frames in a JDesktopPane Container, by adding PropertyChangeListener you can listeng for these events
Add an InternalFrameListener to each internal frame added to the desktop pane, and each time an event is triggered, execute the code you have shown in your question.
This code could be better written though:
setEnabled takes a primitive boolean as argument, not a java.lang.Boolean. Use true and false rather than Boolean.TRUE and Boolean.FALSE.
The expression (selectedFrame != null) evaluates as a boolean. Just write
imageMenu.setEnabled(selectedFrame != null);
instead of
if ((selectedFrame != null)) {
imageMenu.setEnabled(Boolean.TRUE);
} else {
imageMenu.setEnabled(Boolean.FALSE);
}
I would just create a custom event and fire it when a JInternalFrame gets focus (isActivated).
The menu items would listen for this event, intercept it and set their status enabled or disabled accordingly.
The advantage here is that you don't have to handle what menu items should be available for which types of internal frames, just fire the appropriate event. It'll make your life easier if you add more internal frames in the future.
This answer is based on the answer by #mKorbel. This example shows one of the ways to detect focus between internal frames as is demonstrated here:
package com.apexroot.sandbox;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
/**
* author grants unlimited license to modify, reuse and redistribute. based on
* the suggestion by #mKorbel on stackoverflow at
* http://stackoverflow.com/questions/7219860/jinternalframe-selection
* please keep a URL to the original version in the source code.
* http://javajon.blogspot.com/2015/08/windowfocuslistener-for-jinternalframe.html
*
* #author Apexroot
*/
public class InternalFrameFocusListenerExample {
public static final String INTERNAL_FRAME_FOCUS_EVENT_PROPERTY = "selected";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
final JFrame jFrame = new JFrame();
final JDesktopPane jDesktopPane = new JDesktopPane();
final JInternalFrame[] jInternalFrames = new FocusInternalFrame[3];
for (int i = 0; i < jInternalFrames.length; i++) {
jInternalFrames[i] = new FocusInternalFrame();
}
jFrame.dispose();
jFrame.setContentPane(jDesktopPane);
jDesktopPane.setPreferredSize(new Dimension(400, 200));
jFrame.pack();
jFrame.setVisible(true);
for (int i = 0; i < jInternalFrames.length; i++) {
jDesktopPane.add(jInternalFrames[i]);
jInternalFrames[i].setLocation(10 + 60 * i, 10 + 40 * i);
jInternalFrames[i].setVisible(true);
}
}
});
}
public static class FocusInternalFrame extends JInternalFrame {
public FocusInternalFrame() {
final JLabel jLabel = new JLabel("placeholder for pack();");
setContentPane(jLabel);
pack();
this.addPropertyChangeListener(
INTERNAL_FRAME_FOCUS_EVENT_PROPERTY,
new LabelFocusListener(jLabel));
}
}
private static class LabelFocusListener implements PropertyChangeListener {
private final JLabel jLabel;
public LabelFocusListener(JLabel jLabel) {
this.jLabel = jLabel;
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
// please keep a URL to the original version in the source code.
// http://javajon.blogspot.com/2015/08/windowfocuslistener-for-jinternalframe.html
if (INTERNAL_FRAME_FOCUS_EVENT_PROPERTY.equals(
evt.getPropertyName())) {
final Object oldValue = evt.getOldValue();
final Object newValue = evt.getNewValue();
if (oldValue instanceof Boolean
&& newValue instanceof Boolean) {
boolean wasInFocus = (Boolean) oldValue;
boolean isInFocus = (Boolean) newValue;
if (isInFocus && !wasInFocus) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
// focus gained
jLabel.setText("focus gained");
}
});
} else if (wasInFocus && !isInFocus) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
// focus lost
jLabel.setText("focus lost");
}
});
}
}
}
}
}
}

How to stop expanding an infinite SWT Tree upon pressing "*"

I have an SWT Tree in my application that contains an infinite data structure. Upon expanding an item, I generate its children. On Windows though, users can press "*", triggering an "expand all descendants" action, and my application hangs.
There are two acceptable behaviors for me when the user presses "*":
Expand all children of the selected element, but only to the next level
Do nothing
In either case, I will still need to be able to expand items as deep as required (by clicking on the [+] icon, or by pressing "+"), so limiting the tree depth is not a solution. Is there another way that I can achieve either of the above without modifying SWT classes?
I got this far -- maybe it helps someone:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class SWTInfiniteTree {
private boolean expanding;
public SWTInfiniteTree() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Tree tree = new Tree(shell, SWT.BORDER);
tree.setLayoutData(new GridData(GridData.FILL_BOTH));
createItem(tree, "ITEM1");
createItem(tree, "ITEM2");
createItem(tree, "ITEM3");
tree.addTreeListener(new TreeListener() {
#Override
public void treeExpanded(TreeEvent e) {
TreeItem parent = (TreeItem) e.item;
if (expanding) {
e.doit = false;
} else {
expanding = true;
parent.removeAll();
createItem(parent, ".1");
createItem(parent, ".2");
createItem(parent, ".3");
}
}
#Override
public void treeCollapsed(TreeEvent e) {
}
});
tree.addKeyListener(new KeyListener() {
#Override
public void keyReleased(KeyEvent e) {
expanding = false;
}
#Override
public void keyPressed(KeyEvent e) {
}
});
tree.addMouseListener(new MouseListener() {
#Override
public void mouseUp(MouseEvent e) {
expanding = false;
}
#Override
public void mouseDown(MouseEvent e) {
}
#Override
public void mouseDoubleClick(MouseEvent e) {
expanding = false;
}
});
shell.setSize(300, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private TreeItem createItem(Widget parent, String text) {
TreeItem item;
if (parent instanceof Tree) {
item = new TreeItem((Tree) parent, SWT.NULL);
item.setText(text);
} else {
item = new TreeItem((TreeItem) parent, SWT.NULL);
item.setText(((TreeItem) parent).getText() + text);
}
// So that we have a [+] icon
item.setItemCount(1);
return item;
}
public static void main(String[] args) {
new SWTInfiniteTree();
}
}
What it does is it expands the first element, and then goes into "won't expand more" mode, which is lifted whenever a key or the mouse button is released. However, for some reason it will expand my freshly generated items.
I hope someone has a better solution.

Categories

Resources