/*******************************************************************************
 * Copyright (c) 2015 UNIT Information Technologies R&D Ltd
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Ferhat Erata - initial API and implementation
 *     H. Emre Kirmizi - initial API and implementation
 *     Serhat Celik - initial API and implementation
 *     U. Anil Ozturk - initial API and implementation
 *******************************************************************************/
package eu.modelwriter.marker.command;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;

import eu.modelwriter.marker.MarkerActivator;
import eu.modelwriter.marker.internal.MarkerFactory;
import eu.modelwriter.marker.ui.internal.wizard.markallinwswizard.MarkAllInWsWizard;

public class MarkAllInWorkspaceHandler extends AbstractHandler {

  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection selection = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()
        .getSelectionService().getSelection();
    IFile file = (IFile) MarkerActivator.getEditor().getEditorInput().getAdapter(IFile.class);
    if (selection instanceof ITextSelection) {
      ITextSelection textSelection = (ITextSelection) selection;
      if (MarkerFactory.findMarkerWithAbsolutePosition(file, textSelection.getOffset(),
          textSelection.getOffset() + textSelection.getLength()) != null) {

        MessageDialog dialog = new MessageDialog(MarkerActivator.getShell(), "Mark Information",
            null, "In these area, there is already a marker", MessageDialog.WARNING,
            new String[] {"OK"}, 0);
        dialog.open();

        return null;
      } else {
        MarkAllInWsWizard markAllWsWizard = new MarkAllInWsWizard(textSelection, file);
        WizardDialog selectionDialog =
            new WizardDialog(MarkerActivator.getShell(), markAllWsWizard);
        if (selectionDialog.open() == Window.CANCEL) {
          return null;
        }
      }
    }
    return null;
  }
}

@@ -133,19 +133,16 @@
  R&D Ltd
-%0A *
  All rig
@@ -171,16 +171,19 @@
 gram and
+%0A *
  the acc
@@ -201,19 +201,16 @@
 aterials
-%0A *
  are mad
@@ -396,23 +396,16 @@
 ibutors:
-%0A *    
  Ferhat 
@@ -442,23 +442,16 @@
 entation
-%0A *    
  H. Emre
@@ -468,32 +468,35 @@
  initial API and
+%0A *
  implementation%0A
@@ -494,23 +494,16 @@
 entation
-%0A *    
  Serhat 
@@ -544,15 +544,8 @@
 tion
-%0A *    
  U. 
@@ -565,32 +565,35 @@
  initial API and
+%0A *
  implementation%0A
@@ -1131,16 +1131,50 @@
 dDialog;
+%0Aimport org.eclipse.ui.PlatformUI;
 %0A%0Aimport
@@ -1421,99 +1421,24 @@
 r %7B%0A
-%0A
   
-@Override%0A  public Object execute(ExecutionEvent event) throws ExecutionException %7B%0A 
+IFile file;%0A
   
- 
 ISel
@@ -1457,267 +1457,46 @@
 tion
- = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow()%0A        .getSelectionService().getSelection();%0A    IFile file = (IFile) MarkerActivator.getEditor().getEditorInput().getAdapter(IFile.class);%0A    if (selection instanceof ITextSelection
+;%0A%0A  private void createMarkersWs(
 ) %7B%0A
-  
     
@@ -1543,16 +1543,21 @@
 ection) 
+this.
 selectio
@@ -1559,18 +1559,16 @@
 ection;%0A
-  
     if (
@@ -1612,16 +1612,21 @@
 osition(
+this.
 file, te
@@ -1650,18 +1650,16 @@
 fset(),%0A
-  
         
@@ -1724,19 +1724,16 @@
 null) %7B%0A
-%0A  
       Me
@@ -1818,20 +1818,23 @@
 mation%22,
-%0A   
+ null,%0A
         
@@ -1834,21 +1834,16 @@
         
-null,
  %22In the
@@ -1901,28 +1901,16 @@
 WARNING,
-%0A           
  new Str
@@ -1926,15 +1926,23 @@
 K%22%7D,
+%0A         
  0);%0A
-  
     
@@ -1962,32 +1962,8 @@
 ();%0A
-%0A        return null;%0A  
     
@@ -1971,18 +1971,16 @@
  else %7B%0A
-  
       Ma
@@ -2054,17 +2054,20 @@
 on, 
+this.
 file);%0A
-  
     
@@ -2102,20 +2102,8 @@
 og =
-%0A           
  new
@@ -2168,18 +2168,16 @@
 ;%0A      
-  
 if (sele
@@ -2225,64 +2225,516 @@
     
+%7D%0A
     
-return null;%0A     
+%7D%0A  %7D%0A%0A  @Override%0A  public Object execute(ExecutionEvent event) throws ExecutionException %7B%0A    this.file = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()%0A        .getActiveEditor().getEditorInput().getAdapter(IFile.class);%0A    this.selection =%0A
    
-%7D%0A
      
- %7D%0A    %7D%0A    return null
+PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();%0A%0A    this.createMarkersWs();%0A    this.refresh();%0A    return null;%0A  %7D%0A%0A  private void refresh() %7B%0A    MarkerFactory.refreshProjectExp()
 ;%0A  

Mark All In Workspace Handler class has been updated to code re-factoring.