Dialogs API Anton Epple Eppleton IT Consulting Anton Epple - - PowerPoint PPT Presentation

dialogs api
SMART_READER_LITE
LIVE PREVIEW

Dialogs API Anton Epple Eppleton IT Consulting Anton Epple - - PowerPoint PPT Presentation

NetBeans Rich Client Platform Nodes & Explorer API Dialogs API Anton Epple Eppleton IT Consulting Anton Epple http://www.eppleton.de NetBeans Rich Client Platform Introduction The Dialogs API helps in creating dialogs & wizards Is


slide-1
SLIDE 1

NetBeans Rich Client Platform

Nodes & Explorer API

Anton Epple http://www.eppleton.de

Dialogs API

Anton Epple

Eppleton IT Consulting

slide-2
SLIDE 2

2

NetBeans Rich Client Platform

Introduction The Dialogs API helps in creating dialogs & wizards Is based on java.awt.Dialog Helps creating standard dialogs as well as custom

  • nes

Resembles JOptionPane in many aspects Integrated with NetBeans Window System & Help System Simplifies maintaining a standardized Look & Feel

slide-3
SLIDE 3

3

NetBeans Rich Client Platform

Agenda 1.Notifications 2.Standard Dialogs 3.Custom Dialogs 4.Wizards

slide-4
SLIDE 4

4

NetBeans Rich Client Platform

Agenda 1.Notifications 2.Standard Dialogs 3.Custom Dialogs 4.Wizards

slide-5
SLIDE 5

5

NetBeans Rich Client Platform

Notifications

Notifications: NotifyDescriptor for setting the Properties of a Dialog Message as a String, Icon or Component Arrays for more than one message Specify Type of Message (set's the Icon) DialogDisplayer to show the message

slide-6
SLIDE 6

6

NetBeans Rich Client Platform

Notifications

Types of Messages: Defined as constants in NotifyDescriptor

Constant Message type / Symbol PLAIN_MESSAGE Neutral message without symbol INFORMATION_MESSAGE Standard info symbol QUESTION_MESSAGE Questionmark WARNING_MESSAGE Warning sign ERROR_MESSAGE Error sign

slide-7
SLIDE 7

7

NetBeans Rich Client Platform

Notifications

Buttons: Defined as constants in NotifyDescriptor

Constant Controls DEFAULT_OPTIONS Default controls for the chosen dialog type: e.g. OK Button for PLAIN_MESSAGE OK_OPTION OK Button OK_CANCEL_OPTION OK- and Cancel- Button YES_NO_OPTION Yes- and No- Button YES_NO_CANCEL_OPTION Yes-, No- and Cancel-Button

slide-8
SLIDE 8

8

NetBeans Rich Client Platform

Notifications

Example:

  • 1. Create new Action “ShowDialog” in Menu “Window”:
  • 2. Add to actionPerformed:

public void actionPerformed(ActionEvent e) {

NotifyDescriptor d = new NotifyDescriptor( "Text", // Message or Component to show "Title", // Dialog title NotifyDescriptor.OK_CANCEL_OPTION, // Controls NotifyDescriptor.INFORMATION_MESSAGE,// Symbol null, // Custom Controls (Object []) null // initial value ); Object response = DialogDisplayer.getDefault().notify(d); }

slide-9
SLIDE 9

9

NetBeans Rich Client Platform

Notifications

Example:

  • 3. Run and invoke:
slide-10
SLIDE 10

10

NetBeans Rich Client Platform

Notifications

Possible return values:

Constant Action OK_OPTION OK-button was pressed YES_OPTION Yes-button was pressed NO_OPTION No-button was pressed CANCEL_OPTION Cancel-button was pressed CLOSED_OPTION Dialog closed without pressing button

slide-11
SLIDE 11

11

NetBeans Rich Client Platform

Agenda 1.Notifications 2.Standard Dialogs 3.Custom Dialogs 4.Wizards 5.Recap

slide-12
SLIDE 12

12

NetBeans Rich Client Platform

Standard Dialogs

Standard Dialogs For the most common dialog types NotifyDescriptor has four subclasses: NotifyDescriptor.Message NotifyDescriptor.Confirmation NotifyDescriptor.InputLine NotifyDescriptor.Exception

slide-13
SLIDE 13

13

NetBeans Rich Client Platform

Standard Dialogs

NotifyDescriptor.Message Change the NotifyDescriptor in actionPerformed to: NotifyDescriptor d = new NotifyDescriptor.Message(“Place any String here”);

slide-14
SLIDE 14

14

NetBeans Rich Client Platform

Standard Dialogs

NotifyDescriptor.Confirmation Change the NotifyDescriptor in actionPerformed to: NotifyDescriptor d = new NotifyDescriptor.Confirmation("Do you really want to format the harddrive?", "This is a question");

slide-15
SLIDE 15

15

NetBeans Rich Client Platform

Standard Dialogs

NotifyDescriptor.InputLine Change the NotifyDescriptor in actionPerformed to: NotifyDescriptor.InputLine d = new NotifyDescriptor.InputLine("Name:","Please enter your name"); Object response = DialogDisplayer.getDefault().notify(d); System.out.println("name "+ d.getInputText());

slide-16
SLIDE 16

16

NetBeans Rich Client Platform

Standard Dialogs

NotifyDescriptor.Exception Change the NotifyDesccriptor in actionPerformed to: NotifyDescriptor d = new NotifyDescriptor.Exception(new Exception(“Something went wrong!”));

slide-17
SLIDE 17

17

NetBeans Rich Client Platform

Agenda 1.Notifications 2.Standard Dialogs 3.Custom Dialogs 4.Wizards

slide-18
SLIDE 18

18

NetBeans Rich Client Platform

Custom Dialogs

DialogDescriptor For more customized dialogs you can use the DialogDescriptor class DialogDescriptor is an Extension of NotifyDescriptor It accepts a Component to display, an ActionListener to react

  • n controls.

When supplying a HelpCtx it will automatically display a Help button

slide-19
SLIDE 19

19

NetBeans Rich Client Platform

Custom Dialogs

DialogDescriptor Example: Asynchronous Login Dialog As an example we'll create a Login Dialog to display on startup, so we'll create a Module Installer to display it We'll use the Form Builder to design the JPanel To display it asynchronously ( so it doesn't halt the startup process) we'll give it an ActionListener to react on input If the login is incorrect we'll use the LifecycleManager to shutdown the application

slide-20
SLIDE 20

20

NetBeans Rich Client Platform

Custom Dialogs

DialogDescriptor Example: Login Panel

  • 1. Create a new JPanel “LoginPanel” with the Form Builder:
slide-21
SLIDE 21

21

NetBeans Rich Client Platform

Custom Dialogs

  • 2. Add methods to get the UserName and Password, and set a

failure message: public String getUserName() { return jTextField1.getText(); } public char[] getPassword(){ return jPasswordField1.getPassword(); } public void setInfo(String info){ jLabel3.setText(info); }

slide-22
SLIDE 22

22

NetBeans Rich Client Platform

Custom Dialogs

  • 3. Create a new Module Installer (new File → Module

Development → Module Installer) and override restored: private LoginPanel loginPanel = new LoginPanel(); private DialogDescriptor d = null; @Override public void restored() { d = new DialogDescriptor(loginPanel, "Login", true, this); d.setClosingOptions(new Object[]{}); // not closeable DialogDisplayer.getDefault().notifyLater(d); }

slide-23
SLIDE 23

23

NetBeans Rich Client Platform

Custom Dialogs

  • 4. Dialogdescriptor expects an ActionListener as the constructor's

fourth parameter, we fix this by implementing ActionListener:

public class Installer extends ModuleInstall implements ActionListener { … public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == DialogDescriptor.CANCEL_OPTION){ LifecycleManager.getDefault().exit(); } else if (!loginPanel.getUserName().equals("Toni")){// do real check here loginPanel.setInfo("Wrong Username or Password"); } else { d.setClosingOptions(null);// can close } }

slide-24
SLIDE 24

24

NetBeans Rich Client Platform

Custom Dialogs

  • 5. Run the application:
slide-25
SLIDE 25

25

NetBeans Rich Client Platform

Custom Dialogs

  • 6. Finally fix the bug in restored() method :-) :

... // if someone simply closes the dialog

d.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName().equals(DialogDescriptor.PROP_VALUE) && e.getNewValue()== DialogDescriptor.CLOSED_OPTION){ LifecycleManager.getDefault().exit(); } } }); DialogDisplayer.getDefault().notifyLater(d);

slide-26
SLIDE 26

26

NetBeans Rich Client Platform

Custom Dialogs

Creating Dialogs without the help of Dialogs API: File → New File → Java GUI Forms → JDialog Getting the main Frame as parent:

slide-27
SLIDE 27

27

NetBeans Rich Client Platform

Custom Dialogs

Creating Dialogs without the help of Dialogs API: File → New File → Java GUI Forms → JDialog Getting the main Frame as parent: Frame f = WindowManager.getDefault().getMainWindow();

slide-28
SLIDE 28

28

NetBeans Rich Client Platform

Agenda 1.Notifications 2.Standard Dialogs 3.Custom Dialogs 4.Wizards

slide-29
SLIDE 29

29

NetBeans Rich Client Platform

Wizards

NetBeans Wizard Architecture WizardDescriptor: Controller for wizard, manages the Panels Subclass of DialogDescriptor Is typically used as DataModel to store the data collected between individual Wizard steps as Properties Each wizard step typically consists of two classes A Visual Panel, normally a JPanel without dependencies on Wizard specific classes A WizardDescriptor.Panel <Data> acting as a Controller

slide-30
SLIDE 30

30

NetBeans Rich Client Platform

Wizards

NetBeans Wizard Architecture

MyWizardAction MyWizardDescriptor

creates

ChangeListener <<interface>> WizardDescriptor MyWizardPanel1 ChangeListener <<interface>>

creates (1..*)

MyVisualPanel1 EventListener <<interface>>

creates (1)

WizardDescriptor.Panel <<interface>> JPanel

slide-31
SLIDE 31

31

NetBeans Rich Client Platform

Wizards

The Wizard Wizard creates a skeleton implementation

  • 1. New File → Module Development → Wizard
slide-32
SLIDE 32

32

NetBeans Rich Client Platform

Wizards

2.

slide-33
SLIDE 33

33

NetBeans Rich Client Platform

Wizards

  • 3. The Wizard generates 5 Files:

The generated WizardAction uses the standard WizardDescriptor to invoke the Wizard

slide-34
SLIDE 34

34

NetBeans Rich Client Platform

Wizards

Generated code DemoWizardAction:

WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels()); // {0} will be replaced by WizardDesriptor.Panel.getComponent().getName() wizardDescriptor.setTitleFormat(new MessageFormat("{0}")); wizardDescriptor.setTitle("Your wizard dialog title here"); Dialog dialog = DialogDisplayer.getDefault().createDialog(wizardDescriptor); dialog.setVisible(true); dialog.toFront(); boolean cancelled = wizardDescriptor.getValue() != WizardDescriptor.FINISH_OPTION; if (!cancelled) { // Use WizardDescriptor.getProperty to read the values entered by User }

slide-35
SLIDE 35

35

NetBeans Rich Client Platform

Wizards

Generated code DemoWizardAction:

private WizardDescriptor.Panel[] getPanels() { if (panels == null) { // creates the Panels panels = new WizardDescriptor.Panel[]{ new DemoWizardPanel1(), new DemoWizardPanel2() }; String[] steps = new String[panels.length]; for (int i = 0; i < panels.length; i++) { … // sets Properties like display name, etc.

slide-36
SLIDE 36

36

NetBeans Rich Client Platform

Wizards

Client Properties for WizardDescriptor.Panel:

Property (WizardPanel_...) Type Meaning ...autoWizardStyle Boolean Turn on subtitle creation ...contentDisplayed Boolean Show step overview Panel ...helpDisplayed Boolean Show help in extra tab ...contentNumbered Boolean Turn on numbering of steps ...contentSelectedIndex Integer Index number of step (starts with 0) ...contentData String [] Names of steps ...image Image Background for overview Panel ...errorMessage String Shown when invalid state ...helpURL URL URL for this Panel's help

slide-37
SLIDE 37

37

NetBeans Rich Client Platform

Wizards

Generated code DemoWizardPanel1:

public Component getComponent() { // returns the Visual Panel return component; } public boolean isValid() { // can Proceed? return true; } public final void addChangeListener(ChangeListener l) { // the WizardDescriptor registers here } public final void removeChangeListener(ChangeListener l) { }

slide-38
SLIDE 38

38

NetBeans Rich Client Platform

Wizards

Generated code DemoWizardPanel1:

// You can use a settings object to keep track of state. Normally the // settings object will be the WizardDescriptor, so you can use // WizardDescriptor.getProperty & putProperty to store information entered // by the user. public void readSettings(Object settings) { this.model = (WizardDescriptor) settings; } public void storeSettings(Object settings) { model.putProperty(MyVisualPanel1.PROPERTY_SAMPLE, getComponent().getSample() ); }

slide-39
SLIDE 39

39

NetBeans Rich Client Platform

Agenda 1.Notifications 2.Standard Dialogs 3.Custom Dialogs 4.Wizards

slide-40
SLIDE 40

NetBeans Rich Client Platform

Q&A