001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.cheatsheet.simple.actions;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.jface.action.Action;
015: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants;
016: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
017: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
018: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
019: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItem;
020: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSSubItemObject;
021: import org.eclipse.pde.internal.ui.PDEUIMessages;
022: import org.eclipse.pde.internal.ui.util.PDELabelUtility;
023:
024: /**
025: * SimpleCSAddStepAction
026: *
027: */
028: public class SimpleCSAddSubStepAction extends Action {
029:
030: private ISimpleCSItem fItem;
031:
032: private ISimpleCSSubItem fSubitem;
033:
034: /**
035: *
036: */
037: public SimpleCSAddSubStepAction() {
038: setText(PDEUIMessages.SimpleCSAddSubStepAction_0);
039: }
040:
041: /**
042: * @param cheatsheet
043: */
044: public void setDataObject(ISimpleCSObject csObject) {
045: // Determine input
046: if (csObject.getType() == ISimpleCSConstants.TYPE_ITEM) {
047: fSubitem = null;
048: fItem = (ISimpleCSItem) csObject;
049: } else if (csObject.getType() == ISimpleCSConstants.TYPE_SUBITEM) {
050: fSubitem = (ISimpleCSSubItem) csObject;
051: ISimpleCSObject parentObject = fSubitem.getParent();
052: // Determine input's parent object
053: if (parentObject.getType() == ISimpleCSConstants.TYPE_ITEM) {
054: fItem = (ISimpleCSItem) parentObject;
055: } else if (parentObject.getType() == ISimpleCSConstants.TYPE_CONDITIONAL_SUBITEM) {
056: // Not supported by editor, action will not run
057: fItem = null;
058: } else if (parentObject.getType() == ISimpleCSConstants.TYPE_REPEATED_SUBITEM) {
059: // Note supported by editor, action will not run
060: fItem = null;
061: }
062: } else {
063: // Invalid input, action will not run
064: fSubitem = null;
065: fItem = null;
066: }
067: }
068:
069: /* (non-Javadoc)
070: * @see org.eclipse.jface.action.Action#run()
071: */
072: public void run() {
073: // Ensure we have valid input
074: if (fItem == null) {
075: return;
076: }
077: // Create the new subitem
078: ISimpleCSSubItem newSubItem = createNewSubItem();
079: // Insert the new subitem
080: insertNewSubItem(newSubItem);
081: }
082:
083: /**
084: * @return
085: */
086: private ISimpleCSSubItem createNewSubItem() {
087: ISimpleCSModelFactory factory = fItem.getModel().getFactory();
088: // Element: subitem
089: ISimpleCSSubItem subitem = factory.createSimpleCSSubItem(fItem);
090:
091: ISimpleCSSubItemObject[] subItems = fItem.getSubItems();
092: ArrayList subItemNames = new ArrayList(subItems.length);
093:
094: for (int i = 0; i < subItems.length; ++i) {
095: if (subItems[i].getType() == ISimpleCSConstants.TYPE_SUBITEM) {
096: subItemNames.add(((ISimpleCSSubItem) subItems[i])
097: .getLabel());
098: }
099: }
100:
101: String[] names = (String[]) subItemNames
102: .toArray(new String[subItemNames.size()]);
103:
104: // Set on the proper parent object
105: subitem.setLabel(PDELabelUtility.generateName(names,
106: PDEUIMessages.SimpleCSAddSubStepAction_1));
107: return subitem;
108: }
109:
110: /**
111: * @param newSubItem
112: */
113: private void insertNewSubItem(ISimpleCSSubItem newSubItem) {
114: // Insert the new subitem depending on the input specfied
115: if (fSubitem != null) {
116: // Subitem input object
117: // Insert subitem right after the input item object
118: int index = fItem.indexOfSubItem(fSubitem) + 1;
119: fItem.addSubItem(index, newSubItem);
120: } else {
121: // Item input object
122: // Insert subitem as the last child subitem
123: fItem.addSubItem(newSubItem);
124: }
125: }
126:
127: }
|