]> Creatis software - CreaPhase.git/blob - octave_packages/java-1.2.8/listdlg.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / java-1.2.8 / listdlg.m
1 ## Copyright (C) 2010 Martin Hepperle
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function file} {@var{[SEL,OK]} =} listdlg (@var{KEY} ,@var{VALUE} [, @var{KEY} ,@var{VALUE}, ...]])
18 ##
19 ## Returns the user's inputs from a list dialog box in form of a vector of 
20 ## selection indices SEL and a flag OK indicating how the user closed the dialog box.
21 ## The returned flag OK is 1 if the user closed the box with the OK button, 
22 ## otherwise it is 0 and SEL is empty. 
23 ## The indices in SEL are 1 based, i.e. the first list item carries the index 1.
24 ##
25 ## The arguments are specified in form of @var{KEY}, @var{VALUE} pairs. 
26 ## At least the 'ListString' argument pair must be specified.
27 ##
28 ## @var{KEY}s and @var{VALUE}s pairs can be selected from the following list:
29 ##
30 ## @table @samp
31 ## @item ListString
32 ##    a cell array of strings comprising the content of the list.
33 ## @item SelectionMode
34 ##    can be either @samp{Single} or @samp{Multiple}.
35 ## @item ListSize
36 ##    a vector with two elements [width, height] defining the size of the list field in pixels.
37 ## @item InitialValue
38 ##    a vector containing 1-based indices of preselected elements.
39 ## @item Name
40 ##    a string to be used as the dialog caption.
41 ## @item PromptString
42 ##    a cell array of strings to be displayed above the list field.
43 ## @item OKString
44 ##    a string used to label the OK button.
45 ## @item CancelString
46 ##    a string used to label the Cancel  button.
47 ## @end table
48 ##
49 ## Example:
50 ##
51 ## @example
52 ##   [sel, ok] = listdlg ( 'ListString',@{'An item', 'another', 'yet another'@}, 'SelectionMode','Multiple' );
53 ##   if ok == 1
54 ##      imax = length(sel);
55 ##      for i=1:1:imax
56 ##         disp(sel(i));
57 ##      end
58 ##   end
59 ## @end example
60 ##
61 ## @end deftypefn
62 ## @seealso{errordlg, helpdlg, inputdlg, questdlg, warndlg}
63
64 function varargout = listdlg(varargin)
65
66    if nargin < 2
67      print_usage ();
68      return;
69    end
70    
71    listcell = {''};
72    selmode = 'single';
73    listsize = [300,160];   % vector!
74    initialvalue = [1];     % vector!
75    name = '';
76    prompt = {''};
77    okstring = 'OK';
78    cancelstring = 'Cancel';
79    
80    % handle key, value pairs
81    for i=1:2:nargin-1
82       if strcmp(varargin{i},'ListString')
83          listcell = varargin{i+1};
84       elseif strcmp(varargin{i},'SelectionMode')
85          selmode = varargin{i+1};
86       elseif strcmp(varargin{i},'ListSize')
87          listsize = varargin{i+1};
88       elseif strcmp(varargin{i},'InitialValue')
89          initialvalue = varargin{i+1};
90       elseif strcmp(varargin{i},'Name')
91          name = varargin{i+1};
92       elseif strcmp(varargin{i},'PromptString')
93          prompt = varargin{i+1};
94       elseif strcmp(varargin{i},'OKString')
95          okstring = varargin{i+1};
96       elseif strcmp(varargin{i},'CancelString')
97          cancelstring = varargin{i+1};
98       end      
99    end
100
101    % make sure prompt strings are a cell array
102    if ! iscell(prompt)
103       prompt = {prompt};
104    end
105
106    % make sure listcell strings are a cell array
107    if ! iscell(listcell)
108       listcell = {listcell};
109    end
110
111    
112    % transform matrices to cell arrays of strings
113    listsize = arrayfun(@num2str,listsize,'UniformOutput',false);
114    initialvalue = arrayfun(@num2str,initialvalue,'UniformOutput',false);
115    
116    ret = java_invoke ('org.octave.JDialogBox', 'listdlg', listcell, ...
117                       selmode, listsize, initialvalue, name, prompt, ...
118                       okstring, cancelstring );
119    if length(ret) > 0
120       varargout = {ret, 1};
121    else
122       varargout = {{}, 0};
123    end
124   
125
126 end