package net.kldp.logviewer;

import net.kldp.logviewer.common.log.LogViews;
import net.kldp.logviewer.model.GetLogDBInfo;
import net.kldp.logviewer.model.LogViewerEntry;
import net.kldp.logviewer.model.LogViewerGroup;

import org.apache.log4j.Logger;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.plugin.AbstractUIPlugin;

/**
 * 로그 정보를 수정한다.
 * 
 * @author ion
 *
 */
public class ModifyLogAction extends Action implements ISelectionListener, IWorkbenchAction {

	private static final Logger logger = LogViews.getLogger(ModifyLogAction.class);
	
	private final IWorkbenchWindow window;
	public final static String ID = "net.kldp.logviewer.ModifyLog";
	private IStructuredSelection selection;
	String _groupNm = "";
	LogViewerEntry entry = new LogViewerEntry();
	LogViewerEntry oldEntry = new LogViewerEntry();
	
    public ModifyLogAction(IWorkbenchWindow window, String label) {
        super(label);        
        
        this.window = window;
        // The id is used to refer to the action in a menu or toolbar
        setId(ICommandIds.CMD_OPEN_MESSAGE);
        // Associate the action with a pre-defined command, to allow key bindings.
        setActionDefinitionId(ICommandIds.CMD_OPEN_MESSAGE);
        setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKeys.LOG_MODIFY));
        
        window.getSelectionService().addSelectionListener(this);
    }

    public void dispose() {
		window.getSelectionService().removeSelectionListener(this);
	}

	public void selectionChanged(IWorkbenchPart part, ISelection incoming) {
		
		if (incoming instanceof IStructuredSelection) {
			selection = (IStructuredSelection) incoming;
			entry = new LogViewerEntry();
			
			if(selection.size() == 1 &&  selection.getFirstElement() instanceof LogViewerEntry) {
				entry = (LogViewerEntry)selection.getFirstElement();	
				oldEntry = (LogViewerEntry)selection.getFirstElement();
				_groupNm = entry.getParent().getName();
				
				setEnabled(true);
			} else {
				setEnabled(false);
			}
		
		} else {
			// Other selections, for example containing text or of other kinds.
			setEnabled(false);
		}
	}

	public void run() {
		try {
			AddOrModifyLogView view = new AddOrModifyLogView(window.getShell(), _groupNm, entry);
			int code = view.open();
			
			if(code == Window.OK) {					
				LogViewerGroup  group = oldEntry.getParent();
				
				GetLogDBInfo info = new GetLogDBInfo();			
				info.updateLogViewerEntry(view._entry);
				
				// 기존 그룹을 삭제하고 새롭게 추가한다.
				group.removeEntry(oldEntry);  // fix 2006-10-19 henry
				group.addEntry(view._entry);
			}
		} catch(Exception e) {
			e.printStackTrace();
		}		
	}	
}