function h = open(h)
%OPEN  Open GUI for editing h

%   Author(s): A. DiVergilio
%   Copyright 1986-2000 The MathWorks, Inc. 
%   $Revision: 1.8 $  $Date: 2000/08/28 23:02:59 $

if isempty(h.EditorFrame)
   h.EditorFrame = localBuildEditor(h);
else
   set(h.EditorFrame,'Minimized','off','Visible','off','Visible','on');
end


%%%%%%%%%%%%%%%%%%%%
% localBuildEditor %
%%%%%%%%%%%%%%%%%%%%
function Frame = localBuildEditor(h)
% Build Editor Frame
 %---Get Toolbox Preferences
  Prefs = cstprefs.tbxprefs;
 %---Frame
  Frame = com.mathworks.mwt.MWFrame(sprintf('Property Editor'));
  Frame.setLayout(com.mathworks.mwt.MWBorderLayout(0,0));
  Frame.setFont(Prefs.JavaFontP);
  set(Frame,'WindowClosingCallback',{@localClose,h});
 %---Show empty Frame with some status text while loading
  Frame.setSize(java.awt.Dimension(360,280));
  centerfig(Frame);
  s.Status = com.mathworks.mwt.MWLabel(sprintf('Opening Property Editor...'),com.mathworks.mwt.MWLabel.CENTER);
  Frame.add(s.Status,com.mathworks.mwt.MWBorderLayout.CENTER);
  Frame.setVisible(1);
  Frame.toFront;
  xypos1 = Frame.getLocation;

 %---ButtonPanel
  s.ButtonPanel = com.mathworks.mwt.MWPanel(java.awt.FlowLayout(java.awt.FlowLayout.RIGHT,7,0));
  s.ButtonPanel.setInsets(java.awt.Insets(2,5,5,-2));
    %---Close
     s.Close = com.mathworks.mwt.MWButton(sprintf('Close')); s.ButtonPanel.add(s.Close);
     s.Close.setFont(Prefs.JavaFontP);
     set(s.Close,'ActionPerformedCallback',{@localClose,h});
    %---Help
     s.Help = com.mathworks.mwt.MWButton(sprintf('Help')); s.ButtonPanel.add(s.Help);
     s.Help.setFont(Prefs.JavaFontP);
     set(s.Help,'ActionPerformedCallback','ctrlguihelp(''sisotool_properties'');');

 %---TabPanel
  s.TabPanel = com.mathworks.mwt.MWTabPanel;
  s.TabPanel.setInsets(java.awt.Insets(5,5,5,5));
  s.TabPanel.setMargins(java.awt.Insets(12,8,8,8));
  s.TabPanel.setFont(Prefs.JavaFontP);
    %---Labels
     s.Text = text_gui(h);
     s.LabelsTab = localMakeTab(s.Text);
     s.TabPanel.addPanel(sprintf('Labels'),s.LabelsTab);
    %---Limits
     s.XLimits = xlim_gui(h);
     s.ILimits = ilim_gui(h);
     s.MLimits = mlim_gui(h);
     s.PLimits = plim_gui(h);
     s.LStack  = lims_gui(h);
     s.LimitsTab = localMakeTab(s.XLimits,s.ILimits,s.MLimits,s.PLimits,s.LStack);
     s.TabPanel.addPanel(sprintf('Limits'),s.LimitsTab);
    %---Options
     s.Grid = grid_gui(h);
     s.Axis = axis_gui(h);
     s.MagPhase = mp_gui(h);
     s.Characteristics= char_gui(h);
     s.OptionsTab = localMakeTab(s.Grid,s.Axis,s.MagPhase,s.Characteristics);
     s.TabPanel.addPanel(sprintf('Options'),s.OptionsTab);
 %---Start on the first tab panel
  s.TabPanel.selectPanel(0);

 %---Store structure in Frame UserData
  set(Frame,'UserData',s);

 %---Remove the status text, add the real components and pack
  Frame.remove(s.Status);
  Frame.add(s.TabPanel,com.mathworks.mwt.MWBorderLayout.CENTER);
  Frame.add(s.ButtonPanel,com.mathworks.mwt.MWBorderLayout.SOUTH);
  Frame.pack;
  xypos2 = Frame.getLocation;
  if abs(xypos1.x-xypos2.x)<5 & abs(xypos1.y-xypos2.y)<5
     centerfig(Frame);
  end


%%%%%%%%%%%%%%%%
% localMakeTab %
%%%%%%%%%%%%%%%%
function Tab = localMakeTab(varargin)
% Combine panels into a single panel for use in a tab dialog
Tab = com.mathworks.mwt.MWPanel(com.mathworks.mwt.MWBorderLayout(0,0));
for n=1:nargin
   if n<nargin
      %---Not last element, leave 5-pixel spacing
      tmp = com.mathworks.mwt.MWPanel(com.mathworks.mwt.MWBorderLayout(0,5));
   else
      %---Last element, leave 0-pixel spacing
      tmp = com.mathworks.mwt.MWPanel(com.mathworks.mwt.MWBorderLayout(0,0));
   end
   %---Pack towards top
   tmp.add(varargin{n},com.mathworks.mwt.MWBorderLayout.NORTH);
   %---Add java handle to structure
   s(n,1) = tmp;
   %---Add first panel to Tab
   if n==1
      Tab.add(tmp,com.mathworks.mwt.MWBorderLayout.CENTER);
   %---Nest other panels
   else
      tmp2 = s(n-1,1);
      tmp2.add(tmp,com.mathworks.mwt.MWBorderLayout.CENTER);
   end
end
%---Store java handles
set(Tab,'UserData',s);


%%%%%%%%%%%%%%
% localClose %
%%%%%%%%%%%%%%
function localClose(eventSrc,eventData,h)
%---Call close method
 close(h);
