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 org.eclipse.jface.action.Action;
013: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCS;
014: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants;
015: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSDescription;
016: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSIntro;
017: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem;
018: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSModelFactory;
019: import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSObject;
020: import org.eclipse.pde.internal.ui.PDEUIMessages;
021: import org.eclipse.pde.internal.ui.util.PDELabelUtility;
022: import org.eclipse.pde.internal.ui.wizards.cheatsheet.BaseCSCreationOperation;
023:
024: /**
025: * SimpleCSAddStepAction
026: *
027: */
028: public class SimpleCSAddStepAction extends Action {
029:
030: private ISimpleCS fCheatsheet;
031:
032: private ISimpleCSItem fItem;
033:
034: private ISimpleCSIntro fIntro;
035:
036: /**
037: *
038: */
039: public SimpleCSAddStepAction() {
040: setText(PDEUIMessages.SimpleCSAddStepAction_0);
041: }
042:
043: /**
044: * @param csObject
045: */
046: public void setDataObject(ISimpleCSObject csObject) {
047: // Determine input
048: if (csObject.getType() == ISimpleCSConstants.TYPE_CHEAT_SHEET) {
049: fIntro = null;
050: fItem = null;
051: fCheatsheet = (ISimpleCS) csObject;
052: } else if (csObject.getType() == ISimpleCSConstants.TYPE_ITEM) {
053: fIntro = null;
054: fItem = (ISimpleCSItem) csObject;
055: fCheatsheet = fItem.getSimpleCS();
056: } else if (csObject.getType() == ISimpleCSConstants.TYPE_INTRO) {
057: fIntro = (ISimpleCSIntro) csObject;
058: fItem = null;
059: fCheatsheet = fIntro.getSimpleCS();
060: } else {
061: // Invalid input, action will not run
062: fIntro = null;
063: fItem = null;
064: fCheatsheet = null;
065: }
066: }
067:
068: /* (non-Javadoc)
069: * @see org.eclipse.jface.action.Action#run()
070: */
071: public void run() {
072: // Ensure we have valid input
073: if (fCheatsheet == null) {
074: return;
075: }
076: // Create the new item
077: ISimpleCSItem newItem = createNewItem();
078: // Insert the new item
079: insertNewItem(newItem);
080: }
081:
082: /**
083: * @param item
084: */
085: private void insertNewItem(ISimpleCSItem newItem) {
086: // Insert the new item depending on the input specfied
087: if (fIntro != null) {
088: // Intro input object
089: // Insert item as the first cheat sheet child item which is the first
090: // node after the introduction node
091: fCheatsheet.addItem(0, newItem);
092: } else if (fItem != null) {
093: // Item input object
094: // Insert item right after the input item object
095: int index = fCheatsheet.indexOfItem(fItem) + 1;
096: fCheatsheet.addItem(index, newItem);
097: } else {
098: // Cheat sheet input object
099: // Add item as the last cheat sheet child item
100: fCheatsheet.addItem(newItem);
101: }
102: }
103:
104: /**
105: * @return
106: */
107: private ISimpleCSItem createNewItem() {
108: ISimpleCSModelFactory factory = fCheatsheet.getModel()
109: .getFactory();
110: // Create the new item
111: // Element: item
112: ISimpleCSItem item = factory.createSimpleCSItem(fCheatsheet);
113:
114: ISimpleCSItem[] items = fCheatsheet.getItems();
115: String[] itemNames = new String[items.length];
116:
117: for (int i = 0; i < items.length; ++i) {
118: itemNames[i] = items[i].getTitle();
119: }
120:
121: item.setTitle(PDELabelUtility.generateName(itemNames,
122: PDEUIMessages.SimpleCheatSheetCreationOperation_1));
123: // Element: description
124: ISimpleCSDescription description = factory
125: .createSimpleCSDescription(item);
126: description
127: .setContent(BaseCSCreationOperation
128: .formatTextBold(PDEUIMessages.SimpleCheatSheetCreationOperation_2));
129: item.setDescription(description);
130: return item;
131: }
132: }
|