classdef Hauptfenster < handle 
    properties 
        HauptFigure    % Hauptfenster 
        button_hinzu         % Button zum Hinzufügen neuer Textboxen 
        button_loeschen        % Button zum Löschen einer Textbox 
        button_name_aendern       % Button zum Ändern des Namens einer Textbox 
        textbox = matlab.ui.control.UIControl.empty; % neue Textbox anlegen <-- auf leer initialisiert, *alle* Textboxen speichern
        button_geklickt = matlab.ui.control.UIControl.empty; 
        name_aendern_Figure       % Fenster zum Ändern des Textbox-Namens
        name_aendern_edit       % Edit-Box zur Änderung des Names einer Textbox
        
    end 
    methods (Access = public)  % Methoden des Hauptfensters <-- Constructor nicht static
        
        function obj = Hauptfenster() 
            obj.HauptFigure=figure();  % HauptFigure wird erstellt 
            scrsz = get(0,'ScreenSize');  % Bildschirmgröße auslesen 
            set(obj.HauptFigure, ... 
                'Position',[0.3*scrsz(3),0.3*scrsz(4),0.6*scrsz(3),0.6*scrsz(4)],... 
                'Units','normalized',... 
                'Resize','on',... 
                'MenuBar','none',... 
                'Name','Test 2015',... 
                'NumberTitle','off'); 
            
            obj.button_hinzu = uicontrol();    %Button erstellen 
            set(obj.button_hinzu, ... 
                'style', 'pushbutton', ... 
                'FontName', 'Arial', ... 
                'FontSize', 10, ... 
                'string', 'Hinzufügen', ... 
                'Units','normalized', ... 
                'Position', [.49,.15,.1,.07], ... 
                'Callback', @obj.button_hinzu_Callback);  % <-- angepasst
            
            obj.button_loeschen = uicontrol();    %Button erstellen 
            set(obj.button_loeschen, ... 
                'style', 'pushbutton', ... 
                'FontName', 'Arial', ... 
                'FontSize', 10, ... 
                'string', 'Löschen', ... 
                'Units','normalized', ... 
                'Position', [.69,.15,.1,.07], ... 
                'Callback', @obj.button_loeschen_Callback);
            
            obj.button_name_aendern = uicontrol();    %Button erstellen 
            set(obj.button_name_aendern, ... 
                'style', 'pushbutton', ... 
                'FontName', 'Arial', ... 
                'FontSize', 10, ... 
                'string', 'Name ändern', ... 
                'Units','normalized', ... 
                'Position', [.89,.15,.1,.07], ... 
                'Callback', @obj.button_name_aendern_Callback);
        end 
    end 
    methods (Access = private) % <-- nicht static      
        function button_hinzu_Callback(obj, ~, ~) % <-- Interface angepasst
            num_existing = numel(obj.textbox);
            offset = 0.15*num_existing;
            obj.textbox(end+1) = uicontrol(...  % <-- neue Textbox wird drangehängt
                'style', 'text', ... 
                'FontName', 'Arial', ... 
                'FontSize', 10, ... 
                'string', sprintf('Nummer %g', num_existing+1), ... 
                'HorizontalAlignment', 'left', ... 
                'Units','normalized', ... 
                'Position',[0.02 0.85-offset 0.1 0.1], ... 
                'BackgroundColor', 'green', ...
                'Enable', 'Inactive', ...
                'ButtonDownFcn', @obj.geklickte_textbox_Callback);
            
            if isempty(obj.button_geklickt)
                obj.button_geklickt = obj.textbox(num_existing+1);
            else
                set(obj.button_geklickt, 'BackgroundColor', 'blue');
                obj.button_geklickt = obj.textbox(num_existing+1);
            end
        end 
        
        function button_loeschen_Callback(obj, ~, ~) 
            if isempty(obj.button_geklickt)
                
            else
                delete(obj.button_geklickt);
                num_existing = numel(obj.textbox);
                if num_existing == 0
                    obj.button_geklickt = matlab.ui.control.UIControl.empty;
                else
                    obj.button_geklickt = obj.textbox(num_existing);
                end
                
            end
            obj.textbox(~ishandle(obj.textbox)) = [];
            obj.button_geklickt = matlab.ui.control.UIControl.empty;
        end
        
        function geklickte_textbox_Callback(obj, ~, ~)
            set(obj.button_geklickt, 'BackgroundColor', 'blue');
            obj.button_geklickt = gco;
            set(obj.button_geklickt, 'BackgroundColor', 'green');
        end    
        
        
        %% Würde ich gerne auslagern in ein neues Objekt, sprich eine neue Klasse, in welcher die neue Figure erstellt wird.
        % Dann würde ich gerne über Listener den Austausch die
        % Aktualisierung des Namens gestalten
        function button_name_aendern_Callback(obj, ~, ~)
            if isempty(obj.button_geklickt)
                
            else
                scrsz = get(0,'ScreenSize');  % Bildschirmgröße auslesen
                %% Anlegen des Hauptfensters 
                obj.name_aendern_Figure = figure('Position',[0.4*scrsz(3),0.5*scrsz(4),0.2*scrsz(3),0.1*scrsz(4)],...
                                            'Units','normalized',...
                                            'Resize','on',...
                                            'MenuBar','none',...
                                            'Name','Name aendern',...
                                            'NumberTitle','off');
                % Editbox zur Namensänderung
                obj.name_aendern_edit = uicontrol('Style','edit', ...
                                                 'FontName', 'Arial', ...
                                                 'FontSize', 10, ...
                                                 'HorizontalAlignment', 'left', ...
                                                 'String','gewünschten Namen eingeben', ...
                                                 'Units','normalized', ...
                                                 'Position',[0.05 0.6 0.9 0.35], ...
                                                 'Callback', @name_aendern_edit_Callback);                        
            end                            
        end
        
        function name_aendern_edit_Callback(obj, ~, ~)
            string = get(obj.name_aendern_edit, 'String');
            set(obj.button_geklickt, 'String', string);
        end    
    end 
end 
