Mercurial > hg > octave-nkf
changeset 15709:9fee0b741de6
Update Java dialog scrips to latest octave-forge status
* dlgtest.m: strip away all code for reinstalling Java package
* cell2mlstr.m: new function
* helpdlg.m, errordlg.m, inputdlg.m, listdlg.m, questdlg.m, warndlg.m, msgbox.m:
Allow cellstr arrays as first argument (ML compatibilility)
author | Philip Nienhuis <prnienhuis@users.sf.net> |
---|---|
date | Sat, 01 Dec 2012 18:10:55 +0100 |
parents | 916ef285522a |
children | cd3d7f126190 |
files | scripts/java/cell2mlstr.m scripts/java/dlgtest.m scripts/java/errordlg.m scripts/java/helpdlg.m scripts/java/inputdlg.m scripts/java/listdlg.m scripts/java/msgbox.m scripts/java/questdlg.m scripts/java/warndlg.m |
diffstat | 9 files changed, 174 insertions(+), 100 deletions(-) [+] |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/scripts/java/cell2mlstr.m @@ -0,0 +1,36 @@ +## Copyright (C) 2012 Philip Nienhuis <prnienhuis@users.sf.net> +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## cell2mlstr - convert text cells in cellstr arrray to multiline text +## separated by EOL + +## Author: Philip <prnienhuis@users.sf.net> +## Based on a suggestion by D. Bateman, +## https://savannah.gnu.org/bugs/?func=detailitem&item_id=31468#comment4 +## Created: 2012-06-29 + +function [ ret ] = cell2mlstr (cstr) + + if (! iscellstr (cstr)) + ## Only use char elements + cstr = cstr (find (cellfun ("ischar", cstr))); + endif + ## Treat cell string array as multi-line text + cstr(1:2:2*numel (cstr)) = cstr; + cstr(2:2:numel (cstr)) = "\n"; + ret = [cstr{:}]; + +endfunction
--- a/scripts/java/dlgtest.m +++ b/scripts/java/dlgtest.m @@ -1,63 +1,13 @@ % -% Install the java package. -% Test the dlg... functions of the java package. +% Test the dlg... functions of the Octave-Java bridge +% +% Once the Java bridge works OK this function should be dropped from core octave % % Author: Martin Hepperle % Version August 2010 +% Adapted for core Octave Philip Nienhuis 2012 % -function dlgtest ( reinstall ) - - % Windows example paths - if ispc() - % NOTE: do NOT use backslashes as separator, only forward slashes! - pkgpath = 'z:/java-1.2.8.tar.gz'; - java_home = getenv ("JAVA_HOME"); - elseif isunix() - % Linux example paths - pkgpath = '~/java-1.2.8.tar.gz'; - java_home = getenv ("JAVA_HOME"); - else - pkgpath = 'unknown'; - java_home = 'unknown'; - end - - if nargin<1 - disp('usage: dlgtest ( reinstall )'); - disp( 'where: reinstall = 0 : do not reinstall java package'); - disp([' reinstall = 1 : reinstall java package from ', pkgpath, ... - ', using Java JDK from ', java_home]); - return - end - - if ! exist (java_home, "dir") - disp(['Java JDK home directory ', java_home,' does not exist.']); - disp('Please adapt java_home in dlgtest.m.'); - return; - end - - if reinstall == 1 - if ! exist (pkgpath, "file") - disp(['Package file ', pkgpath, ' does not exist.']); - disp('Please adapt pkgpath in dlgtest.m.'); - return; - end - end - - page_screen_output(0); - - if reinstall == 1 - disp('- uninstalling package java'); - pkg uninstall java - - disp(['- installing package java from ',pkgpath]); - disp([' using JDK from ',java_home]); - setenv('JAVA_HOME',java_home) - %% pkg does not understand variables as arguments? - eval(['pkg install ', pkgpath]) - disp('Done.'); - end - - page_screen_output(1); +function dlgtest answer = 1; while (answer > 0 )
--- a/scripts/java/errordlg.m +++ b/scripts/java/errordlg.m @@ -16,22 +16,35 @@ ## -*- texinfo -*- ## @deftypefn {Function file} {@var{P} =} errordlg (@var{MESSAGE} [,@var{TITLE}]) ## -## Displays the @var{MESSAGE} using an error dialog box. -## The @var{TITLE} can be used optionally to decorate the dialog caption. +## Displays the @var{MESSAGE} (character string or cell string array for +## multi-line text) using an error dialog box. +## @var{TITLE} can be used optionally to decorate the dialog caption. ## The return value is always 1. ## ## @end deftypefn ## @seealso{helpdlg, inputdlg, listdlg, questdlg, warndlg} -function ret = errordlg(message,varargin) - +function ret = errordlg (message, varargin) + + if (! ischar (message)) + if (iscell (message)) + message = cell2mlstr (message); + else + error ("errordlg: character string or cellstr array expected for message"); + endif + endif + switch length (varargin) case 0 - title = "Error Dialog"; + title = "Error Dialog"; otherwise - title = varargin{1}; + title = varargin{1}; endswitch + if (! ischar (title)) + error ("errordlg: character string expected for title"); + endif + ret = java_invoke ("org.octave.JDialogBox", "errordlg", message, title); endfunction
--- a/scripts/java/helpdlg.m +++ b/scripts/java/helpdlg.m @@ -16,23 +16,42 @@ ## -*- texinfo -*- ## @deftypefn {Function file} {@var{P} =} helpdlg (@var{MESSAGE} [,@var{TITLE}]) ## -## Displays a @var{MESSAGE} in a help dialog box. -## The help message can have multiple lines, separated by a newline character '\n'. -## The @var{TITLE} can be used optionally to decorate the dialog caption. +## Displays a @var{MESSAGE} in a help dialog box. +## +## @var{message} can have multiple lines separated by newline characters +## ("\n"), or it can be a cellstr array (one element for each line). +## The optional @var{TITLE} (character string) can be used to decorate the +## dialog caption. ## The return value is always 1. ## ## @end deftypefn ## @seealso{errordlg, inputdlg, listdlg, questdlg, warndlg} -function ret = helpdlg(message,varargin) +function ret = helpdlg (message, varargin) + + if (! ischar (message)) + if (iscell (message)) + message = cell2mlstr (message); + else + error ("helpdlg: character string or cellstr array expected for message"); + endif + endif switch length (varargin) case 0 title = "Help Dialog"; otherwise - title = varargin{1}; + if (ischar (varargin {1})) + title = varargin{1}; + else + error ("helpdlg: character string expected for title"); + endif endswitch + if (! ischar (title)) + error ("helpdlg: character string expected for title"); + endif + ret = java_invoke ("org.octave.JDialogBox", "helpdlg", message, title); endfunction
--- a/scripts/java/inputdlg.m +++ b/scripts/java/inputdlg.m @@ -39,27 +39,39 @@ ## @end deftypefn ## @seealso{errordlg, helpdlg, listdlg, questdlg, warndlg} -function varargout = inputdlg(prompt,varargin) - +function varargout = inputdlg (prompt, varargin) + + if (iscell (prompt)) + % Silently extract only char elements + prompt = prompt (find (cellfun ("ischar", prompt))); + elseif (ischar (prompt)) + prompt = {prompt}; + else + error ("inputdlg: character string or cellstr array expected for prompt"); + endif + switch length (varargin) case 0 title = "Input Dialog"; lineNo = 1; - defaults = cellstr(cell(size(prompt))); + defaults = cellstr (cell( size (prompt))); case 1 title = varargin{1}; lineNo = 1; - defaults = cellstr(cell(size(prompt))); + defaults = cellstr (cell (size (prompt))); case 2 title = varargin{1}; lineNo = varargin{2}; - defaults = cellstr(cell(size(prompt))); + defaults = cellstr (cell (size (prompt))); otherwise title = varargin{1}; lineNo = varargin{2}; defaults = varargin{3}; end + if (! ischar (title)) + error ("inputdlg: character string expected for title"); + endif % specification of text field sizes as in Matlab % Matlab requires a matrix for lineNo, not a cell array...
--- a/scripts/java/listdlg.m +++ b/scripts/java/listdlg.m @@ -61,7 +61,7 @@ ## @end deftypefn ## @seealso{errordlg, helpdlg, inputdlg, questdlg, warndlg} -function varargout = listdlg(varargin) +function varargout = listdlg (varargin) if nargin < 2 print_usage ();
--- a/scripts/java/msgbox.m +++ b/scripts/java/msgbox.m @@ -17,7 +17,11 @@ ## @deftypefn {Function file} {@var{P} =} msgbox (@var{MESSAGE} [,@var{TITLE} [,@var{ICON}]]) ## ## Displays the @var{MESSAGE} using a message dialog. -## The @var{TITLE} is an optional string, which can be used to decorate the dialog caption. +## +## @var{message} can have multiple lines separated by newline characters +## ("\n"), or it can be a cellstr array (one element for each line). +## The optional @var{TITLE} (character string) can be used to decorate the +## dialog caption. ## The @var{ICON} can be used optionally to select a dialog icon. ## It can be one of @code{'error'}, @code{'help'} or @code{'warn'}. ## The return value is always 1. @@ -25,30 +29,42 @@ ## @end deftypefn ## @seealso{helpdlg, questdlg, warndlg} -function ret = msgbox(message,varargin) +function ret = msgbox (message, varargin) + + if (! ischar (message)) + if (iscell (message)) + message = cell2mlstr (message); + else + error ("msgbox: character string or cellstr array expected for message"); + endif + endif switch length (varargin) - case 0 - title = ""; - dlg = 'emptydlg'; - case 1 - title = varargin{1}; - dlg = 'emptydlg'; - otherwise - % two or more arguments - title = varargin{1}; - icon = varargin{2}; - if strcmp(icon,'error') == 1 - dlg = 'errordlg'; - elseif strcmp(icon,'help') == 1 - dlg = 'helpdlg'; - elseif strcmp(icon,'warn') == 1 - dlg = 'warndlg'; - else + case 0 + title = ""; + dlg = 'emptydlg'; + case 1 + title = varargin{1}; dlg = 'emptydlg'; - end + otherwise + % two or more arguments + title = varargin{1}; + icon = varargin{2}; + if strcmp (icon,'error') == 1 + dlg = 'errordlg'; + elseif strcmp (icon,'help') == 1 + dlg = 'helpdlg'; + elseif strcmp (icon,'warn') == 1 + dlg = 'warndlg'; + else + dlg = 'emptydlg'; + end endswitch + if (! ischar (title)) + error ("msgbox: character string expected for title"); + endif + ret = java_invoke ('org.octave.JDialogBox', dlg, message, title ); endfunction
--- a/scripts/java/questdlg.m +++ b/scripts/java/questdlg.m @@ -23,7 +23,10 @@ ## The dialog contains two or three buttons which all close the dialog. ## It returns the caption of the activated button. ## -## The @var{TITLE} can be used optionally to decorate the dialog caption. +## @var{message} can have multiple lines separated by newline characters +## ("\n"), or it can be a cellstr array (one element for each line). +## The optional @var{TITLE} (character string) can be used to decorate the +## dialog caption. ## The string @var{DEFAULT} identifies the default button, ## which is activated by pressing the ENTER key. ## It must match one of the strings given in @var{BTN1}, @var{BTN2} or @var{BTN3}. @@ -37,9 +40,9 @@ ## @end deftypefn ## @seealso{errordlg, helpdlg, inputdlg, listdlg, warndlg} -function ret = questdlg(question,varargin) +function ret = questdlg (question, varargin) - if length(varargin) < 1 + if length (varargin) < 1 print_usage(); end @@ -48,8 +51,15 @@ options{3} = 'Cancel'; % button3 options{4} = 'Yes'; % default + if (! ischar (question)) + if (iscell (question)) + question = cell2mlstr (question); + else + error ("questdlg: character string or cellstr array expected for message"); + endif + endif - switch length(varargin) + switch length (varargin) case 1 % title was given title = varargin{1}; @@ -75,6 +85,9 @@ print_usage(); end + if (! ischar (title)) + error ("questdlg: character string expected for title"); + endif ret = java_invoke ('org.octave.JDialogBox', 'questdlg', question, title, options);
--- a/scripts/java/warndlg.m +++ b/scripts/java/warndlg.m @@ -15,20 +15,35 @@ ## -*- texinfo -*- ## @deftypefn {Function file} {@var{P} =} warndlg (@var{MESSAGE} [,@var{TITLE}]) +## Displays the @var{MESSAGE} using a warning dialog box. ## -## Displays the @var{MESSAGE} using a warning dialog box. -## The @var{TITLE} can be used optionally to decorate the dialog caption. +## @var{message} can have multiple lines separated by newline characters +## ("\n"), or it can be a cellstr array (one element for each line). +## The optional @var{TITLE} (character string) can be used to decorate the +## dialog caption. ## ## @end deftypefn ## @seealso{helpdlg, inputdlg, listdlg, questiondlg} -function ret = warndlg(message,varargin) +function ret = warndlg (message, varargin) + + if (! ischar (message)) + if (iscell (message)) + message = cell2mlstr (message); + else + error ("warndlg: character string or cellstr array expected for message"); + endif + endif switch length (varargin) case 0 title = 'Warning Dialog'; otherwise - title = varargin{1}; + if (ischar (varargin{1})) + title = varargin{1}; + else + error ("warndlg: character string expected for title"); + endif endswitch ret = java_invoke ('org.octave.JDialogBox', 'warndlg', message, title);