Logo Search packages:      
Sourcecode: octave-java version File versions  Download package

int org::octave::JDialogBox::genericdlg ( String  message[],
String  list[],
String  caption,
String  on,
int  flag,
Object[]  RowsCols,
Object[]  defaults,
String  okstring,
String  nostring,
String  cancelstring 
) [inline]

A generic dialog creation and display function.

Parameters:
messageString[]
listString[]
captionString
onString
flagint
RowsColsObject[]
defaultsObject[]
okstringString
nostringString
cancelstringString
Returns:
int

Todo:
CHECK handling of RowsCols for inputdlg

Definition at line 385 of file JDialogBox.java.

References getOctaveIcon(), prepareFrameIcon(), and setSystemLnF().

Referenced by emptydlg(), errordlg(), helpdlg(), inputdlg(), listdlg(), questdlg(), and warndlg().

   {
      TeXtranslator theTranslator = new TeXtranslator ();
      setSystemLnF ( true );

      caption = theTranslator.replace ( caption );

      m_ButtonNO = null;
      m_ButtonOK = null;
      m_ButtonCANCEL = null;

      // create a modal dialog with an empty frame as its parent
      m_ParentFrame = new JFrame ();

      // --- trick to bring dialog to the front
      // In Windows, the dialog is not brought to the foreground, but hidden
      // behind the Octave window as long as the parent frame is not visible.
      // To avoid that the frame is visible, we move it outside of the screen.
      m_ParentFrame.setBounds ( Toolkit.getDefaultToolkit ().getScreenSize ().
                                width + 100,
                                Toolkit.getDefaultToolkit ().getScreenSize ().
                                height + 100, 1, 1 );
      m_ParentFrame.setVisible ( true );
      //-- end of trick

      JDialog dlg;
      dlg = new JDialog ( m_ParentFrame );
      dlg.setTitle ( caption );

      dlg.setModal ( true );
      dlg.setDefaultCloseOperation ( JDialog.DISPOSE_ON_CLOSE );

      DlgListener theListener = new DlgListener ( this );

      Container d = dlg.getContentPane ();
      d.setLayout ( new BorderLayout ( 8, 8 ) );

      // spacer
      d.add ( new JLabel ( " " ), BorderLayout.NORTH );
      d.add ( new JLabel ( "  " ), BorderLayout.EAST );

      JPanel p = new JPanel ();

      if ( FLAG_LABEL == ( FLAG_LABEL & flag ) )
      {
         // a single line label
         JLabel l = new JLabel ( theTranslator.replace ( message[0] ) );
         p.add ( l );
      }
      else if ( FLAG_TEXT == ( FLAG_TEXT & flag ) )
      {
         String msg = theTranslator.replace ( message[0] );
         // a multi-line text display for helpdlg
         StringTokenizer st = new StringTokenizer ( msg, "\n" );

         int nRows = ( null == RowsCols ) ? 1 :
                     Integer.parseInt ( RowsCols[0].toString () );
         nRows = Math.max ( nRows, st.countTokens () );
         int nCols = Math.max ( 1, msg.length () / nRows );

         p.setLayout ( new GridLayout ( message.length, 1 ) );
         JTextArea ta = new JTextArea ( msg, nRows, nCols );
         ta.setEditable ( false );
         ta.setFocusable ( false );
         ta.setOpaque ( false );
         // replace ugly monospaced font
         ta.setFont ( p.getFont () );
         p.add ( ta );
      }
      else if ( FLAG_INPUT == ( FLAG_INPUT & flag ) )
      {
         // a multi label/textfield entry dialog for inputdlg
         GridBagConstraints gbc = new GridBagConstraints ();
         gbc.insets.top = 4;
         gbc.insets.left = 8;
         gbc.gridx = 0;
         gbc.anchor = GridBagConstraints.NORTHWEST;

         p.setLayout ( new GridBagLayout () );
         m_TextField = new JTextArea[message.length];

         // default values
         int nRows = 1;
         int nCols = 10;

         for ( int i = 0; i < message.length; i++ )
         {
            String msg = theTranslator.replace ( message[i] );
            JLabel l = new JLabel ( msg );
            l.setHorizontalAlignment ( Label.LEFT );
            gbc.gridy = 2 * i;
            p.add ( l, gbc );
            /**
             * @todo CHECK handling of RowsCols for inputdlg
             */
            if ( RowsCols != null )
            {
               if ( RowsCols.length == 2 * message.length )
               {
                  nRows = Integer.parseInt ( RowsCols[i].toString () );
                  nCols = Integer.parseInt ( RowsCols[RowsCols.length / 2 +
                                             i].toString () );
               }
            }

            m_TextField[i] = new JTextArea ( "", Math.max ( nRows, 1 ), nCols );
            // avoid resizing
            m_TextField[i].setPreferredSize ( new Dimension ( Math.max ( nRows,
               1 ), nCols ) );
            m_TextField[i].setAutoscrolls ( false );
            m_TextField[i].setFont ( p.getFont () );
            m_TextField[i].setBorder ( new javax.swing.border.EtchedBorder () );
            m_TextField[i].addKeyListener ( theListener );

            gbc.gridy = 2 * i + 1;
            p.add ( m_TextField[i], gbc );
         }

         if ( defaults != null )
         {
            if ( defaults.length == message.length )
            {
               for ( int i = 0; i < message.length; i++ )
               {
                  String def = theTranslator.replace ( defaults[i].toString () );
                  m_TextField[i].setText ( def );
               }
            }
         }
      }
      else if ( DLG_LIST == ( DLG_LIST & flag ) )
      {
         GridBagConstraints gbc = new GridBagConstraints ();
         gbc.insets.top = 4;
         gbc.insets.left = 8;
         gbc.gridx = 0;
         gbc.anchor = GridBagConstraints.NORTHWEST;

         p.setLayout ( new GridBagLayout () );

         for ( int i = 0; i < message.length; i++ )
         {
            // a single line label
            String msg = theTranslator.replace ( message[i] );
            JLabel l = new JLabel ( msg );
            gbc.gridy = i;
            p.add ( l, gbc );
         }

         String lst[] = new String[list.length];

         for ( int i = 0; i < list.length; i++ )
         {
            lst[i] = theTranslator.replace ( list[i] );
         }
         m_List = new JList ( lst );

         // replace ugly monospaced font
         m_List.setFont ( p.getFont () );

         m_List.setMinimumSize ( new Dimension ( Math.max ( 1,
            Integer.parseInt ( RowsCols[0].toString () ) ),
                                                 Math.max ( 1,
            Integer.parseInt ( RowsCols[1].toString () ) ) ) );
         m_List.setPreferredSize ( new Dimension ( Math.max ( 1,
            Integer.parseInt ( RowsCols[1].toString () ) ),
            Math.max ( 1,
                       Integer.parseInt ( RowsCols[0].toString () ) ) ) );
         m_List.setBorder ( new javax.swing.border.EtchedBorder () );

         gbc.gridy = message.length;
         gbc.fill = GridBagConstraints.HORIZONTAL;
         p.add ( m_List, gbc );

         if ( on.toLowerCase ().equals ( "single" ) )
         {
            // single selection list
            m_List.setSelectionMode ( ListSelectionModel.SINGLE_SELECTION );

            m_List.setSelectedIndex ( Integer.parseInt (
               defaults[0].toString () ) - 1 );
         }
         else
         {
            // multiple selection possible
            m_List.setSelectionMode ( ListSelectionModel.
                                      MULTIPLE_INTERVAL_SELECTION );

            int selection[] = new int[defaults.length];
            for ( int i = 0; i < defaults.length; i++ )
            {
               selection[i] = Integer.parseInt ( defaults[i].toString () ) - 1;
            }
            m_List.setSelectedIndices ( selection );

            JButton b = new JButton ( "Select All" );
            b.setActionCommand ( "SELALL" );
            b.addActionListener ( theListener );
            gbc.gridy = message.length + 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            p.add ( b, gbc );
         }

      }

      // prepare icon, if any
      String sIconFile = null;
      String sIconResource = null;
      Icon theIcon = null;

      if ( FLAG_ERROR == ( FLAG_ERROR & flag ) )
      {
         sIconFile = "images/error.png";
         // Java for Windows
         sIconResource = "OptionPane.errorIcon";
         // Java for Linux does not offer these standard icons...
      }
      else if ( FLAG_WARNING == ( FLAG_WARNING & flag ) )
      {
         sIconFile = "images/warning.png";
         // Java for Windows
         sIconResource = "OptionPane.warningIcon";
         // Java for Linux does not offer these standard icons...
      }
      else if ( FLAG_QUESTION == ( FLAG_QUESTION & flag ) )
      {
         sIconFile = "images/question.png";
         // Java for Windows
         sIconResource = "OptionPane.questionIcon";
         // Java for Linux does not offer these standard icons...
      }
      else if ( FLAG_INFORMATION == ( FLAG_INFORMATION & flag ) )
      {
         sIconFile = "images/information.png";
         // Java for Windows
         sIconResource = "OptionPane.informationIcon";
         // Java for Linux does not offer these standard icons...
      }

      // first try to find the UIManager specific icon to fit look and feel
      // Note: the Windows XP look and feel offers 50 icons.
      if ( sIconResource != null )
      {
         UIDefaults df = UIManager.getLookAndFeelDefaults ();
         theIcon = df.getIcon ( sIconResource );
      }

      // fallback on bitmap image resource if icon was not found
      if ( theIcon == null &&
           sIconFile != null )
      {
         URL theResource = JDialogBox.class.getResource ( sIconFile );
         if ( theResource != null )
         {
            theIcon = new ImageIcon ( theResource );
         }
      }

      if ( theIcon != null )
      {
         // dummy panel to provide space around icon
         JPanel pi = new JPanel ( new GridLayout ( 1, 3 ) );
         pi.add ( new JLabel () );
         pi.add ( new JLabel ( theIcon ) );
         pi.add ( new JLabel () );
         d.add ( pi, BorderLayout.WEST );

         // use Octave icon if available. otherwise use dialog icon
         Icon theOctaveIcon = getOctaveIcon ();
         prepareFrameIcon ( m_ParentFrame,
                            theOctaveIcon == null ? theIcon : theOctaveIcon );
      }

      d.add ( p, BorderLayout.CENTER );

      // button bar (2 rows of 3 columns each

      p = new JPanel ();
      p.setLayout ( new java.awt.GridLayout ( 2, 3 ) );

      // spacer row
      p.add ( new JLabel () );
      p.add ( new JLabel () );
      p.add ( new JLabel () );

      if ( DLG_QUEST == ( DLG_QUEST & flag ) )
      {
         // questdlg with empty option[2]: only two buttons
         if ( nostring.length () < 1 )
         {
            // spacer: left
            p.add ( new JLabel () );
         }
      }
      else
      {
         // spacer: left
         p.add ( new JLabel () );
      }

      m_ButtonOK = new JButton ( theTranslator.replace ( okstring ) );
      m_ButtonOK.setActionCommand ( "OK" );
      m_ButtonOK.addActionListener ( theListener );
      m_Focus = m_ButtonOK;
      p.add ( m_ButtonOK );

      if ( DLG_QUEST == ( DLG_QUEST & flag ) )
      {
         // questdlg with empty option[2]: only two buttons
         if ( nostring.length () > 01 )
         {
            // questdlg has three buttons
            m_ButtonNO = new JButton ( theTranslator.replace ( nostring ) );
            m_ButtonNO.setActionCommand ( "NO" );
            m_ButtonNO.addActionListener ( theListener );
            p.add ( m_ButtonNO );
            if ( DLG_QUEST == ( DLG_QUEST & flag ) )
            {
               // select default button
               if ( list[3].equals ( nostring ) )
               {
                  m_Focus = m_ButtonNO;
               }
            }
         }
      }

      if ( DLG_INPUT == ( DLG_INPUT & flag ) ||
           DLG_LIST == ( DLG_LIST & flag ) ||
           DLG_QUEST == ( DLG_QUEST & flag ) )
      {
         m_ButtonCANCEL = new JButton ( theTranslator.replace ( cancelstring ) );
         m_ButtonCANCEL.setActionCommand ( "CANCEL" );
         m_ButtonCANCEL.addActionListener ( theListener );
         p.add ( m_ButtonCANCEL );
         if ( DLG_QUEST == ( DLG_QUEST & flag ) )
         {
            // select default button
            if ( list[3].equals ( cancelstring ) )
            {
               m_Focus = m_ButtonCANCEL;
            }
         }
      }
      else
      {
         // spacer: right
         p.add ( new JLabel () );
      }

      d.add ( p, BorderLayout.SOUTH );
      dlg.pack ();

      dlg.addWindowListener ( theListener );

      if ( on.equals ( "on" ) )
      {
         m_ParentFrame.setAlwaysOnTop ( true );
      }

      // center dialog on screen
      Dimension dlgSize = dlg.getSize ();
      Dimension screenSize = Toolkit.getDefaultToolkit ().getScreenSize ();

      dlg.setLocation ( ( screenSize.width - dlgSize.width ) / 2,
                        ( screenSize.height - dlgSize.height ) / 2 );

      dlg.setVisible ( true );
      dlg.requestFocus ();

      m_ParentFrame.setVisible ( false );

      return ( 1 );
   }

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index