Mercurial > hg > ltpda
comparison m-toolbox/m/gui/@jcontrol/jcontrol.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f0afece42f48 |
---|---|
1 function obj=jcontrol(Parent, Style, varargin) | |
2 % JCONTROL constructor for JCONTROL class | |
3 % | |
4 % JCONTROL provides an easy way to integrate a full range of java GUIs | |
5 % from the java.awt and javax.swing libraries into MATLAB. | |
6 % | |
7 % Example: | |
8 % obj=JCONTROL(Parent, Style); | |
9 % obj=JCONTROL(Parent, Style, PropertyName1, PropertyValue1,... | |
10 % PropertyName2, ProeprtyValue2....); | |
11 % Inputs: | |
12 % Parent: the handle of a Matlab figure or other container for the resulting | |
13 % component | |
14 % Style: string describing a java component e.g. 'javax.swing.JPanel', | |
15 % 'javax.swing.JButton' or a variable containing a java object | |
16 % PropertName/PropertyValue pairs: these are automatically assigned to the | |
17 % HG container or the java component as appropriate. | |
18 % | |
19 % Pre-create the java object if you need to pass arguments to the | |
20 % constructor e.g. | |
21 % javaobj=javax.swing...(...............); | |
22 % obj=jcontrol(Parent, javaobj) | |
23 % | |
24 % By default, JCONTROLs are returned with Units set to 'normalized'. | |
25 % | |
26 % USE: | |
27 % Build a GUI with repeated calls to JCONTROL in much the same way as with | |
28 % MATLAB's uicontrol function e.g.: | |
29 % h=jcontrol(gcf,'javax.swing.JPanel',... | |
30 % 'Position',[100 100 200 150],... | |
31 % 'Units','normalized') | |
32 % h(2)=jcontrol(h(1),'javax.swing.JComboBox',... | |
33 % 'Position',[0.1 0.8 0.8 0.1]); | |
34 % h(2).addItem('Item1'); | |
35 % h(2).addItem('Item2'); | |
36 % h(3)=jcontrol(h(1),'javax.swing.JCheckBox',... | |
37 % 'Position',[0.1 0.1 0.1 0.1],... | |
38 % 'Label','My check box'); | |
39 % See the jcontrolDemo() for a fuller example. | |
40 % | |
41 % A JCONTROL aggregates the MATLAB handle graphics container and the Java | |
42 % component (as returned by MATLAB's JAVACOMPONENT function) into a single | |
43 % object. | |
44 % Access to the JCONTROL's properties is provided by GET/SET calls. | |
45 % These automatically determine whether the target property is in | |
46 % the HG container or java object. | |
47 % myobject=jcontrol(gcf,'javax.swing.JPanel',... | |
48 % 'Units', 'normalized',... | |
49 % 'Name', 'MyPanel'); | |
50 % set(myobject, 'Position', [0.4 0.4 0.4 0.2],... | |
51 % 'Enabled', 0); | |
52 % pos=get(myobject,'Units'); | |
53 % Note that you can mix HG container properties (e.g. Units, Position) and | |
54 % java component properties (e.g. Name, Enabled) in single calls to | |
55 % JCONTROL and SET. | |
56 % Use the HG container to control the Units, Position, and Visible | |
57 % properties | |
58 % | |
59 % MATLAB dot notation may also be used. This notation also provides access | |
60 % to the java object's methods | |
61 % pos=myobject.Position; | |
62 % sz=myObject.getSize; | |
63 % myobject.setEnabled(1); | |
64 % myobject.setToolTipText('My tip'); | |
65 % myobject.setOpaque(1); | |
66 % | |
67 %-------------------------------------------------------------------------- | |
68 % UNITS, POSITION and VISIBLE properties | |
69 % Set these by accessing the JCONTROL or its container (not the hgcontrol). | |
70 % MATLAB links these properties between the container and the java control, | |
71 % but unidirectionally. | |
72 % Note that JCONTROL methods always act on/return the Visible property of | |
73 % the container ('on' or 'off') which will also update the java control. | |
74 % Do not use the setVisible() methods. | |
75 %-------------------------------------------------------------------------- | |
76 % | |
77 % Overloaded class methods are case-insensitive for properties but | |
78 % case-sensitive for java methods | |
79 % | |
80 % CALLBACKS | |
81 % Setting up callbacks | |
82 % The simplest way to set up a callback is through the SET method | |
83 % myPanel=jcontrol(gcf,'javax.swing.JPanel',... | |
84 % 'Units','normalized',... | |
85 % 'Position',[0.3 0.3 0.5 0.5]); | |
86 % set(myPanel, 'MouseClickedCallback', 'MyCallback') | |
87 % or | |
88 % set(myPanel, 'MouseClickedCallback', @MyCallback); | |
89 % or | |
90 % set(myPanel ,'MouseClickedCallback', {@MyCallback A B C...}); | |
91 % | |
92 % The callback then takes the usual MATLAB form, e.g. | |
93 % function MyCallback(hObject, EventData) | |
94 % function MyCallback(hObject, EventData, varargin) | |
95 % | |
96 % Accessing JCONTROL objects in callbacks: | |
97 % The handle received by a callback will be that of the java control | |
98 % object contained in the JCONTROL, not the JCONTROL itself. In addition, | |
99 % GCO will return empty and GCBO will not return the parent figure handle. | |
100 % However, the JCONTROL constructor adds the HG container handle to the | |
101 % java component's properties. This can be used to access the container and | |
102 % its parent figure from within the callback e.g. | |
103 % get(hObject.hghandle);% gets the HG container | |
104 % ancestor(hObject.hghandle,'figure')% gets the parent figure handle | |
105 % To cross-reference from the container, JCONTROL places a reference to | |
106 % the java control in the container's UserData area e.g. | |
107 % hgc=findobj('Tag','MyCustomTag') | |
108 % javacontrol=get(hgc, 'UserData'); | |
109 % | |
110 % Accessing data in callbacks | |
111 % Data can be passed to a callback, as above, with optional input | |
112 % arguments. In addition, data that is specific to the control can be stored | |
113 % in the application data area of the control e.g. to return values | |
114 % dependent on the selection of a popup menu | |
115 % data=getappdata(hObject,'data'); | |
116 % returnvalues=data(hObject.getSelectedItem+1); | |
117 % Note: +1 because the item numbering is zero based for the java object. | |
118 % The HG container has a separate application data area. | |
119 % | |
120 % R2006a or higher only: | |
121 % GETAPPDATA, SETAPPDATA ISAPPDATA and RMAPPDATA methods have been | |
122 % overloaded for JCONTROL objects. These place/return data from the | |
123 % application data area of the java control. Take care if removing the whole | |
124 % application data area - TMW may place data in there too. The HG container | |
125 % has a separate application data area. | |
126 % | |
127 % Notes: | |
128 % If a property name occurs in both the HG container and the java object, | |
129 % the JCONTROL methods can not unambiguously identify the source/target | |
130 % and it must be defined explicitly by the user e.g. | |
131 % get(myobject.hgcontainer,'Opaque'); | |
132 % set(myobject.hgcontrol, 'Opaque',0); | |
133 % The JCONTROL methods test for ambiguity and issue an error message when | |
134 % it arises. Note that the test uses MATLAB's isprop and is case | |
135 % insensitive. | |
136 % It may also detect properties not listed by the MATLAB builtin GET | |
137 % function for the hgcontrol such as Visible. The JCONTROL methods always | |
138 % act on the Visible property of the hgcontainer, letting MATLAB update the | |
139 % object automatically (see above). | |
140 % | |
141 % The DeleteFcn property of the hgcontainer is set by the JAVACOMPONENT | |
142 % function. If this property is changed, the new callback must explicitly | |
143 % delete the hgcontrol. | |
144 % | |
145 % See also: | |
146 % jcontrol/get, jcontrol/set, jcontrol/subsasgn, jcontrol/subsref | |
147 % jcontrol/setappdata, jcontrol/getappdata, jcontrol/isappdata | |
148 % jcontrol/rmappdata, jcontrol/delete | |
149 % javacomponent, gco, gcbo | |
150 % | |
151 %-------------------------------------------------------------------------- | |
152 % Acknowledgements: The JCONTROL class was partly inspired by Yair Altman's | |
153 % <a href="http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=14583&objectType=file">uicomponent</a> function. | |
154 % The functions take different routes to achieve similar ends. JCONTROL is rather | |
155 % faster - which is significant when building complex GUIs, but UICOMPONENT | |
156 % accepts the same calling conventions as MATLAB's UICONTROL while JCONTROL | |
157 % does not. | |
158 %-------------------------------------------------------------------------- | |
159 % | |
160 % ------------------------------------------------------------------------- | |
161 % Author: Malcolm Lidierth 07/07 | |
162 % Copyright © The Author & King's College London 2007- | |
163 % ------------------------------------------------------------------------- | |
164 % | |
165 % Revisions: | |
166 % 12.09.07 Allow pre-contructed java objects on input | |
167 | |
168 if nargin==0 | |
169 % Default constructor | |
170 obj.hgcontrol=[]; | |
171 obj.hghandle=[]; | |
172 obj.hgcontainer=[]; | |
173 obj=class(orderfields(obj),'jcontrol'); | |
174 return | |
175 end | |
176 | |
177 | |
178 % Check parent | |
179 if ishandle(Parent) && Parent==0 | |
180 % Root given as Parent-create a new figure | |
181 container=figure('MenuBar','none'); | |
182 elseif strcmp(class(Parent),'jcontrol'); | |
183 % Parent is a jcontrol - use the hgcontainer | |
184 container=Parent.hgcontainer; | |
185 else | |
186 % Use as specified | |
187 container=Parent; | |
188 end | |
189 | |
190 % Check the Parent is a valid handle | |
191 if ishandle(container)==0 | |
192 error('Parent is not a valid handle (has the parent figure been closed?)'); | |
193 end | |
194 | |
195 % Java object | |
196 if ischar(Style) | |
197 % Create a default object | |
198 javaobject=javaObject(Style); | |
199 elseif isjava(Style) | |
200 % or use supplied object | |
201 javaobject=Style; | |
202 end | |
203 | |
204 % Check we have a valid Style | |
205 if isa(javaobject,'java.awt.Window') | |
206 error('%s is a top-level container: \n%s\n',... | |
207 Style, 'it can not have a MATLAB figure as parent/ancestor'); | |
208 end | |
209 | |
210 % If so, add callbacks and place in container | |
211 [obj.hgcontrol containerHandle]=javacomponent(javaobject,[],container); | |
212 % Put a copy of the container handle in obj.... | |
213 obj.hghandle=containerHandle; | |
214 % ... and in the control | |
215 s=schema.prop(obj.hgcontrol ,'hghandle','mxArray'); | |
216 obj.hgcontrol.hghandle=containerHandle; | |
217 s.AccessFlags.PublicSet='off'; | |
218 | |
219 % Put the container in obj | |
220 obj.hgcontainer = handle(containerHandle); | |
221 | |
222 % Construct the instance | |
223 obj=class(orderfields(obj),'jcontrol'); | |
224 | |
225 % Put a reference to the hgcontrol in the UserData area of the handle | |
226 set(containerHandle, 'UserData', obj.hgcontrol); | |
227 | |
228 % Set the properties and return | |
229 % Default to normalized | |
230 set(obj, 'Units','normalized'); | |
231 % Set values as requested | |
232 if ~isempty(varargin) | |
233 set(obj,varargin{:}); | |
234 return | |
235 end |