Java Reference
In-Depth Information
ASKING THE USER
You'll implement the ask-the-user condition by splitting it into two parts:
An AskTheUser class that presents the user with a Swing dialog box asking to
authorize permission requests
AskTheUserCondition , which is a postponed condition that uses the Ask-
TheUser class to evaluate its condition
This is a good example for postponed conditions, because you don't want to bother
the user with questions if the check fails for other reasons and user interaction is slow.
The following listing shows the AskTheUser implementation.
Listing 14.5 AskTheUser dialog box implementation
public class AskTheUser implements Runnable {
private final String m_question;
private volatile boolean m_result;
B
Question
to ask
public AskTheUser(String question) {
m_question = question;
}
public void run() {
m_result = (JOptionPane.YES_OPTION ==
JOptionPane.showConfirmDialog(null, m_question, "Security",
JOptionPane.YES_NO_OPTION));
}
public boolean ask() throws Exception {
SwingUtilities.invokeAndWait(this);
return m_result;
}
}
The constructor accepts the question to ask B , which you display in the ask()
method C . In the ask() method, you use a JOptionPane confirmation dialog box to
query the user. You return true or false depending on whether the user confirms or
rejects the request, respectively. The next listing shows how you implement the condi-
tion itself.
C
Asks
question
Listing 14.6 AskTheUserCondition implementation
public class AskUserCondition implements Condition {
private final Bundle m_bundle;
private final String m_question;
private final boolean m_not;
private boolean m_result = false;
private boolean m_alreadyAsked = false;
private AskUserCondition(Bundle bundle, ConditionInfo info){
m_bundle = bundle;
m_question = info.getArgs()[0].replace(
"$symbolic-name", bundle.getSymbolicName());
m_not = (info.getArgs().length == 2 && "!".equals(info.getArgs()[1]));
}
 
Search WWH ::




Custom Search