/*
 * Perfclispe
 * 
 * 
 * Copyright (c) 2013 Jakub Knetl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.perfclipse.ui.actions;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.ui.IWorkbenchPart;
import org.perfclipse.ui.gef.policies.AbstractPerfCakeComponentEditPolicy;

/**
 * Edit dialog action.
 * 
 * @author Jakub Knetl
 */
public class EditDialogAction extends SelectionAction {

	
	private Request request;

	public EditDialogAction(IWorkbenchPart part) {
		super(part);
		setId(PerfClipseActionConstants.SHOW_EDIT_DIALOG);
		setText("Edit");
		request = new Request(PerfClipseActionConstants.REQ_SHOW_EDIT_DIALOG);
	}
	
	

	@Override
	public void run() {
		@SuppressWarnings("unchecked")
		List<EditPart> selectedParts = getSelectedObjects();
		CompoundCommand command = new CompoundCommand();
		for (EditPart selected : selectedParts){
			command.add(selected.getCommand(request));
		}
		
		execute(command);
	}

	@Override
	protected boolean calculateEnabled() {
		//check if something is selected
		if (getSelectedObjects().isEmpty()){
			return false;
		}

		//check if all selected objects are instance of editPart
		for (Object selected : getSelectedObjects()){
			if (!(selected instanceof EditPart)){
				return false;
			}
		}

		//Check if all selected eidtParts has installed edit policy with edit properties
		for (Object selected : getSelectedObjects()){
			EditPart part = (EditPart) selected;
			EditPolicy policy = part.getEditPolicy(EditPolicy.COMPONENT_ROLE);
			if (!(policy instanceof AbstractPerfCakeComponentEditPolicy)){
				return false;
			}
		}
		
		return true;
	}


}

@@ -655,32 +655,8 @@
 s;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -755,57 +755,8 @@
 st;%0A
-import org.eclipse.gef.commands.CompoundCommand;%0A
 impo
@@ -947,16 +947,70 @@
 on.%0A * %0A
+ * Opens edit dialog for last selected component.%0A * %0A
  * @auth
@@ -1368,69 +1368,30 @@
 %7B%0A%09%09
-@SuppressWarnings(%22unchecked%22)%0A%09%09List%3CEditPart%3E s
+%0A%09%09Object lastS
 elected
-Parts
  = g
@@ -1413,127 +1413,124 @@
 ts()
-;%0A%09%09CompoundCommand command = new CompoundCommand(
+.get(getSelectedObjects().size() - 1
 );%0A
+%0A
 %09%09
-for (EditPart selected : selected
+if (lastSelected instanceof Edit
 Part
-s)%7B
+)
 %0A%09%09%09
-command.add(s
+execute(((EditPart) lastS
 elected
+)
 .get
@@ -1552,35 +1552,8 @@
 ));%0A
-%09%09%7D%0A%09%09%0A%09%09execute(command);%0A
 %09%7D%0A%0A
@@ -1720,28 +1720,22 @@
 all 
-selected
+last
  object
-s are
+ is
  ins
@@ -1754,21 +1754,16 @@
 tPart%0A%09%09
-for (
 Object s
@@ -1762,33 +1762,33 @@
 Object selected 
-:
+=
  getSelectedObje
@@ -1792,20 +1792,55 @@
 bjects()
-)%7B%0A%09
+.get(getSelectedObjects().size() - 1);%0A
 %09%09if (!(
@@ -1870,25 +1870,24 @@
 tPart))%7B%0A%09%09%09
-%09
 return false
@@ -1884,29 +1884,24 @@
 turn false;%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09//Che
@@ -1906,19 +1906,20 @@
 heck if 
-all
+last
  selecte
@@ -1981,57 +1981,8 @@
 ies%0A
-%09%09for (Object selected : getSelectedObjects())%7B%0A%09
 %09%09Ed
@@ -2018,17 +2018,16 @@
 cted;%0A%09%09
-%09
 EditPoli
@@ -2087,17 +2087,16 @@
 OLE);%0A%09%09
-%09
 if (!(po
@@ -2150,17 +2150,16 @@
 licy))%7B%0A
-%09
 %09%09%09retur
@@ -2171,13 +2171,8 @@
 se;%0A
-%09%09%09%7D%0A
 %09%09%7D%0A

Edit invokes only one wizard even if more components are selected.